BZOJ 3196: Tyvj 1730 二逼平衡树(一起来码树套树)
来源:互联网 发布:linux 毫秒级的 sleep 编辑:程序博客网 时间:2024/06/05 14:40
前言:
好久没更博客了,原以为NOIP后就退役了,可是又不想这样轻易离开。最近又在准备二检和学习珂学,11月就这样没了,NOIP游记都没写。总之,一切尽在不言中。下面又开始愉快地吹B了>_<
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:
1.查询k在区间内的排名
2.查询区间内排名为k的值
3.修改某一位值上的数值
4.查询k在区间内的前驱(前驱定义为小于x,且最大的数)
5.查询k在区间内的后继(后继定义为大于x,且最小的数)
Input
第一行两个数 n,m 表示长度为n的有序序列和m个操作
第二行有n个数,表示有序序列
下面有m行,opt表示操作标号
若opt=1 则为操作1,之后有三个数l,r,k 表示查询k在区间[l,r]的排名
若opt=2 则为操作2,之后有三个数l,r,k 表示查询区间[l,r]内排名为k的数
若opt=3 则为操作3,之后有两个数pos,k 表示将pos位置的数修改为k
若opt=4 则为操作4,之后有三个数l,r,k 表示查询区间[l,r]内k的前驱
若opt=5 则为操作5,之后有三个数l,r,k 表示查询区间[l,r]内k的后继
Output
对于操作1,2,4,5各输出一行,表示查询结果
Sample Input
9 6
4 2 2 1 9 4 0 1 1
2 1 4 3
3 4 10
2 1 4 3
1 2 5 9
4 3 9 5
5 2 8 5
Sample Output
2
4
3
4
9
HINT
1.n和m的数据范围:n,m<=50000
2.序列中每个数的数据范围:[0,1e8]
3.虽然原题没有,但事实上5操作的k可能为负数
题解
在写题解之前,让我回忆一下我与这道题的故事吧。那仿佛是遥远的某一天,NOIP前一天,我在洛谷愉悦地刷着板子,突然想试试自己的数据结构水平,就决定去码一题树套树。由于是第一次码,没有经验,就yy了半天,花了一天(还是半天?不记得了)终于码完了。机房的KsCla大佬和lhf大佬也码了这题。然后我发现KsCla码的是线段树套线段树。时间复杂度我的命是有多苦啊。
直接讲作法吧,由于是一道树套树模版题,所以我们考虑树套树(废话)。我们在每个数所在的叶子向上所有的区间的treap中插入这个数。
这个插入是
对于操作1,我们只需找到区间中比x小的数有几个并+1即可。我们对这个区间分解,在
对于操作3,我们和插入一样,找到每个数的位置对应的区间,然后在treap上删除再插入。这里我们要知道删除的是哪个,所以要保存原数。
对于操作4和5,找到对应区间的子区间里treap上的前驱后继,我们找到一堆前驱后继后,再取最值就是答案了。这是显然的。
这几个操作都是带两个
操作2就有些麻烦了,由于多棵treap上不能像权值ST一样二叉下去找第几名是谁,因为每个棵树的值不一样。于是一种更简洁的作法就是二分然后变成第一个问题。这样就多了一个
然后这题就做完了。洛谷的老人机过不了,bzoj却过了。bzoj读入有负数,第一次我的快读被卡了QAQ。还有,洛谷明明写着2s却只给1s,搞的我被卡,天若有情天亦老,我为洛谷续1秒。
一点点总结:我们发现,在以线段树为第一层嵌套数据结构中,包括树套树,线段树上开链表也好,对于单点的操作,要贡献它所在的
其实我还发现第一层的线段树只供分治使用,是“虚无的”,只存了进入下一层的指针,每次进入一个需要的地方,通过这个指针进去拿完信息再出来合并。
Code
#include <cstdio>#include <cstdlib>#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#define mmax(a, b) (((a) > (b)) ? (a) : (b))#define mmin(a, b) (((a) < (b)) ? (a) : (b))#define maxn 50010#define INF 0x7FFFFFFF#define Lg 17using namespace std;int n, m, cnt, tot, num[maxn];struct Treap{ Treap *son[2]; int val; int fix; int siz; inline int Lsiz(){return son[0] ? son[0]->siz : 0;} inline int Rsiz(){return son[1] ? son[1]->siz : 0;} inline void Up(){ siz = Lsiz() + Rsiz() + 1; }}Node[maxn*Lg<<1], *Root[maxn<<2], *P[Lg];inline Treap *NewTnode(int v){ Node[cnt].son[0] = Node[cnt].son[1] = NULL; Node[cnt].val = v; Node[cnt].siz = 1; Node[cnt].fix = rand(); return Node+cnt++;}inline void LRot(Treap *&a){ Treap *b = a->son[1]; a->son[1] = b->son[0]; b->son[0] = a; a = b; a->son[0]->Up(); a->Up();}inline void RRot(Treap *&a){ Treap *b = a->son[0]; a->son[0] = b->son[1]; b->son[1] = a; a = b; a->son[1]->Up(); a->Up();}void Insert(Treap *&p, int v){ if(!p){ p = NewTnode(v); return; } if(v <= p->val){ Insert(p->son[0], v); p->siz ++; if(p->son[0]->fix < p->fix) RRot(p); } else{ Insert(p->son[1], v); p->siz ++; if(p->son[1]->fix < p->fix) LRot(p); }}void Delete(Treap *&p, int v){ if(p->val == v){ if(!p->son[0] || !p->son[1]){ if(p->son[0]) p = p->son[0]; else p = p->son[1]; } else{ if(p->son[0]->fix < p->son[1]->fix){ RRot(p); Delete(p->son[1], v); p->siz --; } else{ LRot(p); Delete(p->son[0], v); p->siz --; } } } else if(p->val > v){ Delete(p->son[0], v); p->siz --; } else{ Delete(p->son[1], v); p->siz --; }}int FindRank(Treap *p, int k){ if(!p) return 0; if(p->val < k) return p->Lsiz() + 1 + FindRank(p->son[1], k); else return FindRank(p->son[0], k);}int FindPre(Treap *p, int k, int now){ if(!p) return now; if(p->val < k) return FindPre(p->son[1], k, p->val); else return FindPre(p->son[0], k, now);}int FindSuc(Treap *p, int k, int now){ if(!p) return now; if(p->val > k) return FindSuc(p->son[0], k, p->val); else return FindSuc(p->son[1], k, now);}void Build(int root, int L, int R, int x){ if(x > R || x < L) return; Insert(Root[root], num[x]); if(L == R) return; int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; Build(Lson, L, mid, x); Build(Rson, mid+1, R, x);}int QueryRank(int root, int L, int R, int x, int y, int k){ if(x > R || y < L) return 0; if(x <= L && y >= R) return FindRank(Root[root], k); int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; int temp1 = QueryRank(Lson, L, mid, x, y, k); int temp2 = QueryRank(Rson, mid+1, R, x, y, k); return temp1 + temp2;}void Search(int root, int L, int R, int x, int y){ if(x > R || y < L) return; if(x <= L && y >= R){ P[++tot] = Root[root]; return; } int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; Search(Lson, L, mid, x, y); Search(Rson, mid+1, R, x, y);}int QueryX(int root, int L, int R, int x, int y, int k){ tot = 0; Search(root, L, R, x, y); int l = 0, r = 1e8 + 1; while(l + 1 < r){ int mid = (l + r) >> 1, res = 0; for(register int i = 1; i <= tot; i++) res += FindRank(P[i], mid); if(res < k) l = mid; else r = mid; } return l;}void Update(int root, int L, int R, int x, int k){ if(x > R || x < L) return; Delete(Root[root], num[x]); Insert(Root[root], k); if(L == R) return; int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; Update(Lson, L, mid, x, k); Update(Rson, mid+1, R, x, k);}int QueryPre(int root, int L, int R, int x, int y, int k){ if(x > R || y < L) return -INF; if(x <= L && y >= R) return FindPre(Root[root], k, -INF); int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; int temp1 = QueryPre(Lson, L, mid, x, y, k); int temp2 = QueryPre(Rson, mid+1, R, x, y, k); return mmax(temp1, temp2);}int QuerySuc(int root, int L, int R, int x, int y, int k){ if(x > R || y < L) return INF; if(x <= L && y >= R) return FindSuc(Root[root], k, INF); int mid = (L + R) >> 1, Lson = root << 1, Rson = root << 1 | 1; int temp1 = QuerySuc(Lson, L, mid, x, y, k); int temp2 = QuerySuc(Rson, mid+1, R, x, y, k); return mmin(temp1, temp2);}int Read(){ int x = 0, f = 1; char ch = getchar(); while(ch > '9' || ch < '0'){if(ch == '-') f = -1; ch = getchar();} while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + ch - '0'; ch = getchar();} return f * x;}void Print(int x){ if(x < 0) putchar('-'), x = -x; if(x > 9) Print(x / 10); putchar(x % 10 + '0');}int main(){ n = Read(); m = Read(); for(register int i = 1; i <= n; i++) num[i] = Read(); for(register int i = 1; i <= (n<<2); i++) Root[i] = NULL; for(register int i = 1; i <= n; i++) Build(1, 1, n, i); int op, l, r, k; for(register int i = 1; i <= m; i++){ op = Read(); if(op == 1){ l = Read(); r = Read(); k = Read(); Print(QueryRank(1, 1, n, l, r, k) + 1); putchar('\n'); } else if(op == 2){ l = Read(); r = Read(); k = Read(); Print(QueryX(1, 1, n, l, r, k)); putchar('\n'); } else if(op == 3){ l = Read(); k = Read(); Update(1, 1, n, l, k); num[l] = k; } else if(op == 4){ l = Read(); r = Read(); k = Read(); Print(QueryPre(1, 1, n, l, r, k)); putchar('\n'); } else{ l = Read(); r = Read(); k = Read(); Print(QuerySuc(1, 1, n, l, r, k)); putchar('\n'); } } return 0;}
后记
以后要勤更博客,嗯,就这样。
我永远喜欢珂朵莉
- BZOJ 3196: Tyvj 1730 二逼平衡树(一起来码树套树)
- 【BZOJ 3196】Tyvj 1730 二逼平衡树
- bzoj 3196 (Tyvj 1730 二逼平衡树
- bzoj 3196: Tyvj 1730 二逼平衡树
- bzoj 3196 Tyvj 1730 二逼平衡树
- 【BZOJ 3196】 Tyvj 1730 二逼平衡树
- 【BZOJ 3196】 Tyvj 1730 二逼平衡树 分块
- bzoj 3196: Tyvj 1730 二逼平衡树
- bzoj 3196: Tyvj 1730 二逼平衡树
- BZOJ 3196 Tyvj 1730 二逼平衡树
- 【BZOJ 3196】[Tyvj 1730]二逼平衡树
- 【bzoj 3196】Tyvj 1730 二逼平衡树
- BZOJ 3196: Tyvj 1730 二逼平衡树 树套树
- bzoj 3196: Tyvj 1730 二逼平衡树 树套树
- bzoj 3196 Tyvj 1730 二逼平衡树(主席树)
- 3196: Tyvj 1730 二逼平衡树
- 3196: Tyvj 1730 二逼平衡树
- 3196: Tyvj 1730 二逼平衡树
- Java的结构体排序
- 好文参考
- Unity安卓移动设备显示Debug.Log信息
- 验证码生成方法
- spring-boot集成redis单机版
- BZOJ 3196: Tyvj 1730 二逼平衡树(一起来码树套树)
- 智能指针
- 聚焦大数据:洞察价值与企业应用落地
- 深入理解脚本化CSS系列第一篇——脚本化行内样式
- 动态规划求编辑距离——算法解题报告
- CodeForces
- YTU.2431: C语言习题 医生值班
- layui回调函数
- Mybatis(一):Mybatis基础架构和入门案例