#include<fstream>
#include<vector>
#include<deque>
#include<algorithm>
#include<iostream>
using namespace std;
//ifstream fi("election.in");
//ofstream fo("election.out");
char S[500005];
vector<pair<int,int> > Q[500005];
deque<int> Canceled;
int Rez[500005];
int MinSuf[2000005],Sum[2000005];
void update(int nod, int st, int dr, int poz, int val)
{
if(st==dr)
{
MinSuf[nod]=min(0,val);
Sum[nod]=val;
return;
}
int mij=(st+dr)/2;
if(poz<=mij)
update(2*nod,st,mij,poz,val);
else
update(2*nod+1,mij+1,dr,poz,val);
Sum[nod]=Sum[2*nod]+Sum[2*nod+1];
MinSuf[nod]=min(MinSuf[2*nod+1],Sum[2*nod+1]+MinSuf[2*nod]);
}
int sumsuf;
int getDecalaj(int nod, int st, int dr, int l, int r)
{
if(l<=st && dr<=r)
{
int _sumsuf=sumsuf;
sumsuf+=Sum[nod];
return _sumsuf+MinSuf[nod];
}
int mij=(st+dr)/2;
int rez=0;
if(r>mij)
rez=min(rez,getDecalaj(2*nod+1,mij+1,dr,l,r));
if(l<=mij)
rez=min(rez,getDecalaj(2*nod,st,mij,l,r));
return rez;
}
int src(int poz)
{
int rez=-1;
for(int i=18; i>=0; i--)
{
if(rez+(1<<i)<Canceled.size() && Canceled[rez+(1<<i)]>poz)
rez+=(1<<i);
}
return Canceled.size()-rez-1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
cin>>(S+1);
int q;
cin>>q;
for(int i=1; i<=q; i++)
{
int x,y;
cin>>x>>y;
Q[x].push_back({y,i});
}
for(int i=n; i>=1; i--)
{
if(S[i]=='C')
{
update(1,1,n,i,1);
if(!Canceled.empty())
{
int last=Canceled.back();
Canceled.pop_back();
update(1,1,n,last,-1);
}
}
else
{
Canceled.push_back(i);
}
for(auto qry:Q[i])
{
int dr=qry.first;
int ind=qry.second;
sumsuf=0;
int stdr=src(dr);
int drst=-getDecalaj(1,1,n,i,dr);
Rez[ind]=stdr+drst;
}
}
for(int i=1; i<=q; i++)
cout<<Rez[i]<<"\n";
//fi.close();
//fo.close();
return 0;
}
Compilation message
election.cpp: In function 'int src(int)':
election.cpp:65:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(rez+(1<<i)<Canceled.size() && Canceled[rez+(1<<i)]>poz)
~~~~~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12288 KB |
Output is correct |
2 |
Correct |
12 ms |
12288 KB |
Output is correct |
3 |
Correct |
12 ms |
12288 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12288 KB |
Output is correct |
2 |
Correct |
12 ms |
12288 KB |
Output is correct |
3 |
Correct |
12 ms |
12288 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
74 ms |
17272 KB |
Output is correct |
7 |
Correct |
65 ms |
16632 KB |
Output is correct |
8 |
Correct |
67 ms |
16760 KB |
Output is correct |
9 |
Correct |
75 ms |
17276 KB |
Output is correct |
10 |
Correct |
71 ms |
17016 KB |
Output is correct |
11 |
Correct |
74 ms |
17272 KB |
Output is correct |
12 |
Correct |
75 ms |
17272 KB |
Output is correct |
13 |
Correct |
74 ms |
17016 KB |
Output is correct |
14 |
Correct |
85 ms |
17276 KB |
Output is correct |
15 |
Correct |
73 ms |
17272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
12288 KB |
Output is correct |
2 |
Correct |
12 ms |
12288 KB |
Output is correct |
3 |
Correct |
12 ms |
12288 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
74 ms |
17272 KB |
Output is correct |
7 |
Correct |
65 ms |
16632 KB |
Output is correct |
8 |
Correct |
67 ms |
16760 KB |
Output is correct |
9 |
Correct |
75 ms |
17276 KB |
Output is correct |
10 |
Correct |
71 ms |
17016 KB |
Output is correct |
11 |
Correct |
74 ms |
17272 KB |
Output is correct |
12 |
Correct |
75 ms |
17272 KB |
Output is correct |
13 |
Correct |
74 ms |
17016 KB |
Output is correct |
14 |
Correct |
85 ms |
17276 KB |
Output is correct |
15 |
Correct |
73 ms |
17272 KB |
Output is correct |
16 |
Correct |
620 ms |
43188 KB |
Output is correct |
17 |
Correct |
476 ms |
38904 KB |
Output is correct |
18 |
Correct |
545 ms |
40348 KB |
Output is correct |
19 |
Correct |
527 ms |
41772 KB |
Output is correct |
20 |
Correct |
611 ms |
42232 KB |
Output is correct |
21 |
Correct |
657 ms |
44408 KB |
Output is correct |
22 |
Correct |
629 ms |
43984 KB |
Output is correct |
23 |
Correct |
643 ms |
43256 KB |
Output is correct |
24 |
Correct |
625 ms |
43384 KB |
Output is correct |
25 |
Correct |
613 ms |
43336 KB |
Output is correct |