bzoj1901:Zju2112 Dynamic Rankings
思路:树套树,我写了两种,一种是线段树套splay,线段树维护区间信息,splay维护第k大,一种是树状数组套权值线段树(并不是什么可持久化线段树,只不过是动态开点罢了,为什么网上一大堆题解都是可持久化线段树。。。。。明明可以直接修改的,不过可持久化线段树应该也是可以写的),树状数组维护前缀和,权值线段树维护第k大,利用那道不带修改的裸题的思想即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define maxn 10005 int n,m; int a[maxn]; char s[3]; inline int read(){ int x=0,f=1; char ch= getchar (); for (;ch< '0' ||ch> '9' ;ch= getchar ()) if (ch== '-' ) f=-1; for (;ch>= '0' &&ch<= '9' ;ch= getchar ()) x=x*10+ch- '0' ; return x*f; } struct splay_tree{ int ch[200*maxn][2],fa[200*maxn],note[200*maxn],val[200*maxn],size[200*maxn],cover[200*maxn],tot; void clear(){ memset (val,-1, sizeof (val)); } void update( int x){ if (!x) return ; size[x]=size[ch[x][0]]+size[ch[x][1]]+cover[x]; } void rotate( int x){ int y=fa[x],z=fa[y],bo=note[x],bo1=note[y]; ch[y][bo]=ch[x][bo^1],fa[ch[x][bo^1]]=y; ch[x][bo^1]=y,fa[y]=x; fa[x]=z; if (z) ch[z][bo1]=x; note[x]=bo1,note[y]=bo^1,note[ch[y][bo]]=bo; update(y); } void splay( int x){ while (note[x]!=2){ if (note[x]==note[fa[x]]) rotate(fa[x]); rotate(x); } update(x); } void insert( int &x, int v){ int y; while (1){ if (val[x]==v){cover[x]++,y=x; break ;} y=ch[x][val[x]<v]; if (!y){ y=++tot; val[y]=v,size[y]=1,cover[y]=1; if (x) note[y]=(val[x]<v); else note[y]=2; fa[y]=x,ch[y][0]=ch[y][1]=0; if (x) ch[x][note[y]]=y; break ; } x=y; } splay(y),x=y; } int find( int x, int v){ splay(x); while (1){ if (val[x]==v) return x; else if (val[x]<v) x=ch[x][1]; else x=ch[x][0]; } } int findk( int x, int v){ splay(x); int ans=0; while (x){ if (v<val[x]) x=ch[x][0]; else if (v==val[x]) return ans+size[ch[x][0]]+cover[x]; else ans+=size[ch[x][0]]+cover[x],x=ch[x][1]; } return ans; } int suc( int x){ splay(x); int tmp=ch[x][1]; while (ch[tmp][0]) tmp=ch[tmp][0]; return tmp; } void del( int &rt, int x){ if (cover[x]>1){ splay(x);rt=x; cover[x]--,size[x]--; return ; } splay(x); int y=suc(x),z=ch[x][0]; if (!y){ if (!z){rt=0; return ;} fa[z]=0,note[z]=2,rt=z; return ; } fa[ch[x][1]]=0,note[ch[x][1]]=2,splay(y); ch[y][0]=z,fa[z]=y,update(y),rt=y; } }S; struct segment_tree{ struct treenode{ int root; }tree[6*maxn]; void build( int p, int l, int r){ for ( int i=l;i<=r;i++) S.insert(tree[p].root,a[i]); if (l==r) return ; int mid=(l+r)>>1; build(p<<1,l,mid),build(p<<1|1,mid+1,r); } void change( int p, int l, int r, int pos, int val){ S.del(tree[p].root,S.find(tree[p].root,a[pos])),S.insert(tree[p].root,val); if (l==r) return ; int mid=(l+r)>>1; if (pos<=mid) change(p<<1,l,mid,pos,val); else change(p<<1|1,mid+1,r,pos,val); } int query( int p, int l, int r, int x, int y, int v){ if (x<=l && r<=y) return S.findk(tree[p].root,v); int mid=(l+r)>>1,ans=0; if (x<=mid) ans+=query(p<<1,l,mid,x,y,v); if (y>mid) ans+=query(p<<1|1,mid+1,r,x,y,v); return ans; } }T; int main(){ n=read(),m=read();S.clear(); for ( int i=1;i<=n;i++) a[i]=read(); T.build(1,1,n); while (m--){ scanf ( "%s" ,s+1); int aa=read(),b=read(); if (s[1]== 'C' ) T.change(1,1,n,aa,b),a[aa]=b; else { int k=read(); int l=-1e9-7,r=1e9+7,ans=0; while (l<=r){ int mid=(l+r)>>1; if (T.query(1,1,n,aa,b,mid)<k) l=mid+1; else r=mid-1,ans=mid; } printf ( "%d\n" ,ans); } } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define maxn 100005 int n,Q,tot,top1,top2,maxv; int a[maxn],t1[maxn],t2[maxn]; char s[2]; struct node{ int val,pos; bool operator <( const node &a) const { return val<a.val;} }b[maxn],c[maxn]; struct query{ int type,a,b,c; }q[maxn]; bool cmp(node a,node b){ return a.pos<b.pos;} struct segment_tree{ int root[maxn],treedeg; struct node{ int sum,ls,rs; }tree[10*maxn]; void change( int &p, int l, int r, int pos, int val){ if (!p) p=++treedeg; int mid=(l+r)>>1; tree[p].sum+=val; if (l==r) return ; if (pos<=mid) change(tree[p].ls,l,mid,pos,val); else change(tree[p].rs,mid+1,r,pos,val); } }T; void change( int i, int pos, int val){ for (;i<=n;i+=i&(-i)) T.change(T.root[i],1,maxv,pos,val); } void query( int x, int y, int rank){ top1=top2=0; for ( int i=x;i;i-=i&(-i)) t1[++top1]=T.root[i]; for ( int i=y;i;i-=i&(-i)) t2[++top2]=T.root[i]; int l=1,r=maxv; while (l<r){ int sum=0,mid=(l+r)>>1; for ( int i=1;i<=top1;i++) sum+=T.tree[T.tree[t1[i]].ls].sum; for ( int i=1;i<=top2;i++) sum-=T.tree[T.tree[t2[i]].ls].sum; if (sum>=rank){ for ( int i=1;i<=top1;i++) t1[i]=T.tree[t1[i]].ls; for ( int i=1;i<=top2;i++) t2[i]=T.tree[t2[i]].ls; r=mid; } else { for ( int i=1;i<=top1;i++) t1[i]=T.tree[t1[i]].rs; for ( int i=1;i<=top2;i++) t2[i]=T.tree[t2[i]].rs; l=mid+1,rank-=sum; } } printf ( "%d\n" ,b[l].val); } int main(){ scanf ( "%d%d" ,&n,&Q); for ( int i=1;i<=n;i++) scanf ( "%d" ,&a[i]),b[i].val=a[i],b[i].pos=i; int tmp=n; for ( int i=1;i<=Q;i++){ scanf ( "%s%d%d" ,s+1,&q[i].a,&q[i].b),q[i].type=(s[1]== 'C' ?0:1); if (q[i].type) scanf ( "%d" ,&q[i].c); else b[++tmp].val=q[i].b,b[tmp].pos=n+i; } sort(b+1,b+tmp+1),sort(a+1,a+n+1); for ( int i=1;i<=tmp;i++) if (b[i].pos>n) q[b[i].pos-n].b=i; else c[++tot].val=i,c[tot].pos=b[i].pos; sort(c+1,c+tot+1,cmp);maxv=tmp; for ( int i=1;i<=n;i++) change(i,c[i].val,1); for ( int i=1;i<=Q;i++){ if (q[i].type==0){ int pos=q[i].a,val=q[i].b; change(pos,c[pos].val,-1),change(pos,val,1),c[pos].val=val; } else query(q[i].b,q[i].a-1,q[i].c); } return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了