-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathPrefix Sum Queries.cpp
62 lines (53 loc) · 1.29 KB
/
Prefix Sum Queries.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int maxN = 2e5+1;
const int SIZE = 4*maxN;
int N, Q, lo[SIZE], hi[SIZE], mp[maxN];
ll sum[SIZE], pre[SIZE];
void pull(int i){
pre[i] = max(pre[2*i], sum[2*i]+pre[2*i+1]);
sum[i] = sum[2*i] + sum[2*i+1];
}
void init(int i, int l, int r){
lo[i] = l; hi[i] = r;
if(l == r){
scanf("%lld", &sum[i]);
pre[i] = max(0LL, sum[i]);
mp[l] = i;
return;
}
int m = (l+r)/2;
init(2*i, l, m);
init(2*i+1, m+1, r);
pull(i);
}
void update(int idx, int val){
int i = mp[idx];
sum[i] = val;
pre[i] = max(0LL, sum[i]);
i >>= 1;
while(i){
pull(i);
i >>= 1;
}
}
pll query(int i, int l, int r){
if(l > hi[i] || r < lo[i]) return {0, 0};
if(l <= lo[i] && hi[i] <= r) return {pre[i], sum[i]};
pll left = query(2*i, l, r);
pll right = query(2*i+1, l, r);
return {max(left.first, left.second+right.first), left.second+right.second};
}
int main(){
scanf("%d %d", &N, &Q);
init(1, 1, N);
for(int q = 0, t, a, b; q < Q; q++){
scanf("%d %d %d", &t, &a, &b);
if(t == 1)
update(a, b);
else if(t == 2)
printf("%lld\n", query(1, a, b).first);
}
}