BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树
【题目分析】
BZOJ这个题目抄的挺霸气。
主席树是第一时间想到的,但是修改又很麻烦。
看了别人的题解,原来还是可以用均摊的思想,用树状数组套主席树。
学到了新的姿势,2333o(* ̄▽ ̄*)ブ
【代码】
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <string> #include <algorithm> #include <vector> #include <iostream> #include <queue> using namespace std; #define maxn 20005 #define mlog 16 #define inf (0x3f3f3f3f) int read() { int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();} while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();} return x*f; } int rt[maxn],ls[maxn*mlog*mlog],rs[maxn*mlog*mlog],siz[maxn*mlog*mlog]; int a[maxn],b[maxn],n,m,top=0,tot=0; int opt[maxn],x[maxn],y[maxn],z[maxn],L[mlog],R[mlog]; char s[11]; void ins(int o1,int & o2,int l,int r,int x,int f) { // printf("ins %d %d %d %d %d %d\n",o1,o2,l,r,x,f); o2=++tot; siz[o2]=siz[o1]+f; if (l==r) return ; int mid=(l+r)/2; if (x<=mid) rs[o2]=rs[o1],ins(ls[o1],ls[o2],l,mid,x,f); else ls[o2]=ls[o1],ins(rs[o1],rs[o2],mid+1,r,x,f); return ; } int query(int l,int r,int x) { // printf("query %d %d %d\n",l,r,x); if (l==r) return l; int mid=(l+r)/2,tmp=0; for (int i=1;i<=L[0];++i) tmp-=siz[ls[L[i]]]; for (int i=1;i<=R[0];++i) tmp+=siz[ls[R[i]]]; // printf("tmp is %d\n",tmp); if (x<=tmp) { for (int i=1;i<=L[0];++i) L[i]=ls[L[i]]; for (int i=1;i<=R[0];++i) R[i]=ls[R[i]]; return query(l,mid,x); } else { for (int i=1;i<=L[0];++i) L[i]=rs[L[i]]; for (int i=1;i<=R[0];++i) R[i]=rs[R[i]]; return query(mid+1,r,x-tmp); } } int main() { top=n=read();m=read(); for (int i=1;i<=n;++i) b[i]=a[i]=read(); for (int i=1;i<=m;++i) { scanf("%s",s); switch(s[0]) { case 'Q': opt[i]=0;x[i]=read();x[i]--;y[i]=read();z[i]=read();break; case 'C': opt[i]=1;x[i]=read();y[i]=read();b[++top]=y[i];break; } } sort(b+1,b+top+1); top=unique(b+1,b+top+1)-b-1; for (int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+top+1,a[i])-b; for (int i=1;i<=m;++i) if (opt[i]) y[i]=lower_bound(b+1,b+top+1,y[i])-b; // printf("lisan is OK!\n"); // for (int i=1;i<=n;++i) printf("%d ",a[i]); printf("\n"); // for (int i=1;i<=m;++i) printf("%d ",y[i]); printf("\n"); for (int i=1;i<=n;++i) { // printf("ins %d %d\n",a[i],i);getchar(); for (int j=i;j<=n;j+=j&(-j)) ins(rt[j],rt[j],1,top,a[i],1); } for (int i=1;i<=m;++i) { if (opt[i]) { // printf("fix %d to %d\n",x[i],y[i]); for (int j=x[i];j<=n;j+=j&(-j)) { ins(rt[j],rt[j],1,top,a[x[i]],-1); ins(rt[j],rt[j],1,top,y[i],1); } a[x[i]]=y[i]; } else { // printf("query %d %d %d \n",x[i],y[i],z[i]); L[0]=0; R[0]=0; for (int j=x[i];j;j-=j&(-j)) L[++L[0]]=rt[j]; for (int j=y[i];j;j-=j&(-j)) R[++R[0]]=rt[j]; // printf("L :"); for (int j=1;j<=L[0];++j) printf("%d ",L[i]); // printf("R :"); for (int j=1;j<=R[0];++j) printf("%d ",R[i]); printf("%d\n",b[query(1,top,z[i])]); } } }
BZOJ 1901 Zju2112 Dynamic Rankings ——树状数组套主席树的更多相关文章
- P2617 Dynamic Rankings(树状数组套主席树)
P2617 Dynamic Rankings 单点修改,区间查询第k大 当然是无脑树套树了~ 树状数组套主席树就好辣 #include<iostream> #include<cstd ...
- ZOJ 2112 Dynamic Rankings(树状数组套主席树 可修改区间第k小)题解
题意:求区间第k小,节点可修改 思路:如果直接用静态第k小去做,显然我更改一个节点后,后面的树都要改,这个复杂度太高.那么我们想到树状数组思路,树状数组是求前缀和,那么我们可以用树状数组套主席树,求出 ...
- LUOGU P2617 Dynamic Rankings(树状数组套主席树)
传送门 解题思路 动态区间第\(k\)大,树状数组套主席树模板.树状数组的每个位置的意思的是每棵主席树的根,维护的是一个前缀和.然后询问的时候\(log\)个点一起做前缀和,一起移动.时空复杂度\(O ...
- BZOJ 3196 Tyvj 1730 二逼平衡树 ——树状数组套主席树
[题目分析] 听说是树套树.(雾) 怒写树状数组套主席树,然后就Rank1了.23333 单点修改,区间查询+k大数查询=树状数组套主席树. [代码] #include <cstdio> ...
- BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...
- BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树
BZOJ_3196_Tyvj 1730 二逼平衡树_树状数组套主席树 Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排 ...
- [COGS257]动态排名系统 树状数组套主席树
257. 动态排名系统 时间限制:5 s 内存限制:512 MB [问题描述]给定一个长度为N的已知序列A[i](1<=i<=N),要求维护这个序列,能够支持以下两种操作:1.查询A[ ...
- 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】
题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
随机推荐
- 【leetcode】Largest Number ★
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【leetcode】Max Points on a Line(hard)☆
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【网络】VPN和代理服务器的区别
来自:http://www.zhihujingxuan.com/19311.html [scotttony的回答(41票)]: VPN和ssh哪个比较好, 要看你怎么定义是“好”. ssh作为一个创建 ...
- eclipse快捷键Alt + / 失效
最近电脑上的Eclipse没有了自动提示功能,也不是全部不提示,大多数情况下按下"alt+/"键还会产生提示,但是当我在java项目中邪main方法和syso的时候,"a ...
- July 2nd, Week 27th Saturday, 2016
The beauty of the journey is found in the scenery along the way. 旅行之美在于沿途所见的风景. I remember that ther ...
- nohup命令
nohup就是不挂起的意思( n ohang up). .nohup command 或者 nohup command & 这之间的差别是带&的命令行,即使terminal(终端)关闭 ...
- Android中shell命令语句
最近学习了Android中碰到了shell命令,故收集终结了一下 Ccat zdd 浏览文件zdd的内容cat zdd1 zdd2 浏览多个文件的内容cat -n zdd浏览文件zdd的内容并显示行号 ...
- Lattice 的 DDR IP核使用调试笔记之工程建立
DDR3的IP核的使用相当重要,尤其是对视频处理方面. 下面接收DDR3 的IP 核的生成步骤. 1. 选择DDR IP核的生成路径.名字以及哪种语言之后就可以设置DDR IP 的参数了. 2.选择存 ...
- RecyclerView导入依赖包
1. eclipse 上的导入: 如下进入Android SDK的如下路径, \android-sdk\extras\android\m2repository\com\android\support\ ...
- Oracle12c client安裝報錯[INS-20802] Oracle Net Configuration Assistant failed完美解決
Doc ID 2082662.1 1.錯誤碼 Installation Of Oracle Client 12.1.0.2.0 (32-bit) Fails With An Error Message ...