Q: There is a N * N square with some mobile phones in it. A grid can be changed, increased, or reduced. Ask how many mobile phones are in a small matrix.
Idea: bare two-dimensional tree array.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
Typedef long LL;
Const int n= 1030;
Int num [N] [N];
Int lowbit (int x ){
Return x & (-x );
}
Void update (int x, int y, int add ){
Int t = y;
While (x <N ){
Y = t;
While (y <N ){
Num [x] [y] + = add;
If (num [x] [y] <0)
Num [x] [y] = 0;
Y + = lowbit (y );
}
X + = lowbit (x );
}
}
LL sum (int x, int y ){
Int t = y;
LL s = 0;
While (x> 0 ){
Y = t;
While (y> 0 ){
S + = num [x] [y];
Y-= lowbit (y );
}
X-= lowbit (x );
}
Return s;
}
Int main (){
// Freopen ("1.txt"," r ", stdin );
Int id, n;
While (scanf ("% d", & id, & n )! = EOF ){
Memset (num, 0, sizeof (num ));
Int x1, y1, x2, y2, value;
While (1 ){
Scanf ("% d", & id );
If (id = 3)
Break;
Else if (id = 1 ){
Scanf ("% d", & x1, & y1, & value );
Update (x1 + 1, y1 + 1, value );
}
Else if (id = 2 ){
Scanf ("% d", & x1, & y1, & x2, & y2 );
Printf ("% lld \ n", sum (x2 + 1, y2 + 1)-sum (x1, y2 + 1)-sum (x2 + 1, y1) + sum (x1, y1 ));
}
}
}
Return 0;
}