This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
int n,q,sol[500005];
vector<pii> qr[500005];
string ss;
int v[500005];
ll arb[2000005];
int toprop[2000005];
void prop(int nod)
{
ll val=toprop[nod];
for(int i=nod*2;i<=nod*2+1;i++)
{
arb[i]+=val;
toprop[i]+=val;
}
}
void update(int nod,int st,int dr,int a,int b,int val)
{
if(toprop[nod]&&st!=dr)
prop(nod);
toprop[nod]=0;
if(st>=a&&dr<=b)
{
arb[nod]+=val;
toprop[nod]+=val;
return;
}
int 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]=min(arb[nod*2],arb[nod*2+1]);
}
ll query(int nod,int st,int dr, int a, int b)
{
if(toprop[nod]&&st!=dr)
prop(nod);
toprop[nod]=0;
if(st>=a&&dr<=b)
return arb[nod];
int mij=(st+dr)/2;
if(a<=mij)
return query(nod*2,st,mij,a,b);
if(b>mij)
return query(nod*2+1,mij+1,dr,a,b);
assert(false);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cin>>ss;
for(int i=0;i<n;i++)
{
if(ss[i]=='C')
v[i+1]=1;
else
v[i+1]=-1;
}
cin>>q;
for(int i=1;i<=q;i++)
{
int l,r;
cin>>l>>r;
qr[l].push_back({r,i});
}
deque<int> coada;
for(int i=n;i>=1;i--)
{
if(v[i]==-1)
coada.push_front(i);
else
{
update(1,1,n,1,i,1);
if(!coada.empty())
{
int poz=coada.front();
update(1,1,n,1,poz,-1);
coada.pop_front();
}
}
for(auto j:qr[i])
{
int poz=j.first,index=j.second;
int val=0;
int st=0;
int dr=coada.size();
dr--;
while(st<=dr)
{
int mij=(st+dr)/2;
if(coada[mij]<=poz)
{
val=mij+1;
st=mij+1;
}
else
dr=mij-1;
}
ll a=query(1,1,n,i,poz);
int x=a;
if(poz+1<=n)
{
a=query(1,1,n,poz+1,poz+1);
x-=a;
}
if(x<0)
val+=abs(x);
sol[index]=val;
}
}
for(int i=1;i<=q;i++)
cout<<sol[i]<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |