#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e17;
typedef pair<int,int> pii;
ll n,v[100005],q,s[100005],mars[100005];
int solve(int l,int r)
{
s[l-1]=0;
mars[l-1]=0;
for(int i=l;i<=r;i++)
{
s[i]=s[i-1]+v[i];
mars[i]=0;
}
stack<int> coada;
for(int i=l;i<=r;i++)
{
while(!coada.empty()&&v[coada.top()]<v[i])
coada.pop();
int st=l-1;
if(!coada.empty())
st=coada.top();
if(s[i-1]-s[st]<v[i])
{
mars[st+1]++;
mars[i]--;
}
coada.push(i);
}
while(!coada.empty())
coada.pop();
for(int i=r;i>=l;i--)
{
while(!coada.empty()&&v[coada.top()]<v[i])
coada.pop();
int dr=r+1;
if(!coada.empty())
dr=coada.top();
if(s[dr-1]-s[i]<v[i])
{
mars[i+1]++;
mars[dr]--;
}
coada.push(i);
}
int ans=0;
for(int i=l;i<=r;i++)
{
mars[i]+=mars[i-1];
if(mars[i]==0)
ans++;
}
return ans;
}
ll aib[100005];
ll lsb(ll x)
{
return x&(-x);
}
void aibupd(ll poz,ll val)
{
for(int i=poz;i<=n;i+=lsb(i))
aib[i]+=val;
}
ll suma(ll poz)
{
ll rez=0;
for(int i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
struct date
{
ll minim,cnt;
} arb[4*100005];
ll toprop[4*100005];
struct prostie
{
ll maxim,poz;
} arbdir[2][4*100005]; /// 0 -> st, 1 -> dr
ll topropdir[2][4*100005];
date combine(date a, date b)
{
if(a.minim<b.minim)
return a;
if(a.minim>b.minim)
return b;
a.cnt+=b.cnt;
return a;
}
void prop(ll nod)
{
ll val=toprop[nod];
arb[nod*2].minim+=val;
arb[nod*2+1].minim+=val;
toprop[nod*2]+=val;
toprop[nod*2+1]+=val;
}
void update(ll nod,ll st,ll dr,ll a,ll b,ll val)
{
if(st!=dr)
prop(nod);
toprop[nod]=0;
if(st>=a&&dr<=b)
{
arb[nod].minim+=val;
toprop[nod]+=val;
return;
}
ll mij=(st+dr)/2;
if(a<=mij)
update(nod*2,st,mij,a,b,val);
if(b>mij)
update(nod*2+1,mij+1,dr,a,b,val);
arb[nod]=combine(arb[nod*2],arb[nod*2+1]);
}
prostie combinedir(prostie a, prostie b)
{
if(a.maxim>=b.maxim)
return a;
return b;
}
void propdir(ll ind,ll nod)
{
ll val=topropdir[ind][nod];
arbdir[ind][nod*2].maxim+=val;
arbdir[ind][nod*2+1].maxim+=val;
topropdir[ind][nod*2]+=val;
topropdir[ind][nod*2+1]+=val;
}
void updatedir(ll ind,ll nod,ll st,ll dr,ll a,ll b,ll val)
{
if(st!=dr)
propdir(ind,nod);
topropdir[ind][nod]=0;
if(st>=a&&dr<=b)
{
arbdir[ind][nod].maxim+=val;
topropdir[ind][nod]+=val;
return;
}
ll mij=(st+dr)/2;
if(a<=mij)
updatedir(ind,nod*2,st,mij,a,b,val);
if(b>mij)
updatedir(ind,nod*2+1,mij+1,dr,a,b,val);
arbdir[ind][nod]=combinedir(arbdir[ind][nod*2],arbdir[ind][nod*2+1]);
}
prostie querydir(ll ind,ll nod,ll st,ll dr,ll a,ll b)
{
if(st!=dr)
propdir(ind,nod);
topropdir[ind][nod]=0;
if(st>=a&&dr<=b)
return arbdir[ind][nod];
prostie rez={-2LL*INF,0};
ll mij=(st+dr)/2;
if(a<=mij)
rez=combinedir(rez,querydir(ind,nod*2,st,mij,a,b));
if(b>mij)
rez=combinedir(rez,querydir(ind,nod*2+1,mij+1,dr,a,b));
return rez;
}
void build(int nod,int st,int dr)
{
arb[nod].cnt=(dr-st+1);
if(st==dr)
return;
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
}
void builddir(int nod,int st,int dr)
{
arbdir[0][nod]=arbdir[1][nod]={0,st};
if(st==dr)
return;
int mij=(st+dr)/2;
builddir(nod*2,st,mij);
builddir(nod*2+1,mij+1,dr);
}
set<pii> setik;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>v[i];
cin>>q;
if(q<=1000)
{
while(q--)
{
int tip;
cin>>tip;
if(tip==1)
{
int poz,val;
cin>>poz>>val;
v[poz]=val;
}
else
{
int l,r;
cin>>l>>r;
cout<<solve(l,r)<<'\n';
}
}
return 0;
}
v[0]=INF;
v[n+1]=INF;
build(1,1,n);
builddir(1,1,n);
for(int i=1;i<=n;i++)
aibupd(i,v[i]);
stack<int> coada;
for(int i=1;i<=n;i++)
{
while(!coada.empty()&&v[coada.top()]<v[i])
coada.pop();
int st=0;
if(!coada.empty())
st=coada.top();
if(suma(i-1)-suma(st)<v[i])
{
/*mars[st+1]++;
mars[i]--;*/
if(st+1<=i-1)
setik.insert({st+1,i-1});
}
coada.push(i);
}
while(!coada.empty())
coada.pop();
for(int i=n;i>=1;i--)
{
while(!coada.empty()&&v[coada.top()]<v[i])
coada.pop();
int dr=n+1;
if(!coada.empty())
dr=coada.top();
if(suma(dr-1)-suma(i)<v[i])
{
/*mars[i+1]++;
mars[dr]--;*/
if(i+1<=dr-1)
setik.insert({i+1,dr-1});
}
coada.push(i);
}
while(!coada.empty())
coada.pop();
for(int i=1;i<=n;i++)
{
updatedir(0,1,1,n,i,i,v[i]);
updatedir(1,1,1,n,i,i,v[i]);
if(i+1<=n)
updatedir(0,1,1,n,i+1,n,-v[i]);
if(i-1>=1)
updatedir(1,1,1,n,1,i-1,-v[i]);
}
for(auto p:setik)
update(1,1,n,p.first,p.second,+1);
while(q--)
{
ll tip;
cin>>tip;
if(tip==1)
{
ll i,val;
cin>>i>>val;
updatedir(0,1,1,n,i,i,-v[i]);
updatedir(1,1,1,n,i,i,-v[i]);
if(i+1<=n)
updatedir(0,1,1,n,i+1,n,v[i]);
if(i-1>=1)
updatedir(1,1,1,n,1,i-1,v[i]);
aibupd(i,-v[i]);
v[i]=val;
updatedir(0,1,1,n,i,i,v[i]);
updatedir(1,1,1,n,i,i,v[i]);
if(i+1<=n)
updatedir(0,1,1,n,i+1,n,-v[i]);
if(i-1>=1)
updatedir(1,1,1,n,1,i-1,-v[i]);
aibupd(i,v[i]);
vector<int> lft,rgt;
if(i>1)
{
ll S=suma(n)-suma(i-1);
while(true)
{
prostie a=querydir(1,1,1,n,1,i-1);
a.maxim+=S;
if(a.maxim<0)
break;
lft.push_back(a.poz);
updatedir(1,1,1,n,a.poz,a.poz,-INF);
}
for(ll p:lft)
updatedir(1,1,1,n,p,p,+INF);
}
if(i+1<=n)
{
ll S=suma(i);
while(true)
{
prostie a=querydir(0,1,1,n,i+1,n);
a.maxim+=S;
if(a.maxim<0)
break;
rgt.push_back(a.poz);
updatedir(0,1,1,n,a.poz,a.poz,-INF);
}
for(ll p:rgt)
updatedir(0,1,1,n,p,p,+INF);
}
lft.push_back(i);
rgt.push_back(i);
lft.push_back(0);
rgt.push_back(n+1);
for(ll l:lft)
for(ll r:rgt)
{
ll st=l+1;
ll dr=r-1;
if(st<=dr)
{
if(suma(dr)-suma(st-1)<min(v[l],v[r]))
{
if(setik.find({st,dr})==setik.end())
{
setik.insert({st,dr});
update(1,1,n,st,dr,+1);
}
}
else
{
if(setik.find({st,dr})!=setik.end())
{
setik.erase({st,dr});
update(1,1,n,st,dr,-1);
}
}
}
}
}
else
{
int l,r;
cin>>l>>r;
cout<<arb[1].cnt<<'\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2468 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
3392 KB |
Output is correct |
3 |
Correct |
7 ms |
3164 KB |
Output is correct |
4 |
Correct |
8 ms |
3164 KB |
Output is correct |
5 |
Correct |
7 ms |
3160 KB |
Output is correct |
6 |
Correct |
11 ms |
3460 KB |
Output is correct |
7 |
Correct |
7 ms |
2908 KB |
Output is correct |
8 |
Correct |
11 ms |
3420 KB |
Output is correct |
9 |
Correct |
8 ms |
2880 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
7 ms |
3020 KB |
Output is correct |
12 |
Correct |
8 ms |
2908 KB |
Output is correct |
13 |
Correct |
8 ms |
2908 KB |
Output is correct |
14 |
Correct |
8 ms |
3156 KB |
Output is correct |
15 |
Correct |
9 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2468 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
8 ms |
3392 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
8 ms |
3164 KB |
Output is correct |
25 |
Correct |
7 ms |
3160 KB |
Output is correct |
26 |
Correct |
11 ms |
3460 KB |
Output is correct |
27 |
Correct |
7 ms |
2908 KB |
Output is correct |
28 |
Correct |
11 ms |
3420 KB |
Output is correct |
29 |
Correct |
8 ms |
2880 KB |
Output is correct |
30 |
Correct |
8 ms |
3164 KB |
Output is correct |
31 |
Correct |
7 ms |
3020 KB |
Output is correct |
32 |
Correct |
8 ms |
2908 KB |
Output is correct |
33 |
Correct |
8 ms |
2908 KB |
Output is correct |
34 |
Correct |
8 ms |
3156 KB |
Output is correct |
35 |
Correct |
9 ms |
3160 KB |
Output is correct |
36 |
Correct |
238 ms |
3388 KB |
Output is correct |
37 |
Correct |
343 ms |
3164 KB |
Output is correct |
38 |
Correct |
409 ms |
3420 KB |
Output is correct |
39 |
Correct |
88 ms |
3376 KB |
Output is correct |
40 |
Correct |
456 ms |
3468 KB |
Output is correct |
41 |
Correct |
607 ms |
3440 KB |
Output is correct |
42 |
Correct |
792 ms |
3668 KB |
Output is correct |
43 |
Correct |
623 ms |
3160 KB |
Output is correct |
44 |
Correct |
772 ms |
3080 KB |
Output is correct |
45 |
Correct |
224 ms |
3164 KB |
Output is correct |
46 |
Correct |
329 ms |
3372 KB |
Output is correct |
47 |
Correct |
524 ms |
3164 KB |
Output is correct |
48 |
Correct |
238 ms |
2908 KB |
Output is correct |
49 |
Correct |
807 ms |
3120 KB |
Output is correct |
50 |
Correct |
343 ms |
3384 KB |
Output is correct |
51 |
Correct |
1672 ms |
3392 KB |
Output is correct |
52 |
Correct |
1687 ms |
3388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
3392 KB |
Output is correct |
3 |
Correct |
7 ms |
3164 KB |
Output is correct |
4 |
Correct |
8 ms |
3164 KB |
Output is correct |
5 |
Correct |
7 ms |
3160 KB |
Output is correct |
6 |
Correct |
11 ms |
3460 KB |
Output is correct |
7 |
Correct |
7 ms |
2908 KB |
Output is correct |
8 |
Correct |
11 ms |
3420 KB |
Output is correct |
9 |
Correct |
8 ms |
2880 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
7 ms |
3020 KB |
Output is correct |
12 |
Correct |
8 ms |
2908 KB |
Output is correct |
13 |
Correct |
8 ms |
2908 KB |
Output is correct |
14 |
Correct |
8 ms |
3156 KB |
Output is correct |
15 |
Correct |
9 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Incorrect |
146 ms |
34132 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
8 ms |
3392 KB |
Output is correct |
3 |
Correct |
7 ms |
3164 KB |
Output is correct |
4 |
Correct |
8 ms |
3164 KB |
Output is correct |
5 |
Correct |
7 ms |
3160 KB |
Output is correct |
6 |
Correct |
11 ms |
3460 KB |
Output is correct |
7 |
Correct |
7 ms |
2908 KB |
Output is correct |
8 |
Correct |
11 ms |
3420 KB |
Output is correct |
9 |
Correct |
8 ms |
2880 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
7 ms |
3020 KB |
Output is correct |
12 |
Correct |
8 ms |
2908 KB |
Output is correct |
13 |
Correct |
8 ms |
2908 KB |
Output is correct |
14 |
Correct |
8 ms |
3156 KB |
Output is correct |
15 |
Correct |
9 ms |
3160 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1831 ms |
34268 KB |
Output is correct |
18 |
Correct |
1153 ms |
34888 KB |
Output is correct |
19 |
Correct |
1143 ms |
33804 KB |
Output is correct |
20 |
Correct |
931 ms |
34616 KB |
Output is correct |
21 |
Correct |
1699 ms |
34304 KB |
Output is correct |
22 |
Correct |
1251 ms |
34728 KB |
Output is correct |
23 |
Correct |
1537 ms |
33776 KB |
Output is correct |
24 |
Correct |
1096 ms |
34648 KB |
Output is correct |
25 |
Correct |
1319 ms |
33984 KB |
Output is correct |
26 |
Correct |
482 ms |
33284 KB |
Output is correct |
27 |
Correct |
687 ms |
33244 KB |
Output is correct |
28 |
Correct |
762 ms |
33644 KB |
Output is correct |
29 |
Correct |
566 ms |
33284 KB |
Output is correct |
30 |
Correct |
737 ms |
33236 KB |
Output is correct |
31 |
Correct |
913 ms |
33480 KB |
Output is correct |
32 |
Correct |
1112 ms |
34016 KB |
Output is correct |
33 |
Correct |
585 ms |
31892 KB |
Output is correct |
34 |
Correct |
1092 ms |
34644 KB |
Output is correct |
35 |
Correct |
528 ms |
32672 KB |
Output is correct |
36 |
Correct |
987 ms |
33752 KB |
Output is correct |
37 |
Correct |
813 ms |
33260 KB |
Output is correct |
38 |
Correct |
530 ms |
33148 KB |
Output is correct |
39 |
Correct |
580 ms |
34068 KB |
Output is correct |
40 |
Correct |
311 ms |
33848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2468 KB |
Output is correct |
13 |
Correct |
2 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2520 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
8 ms |
3392 KB |
Output is correct |
23 |
Correct |
7 ms |
3164 KB |
Output is correct |
24 |
Correct |
8 ms |
3164 KB |
Output is correct |
25 |
Correct |
7 ms |
3160 KB |
Output is correct |
26 |
Correct |
11 ms |
3460 KB |
Output is correct |
27 |
Correct |
7 ms |
2908 KB |
Output is correct |
28 |
Correct |
11 ms |
3420 KB |
Output is correct |
29 |
Correct |
8 ms |
2880 KB |
Output is correct |
30 |
Correct |
8 ms |
3164 KB |
Output is correct |
31 |
Correct |
7 ms |
3020 KB |
Output is correct |
32 |
Correct |
8 ms |
2908 KB |
Output is correct |
33 |
Correct |
8 ms |
2908 KB |
Output is correct |
34 |
Correct |
8 ms |
3156 KB |
Output is correct |
35 |
Correct |
9 ms |
3160 KB |
Output is correct |
36 |
Correct |
238 ms |
3388 KB |
Output is correct |
37 |
Correct |
343 ms |
3164 KB |
Output is correct |
38 |
Correct |
409 ms |
3420 KB |
Output is correct |
39 |
Correct |
88 ms |
3376 KB |
Output is correct |
40 |
Correct |
456 ms |
3468 KB |
Output is correct |
41 |
Correct |
607 ms |
3440 KB |
Output is correct |
42 |
Correct |
792 ms |
3668 KB |
Output is correct |
43 |
Correct |
623 ms |
3160 KB |
Output is correct |
44 |
Correct |
772 ms |
3080 KB |
Output is correct |
45 |
Correct |
224 ms |
3164 KB |
Output is correct |
46 |
Correct |
329 ms |
3372 KB |
Output is correct |
47 |
Correct |
524 ms |
3164 KB |
Output is correct |
48 |
Correct |
238 ms |
2908 KB |
Output is correct |
49 |
Correct |
807 ms |
3120 KB |
Output is correct |
50 |
Correct |
343 ms |
3384 KB |
Output is correct |
51 |
Correct |
1672 ms |
3392 KB |
Output is correct |
52 |
Correct |
1687 ms |
3388 KB |
Output is correct |
53 |
Correct |
1 ms |
2392 KB |
Output is correct |
54 |
Incorrect |
146 ms |
34132 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |