# 【BZOJ1901】Dynamic Rankings，树状数组套主席树

Time:2016.05.09
Author:xiaoyimi

1901: Zju2112 Dynamic Rankings

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 6678 Solved: 2777
[Submit][Status][Discuss]
Description

Input

Output

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。

#include<bits/stdc++.h>
#define M 20004
#define ls(x) a[x].ch[0]
#define rs(x) a[x].ch[1]
#define low(x) (x&(-x))
using namespace std;
int n,m,tot,cnt;
int c[M>>1],ID[M],S1[M],S2[M],root[M];
{
int x,y,k;
bool opt;
}q[M>>1];
struct disc
{
int data,id;
bool operator <(const disc other)const
{
return data<other.data;
}
}b[M];
struct Chairman_tree
{
int siz,ch[2];
}a[M*250];
int build(int now,int L,int R,int k,int val)
{
int rt=++cnt;
a[rt].siz=a[now].siz+k;
ls(rt)=ls(now);rs(rt)=rs(now);
if (L==R) return rt;
int mid=(L+R)>>1;
if (mid>=val)
ls(rt)=build(ls(now),L,mid,k,val);
else
rs(rt)=build(rs(now),mid+1,R,k,val);
return rt;
}
int get(int begin,int end,int k)
{
if (begin==end) return end;
int mid=(begin+end)>>1,t=0;
for (int i=1;i<=S1[0];i++) t-=a[ls(S1[i])].siz;
for (int i=1;i<=S2[0];i++) t+=a[ls(S2[i])].siz;
if (k<=t)
{
for (int i=1;i<=S1[0];i++) S1[i]=ls(S1[i]);
for (int i=1;i<=S2[0];i++) S2[i]=ls(S2[i]);
return get(begin,mid,k);
}
else
{
for (int i=1;i<=S1[0];i++) S1[i]=rs(S1[i]);
for (int i=1;i<=S2[0];i++) S2[i]=rs(S2[i]);
return get(mid+1,end,k-t);
}
}
main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",c+i),
b[i].id=i,b[i].data=c[i];
int x,y;
tot=n;
for (int i=1;i<=m;i++)
{
char ch=getchar();
while (ch!='Q'&&ch!='C') ch=getchar();
scanf("%d%d",&q[i].x,&q[i].y);
if (ch=='C')
q[i].opt=1,
b[++tot].id=i+n,b[tot].data=q[i].y;
else scanf("%d",&q[i].k);
}
sort(b+1,b+tot+1);
for (int i=1;i<=tot;i++)
{
ID[i]=b[i].data;
if (b[i].id>n) q[b[i].id-n].y=i;
else c[b[i].id]=i;
}
root[0]=++cnt;
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j+=low(j))
root[j]=build(root[j],1,tot,1,c[i]);
for (int i=1;i<=m;i++)
{
if (!q[i].opt)
{
S1[0]=0;S2[0]=0;
for (int j=q[i].x-1;j;j-=low(j)) S1[++S1[0]]=root[j];
for (int j=q[i].y;j;j-=low(j)) S2[++S2[0]]=root[j];
printf("%d\n",ID[get(1,tot,q[i].k)]);
}
else
{
for (int j=q[i].x;j<=n;j+=low(j))
root[j]=build(root[j],1,tot,-1,c[q[i].x]);
c[q[i].x]=q[i].y;
for (int j=q[i].x;j<=n;j+=low(j))
root[j]=build(root[j],1,tot,1,c[q[i].x]);
}
}
}

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客