/*
(X1, Y2) ____________ (X2, Y2)
|
|
|
| ____________ |
(X1, Y1) (X2, Y1)
In the above rectangle, this question uses a tree array for calculation, so sum (X2, Y2) calculates all
The number of mobile phone users, but it calculates sum (X2, Y1) and sum (x1, Y2) or lower.
Ans = sum (X2, Y2)-sum (X2, Y1)-sum (x1, Y2), then the sum (x1, Y1) is subtracted, so we have to add
The previous part.
In addition, in update (), sum (), it is easier to understand because it is only one-dimensional.Code, New
I-= lowbit (I) = I & (I-1)
I + = lowbit (I) = I = (I | (I-1) + 1
When the input data is large, the speed is much faster. For example, before this question is changed, my code needs 1.39 s on hoj and 1.24 s after the change.
*/
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Const int x = 1026;
Int C [x] [X], X, Y, N, A, Q, row, Col;
/* Int lowbit (int xx)
{
Return XX &-xx;
}*/
Int sum (int I, Int J)
{
Int temp, ANS = 0;
While (I> 0)
{
Temp = J;
While (temp> 0)
{
Ans + = C [I] [temp];
// Temp-= lowbit (temp );
Temp = temp & (temp-1 );
}
// I-= lowbit (I );
I = I & (I-1 );
}
Return ans;
}
Void Update (int I, Int J, int num)
{
Int temp;
While (I <= row)
{
Temp = J;
While (temp <= col)
{
C [I] [temp] + = num;
// Temp + = lowbit (temp );
Temp = (temp | (temp-1) + 1;
}
// I + = lowbit (I );
I = (I | (I-1) + 1;
}
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int x1, x2, Y1, Y2;
While (scanf ("% d", & Q )! = EOF) // multiple case Input
{
If (! Q) // when the instruction is 0, reload the rectangle size and clear the array C [] []
{
Scanf ("% d", & N );
Row = Col = N;
For (INT I = 0; I <= N; I ++)
For (Int J = 0; j <= N; j ++)
C [I] [J] = 0;
}
Else if (q = 1) // when the command is 1, add a new number to the vertex (x, y), that is, update (X, Y, num)
{
Scanf ("% d", & X, & Y, & );
X ++; // Add one to X and Y to avoid endless loops when the value is 0.
Y ++;
Update (X, Y, );
}
When else if (q = 2) // when the command is 2, calculate and output the number of all mobile phones on the rectangle (x1, Y1) (X2, Y2) (approximate meaning)
{
Scanf ("% d", & X1, & Y1, & X2, & Y2 );
X2 ++;
Y2 ++;
Printf ("% d \ n", sum (X2, Y2) + sum (x1, Y1)-sum (x1, Y2)-sum (X2, Y1 ));
}
}
Return 0;
}