#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];
ll rez=1e10;
int mij=(st+dr)/2;
if(a<=mij)
rez=min(rez,query(nod*2,st,mij,a,b));
if(b>mij)
rez=min(rez,query(nod*2+1,mij+1,dr,a,b));
return rez;
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12112 KB |
Output is correct |
2 |
Correct |
9 ms |
12108 KB |
Output is correct |
3 |
Correct |
8 ms |
12136 KB |
Output is correct |
4 |
Correct |
8 ms |
12108 KB |
Output is correct |
5 |
Correct |
8 ms |
12108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12112 KB |
Output is correct |
2 |
Correct |
9 ms |
12108 KB |
Output is correct |
3 |
Correct |
8 ms |
12136 KB |
Output is correct |
4 |
Correct |
8 ms |
12108 KB |
Output is correct |
5 |
Correct |
8 ms |
12108 KB |
Output is correct |
6 |
Correct |
74 ms |
17392 KB |
Output is correct |
7 |
Correct |
89 ms |
17048 KB |
Output is correct |
8 |
Correct |
75 ms |
18004 KB |
Output is correct |
9 |
Correct |
72 ms |
18352 KB |
Output is correct |
10 |
Correct |
77 ms |
18272 KB |
Output is correct |
11 |
Correct |
103 ms |
18500 KB |
Output is correct |
12 |
Correct |
80 ms |
18512 KB |
Output is correct |
13 |
Correct |
84 ms |
18664 KB |
Output is correct |
14 |
Correct |
77 ms |
18440 KB |
Output is correct |
15 |
Correct |
99 ms |
18396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12112 KB |
Output is correct |
2 |
Correct |
9 ms |
12108 KB |
Output is correct |
3 |
Correct |
8 ms |
12136 KB |
Output is correct |
4 |
Correct |
8 ms |
12108 KB |
Output is correct |
5 |
Correct |
8 ms |
12108 KB |
Output is correct |
6 |
Correct |
74 ms |
17392 KB |
Output is correct |
7 |
Correct |
89 ms |
17048 KB |
Output is correct |
8 |
Correct |
75 ms |
18004 KB |
Output is correct |
9 |
Correct |
72 ms |
18352 KB |
Output is correct |
10 |
Correct |
77 ms |
18272 KB |
Output is correct |
11 |
Correct |
103 ms |
18500 KB |
Output is correct |
12 |
Correct |
80 ms |
18512 KB |
Output is correct |
13 |
Correct |
84 ms |
18664 KB |
Output is correct |
14 |
Correct |
77 ms |
18440 KB |
Output is correct |
15 |
Correct |
99 ms |
18396 KB |
Output is correct |
16 |
Correct |
711 ms |
48928 KB |
Output is correct |
17 |
Correct |
514 ms |
44956 KB |
Output is correct |
18 |
Correct |
639 ms |
46180 KB |
Output is correct |
19 |
Correct |
579 ms |
47608 KB |
Output is correct |
20 |
Correct |
714 ms |
48048 KB |
Output is correct |
21 |
Correct |
735 ms |
50228 KB |
Output is correct |
22 |
Correct |
752 ms |
49828 KB |
Output is correct |
23 |
Correct |
715 ms |
48924 KB |
Output is correct |
24 |
Correct |
679 ms |
49916 KB |
Output is correct |
25 |
Correct |
671 ms |
49220 KB |
Output is correct |