-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightOJ__1112.c
executable file
·70 lines (58 loc) · 1.88 KB
/
lightOJ__1112.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
#include <stdio.h>
#define SIZE 100005
long binaryIndexTree[SIZE];
long amount[SIZE];
long n;
void add ( long index, long val ){
while ( index <= n ){
binaryIndexTree[index] += val;
index += index & -index;
}
}
long total ( long index ){
long sum = 0;
while ( index ){
sum += binaryIndexTree[index];
index -= index & -index;
}
return sum;
}
long sumInterval ( long from, long to ){
return total(to) - total(from - 1);
}
int main(){
unsigned test, kase, query;
long i, p, v, q;
while ( scanf ("%u", &test ) != EOF ){
for ( kase = 1 ; kase <= test ; kase++ ){
scanf("%ld%u", &n, &query);
for ( i = 1 ; i <= n ; i++ ){
scanf("%ld", &amount[i]);
add( i, amount[i] );
}
printf("Case %d:\n", kase);
while ( query-- ){
scanf("%ld", &p);
switch ( p ){
case 1 : scanf("%ld", &p);
p++;
printf("%ld\n", amount[p]);
/*printf("%ld\n", sumInterval(p-1, p));*/
add( p, -amount[p]);
amount[p] = 0;
break;
case 2 : scanf("%ld%ld", &p, &q);
p++;
add( p, q );
amount[p] += q;
break;
case 3 : scanf("%ld%ld", &p, &q); p++, q++;
printf("%ld\n", sumInterval(p, q));
break;
}
}
for ( i = 1 ; i <= n ; i++ ) amount[i] = binaryIndexTree[i] = 0;
}
}
return 0;
}