This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 250000 + 5;
int n,st,q;
int a[maxn];
int pos[maxn];
int seg[maxn*4];
void update(int pos, int l, int r, int x, int val) {
if(l>r || r<x || x<l) return ;
if(l==r) {
seg[pos] = val;
return ;
}
int mid = (l+r)/2;
update(pos<<1,l,mid,x,val); update(pos<<1|1,mid+1,r,x,val);
seg[pos] = max(seg[pos<<1], seg[pos<<1|1]);
}
int query(int pos, int l, int r, int x, int y) {
if(l>r || r<x || y<l) return 0;
if(x<=l && r<=y) return seg[pos];
int mid = (l+r)/2;
return max(query(pos<<1,l,mid,x,y), query(pos<<1|1,mid+1,r,x,y));
}
int main() {
scanf("%d%d",&n,&st);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) {
pos[n-a[i]+1] = i;
update(1,1,n,i,a[i]);
}
scanf("%d",&q);
while(q--) {
char type; scanf(" %c",&type);
// for(int i=1;i<=n;i++) printf("%d ",query(1,1,n,i,i));
// printf("\n");
if(type=='F') {
int x; scanf("%d",&x);
if(x==st) printf("0\n");
else if(x>st) {
int mx = query(1,1,n,st+1,x);
int l = 1, r = st-1, mid, res = st;
while(l<=r) {
mid = (l+r)/2;
if(query(1,1,n,mid,st-1)<=mx) {
res = mid;
r = mid-1;
}
else l = mid+1;
}
printf("%d\n",x-res);
}
else {
int mx = query(1,1,n,x,st-1);
int l = st+1, r = n, mid, res = st;
while(l<=r) {
mid = (l+r)/2;
if(query(1,1,n,st+1,mid)<=mx) {
res = mid;
l = mid+1;
}
else r = mid-1;
}
printf("%d\n",res-x);
}
}
else {
int x,ra; scanf("%d%d",&x,&ra);
for(int i=ra-1;i>=1;i--) update(1,1,n,pos[i],query(1,1,n,pos[i],pos[i])+1);
update(1,1,n,x,query(1,1,n,pos[ra],pos[ra])+1);
int hey = 15;
for(int i=1;i<=15;i++) if(pos[i]==x) hey = i;
for(int i=hey;i>=ra;i--) pos[i] = pos[i-1];
pos[ra] = x;
// for(int i=1;i<=n;i++) printf("%d ",pos[i]);
// printf("\n");
}
}
}
Compilation message (stderr)
cake.cpp: In function 'int main()':
cake.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&st);
~~~~~^~~~~~~~~~~~~~~
cake.cpp:31:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
cake.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&q);
~~~~~^~~~~~~~~
cake.cpp:38:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
char type; scanf(" %c",&type);
~~~~~^~~~~~~~~~~~~
cake.cpp:42:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%d",&x);
~~~~~^~~~~~~~~
cake.cpp:72:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x,ra; scanf("%d%d",&x,&ra);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |