# 【bzoj1901】Zju2112 Dynamic Rankings 树状数组套主席树

## Sample Input

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3


## Sample Output

3

6


## HINT

20%的数据中，m,n≤100; 40%的数据中，m,n≤1000; 100%的数据中，m,n≤10000。

## Source

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;
const int SZ = 1000010;
const int INF = 1000000010;
const double eps = 1e-6;

struct haha{
char opt[3];
int a,b,c;

int num[SZ],bits[SZ],len,n,m;

int lsh[SZ];

struct node{
int l,r;
int cnt;
}tree[5000000];

int Tcnt = 0;

int rt[SZ];

void insert(int l,int r,int last,int &now,int v,int x)
{
now = ++ Tcnt;
tree[now] = tree[last];
tree[now].cnt += x;
if(l == r) return;
int mid = (l + r) >> 1;
if(v <= mid) insert(l,mid,tree[last].l,tree[now].l,v,x);
else insert(mid + 1,r,tree[last].r,tree[now].r,v,x);
}

void change(int pos,int v,int x)
{
for(int i = pos;i <= n;i += (i & -i))
insert(1,len,rt[i],rt[i],v,x);
}

int tl,tr;

int tml[SZ],tmr[SZ];

int query(int l,int r,int k)
{
if(l == r) return l;
int suml = 0,sumr = 0;
for(int i = 1;i <= tl;i ++)
suml += tree[tree[tml[i]].l].cnt;
for(int i = 1;i <= tr;i ++)
sumr += tree[tree[tmr[i]].l].cnt;
int mid = l + r >> 1;
if(sumr - suml >= k)
{
for(int i = 1;i <= tl;i ++)
tml[i] = tree[tml[i]].l;
for(int i = 1;i <= tr;i ++)
tmr[i] = tree[tmr[i]].l;
return query(l,mid,k);
}
else
{
for(int i = 1;i <= tl;i ++)
tml[i] = tree[tml[i]].r;
for(int i = 1;i <= tr;i ++)
tmr[i] = tree[tmr[i]].r;
return query(mid + 1,r,k - (sumr - suml));
}
}

{
l --;
tl = tr = 0;
for(int i = l;i > 0;i -= (i & -i))
tml[++ tl] = rt[i];
for(int i = r;i > 0;i -= (i & -i))
tmr[++ tr] = rt[i];
return query(1,len,k);
}

int main()
{
//  freopen("1901.in","r",stdin);
//  freopen("1901.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i ++)
{
scanf("%d",&num[i]);
lsh[++ lsh[0]] = num[i];
}
for(int i = 1;i <= m;i ++)
{
else
}
sort(lsh + 1,lsh + 1 + lsh[0]);
len = unique(lsh + 1,lsh + 1 + lsh[0]) - lsh - 1;

for(int i = 1;i <= n;i ++)
{
int x = lower_bound(lsh + 1,lsh + 1 + len,num[i]) - lsh;
change(i,x,1);
}

for(int i = 1;i <= m;i ++)
{
{
}
else
{
int x = lower_bound(lsh + 1,lsh + 1 + len,num[pos]) - lsh;
change(pos,x,-1);
x = lower_bound(lsh + 1,lsh + 1 + len,num[pos]) - lsh;
change(pos,x,1);
}
}

return 0;
}



©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客