-
Notifications
You must be signed in to change notification settings - Fork 4
/
MATSUM.cpp
71 lines (63 loc) · 1.41 KB
/
MATSUM.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
65
66
67
68
69
70
71
//BISMILLAHIRRAHMANIRRAHIM
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#define MAX 1024+10
#define memo(a,b) memset((a),(b),sizeof(a))
using namespace std;
int Row,Col,MaxVal=MAX,tree[MAX][MAX],input[MAX][MAX];
void update(int r,int c,int val)
{
int i,j;
for(i=r; i<=Row; i+=(i & -i))
for(j=c; j<=Col; j+=(j & -j))
tree[i][j]+=val;
}
int read(int r,int c)
{
int i,j,sum=0;
for(i=r; i>0; i&=i-1)
for(j=c; j>0; j &=j-1)
sum+=tree[i][j];
return sum;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
scanf("%d",&Row);
Col=Row;
memo(tree,0);
memo(input,0);
char s[100];
while(scanf("%s",s))
{
if (!strcmp(s,"SUM"))
{
int r1,c1,r2,c2;
scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
r1++,c1++,r2++,c2++;
int res =0;
res+=read(r2, c2) ;
res-=read(r1 - 1, c2);
res-=read(r2, c1 - 1);
res+=read(r1 - 1, c1 - 1);
printf("%d\n",res);
}
else if(!strcmp(s,"SET"))
{
int r,c,val;
scanf("%d %d %d",&r,&c,&val);
r++,c++;
update(r, c, -input[r][c] + val);
input[r][c] = val;
}
else break;
}
puts("");
}
}