Skip to content

Commit f95ef29

Browse files
authored
Create 1190-SubarraySumQueries.cpp
1 parent b4e284f commit f95ef29

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// JAI BAJARANG BALI
2+
3+
// manitianajay45
4+
5+
// give me some sunshine, give me some rain ,give me another chance to grow up once again....
6+
7+
// sab moh maya hai....
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
#define ll long long
13+
14+
15+
struct node{
16+
ll prefix,suffix,mx,total;
17+
};
18+
19+
vector<node> seg(800005, {0,0,0,0});
20+
21+
22+
node merge(node a,node b){
23+
node c;
24+
c.prefix=max(a.total+b.prefix,a.prefix);
25+
c.suffix=max(a.suffix+b.total,b.suffix);
26+
c.total=a.total+b.total;
27+
c.mx=max(a.mx,b.mx);
28+
c.mx=max(c.mx,c.suffix);
29+
c.mx=max(c.mx,c.prefix);
30+
c.mx=max(c.mx,a.suffix+b.prefix);
31+
return c;
32+
}
33+
34+
35+
36+
void set1(ll i,ll val,ll x,ll l,ll r){
37+
if(r-l==1){
38+
seg[x].total=val;
39+
seg[x].mx=max(0ll,val);
40+
seg[x].prefix=max(0ll,val);
41+
seg[x].suffix=max(0ll,val);
42+
return ;
43+
}
44+
ll mid=(l+r)/2;
45+
46+
if(i<mid){
47+
set1(i,val,2*x+1,l,mid);
48+
}else{
49+
set1(i,val,2*x+2,mid,r);
50+
}
51+
seg[x]=merge(seg[2*x+1],seg[2*x+2]);
52+
}
53+
54+
55+
node ans(ll l,ll r,ll x,ll lf,ll rf){
56+
if(lf>=l && rf<=r){
57+
return seg[x];
58+
}else if(lf>=r || rf<=l){
59+
return {0,0,0,0};
60+
}
61+
ll mid=(lf+rf)/2;
62+
return merge(ans(l,r,2*x+1,lf,mid),ans(l,r,2*x+2,mid,rf));
63+
}
64+
65+
66+
67+
68+
69+
int main()
70+
{
71+
72+
ios_base::sync_with_stdio(false);
73+
74+
cin.tie(NULL);
75+
76+
ll n, q;
77+
cin >> n >> q;
78+
79+
for (ll i = 0; i < n; i++)
80+
{
81+
ll x;
82+
cin>>x;
83+
84+
set1(i,x,0,0,n);
85+
}
86+
87+
while(q--){
88+
ll i,u;
89+
cin>>i>>u;
90+
i--;
91+
set1(i,u,0,0,n);
92+
93+
94+
95+
node an=ans(0,n,0,0,n);
96+
cout<<an.mx<<endl;
97+
}
98+
return 0;
99+
}

0 commit comments

Comments
 (0)