According to the following information, we initially learned the tree array:
Top group question set Co., http://download.csdn.net/detail/handong1587/5659973
Http://dongxicheng.org/structure/binary_indexed_tree/
Http://community.topcoder.com/tc? Module = static & d1 = tutorials & D2 = binaryindexedtrees!
Using in-place operations, I feel a little strange. After understanding the correspondence between the original array and the tree array, it is relatively simple. However, you have to perform more exercises to further use the tree array.
BelowCodeAC passed, but it is strange that the compiler always prompts timeout when selecting g ++. c ++ will be able to select AC, memory: 4204 K, time: 1672 Ms.
# Include <iostream> # include <cstring> using namespace STD; int X, N, T; int matrix [1005] [1005]; void updatematval (int x, int y, int Val) {While (x <= N) {int tmpy = y; while (tmpy <= N) {matrix [x] [tmpy] + = val; tmpy + = tmpy & (-tmpy);} X + = x & (-x) ;}} void update2d (INT X1, int Y1, int X2, int Y2) {updatematval (x1, Y1, 1); updatematval (x1, y2 + 1,-1); updatematval (X2 + 1, Y1,-1); updatematval (X2 + 1, y2 + 1, 1);} int getmatsum (int x, int y) {int sum = 0; while (x> 0) {int tmpy = y; while (tmpy> 0) {sum + = matrix [x] [tmpy]; tmpy-= tmpy & (-tmpy);} X-= x & (-x );} return sum;} int main () {// freopen ("poj2155_matrix.txt", "r", stdin); CIN> X; while (X --) {CIN> N> T; memset (matrix, 0, sizeof (matrix); For (Int J = 0; j <t; j ++) {char cmd; cin> cmd; If (cmd = 'q') {int X, Y; CIN> x> Y; cout <getmatsum (x, y) % 2 <Endl;} If (cmd = 'C') {int X1, Y1, X2, Y2; CIN> x1> Y1> X2> Y2; update2d (x1, Y1, X2, Y2) ;}} if (x> 0) cout <Endl ;}return 0 ;}