#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,st;
int val,idx,maxi;
bool done;
int ns[250000];
int pos[250000];
vector<int> seg;
void init(){
int s=1;
for(;s<n;s<<=1); s<<=1;
seg.assign(s,0);
}
int query(int p, int lo ,int hi, int l, int r){
if(lo>=l && hi<=r) return seg[p];
if(hi<l || lo>r) return 0;
int mid=(lo+hi)/2;
int x=query(p*2,lo,mid,l,r);
int y=query(p*2+1,mid+1,hi,l,r);
return max(x,y);
}
void build(int p, int lo, int hi){
if(lo==hi){
seg[p]=(ll)ns[lo];
pos[lo]=p;
return;
}
int mid=(lo+hi)/2;
build(p*2,lo,mid);
build(p*2+1,mid+1,hi);
seg[p]=max(seg[p*2],seg[p*2+1]);
}
void update(int p, int lo, int hi, int dx, int va){
if(lo==hi){
if(lo==dx) seg[p]=va;
return;
}
int mid=(lo+hi)/2;
if(dx<=mid) update(p*2,lo,mid,dx,va);
else update(p*2+1,mid+1,hi,dx,va);
seg[p]=max(seg[p*2],seg[p*2+1]);
}
void le(int p ,int lo, int hi){
if(seg[p]<val) return;
if(lo==hi && !done){
done=true;
idx=lo;
return;
}
if(hi<=st) return;
int mid=(lo+hi)/2;
le(p*2,lo,mid);
if(done) return;
le(p*2+1,mid+1,hi);
}
void ri(int p ,int lo, int hi){
if(seg[p]<val) return;
if(lo==hi && !done){
done=true;
idx=lo;
return;
}
if(lo>=st) return;
int mid=(lo+hi)/2;
ri(p*2+1,mid+1,hi);
if(done) return;
ri(p*2,lo,mid);
}
int main(){
scanf("%d%d",&n,&st); st--; maxi=n;
priority_queue<pair<int,int> > pq;
for(int i=0;i<n;i++){
scanf("%d",&ns[i]);
pq.push({ns[i],i});
}
int Q; scanf("%d",&Q); init(); build(1,0,n-1);
while(Q--){
char q; cin>>q;
if(q=='F'){done=false;
int i; scanf("%d",&i); i--;
if(i==st) printf("0\n");
else if(i<st){
val=query(1,0,n-1,i,st-1);
le(1,0,n-1);
if(!done) printf("%d\n", st-i+(n-st)-1);
else printf("%d\n", (st-i)+(idx-st)-1);
}
else{
val=query(1,0,n-1,st+1,i);
ri(1,0,n-1);
if(done) printf("%d\n", i-st+(st-idx)-1);
else printf("%d\n", (i-st)+st-1);
}
}
else{
int i,j; scanf("%d%d",&i,&j); i--; int cp=maxi+1; maxi+=j;
for(int I=0;I<j-1;I++){
pair<int,int> k = pq.top();
pq.pop();
update(1,0,n-1,k.second,maxi-I);
pq.push({maxi-I,k.second});
}
pq.push({cp,i});
update(1,0,n-1,i,cp);
//for(int i=0;i<n;i++) printf("%d ", seg[pos[i]]); printf("\n");
}
}
}
Compilation message
cake.cpp: In function 'void init()':
cake.cpp:12:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for(;s<n;s<<=1); s<<=1;
^~~
cake.cpp:12:19: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
for(;s<n;s<<=1); s<<=1;
^
cake.cpp: In function 'int main()':
cake.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&st); st--; maxi=n;
~~~~~^~~~~~~~~~~~~~~
cake.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&ns[i]);
~~~~~^~~~~~~~~~~~~
cake.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int Q; scanf("%d",&Q); init(); build(1,0,n-1);
~~~~~^~~~~~~~~
cake.cpp:81:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int i; scanf("%d",&i); i--;
~~~~~^~~~~~~~~
cake.cpp:97:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int i,j; scanf("%d%d",&i,&j); i--; int cp=maxi+1; maxi+=j;
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
548 ms |
9236 KB |
Output isn't correct |
2 |
Incorrect |
514 ms |
13860 KB |
Output isn't correct |
3 |
Incorrect |
411 ms |
18044 KB |
Output isn't correct |
4 |
Incorrect |
277 ms |
22528 KB |
Output isn't correct |
5 |
Incorrect |
602 ms |
27768 KB |
Output isn't correct |
6 |
Incorrect |
570 ms |
37100 KB |
Output isn't correct |
7 |
Incorrect |
571 ms |
41908 KB |
Output isn't correct |
8 |
Incorrect |
317 ms |
46960 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
282 ms |
46960 KB |
Output isn't correct |
2 |
Incorrect |
274 ms |
46960 KB |
Output isn't correct |
3 |
Incorrect |
240 ms |
46960 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
46960 KB |
Output isn't correct |
5 |
Incorrect |
350 ms |
51392 KB |
Output isn't correct |
6 |
Incorrect |
360 ms |
54040 KB |
Output isn't correct |
7 |
Incorrect |
260 ms |
56028 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
90 ms |
56028 KB |
Output isn't correct |
2 |
Incorrect |
83 ms |
56028 KB |
Output isn't correct |
3 |
Incorrect |
151 ms |
56028 KB |
Output isn't correct |
4 |
Incorrect |
172 ms |
56028 KB |
Output isn't correct |
5 |
Incorrect |
298 ms |
56028 KB |
Output isn't correct |
6 |
Incorrect |
335 ms |
59204 KB |
Output isn't correct |
7 |
Incorrect |
398 ms |
59204 KB |
Output isn't correct |
8 |
Incorrect |
254 ms |
65188 KB |
Output isn't correct |
9 |
Incorrect |
1234 ms |
75180 KB |
Output isn't correct |
10 |
Incorrect |
920 ms |
75180 KB |
Output isn't correct |
11 |
Incorrect |
1178 ms |
79620 KB |
Output isn't correct |
12 |
Incorrect |
1265 ms |
88312 KB |
Output isn't correct |
13 |
Incorrect |
1159 ms |
95460 KB |
Output isn't correct |