您的位置:首页 > 其它

ZOJ 2112 Dynamic Rankings (主席树+单点修改,询问区间第K值)

2017-07-13 08:43 351 查看
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They
have developed a more powerful system such that for N numbers a[1], a[2], ..., a
, you can ask it like: what is the k-th smallest number of a[i], a[i+1], ..., a[j]? (For some i<=j, 0<k<=j+1-i that you have given to it). More powerful, you can even change
the value of some a[i], and continue to query, all the same.

Your task is to write a program for this computer, which

- Reads N numbers from the input (1 <= N <= 50,000)

- Processes M instructions of the input (1 <= M <= 10,000). These instructions include querying the k-th smallest number of a[i], a[i+1], ..., a[j] and change some a[i] to t.

Input

The first line of the input is a single number X (0 < X <= 4), the number of the test cases of the input. Then X blocks each represent a single test case.

The first line of each block contains two integers N and M, representing N numbers and M instruction. It is followed by N lines. The (i+1)-th line represents the number a[i]. Then M lines that is in the following format

Q i j k or

C i t

It represents to query the k-th number of a[i], a[i+1], ..., a[j] and change some a[i] to t, respectively. It is guaranteed that at any time of the operation. Any number a[i] is a non-negative integer that is less than 1,000,000,000.

There're NO breakline between two continuous test cases.

Output

For each querying operation, output one integer to represent the result. (i.e. the k-th smallest number of a[i], a[i+1],..., a[j])

There're NO breakline between two continuous test cases.

Sample Input

2

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3

5 3

3 2 1 4 7

Q 1 4 3

C 2 6

Q 2 5 3

Sample Output

3

6

3

6

题意:N个数,M次操作,操作1,询问区间第K小值,操作2,修改第I个位置的值

分析:

在主席树的基础上,如果有修改操作,则要通过套树状数组来实现任意区间求第k小的问题。

刚开始看不明白什么意思,现在有一点理解。树状数组的每个元素是一个线段树,来维护修改后的前后缀和,树状数组能在log时间内更整个数组,现在用相同的方式更新整个线段树数组,每次更新一个点时,要更新这个点代表的整个线段树。同样的,求和时用一个use数组记录所要更新的点的下标,每次求不同线段树的同一位置的和。

静态初值不要用来初始化树状数组。

考虑到前缀和,我们通过树状数组来优化,即树状数组套主席树,每个节点都对应一棵主席树,那么修改操作就只要修改logn棵树,

O(nlognlogn+Mlognlogn)时间是可以的,但是直接建树要nlogn*logn(10^7)会MLE。

我们发现对于静态的建树我们只要nlogn个节点就可以了,而且对于修改操作,只是修改M次,每次改变俩个值(减去原先的,加上现在的)也就是说如果把所有初值都插入到树状数组里是不值得的,所以我们分两部分来做,所有初值按照静态来建,内存O(nlogn),

而修改部分保存在树状数组中,每次修改logn棵树,每次插入增加logn个节点O(M*logn*logn+nlogn)。

网上很多的题解都是直接把初始值插入到树状数组里面的(见代码2),在BZOJ里是能过得,在ZOJ是过不去的,我也因为这个错了很多次。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
//#define lson l,m,rt<<1
//#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
const int maxn=60005;
int n,m,tot,len,cnt;
int v[maxn],h[maxn],use[maxn],head[maxn],S[maxn];
int q[10005][3],lson[2500010],rson[2500010],siz[2500010];
char s[3];

int getid(int x) {return lower_bound(v+1,v+len+1,x)-v;}
int lowbit(int x) {return -x&x;}

int update(int pre,int k,int l,int r,int val){
int now=++tot;
lson[now]=lson[pre];
rson[now]=rson[pre];
siz[now]=siz[pre]+val;
if(l==r)return now;
int m=(l+r)>>1;
if(m>=k) lson[now]=update(lson[pre],k,l,m,val);
else rson[now]=update(rson[pre],k,m+1,r,val);
return now;
}

void add(int k,int num,int val){
for(int i=k;i<=n;i+=lowbit(i)){
S[i]=update(S[i],num,1,len,val);
}
}

