-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrefix Sum Queries.cpp
76 lines (68 loc) · 1.66 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "bits/stdc++.h"
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define ll long long
const int N = 200005;
int n,q,ara[N];
struct Node{
ll sum;
ll pref;
friend Node operator+(const Node &A, const Node &B){
return {A.sum+B.sum, max(A.pref,A.sum+B.pref) };
}
};
Node segtree[4*N];
void build(int index = 1,int l = 1,int r = n){
if( l>r ) return;
if( l == r ){
segtree[index] = {ara[l],max(0,ara[l])};
return;
}
int mid = (l+r)/2;
build(index*2,l,mid);
build(index*2+1,mid+1,r);
segtree[index] = segtree[index*2] + segtree[index*2+1];
}
void update(int index,int pos,int l,int r,int x){
if( r<pos or l>pos ) return;
if( l == r ){
segtree[index] = {x,x};
return;
}
int mid = (l+r)/2;
update(index*2,pos,l,mid,x);
update(index*2+1,pos,mid+1,r,x);
segtree[index] = segtree[index*2] + segtree[index*2+1];
}
Node query(int index,int L,int R,int l,int r){
if( r<L or l>R ) return {0,0};
if( l>=L and r<=R ) return segtree[index];
int mid = (l+r)/2;
Node q1 = query(index*2,L,R,l,mid);
Node q2 = query(index*2+1,L,R,mid+1,r);
return q1+q2;
}
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>ara[i];
build();
while(q--){
int t;
cin>>t;
if( t == 1 ){
int k,u;
cin>>k>>u;
update(1,k,1,n,u);
}else{
int a,b;
cin>>a>>b;
cout<<max(query(1,a,b,1,n).pref,0LL)<<endl;
}
}
}