-
Notifications
You must be signed in to change notification settings - Fork 1
/
P4868.cc
65 lines (59 loc) · 1.17 KB
/
P4868.cc
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
#include "stdio.h"
#include "stdlib.h"
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
int lowbit(int x) {
return x & -x;
}
int n;
void update(LL * bit, int idx, LL k) {
for (; idx <= n; idx += lowbit(idx)) {
bit[idx] += k;
}
}
LL sum(LL * bit, int idx) {
LL result = 0;
for (; idx > 0; idx -= lowbit(idx)) {
result += bit[idx];
}
return result;
}
int main() {
int m;
scanf("%d %d", &n, &m);
LL * bit_a = (LL *)malloc((n + 1) * sizeof(LL));
LL * bit_b = (LL *)malloc((n + 1) * sizeof(LL));
for (int i = 1; i < n + 1; i++) {
bit_a[i] = bit_b[i] = 0;
}
LL *a = (LL *)malloc((n + 1) * sizeof(LL));
for (int i = 1; i < n + 1; i++) {
scanf("%lld", a + i);
update(bit_a, i, a[i]);
update(bit_b, i, a[i]*i);
}
//char cmd[6];
string cmd;
int idx;
LL x;
for (int i = 0; i < m; i++) {
//scanf("%s", cmd);
cin >> cmd;
if (cmd[0] == 'M') {
scanf("%d %lld", &idx, &x);
update(bit_a, idx, x - a[idx]);
update(bit_b, idx, (x - a[idx]) * idx);
a[idx] = x;
}
else {
scanf("%d", &idx);
printf("%lld\n", sum(bit_a, idx) * (idx + 1) - sum(bit_b, idx));
}
}
free(bit_a);
free(bit_b);
free(a);
return 0;
}