int getsum(int x){
int ans=0;
for(int i=x;i;i-=lowbit(i))ans+=siz[lson[use[i]]];
return ans;
}
void dfs(int a,int b,int c);
int query(int x,int y,int k){

int l=1,r=len,lt=head[x],rt=head[y];
for(int i=x;i;i-=lowbit(i))use[i]=S[i];
for(int i=y;i;i-=lowbit(i))use[i]=S[i];
while(l<r){
int m=(l+r)>>1;
int tmp=getsum(y)-getsum(x)+siz[lson[rt]]-siz[lson[lt]];
//cout<<l<<r<<" == "<<tmp<<endl;;
if(k<=tmp){
r=m;
lt=lson[lt];rt=lson[rt];
for(int i=x;i;i-=lowbit(i))use[i]=lson[use[i]];
for(int i=y;i;i-=lowbit(i))use[i]=lson[use[i]];
}
else{
l=m+1;
k-=tmp;
lt=rson[lt];rt=rson[rt];
for(int i=x;i;i-=lowbit(i))use[i]=rson[use[i]];
for(int i=y;i;i-=lowbit(i))use[i]=rson[use[i]];
}
}
return l;
}
void dfs(int rt,int l,int r){
cout<<l<<" "<<r<<"="<<siz[rt]<<endl;
if(r==l)return ;
int m=(l+r)>>1;
dfs(lson[rt],l,m);
dfs(rson[rt],m+1,r);
}
void build(int &rt,int l,int r){
rt=tot++;
siz[rt]=0;
if(l==r) return ;
int m=(l+r)>>1;
build(lson[rt],l,m);
build(rson[rt],m+1,r);
}
int main()
{
int t=1;
cin>>t;
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",h+i);
v[i]=h[i];
}
cnt=n;
for(int i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='Q'){
scanf("%d%d%d",q[i],q[i]+1,q[i]+2);
}
else scanf("%d%d",q[i]+1,q[i]+2),q[i][0]=0,v[++cnt]=q[i][2];
}
sort(v+1,v+cnt+1);
len=unique(v+1,v+cnt+1)-v-1;

tot=0;
build(head[0],1,len);
for(int i=1;i<=n;i++){
head[i]=update(head[i-1],getid(h[i]),1,len,1);
}
//dfs(head[5],1,len);
for(int i=1;i<=n;i++)S[i]=head[0];
for(int i=1;i<=m;i++){

if(q[i][0]){
cout<<v[query(q[i][0]-1,q[i][1],q[i][2])]<<endl;
}
else{
add(q[i][1],getid(h[q[i][1]]),-1);
add(q[i][1],getid(q[i][2]),1);
h[q[i][1]]=q[i][2];
}
}
}

return 0;
}


代码2;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define maxn 10005
using namespace std;
int use[maxn*10],n,m,size,tot=0,all=0,h[maxn*10],v[maxn*10],t[maxn*10];
struct chairtree
{
int l,r,size;
}a[maxn*300];
struct question
{
int l,r,k;
}q[maxn];
int lowbit(int x)
{
return x&(-x);
}
void Hash1()
{
sort(h+1,h+1+all);
size=unique(h+1,h+1+all)-h-1;
}
int Hash(int x)
{
return lower_bound(h+1,h+1+size,x)-h;
}
int Build(int l,int r)
{
int now=++tot;
a[now].size=0;
if (l==r) return now;
int m=(l+r)>>1;
a[now].l=Build(l,m);
a[now].r=Build(m+1,r);
return now;
}
int Update(int root,int p,int val)
{
int now=++tot,tmp=now;
int l=1,r=size;
a[now].size=a[root].size+val;
while (l<r)
{
int m=(l+r)>>1;
if (p<=m)
{
a[now].l=++tot;
a[now].r=a[root].r;
root=a[root].l;
now=a[now].l;
r=m;
}
else
{
a[now].l=a[root].l;
a[now].r=++tot;
root=a[root].r;
now=a[now].r;
l=m+1;
}
a[now].size=a[root].size+val;
}
return tmp;
}
void Add(int x,int p,int val)
{
for (int i=x;i<=n;i+=lowbit(i)) //树状数组只需修改logn个
t[i]=Update(t[i],p,val);
}
int Getsum(int x)
{
int ans=0;
for (int i=x;i;i-=lowbit(i))
ans+=a[a[use[i]].l].size;
return ans;
}
int Query(int lx,int rx,int k)
{
int l=1,r=size;
for (int i=lx-1;i;i-=lowbit(i)) use[i]=t[i];
for (int i=rx;i;i-=lowbit(i)) use[i]=t[i];
while (l<r)
{
int m=(l+r)>>1;
int tmp=Getsum(rx)-Getsum(lx-1);
if (tmp>=k)
{
for (int i=lx-1;i;i-=lowbit(i))
use[i]=a[use[i]].l;
for (int i=rx;i;i-=lowbit(i))
use[i]=a[use[i]].l;
r=m;
}
else
{
for (int i=lx-1;i;i-=lowbit(i))
use[i]=a[use[i]].r;
for (int i=rx;i;i-=lowbit(i))
use[i]=a[use[i]].r;
k-=tmp;
l=m+1;
}
}
return l;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&v[i]),h[i]=v[i];
all=n;
for (int i=1;i<=m;i++)
{
char str[10];
int l,r,k;
scanf("%s",str);
if (str[0]=='Q')
{
scanf("%d%d%d",&l,&r,&k);
q[i].l=l,q[i].r=r,q[i].k=k;
}
else
{
scanf("%d%d",&r,&k);
q[i].l=0,q[i].r=r,q[i].k=k;
h[++all]=k;
}
}
Hash1();
t[0]=Build(1,size);
for (int i=1;i<=n;i++)
t[i]=t[0];
for (int i=1;i<=n;i++)
Add(i,Hash(v[i]),1);
for (int i=1;i<=m;i++)
{
if (q[i].l)
{
printf("%d\n",h[Query(q[i].l,q[i].r,q[i].k)]);
}
else
{
Add(q[i].r,Hash(v[q[i].r]),-1);
Add(q[i].r,Hash(q[i].k),1);
v[q[i].r]=q[i].k;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: