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直接爆炸,可能 ...
随机推荐
- Educational Codeforces Round 18 A
Description There are n cities situated along the main road of Berland. Cities are represented by th ...
- canvas绘图出现模糊,解决方法
在项目开发中发现,canvas有一个问题,绘制的图会出现模糊现象. 解决方法之一:将canvas元素放大2倍,然后将整个canvas元素或者其父元素缩小两倍. <!DOCTYPE html> ...
- Python实现决策树C4.5算法
为什么要改进成C4.5算法 原理 C4.5算法是在ID3算法上的一种改进,它与ID3算法最大的区别就是特征选择上有所不同,一个是基于信息增益比,一个是基于信息增益. 之所以这样做是因为信息增益倾向于选 ...
- Centos系统安装 phpredis 扩展
Git地址:https://github.com/nicolasff/phpredis 一.安装: phpize ./configure make && make install 其中 ...
- json数组某个数值对应渲染
当你统计某一年的某个值它对应的月份总数时,后台没有直接处理好,某个月对应某个值,这样会增加统计的负担,但当数据时这样的时候,在angularjs中时不能直接引用的. "data": ...
- ASP.NET相关事件及JS的执行顺序
实验代码: ASPX: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="We ...
- LN : leetcode 733 Flood Fill
lc 733 Flood Fill 733 Flood Fill An image is represented by a 2-D array of integers, each integer re ...
- 使用windows的fsutil命令创建指定大小及类型的测试文件
在软件测试中,对于上传.下载一类功能常常需要用不同大小的文件进行测试. 使用Windows命令fsutil可以生成任意大小.任意类型文件. C:\Users\axia\fsutil file crea ...
- smile domain name www.bn-nd.com for sell. Please contact boyanzheng at foxmail.com 微笑的域名。请联系邮箱。
- Socket是什么呢?中间软件抽象层
代表着网络连接 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用 ...