【bzoj1901】带修改的区间第k大 主席树+树状数组
2017-02-15 20:35
573 查看
【题目大意】
给定一段序列,要求一个数据结构,支持两个操作。1.修改某个数。2,查询某段区间的第K大。
【题解】
我们知道如果没有修改操作,那么直接将两个版本的线段树差分即可。
其实这个差分用的就是前缀和的思想,如果带修改操作的话,可以考虑用树状数组维护。
我们可以在树状数组中插入线段树(想想都是exciting),每次修改权值也是在树状数组中对应的线段树修改,查询时用树状数组查找根的编号。
通过这道题我明白了计算主席树空间复杂度的方法:一共有n个版本的线段树,每个线段树向下延伸logn层,所以空间复杂度nlogn,而这道题中套了一个树状数组,所以空间复杂度nlogn+mlog^2n
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define FILE "read"
#define MAXN 20010
#define up(i,j,n) for(int i=j;i<=n;++i)
#define dn(i,j,n) for(int i=j;i>=n;--i)
namespace INIT{
char buf[1<<15],*fs,*ft;
inline char getc(){return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline int read(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
}using namespace INIT;
int n,m,top,tot,cnt,lcnt,rcnt,a[MAXN],num[MAXN],A[MAXN],B[MAXN],K[MAXN],L[MAXN],R[MAXN],hash[MAXN];
int flag[MAXN],sum[MAXN*182],root[MAXN*182],son[MAXN*182][2];
int lowbit(int x){return x&(-x);}
int find(int x){
int l=1,r=tot;
while(l+1<r){
int mid=(l+r)>>1;
if(hash[mid]<=x) l=mid;
else r=mid;
}
return hash[l]==x?l:r;
}
int newroot(int last,int v){
sum[++cnt]=sum[last]+v;
son[cnt][0]=son[last][0];
son[cnt][1]=son[last][1];
return cnt;
}
void updata(int l,int r,int &root,int last,int x,int v){
root=newroot(last,v);
if(l==r) return;
int mid=(l+r)>>1;
if(x<=mid) updata(l,mid,son[root][0],son[last][0],x,v);
else updata(mid+1,r,son[root][1],son[last][1],x,v);
}
int query(int l,int r,int x){
if(l==r) return l;
int suml(0),sumr(0),mid=(l+r)>>1;
up(i,1,lcnt) suml+=sum[son[L[i]][0]];
up(i,1,rcnt) sumr+=sum[son[R[i]][0]];
if(sumr-suml>=x){
up(i,1,lcnt) L[i]=son[L[i]][0];
up(i,1,rcnt) R[i]=son[R[i]][0];
return query(l,mid,x);
}
else{
up(i,1,lcnt) L[i]=son[L[i]][1];
up(i,1,rcnt) R[i]=son[R[i]][1];
return query(mid+1,r,x-(sumr-suml));
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); m=read();
up(i,1,n) a[i]=read(),num[++top]=a[i];
up(i,1,m){
char ch[5]; scanf("%s",ch);
A[i]=read(); B[i]=read();
if(ch[0]=='Q') K[i]=read(),flag[i]=1;
else num[++top]=B[i];
}
sort(num+1,num+top+1); hash[++tot]=num[1];
up(i,2,top) if(num[i]!=num[i-1]) hash[++tot]=num[i];
up(i,1,n){
int t=find(a[i]);
for(int j=i;j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,1);
}
up(i,1,m){
if(flag[i]){
lcnt=rcnt=0; A[i]--;
for(int j=A[i];j>0;j-=lowbit(j)) L[++lcnt]=root[j];
for(int j=B[i];j>0;j-=lowbit(j)) R[++rcnt]=root[j];
printf("%d\n",hash[query(1,tot,K[i])]);
}
else{
int t=find(a[A[i]]);
for(int j=A[i];j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,-1);
a[A[i]]=B[i]; t=find(B[i]);
for(int j=A[i];j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,1);
}
}
return 0;
}
给定一段序列,要求一个数据结构,支持两个操作。1.修改某个数。2,查询某段区间的第K大。
【题解】
我们知道如果没有修改操作,那么直接将两个版本的线段树差分即可。
其实这个差分用的就是前缀和的思想,如果带修改操作的话,可以考虑用树状数组维护。
我们可以在树状数组中插入线段树(想想都是exciting),每次修改权值也是在树状数组中对应的线段树修改,查询时用树状数组查找根的编号。
通过这道题我明白了计算主席树空间复杂度的方法:一共有n个版本的线段树,每个线段树向下延伸logn层,所以空间复杂度nlogn,而这道题中套了一个树状数组,所以空间复杂度nlogn+mlog^2n
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define FILE "read"
#define MAXN 20010
#define up(i,j,n) for(int i=j;i<=n;++i)
#define dn(i,j,n) for(int i=j;i>=n;--i)
namespace INIT{
char buf[1<<15],*fs,*ft;
inline char getc(){return (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),fs==ft))?0:*fs++;}
inline int read(){
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
}using namespace INIT;
int n,m,top,tot,cnt,lcnt,rcnt,a[MAXN],num[MAXN],A[MAXN],B[MAXN],K[MAXN],L[MAXN],R[MAXN],hash[MAXN];
int flag[MAXN],sum[MAXN*182],root[MAXN*182],son[MAXN*182][2];
int lowbit(int x){return x&(-x);}
int find(int x){
int l=1,r=tot;
while(l+1<r){
int mid=(l+r)>>1;
if(hash[mid]<=x) l=mid;
else r=mid;
}
return hash[l]==x?l:r;
}
int newroot(int last,int v){
sum[++cnt]=sum[last]+v;
son[cnt][0]=son[last][0];
son[cnt][1]=son[last][1];
return cnt;
}
void updata(int l,int r,int &root,int last,int x,int v){
root=newroot(last,v);
if(l==r) return;
int mid=(l+r)>>1;
if(x<=mid) updata(l,mid,son[root][0],son[last][0],x,v);
else updata(mid+1,r,son[root][1],son[last][1],x,v);
}
int query(int l,int r,int x){
if(l==r) return l;
int suml(0),sumr(0),mid=(l+r)>>1;
up(i,1,lcnt) suml+=sum[son[L[i]][0]];
up(i,1,rcnt) sumr+=sum[son[R[i]][0]];
if(sumr-suml>=x){
up(i,1,lcnt) L[i]=son[L[i]][0];
up(i,1,rcnt) R[i]=son[R[i]][0];
return query(l,mid,x);
}
else{
up(i,1,lcnt) L[i]=son[L[i]][1];
up(i,1,rcnt) R[i]=son[R[i]][1];
return query(mid+1,r,x-(sumr-suml));
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); m=read();
up(i,1,n) a[i]=read(),num[++top]=a[i];
up(i,1,m){
char ch[5]; scanf("%s",ch);
A[i]=read(); B[i]=read();
if(ch[0]=='Q') K[i]=read(),flag[i]=1;
else num[++top]=B[i];
}
sort(num+1,num+top+1); hash[++tot]=num[1];
up(i,2,top) if(num[i]!=num[i-1]) hash[++tot]=num[i];
up(i,1,n){
int t=find(a[i]);
for(int j=i;j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,1);
}
up(i,1,m){
if(flag[i]){
lcnt=rcnt=0; A[i]--;
for(int j=A[i];j>0;j-=lowbit(j)) L[++lcnt]=root[j];
for(int j=B[i];j>0;j-=lowbit(j)) R[++rcnt]=root[j];
printf("%d\n",hash[query(1,tot,K[i])]);
}
else{
int t=find(a[A[i]]);
for(int j=A[i];j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,-1);
a[A[i]]=B[i]; t=find(B[i]);
for(int j=A[i];j<=n;j+=lowbit(j)) updata(1,tot,root[j],root[j],t,1);
}
}
return 0;
}
相关文章推荐
- 区间第k大 可修改主席树,树状数组套线段树。
- HDU 4267 - A Simple Problem with Integers 树状数组区间修改
- HDU 1556(树状数组-区间修改,查询点)
- 【POJ3468】【树状数组区间修改】A Simple Problem with Integers
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)
- 主席树套树状数组 动态区间第k小
- hdu 1166 敌兵布阵 树状数组求区间和和修改点的值
- poj3468(树状数组:区间修改 区间求和)
- hdu1556 树状数组 区间修改,点查询
- 树状数组的区间修改,单点查询
- HDU - 3584 Cube (三维树状数组 + 区间修改 + 单点求值)
- BZOJ1901 - Dynamic Rankings(树状数组套主席树)
- 关于树状数组修改区间,求点值
- A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267
- POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】
- 【树状数组】点修改&区间查询
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- poj 3468A Simple Problem with Integers(树状数组区间修改)
- hdu 1556 Color the ball 【树状数组:区间修改、单点求值】
- [BZOJ 1901][ZOJ 2112]Dynamic Rankings(树状数组套主席树、动态区间第k大值查询)