forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatsum.c
117 lines (116 loc) · 1.7 KB
/
matsum.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* 2009-04-19 */
#include <stdio.h>
#include <string.h>
#include <assert.h>
char* opos;
char* ipos;
char infile[20000000];
char outfile[10000000];
int BITPool[20971520];
int arrPool[20971520];
int* BIT;
int* arr;
int N,cnt;
int* p;
int query(int x,int y)
{
int res=0; int z;
for(;x>=0;x=(x&(x+1))-1)
{
p=BIT+x*N;
for (z=y;z>=0;z=(z&(z+1))-1)
res+=p[z];
}
return res;
}
int query_experimental(int x,int y1,int y2)
{
int res=0; int z;
for(;x>=0;x=(x&(x+1))-1)
{
for (z=y1; z>=0; z=(z&(z+1))-1)
res+=BIT[(x<<cnt)+z];
for (z=y2; z>=0; z=(z&(z+1))-1)
res-=BIT[(x<<cnt)+z];
}
return res;
}
int input()
{
int x=0;
int neg=0;
char c;
for(;;)
{
c=*ipos++;
if (c<=32)
return neg?-x:x;
if (c=='-')
neg=1;
else
x=(x<<1)+(x<<3)+c-'0';
}
return x;
}
char D[20];
void output(int x)
{
int y,dig=0;
if (x<0)
{
*opos++='-';
x=-x;
}
while (x||!dig)
{
y=x/10;
D[dig++]=x-(10*y)+'0';
x=y;
}
while (dig--)
*opos++=D[dig];
*opos++='\n';
}
int main()
{
ipos=infile;
opos=outfile;
int T,x,y,num,res,diff,x1,x2,y1,y2,X,Y,sum,i,j;
char c;
fread_unlocked(infile,20000000,1,stdin);
T=input();
assert (T<=20);
BIT=BITPool;
arr=arrPool;
while (T--)
{
N=input();
for(;;)
{
ipos+=2; c=*ipos++; ipos++;
if (c=='D') break;
if (c=='T')
{
x=input(); y=input(); num=input();
res=0;
diff=num-arr[x*N+y];
for(X=x;X<N;X|=X+1)
{
p=BIT+X*N;
for (Y=y;Y<N;Y|=Y+1)
p[Y]+=diff;
}
arr[x*N+y]=num;
}
else
{
x1=input(); y1=input(); x2=input(); y2=input();
output(query(x2,y2)-query(x1-1,y2)-query(x2,y1-1)+query(x1-1,y1-1));
}
}
BIT+=1048576;
arr+=1048576;
}
fwrite_unlocked(outfile,opos-outfile,1,stdout);
return 0;
}