-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBinaryIndexedTree2D.cpp
More file actions
76 lines (70 loc) · 1.42 KB
/
BinaryIndexedTree2D.cpp
File metadata and controls
76 lines (70 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
//2D BIT
//1-based indexing everywhere
long long tree[1050][1050];
long long getSum(int x,int y)
{
long long sum = 0;
while (x)
{
int yy=y;
while(yy){
sum += tree[x][yy];
yy-= yy & (-yy);
}
x-= (x&(-x));
}
return sum;
}
void update(int x,int y,int val,int MAX)
{
while (x<=MAX)
{
int yy=y;
while(yy<=MAX){
tree[x][yy] += val;
yy+= yy & (-yy);
}
x+= (x&(-x));
}
}
int *constructBITree(int n)
{
int *BITree = new int[n+1];
for (int i=1; i<=n; i++)
BITree[i] = 0;
return BITree;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
memset(tree,0,sizeof tree);
while(1)
{
char s[10];
scanf("%s",s);
if(s[1]=='E'){
int x,y,val;
scanf("%d%d%d",&x,&y,&val);
long long p_val = getSum(x+1,y+1) + getSum(x,y) - getSum(x+1,y) - getSum(x,y+1);
update(x+1,y+1,val-p_val,n+9);
}
else if(s[1]=='U'){
long long sumx=0;
int x1,y1,x,y;
scanf("%d%d%d%d",&x,&y,&x1,&y1);
sumx = getSum(x1+1,y1+1) + getSum(x,y) - getSum(x1+1,y) - getSum(x,y1+1);
printf("%lld\n",sumx);
}
else
break;
}
printf("\n");
}
return 0;
}