【Bzoj1901】Dynamic Ranking

解析

#include <cstdio>
#include <algorithm>

#define Rep( i , _begin , _end ) for(int i=(_begin),i##_END=(_end);i<=(i##_END);i++)
#define For( i , _begin , _end ) for(int i=(_begin),i##_END=(_end);i!=(i##_END);i++)
#define Lop( i , _begin , _end ) for(int i=(_begin),i##_END=(_end);i>=(i##_END);i++)
#define Dnt( i , _begin , _end ) for(int i=(_begin),i##_END=(_end);i!=(i##_END);i++)

using std :: max;
using std :: min;

const int maxx = (10000 << 2) + 25;
const int maxm = 10000 * 4 * 16 + 25;
const int Inf = 1000000000;

int n,m,x,y,z,k,num,ans,f;
int ch[maxm][2],fix[maxm],v[maxm],size[maxm],cnt[maxm];
int rt[maxx],a[maxx];

char s[10];

namespace Treap{

int chk(int x,int y) {return x == y? 20011025 : x > y;}

void upt(int i) {size[i] = size[ch[i][0]] + size[ch[i][1]] + cnt[i];}

void rotate(int &i,int f){
int t = ch[i][f];
ch[i][f] = ch[t][f^1];
ch[t][f^1] = i;
size[t] = size[i];
upt(i);i = t;
}

void insert(int &i,int x){
if(!i) {v[i=++num] = x;size[i] = cnt[i] = 1;fix[i] = rand();return;}
size[i] ++;
if(v[i] == x) {cnt[i] ++;return;}
int tmp = chk(x,v[i]);
insert(ch[i][tmp],x);
if(fix[ch[i][tmp]] < fix[i]) rotate(i,tmp);
}

void remove(int &i,int x){
if(!i) return;
int tmp = chk(x,v[i]);
if(x != v[i]) size[i] --,remove(ch[i][tmp],x);
if(x == v[i]){
if(cnt[i] > 1) {cnt[i] --;size[i] --;return;}
if(ch[i][0] * ch[i][1] == 0) {i = ch[i][0] + ch[i][1];return;}
tmp = chk(fix[ch[i][0]],fix[ch[i][1]]);
rotate(i,tmp);remove(i,x);
}
}

int Dfs(int i,int x){
if(v[i] == x) return cnt[i];
if(!i) return 0;
return x < v[i]? Dfs(ch[i][0],x) : Dfs(ch[i][1],x);
}

int Qrank(int i,int x){
if(!i) return 0;
if(v[i] == x) return size[ch[i][0]];
else return x <= v[i]? Qrank(ch[i][0],x) : cnt[i] + size[ch[i][0]] + Qrank(ch[i][1],x);
}

}

namespace Segment_Tree{

using namespace Treap;

void Build(int t,int l,int r){
Rep( i , l , r ) insert(rt[t],a[i]);
if(l == r) return;int mid = (l+r) >> 1;
Build(t<<1,l,mid);Build(t<<1|1,mid+1,r);
}

void modify(int i,int pos,int l,int r,int k){
remove(rt[i],a[pos]);
insert(rt[i],k);
if(l == r) return;int mid = (l+r) >> 1;
if(pos <= mid) modify(i<<1,pos,l,mid,k);
if(pos >  mid) modify(i<<1|1,pos,mid+1,r,k);
}

int Query(int i,int x,int y,int l,int r,int k){
if(x <= l && r <= y) return Qrank(rt[i],k);
int mid = (l+r) >> 1;int ans = 0;
if(x <= mid) ans += Query(i<<1,x,y,l,mid,k);
if(y >  mid) ans += Query(i<<1|1,x,y,mid+1,r,k);
return ans;
}

int Ex_Order(int x,int y,int k){
int l = 0,r = Inf,ans;
while(l <= r){
int mid = (l+r) >> 1;
if(Query(1,x,y,1,n,mid) + 1 <= k) ans = mid,l = mid + 1;
else r = mid - 1;
}
return ans;
}

}

namespace Bit{

using namespace Treap;

for(int i=x;i<=n;i+=i&(-i))
f? insert(rt[i],k) : remove(rt[i],k);
}

int Query_B(int x,int k){
int ans = 0;
for(int i=x;i;i-=i&(-i))
ans += Qrank(rt[i],k);
return ans;
}

int Ex_Order_B(int x,int y,int k){
int l = 0,r = Inf,ans;
while(l <= r){
int mid = (l+r) >> 1;
if(Query_B(y,mid) - Query_B(x-1,mid) + 1 <= k) ans = mid,l = mid + 1;
else r = mid - 1;
}
return ans;
}

}

using namespace Segment_Tree;
using namespace Bit;

int main(){
scanf("%d%d",&n,&m);
Rep( i , 1 , n ) scanf("%d",&a[i]);
Rep( i , 1 , n ) Add(i,a[i],1);
while( m-- ){
scanf("%s",s);
}