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;
char a[500010];
bool e[500010];
int n,q,x,y,ans[500010];
vector<pair<int,int>>qu[500010];
struct arboreintervale{int sum,minp;}aint[2000010],zero;
arboreintervale join(arboreintervale a,arboreintervale b)
{
arboreintervale c;
c.sum=a.sum+b.sum;
c.minp=min(a.minp,a.sum+b.minp);
return c;
}
stack<int>s;
void update(int poz,int b,int e,int pozu,int val)
{
if(b>pozu||e<pozu)
return;
if(b==e)
{
aint[poz].sum=val;
aint[poz].minp=min(0,val);
return;
}
update(poz*2,b,(b+e)/2,pozu,val);
update(poz*2+1,(b+e)/2+1,e,pozu,val);
aint[poz]=join(aint[poz*2],aint[poz*2+1]);
}
arboreintervale interval(int poz,int b,int e,int lim)
{
if(e<lim)
return zero;
if(b>=lim)
return aint[poz];
return join(interval(poz*2,b,(b+e)/2,lim),interval(poz*2+1,(b+e)/2+1,e,lim));
}
int aib[500010];
int suma(int poz)
{
int ans=0;
for(;poz>=1;poz-=(poz&(-poz)))
ans+=aib[poz];
return ans;
}
void aduna(int poz,int val)
{
for(;poz<=n;poz+=(poz&(-poz)))
aib[poz]+=val;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>a+1>>q;
for(int i=1;i<=q;++i)
{
cin>>x>>y;
qu[y].push_back({x,i});
}
for(int i=1;i<=n;++i)
{
if(a[i]=='T')
{
s.push(i);
aduna(i,1);
}
else
{
update(1,1,n,i,1);
if(!s.empty())
{
update(1,1,n,s.top(),-1);
aduna(s.top(),-1);
s.pop();
}
}
for(auto x:qu[i])
{
arboreintervale arb=interval(1,1,n,x.first);
ans[x.second]=s.size()-arb.minp-suma(x.first-1);
}
}
for(int i=1;i<=q;++i)
cout<<ans[i]<<'\n';
return 0;
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:56:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | cin>>n>>a+1>>q;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |