#include <bits/stdc++.h>
using namespace std;
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define INF 2023456789
#define all(x) x.begin(),x.end()
typedef pair<int,int> ii;
typedef long long ll;
#define maxn 120005
int n,m,q,c[maxn],a[maxn];
struct node{
int s,e,m,v,lz;
node *l,*r;
node(int _s,int _e){
s=_s,e=_e,m=(s+e)>>1,lz=-1;
if(s!=e){
l=new node(s,m),r=new node(m+1,e);
v=l->v+r->v;
}
else v=c[s];
}
void ppo(){
if(lz!=-1){
v=(e-s+1)*lz;
if(s!=e){
l->lz=lz;
r->lz=lz;
}
lz=-1;
}
}
void up(int x,int y,int nv){
if(s==x&&e==y){lz=nv;return;}
if(y<=m)l->up(x,y,nv);
else if(x>m)r->up(x,y,nv);
else l->up(x,m,nv),r->up(m+1,y,nv);
l->ppo(),r->ppo();
v=l->v+r->v;
}
int qry(int x,int y){
if(x>y)return 0;
ppo();
if(s==x&&e==y)return v;
if(y<=m)return l->qry(x,y);
if(x>m)return r->qry(x,y);
return l->qry(x,m)+r->qry(m+1,y);
}
int findR(int num){
ppo();
if(s==e){
if(num==1&&v==0)return s;
else return -1;
}
l->ppo(),r->ppo();
int lnum=l->e-l->s+1-l->v;
if(lnum>=num)return l->findR(num);
else return r->findR(num-lnum);
}
int findB(int num){
ppo();
if(s==e){
if(num==1&&v==1)return s;
else return -1;
}
l->ppo(),r->ppo();
if(l->v>=num)return l->findB(num);
else return r->findB(num-l->v);
}
}*rt;
int main(){
sf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
char ch;sf(" %c",&ch);
c[i]=(ch=='B');
}
for(int i=1;i<=m;++i)sf("%d",&a[i]);
sf("%d",&q);
for(int _=0;_<q;++_){
int l,r;sf("%d%d",&l,&r);
rt=new node(1,n);
for(int i=l;i<=r;++i){
rt->up(a[i],a[i],0);
int lv=a[i]-1-rt->qry(1,a[i]-1);//number of R
int rv=rt->qry(a[i]+1,n);//number of B
if(rv<=lv){//the rv-th R to n become B
int x=lv-rv+1;
int p=rt->findR(x);
if(p==-1)p=a[i];
rt->up(p,n,1);
}
else{//1 to the lv+1-th B become R
int x=rt->qry(1,a[i])+lv+1;
int p=rt->findB(x);
rt->up(1,p,0);
}
}
pf("%d\n",n-rt->qry(1,n));
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:79:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | sf("%d%d",&n,&m);
| ^
Main.cpp:81:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | char ch;sf(" %c",&ch);
| ^
Main.cpp:84:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | for(int i=1;i<=m;++i)sf("%d",&a[i]);
| ^
Main.cpp:85:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | sf("%d",&q);
| ^
Main.cpp:87:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | int l,r;sf("%d%d",&l,&r);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
1040 KB |
Output is correct |
11 |
Correct |
4 ms |
980 KB |
Output is correct |
12 |
Correct |
6 ms |
980 KB |
Output is correct |
13 |
Correct |
5 ms |
1024 KB |
Output is correct |
14 |
Correct |
5 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
1040 KB |
Output is correct |
11 |
Correct |
4 ms |
980 KB |
Output is correct |
12 |
Correct |
6 ms |
980 KB |
Output is correct |
13 |
Correct |
5 ms |
1024 KB |
Output is correct |
14 |
Correct |
5 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
339 ms |
58196 KB |
Output is correct |
19 |
Correct |
450 ms |
58108 KB |
Output is correct |
20 |
Correct |
734 ms |
58320 KB |
Output is correct |
21 |
Correct |
906 ms |
58584 KB |
Output is correct |
22 |
Correct |
432 ms |
58032 KB |
Output is correct |
23 |
Correct |
426 ms |
58340 KB |
Output is correct |
24 |
Correct |
379 ms |
57808 KB |
Output is correct |
25 |
Correct |
376 ms |
58060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
3059 ms |
1188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
3059 ms |
1188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
3059 ms |
1188 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
4 ms |
1040 KB |
Output is correct |
11 |
Correct |
4 ms |
980 KB |
Output is correct |
12 |
Correct |
6 ms |
980 KB |
Output is correct |
13 |
Correct |
5 ms |
1024 KB |
Output is correct |
14 |
Correct |
5 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
339 ms |
58196 KB |
Output is correct |
19 |
Correct |
450 ms |
58108 KB |
Output is correct |
20 |
Correct |
734 ms |
58320 KB |
Output is correct |
21 |
Correct |
906 ms |
58584 KB |
Output is correct |
22 |
Correct |
432 ms |
58032 KB |
Output is correct |
23 |
Correct |
426 ms |
58340 KB |
Output is correct |
24 |
Correct |
379 ms |
57808 KB |
Output is correct |
25 |
Correct |
376 ms |
58060 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Execution timed out |
3059 ms |
1188 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |