# bzoj 1901 ZOJ 2112 Dynamic Rankings [树状数组套主席树] [线段树套平衡树]

Dynamic Rankings

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 6900 Solved: 2870

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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 10005;
int n,lim;
struct Node
{
int sum; // number cnt
int ch[2];
}node[maxn<<8];
int maxnode;
#define sum(x) node[x].sum
#define ch(x,d) node[x].ch[d]
void build(int &root,int l,int r)
{
root = ++maxnode;
if(l==r) return;
int m=(l+r)>>1;
build(ch(root,0),l,m);
build(ch(root,1),m+1,r);
}
void insert(int &root,int last,int l,int r,int pos,int val)
{
root = ++maxnode;
node[root] = node[last];
sum(root) += val;
if(l==r) return;
int m=(l+r)>>1;
if(pos<=m) insert(ch(root,0),ch(last,0),l,m,pos,val);
else insert(ch(root,1),ch(last,1),m+1,r,pos,val);
}
#define lowbit(x) ((x)&(-x))
int use[maxn]; // current node of trees
int root[maxn]; // BIT with Chairman_tree of adjustment
{
for(int i=x;i<=n;i+=lowbit(i))
insert(root[i],root[i],1,lim,pos,val);
}
int Sum(int x)
{
int ret = 0;
for(int i=x;i;i-=lowbit(i))
ret += sum(ch(use[i],0)); // in query, visiting the left node to adjust and judge directions
return ret;
}
int query(int s,int t,int root,int last,int l,int r,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int lsize = Sum(t) - Sum(s) + sum(ch(root,0)) - sum(ch(last,0)); // Sum of sigma left
if(k<=lsize)
{
for(int i=s;i;i-=lowbit(i))
use[i] = ch(use[i],0);
for(int i=t;i;i-=lowbit(i))
use[i] = ch(use[i],0);
return query(s,t,ch(root,0),ch(last,0),l,m,k);
}
else
{
for(int i=s;i;i-=lowbit(i))
use[i] = ch(use[i],1);
for(int i=t;i;i-=lowbit(i))
use[i] = ch(use[i],1);
return query(s,t,ch(root,1),ch(last,1),m+1,r,k-lsize);
}
}

