#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 【模板】二逼平衡树(树套树)的更多相关文章

  1. bzoj 3196 Tyvj 1730 二逼平衡树(线段树套名次树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1807  Solved: 772[Submit][Stat ...

  2. bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)

    3196: Tyvj 1730 二逼平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description ...

  3. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...

  4. BZOJ3196 二逼平衡树 【线段树套平衡树】

    题目 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的数值 4.查询k在区间内的前驱(前驱 ...

  5. BZOJ 3196 Tyvj 1730 二逼平衡树:线段树套splay

    传送门 题意 给你一个长度为 $ n $ 有序数列 $ a $ ,进行 $ m $ 次操作,操作有如下几种: 查询 $ k $ 在区间 $ [l,r] $ 内的排名 查询区间 $ [l,r] $ 内排 ...

  6. luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)

    带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...

  7. [BZOJ3196] [Tyvj1730] 二逼平衡树(线段树 套 Splay)

    传送门 至少BZOJ过了,其他的直接弃. 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 1.查询k在区间内的排名 2.查询区间内排名为k的值 3.修改某一位值上的 ...

  8. bzoj 3196 Tyvj 1730 二逼平衡树【线段树 套 splay】

    四舍五入就是个暴力. 对于线段树的每个区间都开一棵按权值排序的splay 对于第二个操作,二分一下,每次查询mid的排名,复杂度 $ O(nlog(n)^{3}) $ 其余的操作都是$ O(nlog( ...

  9. 「luogu3380」【模板】二逼平衡树(树套树)

    「luogu3380」[模板]二逼平衡树(树套树) 传送门 我写的树套树--线段树套平衡树. 线段树上的每一个节点都是一棵 \(\text{FHQ Treap}\) ,然后我们就可以根据平衡树的基本操 ...

  10. [luogu3380][bzoj3196]【模板】二逼平衡树【树套树】

    题目地址 [洛谷传送门] 题目大意 区间查询k的排名,查找k排名的数,单点修改,区间前驱,区间后继. 感想 真的第一次写树套树,整个人都不对了.重构代码2次,发现样例都过不了,splay直接爆炸,可能 ...

随机推荐

  1. bzoj 5015 [Snoi2017]礼物

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5015 题解 首先把k=1,k=2,k=3的手推一遍 然后发现一些规律 就是数列可以表示成$a ...

  2. [洛谷P2417]课程

    题目链接: 点我 题目分析: 二分图最大匹配裸题,跑完匈牙利判断\(ans\)是否等于教室数即可 多组数据请注意初始化. 代码: #include<bits/stdc++.h> #defi ...

  3. Testing Round #12 C

    Description For the given sequence with n different elements find the number of increasing subsequen ...

  4. 486 Predict the Winner 预测赢家

    给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...

  5. php一致性hash性能测试(flexihash/memcache/memcached)

    一致性hash的使用在PHP中有三种选择分别是原生的memcache扩展,memcached扩展,还有一个是网上比较流行的flexihash类. 最近有项目需要使用flexihash类操作memcac ...

  6. springboot之项目打包

    通过win中的cmd或者idea中终端,打包并启动项目: 1.mvn package     [打包,在target中生成jar] 2.java -jar xxxxx.jar  [启动jar]

  7. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  8. UVa OJ 494

     Kindergarten Counting Game  Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...

  9. filter和map的使用

    if ( this.dataAggridvue.filter( item => item.Accepted == true && item.InvoiceGroupCode != ...

  10. Android获取本地相册图片、拍照获取图片

    需求:从本地相册找图片,或通过调用系统相机拍照得到图片. 容易出错的地方: 1,当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量 ...