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 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);
else
{
update(1,1,n,i,1);
if(!s.empty())
{
update(1,1,n,s.top(),-1);
s.pop();
}
}
for(auto x:qu[i])
{
arboreintervale arb=interval(1,1,n,x.first);
ans[x.second]=-arb.minp+s.size();
}
}
for(int i=1;i<=q;++i)
cout<<ans[i]<<'\n';
return 0;
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | 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... |