bzoj1901: Zju2112 Dynamic Rankings 主席树+树状数组
主席树+树状数组求带修改的区间第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 | #include<bits/stdc++.h> using namespace std; int n,m,root[200100],q1[100000],q2[100000],tot=0,dd,len=0,a[100010],hash[200010],sum[5001000],l[5010000],r[5010000],b[1000100],c[1000010],d[1000010]; char s[10010]; int lowbit( int pp) { return pp&(-pp);} void update( int &rt, int L, int R, int x, int val) { if (!rt)rt=++len; sum[rt]+=val; if (L<R) { int mid=(L+R)>>1; if (x<=mid)update(l[rt],L,mid,x,val); else update(r[rt],mid+1,R,x,val); } } int main() { //freopen("xf.in","r",stdin); //freopen("xf.out","w",stdout); scanf( "%d%d" ,&n,&m); for ( int i=1;i<=n;i++) { scanf( "%d" ,&a[i]); hash[++tot]=a[i]; } for ( int i=1;i<=m;i++) { cin>>s[i]; if (s[i]== 'Q' )scanf( "%d%d%d" ,&b[i],&c[i],&d[i]); else {scanf( "%d%d" ,&b[i],&c[i]);hash[++tot]=c[i];} } sort(hash+1,hash+tot+1); dd=unique(hash+1,hash+tot+1)-hash-1; for ( int i=1;i<=n;i++) { int x=lower_bound(hash+1,hash+dd+1,a[i])-hash; for ( int j=i;j<=n;j+=lowbit(j))update(root[j],1,dd,x,1); } for ( int i=1;i<=m;i++) { if (s[i]== 'Q' ) { int xx=b[i]-1,yy=c[i],L=1,R=dd; int x=d[i]; int tail1=0,tail2=0; while (xx) { q1[++tail1]=root[xx];xx-=lowbit(xx); } while (yy) { q2[++tail2]=root[yy];yy-=lowbit(yy); } while (L<R) { int ans=0; for ( int j=1;j<=tail1;j++)ans-=sum[l[q1[j]]]; for ( int j=1;j<=tail2;j++)ans+=sum[l[q2[j]]]; int mid=(L+R)>>1; if (x<=ans) { R=mid; for ( int j=1;j<=tail1;j++) q1[j]=l[q1[j]]; for ( int j=1;j<=tail2;j++) q2[j]=l[q2[j]]; } else { L=mid+1; x-=ans; for ( int j=1;j<=tail1;j++) q1[j]=r[q1[j]]; for ( int j=1;j<=tail2;j++) q2[j]=r[q2[j]]; } } printf( "%d\n" ,hash[L]); } else { int x=lower_bound(hash+1,hash+dd+1,a[b[i]])-hash; for ( int j=b[i];j<=n;j+=lowbit(j))update(root[j],1,dd,x,-1); x=lower_bound(hash+1,hash+dd+1,c[i])-hash; for ( int j=b[i];j<=n;j+=lowbit(j))update(root[j],1,dd,x,1); a[b[i]]=c[i]; } } 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:你的「微服务管家」又秀新绝活了