/* Optimal solution for Season 6 Round 6 Problem 2 Senior Author: Anton Chernev */ #include #include using std::pair; typedef pair Pair; const size_t MAX_N = 5001; int N; int table[MAX_N][MAX_N]; int diagonals1[2 * MAX_N], diagonals2[2 * MAX_N]; Pair getIndices(size_t x, size_t y) { Pair answer; answer.first = x + y - 1; answer.second = x + (N - y + 1) - 1; return answer; } void readTable() { size_t index1, index2; Pair indices; scanf("%d", &N); for(size_t i = 1; i <= N; i++) for(size_t j = 1; j <= N; j++) { scanf("%d", &table[i][j]); indices = getIndices(i, j); diagonals1[indices.first] += table[i][j]; diagonals2[indices.second] += table[i][j]; } } void answerQueries() { int Q, answer; size_t x, y; Pair indices; scanf("%d", &Q); for(size_t i = 0; i < Q; i++) { scanf("%d%d", &x, &y); indices = getIndices(x, y); answer = diagonals1[indices.first] + diagonals2[indices.second] - table[x][y]; printf("%d\n", answer); } } int main() { freopen("diagonalsum.in", "r", stdin); freopen("diagonalsum.out", "w", stdout); readTable(); answerQueries(); } /* 6 1 16 7 9 36 6 17 26 2 15 20 27 3 14 34 24 4 19 25 35 22 31 12 30 8 18 5 29 13 10 33 23 11 32 21 28 */