forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1195(AC).cpp
84 lines (72 loc) · 1.19 KB
/
POJ1195(AC).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
72
73
74
75
76
77
78
79
80
81
82
83
84
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXN = 1024;
int c[MAXN + 1][MAXN + 1];
int n;
int low_bit(int x)
{
return x & (-x);
}
void add_value(int i, int j, int v, int n)
{
int j1;
while(i <= n){
j1 = j;
while(j1 <= n){
c[i][j1] += v;
j1 += low_bit(j1);
}
i += low_bit(i);
}
}
int get_sum(int i, int j)
{
int j1;
int sum;
if(i <= 0 || j <= 0){
return 0;
}
sum = 0;
while(i > 0){
j1 = j;
while(j1 > 0){
sum += c[i][j1];
j1 -= low_bit(j1);
}
i -= low_bit(i);
}
return sum;
}
int partial_sum(int x1, int y1, int x2, int y2)
{
return get_sum(x2, y2) + get_sum(x1 - 1, y1 - 1) - get_sum(x2, y1 - 1) - get_sum(x1 - 1, y2);
}
int main()
{
int op;
int x1, y1, x2, y2, v;
memset(c, 0, (MAXN + 1) * (MAXN + 1) * sizeof(int));
while(scanf("%d", &op) == 1){
if(op == 0){
scanf("%d", &n);
}else if(op == 1){
scanf("%d%d%d", &x1, &y1, &v);
++x1;
++y1;
add_value(x1, y1, v, n);
}else if(op == 2){
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
++x1;
++y1;
++x2;
++y2;
printf("%d\n", partial_sum(x1, y1, x2, y2));
}else if(op == 3){
break;
}
}
return 0;
}