luogu3380 【模板】二逼平衡树(树套树)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
int n, m, a[50005], opt, uu, vv, ww, ans;
const int oo=2147483647;
namespace treap{
struct Node{
int l, r, siz, val, hav, rnd;
}nd[5000005];
int tot;
void upd(int k){
nd[k].siz = nd[nd[k].l].siz + nd[nd[k].r].siz + nd[k].hav;
}
void lRotate(int &k){
int t=nd[k].r; nd[k].r = nd[t].l; nd[t].l = k;
nd[t].siz = nd[k].siz; upd(k); k = t;
}
void rRotate(int &k){
int t=nd[k].l; nd[k].l = nd[t].r; nd[t].r = k;
nd[t].siz = nd[k].siz; upd(k); k = t;
}
void insert(int &k, int x){
if(!k){
k = ++tot; nd[k].siz = nd[k].hav = 1;
nd[k].val = x; nd[k].rnd = rand();
return ;
}
nd[k].siz++;
if(nd[k].val==x) nd[k].hav++;
else if(nd[k].val>x){
insert(nd[k].l, x);
if(nd[nd[k].l].rnd<nd[k].rnd) rRotate(k);
}
else{
insert(nd[k].r, x);
if(nd[nd[k].r].rnd<nd[k].rnd) lRotate(k);
}
}
void shanchu(int &k, int x){
if(!k) return ;
if(nd[k].val==x){
if(nd[k].hav>1){
nd[k].hav--;
nd[k].siz--;
return ;
}
if(nd[k].l*nd[k].r==0) k = nd[k].l + nd[k].r;
else if(nd[nd[k].l].rnd<nd[nd[k].r].rnd) rRotate(k), shanchu(k, x);
else lRotate(k), shanchu(k, x);
}
else if(nd[k].val>x)
nd[k].siz--, shanchu(nd[k].l, x);
else
nd[k].siz--, shanchu(nd[k].r, x);
}
int queryRank(int k, int x){
if(!k) return 0;
if(nd[k].val>x) return queryRank(nd[k].l, x);
else if(nd[k].val<x) return queryRank(nd[k].r, x)+nd[nd[k].l].siz+nd[k].hav;
else return nd[nd[k].l].siz;
}
void queryPre(int k, int x){
if(!k) return ;
if(nd[k].val<x) ans = max(ans, nd[k].val), queryPre(nd[k].r, x);
else queryPre(nd[k].l, x);
}
void queryNxt(int k, int x){
if(!k) return ;
if(nd[k].val>x) ans = min(ans, nd[k].val), queryNxt(nd[k].l, x);
else queryNxt(nd[k].r, x);
}
}
namespace sgt{
int rot[200005];
void insert(int o, int l, int r, int x, int k){
treap::insert(rot[o], k);
if(l==r) ;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(x<=mid) insert(lson, l, mid, x, k);
if(mid<x) insert(rson, mid+1, r, x, k);
}
}
void shanchu(int o, int l, int r, int x, int k){
treap::shanchu(rot[o], k);
if(l==r) ;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(x<=mid) shanchu(lson, l, mid, x, k);
if(mid<x) shanchu(rson, mid+1, r, x, k);
}
}
int queryRank(int o, int l, int r, int x, int y, int k){
if(l>=x && r<=y) return treap::queryRank(rot[o], k);
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
int re=0;
if(x<=mid) re += queryRank(lson, l, mid, x, y, k);
if(mid<y) re += queryRank(rson, mid+1, r, x, y, k);
return re;
}
}
void queryPre(int o, int l, int r, int x, int y, int k){
if(l>=x && r<=y) treap::queryPre(rot[o], k);
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(x<=mid) queryPre(lson, l, mid, x, y, k);
if(mid<y) queryPre(rson, mid+1, r, x, y, k);
}
}
void queryNxt(int o, int l, int r, int x, int y, int k){
if(l>=x && r<=y) treap::queryNxt(rot[o], k);
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(x<=mid) queryNxt(lson, l, mid, x, y, k);
if(mid<y) queryNxt(rson, mid+1, r, x, y, k);
}
}
}
int queryNum(int uu, int vv, int ww){
int l=0, r=1e8, mid, re;
while(l<=r){
mid = (l + r) >> 1;
if(sgt::queryRank(1, 1, n, uu, vv, mid)+1<=ww) re = mid, l = mid + 1;
else r = mid - 1;
}
return re;
}
int main(){
srand(time(NULL));
cin>>n>>m;
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
sgt::insert(1, 1, n, i, a[i]);
}
while(m--){
scanf("%d", &opt);
if(opt==1){
scanf("%d %d %d", &uu, &vv, &ww);
printf("%d\n", sgt::queryRank(1, 1, n, uu, vv, ww)+1);
}
if(opt==2){
scanf("%d %d %d", &uu, &vv, &ww);
printf("%d\n", queryNum(uu, vv, ww));
}
if(opt==3){
scanf("%d %d", &uu, &vv);
sgt::shanchu(1, 1, n, uu, a[uu]);
a[uu] = vv;
sgt::insert(1, 1, n, uu, a[uu]);
}
if(opt==4){
scanf("%d %d %d", &uu, &vv, &ww);
ans = -oo;
sgt::queryPre(1, 1, n, uu, vv, ww);
printf("%d\n", ans);
}
if(opt==5){
scanf("%d %d %d", &uu, &vv, &ww);
ans = oo;
sgt::queryNxt(1, 1, n, uu, vv, ww);
printf("%d\n", ans);
}
}
return 0;
}
luogu3380 【模板】二逼平衡树(树套树)的更多相关文章
- bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1807 Solved: 772[Submit][Stat ...
- bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)
3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description ...
- BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)
我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...
- BZOJ3196 二逼平衡树 【线段树套平衡树】
题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...
- BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay
传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...
- luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...
- [BZOJ3196] [Tyvj1730] 二逼平衡树(线段树 套 Splay)
传送门 至少BZOJ过了,其他的直接弃. 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的 ...
- bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】
四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...
- 「luogu3380」【模板】二逼平衡树(树套树)
「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...
- [luogu3380][bzoj3196]【模板】二逼平衡树【树套树】
题目地址 [洛谷传送门] 题目大意 区间查询k的排名,查找k排名的数,单点修改,区间前驱,区间后继. 感想 真的第一次写树套树,整个人都不对了.重构代码2次,发现样例都过不了,splay直接爆炸,可能 ...
随机推荐
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
Description Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their comm ...
- 转 google 制作 邀请函
http://blog.csdn.net/adali/article/details/8315619
- Win7系统出现提示: “Windows已遇到关键问题,将在一分钟后自动重新启动。”
1. 若用户在使用Win7系统时,遇到上述系统故障,建议重启电脑.等电脑开机自检一过,马上按键盘上的F8键,选择进入安全模式.在安全模式下,进行系统还原.其他的解决方法见下. 1.或者,在安全模式下, ...
- Mysql中的索引问题
索引的用途 提高查询的效率,相当于在字典中建立的字母表或者偏旁部首表,这样查询当然比一行一行查询要快的多 每个存储引擎可以建立索引的长度是不一样的,但每个表至少支持16个索引,总的索引长度至少为256 ...
- Suricata产生的数据存储目录
不多说,直接上干货! 我这里呢,分两种常用的Suricata. 一.源码编译安装的Suricata 这里不多说,大家可以去看我下面写的博客 使用 Suricata 进行入侵监控(一个简单小例子访问百度 ...
- SASS @mixin 遇到的坑
@mixin borderTop($size:1px,$type:solid,$color:red){ border-top:$size $type $color; } .border_top{ @i ...
- memcache的分布式配置
public static class MemcacheHelper { private static MemcachedClient mc; static MemcacheHelper() { St ...
- Git-远程操作
远程分支:远程跟踪分支remote branch是对远程分支状态的引用,是不能移动的,它会根据远程分支变化以及网络通信自动移动.Git服务器包含了远程分支master,在My Computer中的re ...
- gp服务输出的结果文件输出到绝对路径
gp服务跟本地用arcmap执行gp有个不同,就是输出的文件一般只能输出到arcgis server默认的output目录里面(arcgis server有此限制,无论怎么配还是写到output目录里 ...
- 伟景行 citymaker 从入门到精通(1)——js开发,最基本demo,加载cep工程文件
开发环境:citymaker 7(以下简称cm),jquery,easyui 1.4(界面),visual studio 2012(没有vs,不部署到IIS也行,html文件在本地目录双击打开可用) ...