#include<bits/stdc++.h>
using namespace std;
const int nmax=2.5e5+42;
int n,a;
int inp[nmax],where[nmax];
int tree[4*nmax];
void update(int node,int l,int r,int pos,int val)
{
if(l==r)
{
tree[node]=val;
return;
}
int av=(l+r)/2;
if(pos<=av)update(node*2,l,av,pos,val);
else update(node*2+1,av+1,r,pos,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
int query(int node,int l,int r,int lq,int rq)
{
if(l==lq&&r==rq)return tree[node];
int av=(l+r)/2,ret=0;
if(lq<=av)ret=max(ret,query(node*2,l,av,lq,min(av,rq)));
if(av<rq)ret=max(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));
return ret;
}
int pointer=0;
set< pair<int,int> > active;
void make_best(int pos)
{
pointer++;
active.erase({inp[pos],pos});
inp[pos]=pointer;
active.insert({inp[pos],pos});
update(1,1,n,pos,inp[pos]);
//cout<<"make best ";for(int i=1;i<=n;i++)cout<<inp[i]<<" ";cout<<endl;
}
int go_right(int val)
{
int ok=a,not_ok=n+1;
while(not_ok-ok>1)
{
int av=(ok+not_ok)/2;
if(query(1,1,n,a+1,av)<val)ok=av;
else not_ok=av;
}
return ok;
}
int go_left(int val)
{
int ok=a,not_ok=0;
while(ok-not_ok>1)
{
int av=(ok+not_ok)/2;
if(query(1,1,n,av,a-1)<val)ok=av;
else not_ok=av;
}
return ok;
}
int main()
{
scanf("%i%i",&n,&a);
for(int i=1;i<=n;i++)
{
scanf("%i",&inp[i]);
where[inp[i]]=i;
}
for(int i=1;i<=n;i++)make_best(where[i]);
int q;
scanf("%i",&q);
for(int i=1;i<=q;i++)
{
char c=getchar();
while(c!='E'&&c!='F')c=getchar();
if(c=='F')
{
int pos;
scanf("%i",&pos);
if(pos==a)
{
printf("0\n");
continue;
}
int maxi=0;
if(pos<a)
{
maxi=query(1,1,n,pos,a-1);
int outp=a-pos;
if(a!=n)outp+=go_right(maxi)-a;
printf("%i\n",outp);
}
else//pos>a
{
maxi=query(1,1,n,a+1,pos);
int outp=pos-a;
if(a!=1)outp+=a-go_left(maxi);
printf("%i\n",outp);
}
}
else
{
int pos,val;
scanf("%i%i",&pos,&val);
make_best(pos);
set< pair<int,int> >::iterator it=active.end();
it--;
vector<int> noted={};
while(1)
{
if((*it).second==pos){it--;continue;}
if(val==1)break;
noted.push_back((*it).second);
val--;
it--;
}
reverse(noted.begin(),noted.end());
for(auto w:noted)make_best(w);
}
}
return 0;
}
Compilation message
cake.cpp: In function 'int main()':
cake.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i",&n,&a);
~~~~~^~~~~~~~~~~~~~
cake.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&inp[i]);
~~~~~^~~~~~~~~~~~~~
cake.cpp:93:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&q);
~~~~~^~~~~~~~~
cake.cpp:103:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&pos);
~~~~~^~~~~~~~~~~
cake.cpp:138:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i",&pos,&val);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
13 ms |
512 KB |
Output is correct |
5 |
Correct |
34 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
692 ms |
5500 KB |
Output is correct |
2 |
Correct |
392 ms |
5624 KB |
Output is correct |
3 |
Correct |
442 ms |
5500 KB |
Output is correct |
4 |
Correct |
271 ms |
5624 KB |
Output is correct |
5 |
Correct |
773 ms |
6604 KB |
Output is correct |
6 |
Correct |
626 ms |
7032 KB |
Output is correct |
7 |
Correct |
516 ms |
6904 KB |
Output is correct |
8 |
Correct |
316 ms |
6904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
476 ms |
8864 KB |
Output is correct |
2 |
Correct |
322 ms |
8700 KB |
Output is correct |
3 |
Correct |
337 ms |
8696 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
565 ms |
19168 KB |
Output is correct |
6 |
Correct |
646 ms |
19192 KB |
Output is correct |
7 |
Correct |
539 ms |
19192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
1108 KB |
Output is correct |
2 |
Correct |
94 ms |
1272 KB |
Output is correct |
3 |
Correct |
244 ms |
4760 KB |
Output is correct |
4 |
Correct |
265 ms |
4728 KB |
Output is correct |
5 |
Correct |
267 ms |
2040 KB |
Output is correct |
6 |
Correct |
489 ms |
6520 KB |
Output is correct |
7 |
Correct |
339 ms |
3192 KB |
Output is correct |
8 |
Correct |
314 ms |
9464 KB |
Output is correct |
9 |
Execution timed out |
2071 ms |
24140 KB |
Time limit exceeded |
10 |
Correct |
862 ms |
5264 KB |
Output is correct |
11 |
Correct |
1258 ms |
7544 KB |
Output is correct |
12 |
Execution timed out |
2080 ms |
21040 KB |
Time limit exceeded |
13 |
Execution timed out |
2091 ms |
24184 KB |
Time limit exceeded |