Dynamic Rankings
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 6900 Solved: 2870
Description
给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]……a[j]中第k小的数是多少(1≤k≤j-i+1),并且,你可以改变一些a[i]的值,改变后,程序还能针对改变后的a继续回答上面的问题。你需要编一个这样的程序,从输入文件中读入序列a,然后读入一系列的指令,包括询问指令和修改指令。对于每一个询问指令,你必须输出正确的回答。 第一行有两个正整数n(1≤n≤10000),m(1≤m≤10000)。分别表示序列的长度和指令的个数。第二行有n个数,表示a[1],a[2]……a[n],这些数都小于10^9。接下来的m行描述每条指令,每行的格式是下面两种格式中的一种。 Q i j k 或者 C i t Q i j k (i,j,k是数字,1≤i≤j≤n, 1≤k≤j-i+1)表示询问指令,询问a[i],a[i+1]……a[j]中第k小的数。C i t (1≤i≤n,0≤t≤10^9)表示把a[i]改变成为t。
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。
一句话题意:动态查询区间第k小值
两种写法。。
先考虑树状数组套主席树。。
如果是静态查询那么直接主席树就好了,但是设计修改,如果还是用前缀和的话修改的时间复杂度将会极高。
那么可以考虑用树状数组优化,要修改的就把对应整条lowbit路径上的树修改即可。
但是如果直接把原来的主席树修改,那么还是很不高效,那么就可以分成两部分,一部分就是原来的静态主席树查询,另一部分是树状数组套上主席树,专门负责修改操作,查询的时候对应地把两部分加起来就好了。。
然后use数组一开始不是很明白,实际上就是当前访问到的节点标号。。。
另外,离线操作是为了得到所有使用的值,方便离散化。。。
还有的话,如果用 rev() 来作为 orig() 函数的反函数的话,会RE的。。。。因为数值大小是在10^9啊。。。。一开始在这里无限RE。。
还要注意修改之后也要同时把a() 和 orig() 同时修改!!!!!不然会WA的。。。
参考博客:http://www.cnblogs.com/Empress/p/4659824.html
#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 head[maxn];
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
void add(int x,int pos,int val)
{
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
build(head[0],1,lim);
for(int i=1;i<=n;i++)
insert(head[i],head[i-1],1,lim,a[i],1);
for(int i=1;i<=n;i++) root[i]=head[0]; // tree of adjustion is empty
for(int i=1;i<=q;i++)
if(que[i].flag)
{
add(que[i].x,a[que[i].x],-1);
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;
add(que[i].x,a[que[i].x],1);
}
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];
int ans = query(que[i].x-1,que[i].y,head[que[i].y],head[que[i].x-1],1,lim,que[i].z);
printf("%d\n",orig[ans]);
}
return 0;
}
线段树套平衡树写法
当初这道题就是神一样的数据结构,在写过树状数组套主席树之后其实发现也挺好写的,完全没有看网上的代码(题解还是有看。。。。)
其实线段树的每一个节点就是一棵平衡树,保存了区间的值的信息
然后修改就是在所有包含这个点的区间的平衡树内先删除再插入。。。
查询稍微麻烦一点,是一个二分答案,如果二分数字的话是 log(10^9) 稍微有点大,那么我们可以在线段树根节点里面二分排名,那么每次确定整个数组里的一个排名所对应的数时候,到所有查询区间里的小区间里面去求一个rank,但是要注意有重复的情况!!那么把∑rank求出来就可以和当前查询的k比较了,然后答案要保留在左区间。
#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;
}
时空复杂度对比。。。。