-
Notifications
You must be signed in to change notification settings - Fork 2
/
Subarray Sum Queries
81 lines (75 loc) · 1.67 KB
/
Subarray Sum Queries
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
//g++ 5.4.0
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair< int,int >
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define L(p) 2*p+1
#define R(p) 2*p+2
const int nax = 2e5+10;
struct node
{
int prefix_mx , suffix_mx , total_mx , total_sum;
node()
{
prefix_mx = suffix_mx = total_mx = total_sum = 0;
}
};
node jt[4*nax];
node fun(node &a,node &b)
{
node c;
c.total_sum = a.total_sum + b.total_sum;
c.prefix_mx = max( a.prefix_mx , a.total_sum + b.prefix_mx );
c.suffix_mx = max( b.suffix_mx , b.total_sum + a.suffix_mx );
c.total_mx = max( a.total_mx , max( b.total_mx , a.suffix_mx + b.prefix_mx ) );
return c;
// whatever u wish
}
void build(int l , int h , int p )
{
if(l == h)
{
cin >> jt[p].total_sum;
jt[p].prefix_mx = jt[p].suffix_mx = jt[p].total_mx = max(0LL, jt[p].total_sum );
return;
}
int m = (l+h)>>1;
build(l , m , L(p));
build(m+1 , h , R(p));
jt[p] = fun(jt[L(p)],jt[R(p)]);
return;
}
void update(int l ,int h , int p, int ql, int qh,int v)
{
if(l > qh || h < ql)
return;
else if(l >= ql && h <= qh)
{
jt[p].total_sum = v;
jt[p].prefix_mx = jt[p].suffix_mx = jt[p].total_mx = max(0LL, jt[p].total_sum );
return;
}
int m = (l + h )>> 1;
update( l , m , L(p) ,ql ,qh,v);
update(m+1 , h , R(p) , ql ,qh,v);
jt[p] = fun(jt[L(p)],jt[R(p)]);
return;
}
signed main()
{
fast;
int n , q;
cin >> n >> q;
build( 1,n,0 );
while( q-- )
{
int a,b;
cin >> a >> b;
update( 1,n,0,a,a,b);
cout << jt[0].total_mx << "\n";
}
}