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>
#define N 250050
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
int n, a, v[N], ans[N], q, bit[N];
void update_BIT(int pos, int val)
{
//cout<<"COLOCA "<<pos<<"\n";
for(int i = pos; i < N; i += (i&-i)) bit[i] += val;
}
int Find_kth(int x)
{
int resp = 0;
x--;
for(int i = 20; i >= 0; i--)
{
if(resp + (1<<i) >= N) continue;
if(bit[resp + (1 << i)] <= x)
x -= bit[resp + (1 << i)], resp = resp + (1 << i);
}
return resp + 1;
}
inline void process()
{
int st = a, en = a, cnt = 1;
ans[a] = 0;
while(st > 1 or en < n)
{
if( (v[st - 1] > v[en + 1] and en < n) or st <= 1)
{
ans[en + 1] = cnt;
en ++;
}
else ans[st - 1] = cnt, st --;
cnt ++;
}
}
pii val[N];
void query()
{
int st = a, en = a, cnt = 1;
ans[a] = 0;
while(st > 1 or en < n)
{
if( (val[st - 1] > val[en + 1] and en < n) or st <= 1)
{
ans[en + 1] = cnt;
en ++;
}
else ans[st - 1] = cnt, st --;
cnt ++;
}
}
int esq[N], dir[N];
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>a;
for(int i = 1; i <= n; i++) cin>>v[i], val[i] = {v[i], 0}, update_BIT(v[i], 1);
query();
cin>>q;
for(int i = 1; i <= q; i++)
{
char c; int a, b;
cin>>c>>a;
if(c == 'F')
{
query();
cout<<ans[a]<<"\n";
}
else
{
cin>>b;
//cout<<n - b + 1<<" th = "<<Find_kth(n - b + 1)<<"\n";
int kth = Find_kth(n - b + 1);
val[a] = {kth, i};
update_BIT(val[a].f, -1);
update_BIT(val[a].f, +1);
}
}
}
# | 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... |