struct Query
{
bool flag; // false is Q
int x,y,z; // (pos,t) (i,j,k)
}que[maxn];
struct Data
{
int val,order;
bool operator < (const Data t) const
{
return val < t.val;
}
}data[maxn<<1]; // count the queries
int maxdata;
int a[maxn]; // sequence after discretes
int orig[maxn<<1]; // quick link from val_discretion to val_original
int main()
{
freopen("ranking.in","r",stdin);
freopen("ranking.out","w",stdout);
int q;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",&data[i].val),data[i].order=i;
maxdata = n;
for(int i=1;i<=q;i++) // off line to discrete!!
{
char ch = getchar();
while(ch^'Q' && ch^'C') ch = getchar();
if(ch=='C') que[i].flag=true;
scanf("%d%d",&que[i].x,&que[i].y);
if(ch=='Q') scanf("%d",&que[i].z);
if(ch=='C') data[++maxdata].val=que[i].y;
}
sort(data+1,data+maxdata+1);
int last=-INF;
for(int i=1;i<=maxdata;i++)
if(last^data[i].val) last = data[i].val , a[data[i].order]=++lim , orig[lim]=data[i].val; // ignore a[0]
else a[data[i].order]=lim;
//end discretization
for(int i=1;i<=n;i++)
for(int i=1;i<=q;i++)
if(que[i].flag)
{
a[que[i].x] = lower_bound(orig+1,orig+lim+1,que[i].y)-orig; // update not only the a but also the orig!!!!
orig[a[que[i].x]] = que[i].y;
}
else
{
for(int j=que[i].x-1;j;j-=lowbit(j))
use[j] = root[j];
for(int j=que[i].y;j;j-=lowbit(j))
use[j] = root[j];
printf("%d\n",orig[ans]);
}
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 10005;

struct Node
{
int val;
int size,cnt;
int ch[2],fa;
}node[maxn<<9];
int maxnode;
#define val(x) node[x].val
#define size(x) node[x].size
#define cnt(x) node[x].cnt
#define fa(x) node[x].fa
#define ch(x,d) node[x].ch[d]
int root[maxn<<2]; //segment tree
inline void update(int x)
{
size(x) = cnt(x);
int l=ch(x,0) , r=ch(x,1);
if(l) size(x)+=size(l);
if(r) size(x)+=size(r);
}
inline void rotate(int x,int &to)
{
int y=fa(x),z=fa(y);
int l= ch(y,1)==x , r=l^1;
if(y==to) to=x;
else ch(z,ch(z,1)==y)=x;
fa(ch(x,r))=y; fa(y)=x; fa(x)=z;
ch(y,l)=ch(x,r); ch(x,r)=y;
update(y); update(x);
}
void splay(int x,int &to)
{
while(x^to)
{
int y=fa(x),z=fa(y);
if(y^to)
if(ch(y,0)==x ^ ch(z,0)==y) rotate(x,to);
else rotate(y,to);
rotate(x,to);
}
}
void insert(int &rt,int key)
{
if(!rt) { rt=++maxnode; val(rt)=key; size(rt)=cnt(rt)=1; return; }
int x=rt,y=0;
while(x && val(x)^key) y=x,x=ch(x,key>val(x));
if(x) cnt(x)++;
else
{
x=++maxnode;
val(x)=key;
cnt(x)=1;
fa(x)=y;
if(y) ch(y,key>val(y))=x;
}
update(x); // incase s is root!!
splay(x,rt);
}
inline int find(int &rt,int key) // provided the key exists
{
int x = rt;
while(val(x) ^ key) x = ch(x,key>val(x));
return x;
}
inline int pre(int x) // provided the pre exists
{
x = ch(x,0);
while(ch(x,1)) x = ch(x,1);
return x;
}
inline int next(int x)
{
x = ch(x,1);
while(ch(x,0)) x = ch(x,0);
return x;
}
void erase(int &rt,int key)
{
if(size(rt)==1)
{
rt = 0;
return;
}
int x=find(rt,key);
splay(x,rt);
if(cnt(rt)>1)
{
size(rt)--;
cnt(rt)--;
return;
}
if(!ch(x,0) && !ch(x,1)) rt=0;
if(!ch(x,0) && ch(x,1)) rt=ch(x,1); // list all the limits!!!
if(!ch(x,1) && ch(x,0)) rt=ch(x,0);
if(ch(x,0) && ch(x,1))
{
int l=pre(x),r=next(x);
splay(l,rt); splay(r,ch(l,1));
size(ch(r,0))--;
if(--cnt(ch(r,0)) == 0) ch(r,0)=0;
update(r); update(l);
}
}
int rank(int &rt,int key) // super lower rank , where key is NOT provided to exist
{
insert(rt,key); // with splay
int ret = cnt(rt)-1; // necessary to count the equality
if(ch(rt,0)) ret += size(ch(rt,0));
erase(rt,key);
return ret;
}
int kth(int rt,int k)
{
int lsize = 0;
if(ch(rt,0)) lsize = size(ch(rt,0));
if(lsize<k && k<=lsize+cnt(rt)) return val(rt);
if(k<=lsize) return kth(ch(rt,0),k);
else return kth(ch(rt,1),k-lsize-cnt(rt));
}

int a[maxn],n;
void build(int rt,int l,int r)
{
for(int i=l;i<=r;i++) insert(root[rt],a[i]);
if(l==r) return;
int m=(l+r)>>1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
}
void modify(int rt,int l,int r,int pos,int key) // remember to update a[pos] after
{
erase(root[rt],a[pos]);
insert(root[rt],key);
if(l==r) return;
int m=(l+r)>>1;
if(pos<=m) modify(rt<<1,l,m,pos,key);
else modify(rt<<1|1,m+1,r,pos,key);
}
int query(int rt,int l,int r,int x,int y,int key)
{
if(x<=l && r<=y) return rank(root[rt],key);
int m=(l+r)>>1;
int ret = 0;
if(x<=m && l<=y) ret += query(rt<<1,l,m,x,y,key);
if(y>=m+1 && r>=x) ret += query(rt<<1|1,m+1,r,x,y,key);
return ret;
}
int search(int x,int y,int k)
{
int l=1,r=n;
while(l<r)
{
int m=(l+r)>>1;
int rnk = query(1,1,n,x,y,kth(root[1],m)); // as counted above , not needed to +1
if(rnk < k) l=m+1;
else r=m;
}
return kth(root[1],l);
}
void change(int pos,int key)
{
modify(1,1,n,pos,key);
a[pos]=key;
}
int main()
{
freopen("ranking.in","r",stdin);
freopen("ranking.out","w",stdout);
int q;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++) scanf("%d",a+i);
build(1,1,n);
for(int i=1;i<=q;i++)
{
char ch=getchar();
while(ch^'C' && ch^'Q') ch=getchar();
int x,y,z;
if(ch=='C')
{
scanf("%d%d",&x,&y);
change(x,y);
}
else
{
scanf("%d%d%d",&x,&y,&z);
int ans = search(x,y,z);
printf("%d\n",ans);
}
}
return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客