# 【bzoj1901】Zju2112 Dynamic Rankings 离散化+主席树+树状数组

5 3
3 2 1 4 7
Q 1 4 3
C 2 6
Q 2 5 3

3
6

#include <cstdio>
#include <algorithm>
using namespace std;
struct data
{
int num , rank;
}v[40001];
int a[20001] , flag[20001] , A[20001] , B[20001] , K[20001] , val[40001] , root[20001];
int L[40] , R[40] , tot , top , cnt;
int si[4400000] , lp[4400000] , rp[4400000];
char str[5];
bool cmp1(data a , data b)
{
return a.num < b.num;
}
bool cmp2(data a , data b)
{
return a.rank < b.rank;
}
void pushup(int x)
{
si[x] = si[lp[x]] + si[rp[x]];
}
void update(int x , int &y , int l , int r , int p , int a)
{
y = ++tot;
if(l == r)
{
si[y] = si[x] + a;
return;
}
int mid = (l + r) >> 1;
if(p <= mid) rp[y] = rp[x] , update(lp[x] , lp[y] , l , mid , p , a);
else lp[y] = lp[x] , update(rp[x] , rp[y] , mid + 1 , r , p , a);
pushup(y);
}
int query(int a , int b , int l , int r , int p)
{
if(l == r) return val[l];
int i , suml = 0 , sumr = 0 , mid = (l + r) >> 1;
for(i = 1 ; i <= a ; i ++ ) suml += si[lp[L[i]]];
for(i = 1 ; i <= b ; i ++ ) sumr += si[lp[R[i]]];
if(sumr - suml >= p)
{
for(i = 1 ; i <= a ; i ++ ) L[i] = lp[L[i]];
for(i = 1 ; i <= b ; i ++ ) R[i] = lp[R[i]];
return query(a , b , l , mid , p);
}
else
{
for(i = 1 ; i <= a ; i ++ ) L[i] = rp[L[i]];
for(i = 1 ; i <= b ; i ++ ) R[i] = rp[R[i]];
return query(a , b , mid + 1 , r , p - sumr + suml);
}
}
int main()
{
int n , m , i , j , x , y;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d" , &a[i]);
v[++top].num = a[i];
v[top].rank = top;
}
for(i = 1 ; i <= m ; i ++ )
{
scanf("%s%d%d" , str , &A[i] , &B[i]);
if(str[0] == 'Q') scanf("%d" , &K[i]) , flag[i] = 1;
else v[++top].num = B[i] , v[top].rank = top;
}
sort(v + 1 , v + top + 1 , cmp1);
val[0] = -1;
for(i = 1 ; i <= top ; i ++ )
{
if(v[i].num == val[cnt]) v[i].num = cnt;
else  val[++cnt] = v[i].num , v[i].num = cnt;
}
sort(v + 1 , v + top + 1 , cmp2);
for(i = 1 ; i <= n ; i ++ )
for(j = i ; j <= n ; j += j & (-j))
update(root[j] , root[j] , 1 , cnt , v[i].num , 1);
top = n;
for(i = 1 ; i <= m ; i ++ )
{
if(flag[i])
{
x = y = 0;
for(j = A[i] - 1 ; j > 0 ; j -= j & (-j))
L[++x] = root[j];
for(j = B[i] ; j > 0 ; j -= j & (-j))
R[++y] = root[j];
printf("%d\n" , query(x , y , 1 , cnt , K[i]));
}
else
{
for(j = A[i] ; j <= n ; j += j & (-j))
update(root[j] , root[j] , 1 , cnt , v[A[i]].num , -1);
top ++ ;
v[A[i]].num = v[top].num;
for(j = A[i] ; j <= n ; j += j & (-j))
update(root[j] , root[j] , 1 , cnt , v[A[i]].num , 1);
}
}
return 0;
}
posted @ 2017-01-17 10:29  GXZlegend  阅读(...)  评论(...编辑  收藏