-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMATSUM(spoj).cpp
64 lines (60 loc) · 1.05 KB
/
MATSUM(spoj).cpp
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
#include<bits/stdc++.h>
using namespace std;
int n;
int BIT[1025][1025],data[1025][1025];
void update(int x,int y,int val)
{
int i,j;
for(i=x;i<=n;i+=i&-i)
for(j=y;j<=n;j+=j&-j)
BIT[i][j]+=val;
}
int getSum(int x, int y)
{
int sum=0,i,j;
for(i=x;i>0;i&=i-1)
for(j=y;j>0;j&=j-1)
sum+=BIT[i][j];
return sum;
}
int main()
{
ios::sync_with_stdio(false);
int test,x,y,num,x1,y1,x2,y2;
cin>>test;
while(test--)
{
cin>>n;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
{
BIT[i][j]=0;
data[i][j]=0;
}
string str;
while(1)
{
cin>>str;
if(str=="SET")
{
cin>>x>>y>>num;
update(x+1,y+1,-data[x+1][y+1]+num);
data[x+1][y+1]=num;
}
else if(str=="SUM")
{
cin>>x1>>y1>>x2>>y2;
x1++;y1++;x2++;y2++;
int ans=0;
ans+=getSum(x2, y2) ;
ans-=getSum(x1 - 1, y2);
ans-=getSum(x2, y1 - 1);
ans+=getSum(x1 - 1, y1 - 1);
cout<<ans<<endl;
}
else
break;
}
cout<<endl;
}
}