做数据结构一定要平\((fo)\)心\((de)\)静\((yi)\)气\((pi)\)。。。不然会四处出锅的\(QAQ\)

写法:线段树套平衡树,\(O(Nlog^3N)\)。五个操作如果是对于整个区间的,那么就是平衡树板子题。现在既然是放在子区间上的,那么套一个线段树就好啦~

#include <bits/stdc++.h>
using namespace std; const int N = 50000 + 5;
const int INF = 1e8 + 5; #define ls (p << 1)
#define rs (p << 1 | 1)
#define mid ((l + r) >> 1) int n, m, tot, arr[N]; struct FHQ_Node {
int w, lc, rc, sz, rd;
}t[N << 8]; struct FHQ_Treap { int rt; int NewNode (int w) {
t[++tot] = FHQ_Node {w, 0, 0, 1, rand ()};
return tot;
} void push_up (int p) {
t[p].sz = 1;
if (t[p].lc) t[p].sz += t[t[p].lc].sz;
if (t[p].rc) t[p].sz += t[t[p].rc].sz;
} void split (int now, int &A, int &B, int w) {
if (now == 0) {
A = B = 0; return;
}
if (t[now].w <= w) {
A = now; split (t[now].rc, t[A].rc, B, w);
} else {
B = now; split (t[now].lc, A, t[B].lc, w);
}
push_up (now);
} int merge (int A, int B) {
if (A * B == 0) {
return A + B;
}
if (t[A].rd < t[B].rd) {
t[A].rc = merge (t[A].rc, B);
push_up (A); return A;
} else {
t[B].lc = merge (A, t[B].lc);
push_up (B); return B;
}
} int x, y, z; void Insert (int w) {
split (rt, x, y, w);
rt = merge (x, merge (NewNode (w), y));
// cout << "rt = " << rt << endl;
} void Delete (int w) {
split (rt, x, y, w - 1);
split (y, y, z, w);
y = merge (t[y].lc, t[y].rc);
rt = merge (x, merge (y, z));
} int pre (int w) {
split (rt, x, y, w - 1);
int p = x;
if (p == 0) {
return -0x7fffffff;
}
while (t[p].rc != 0) {
p = t[p].rc;
}
rt = merge (x, y);
return t[p].w;
} int nxt (int w) {
split (rt, x, y, w);
int p = y;
if (p == 0) {
return +0x7fffffff;
}
while (t[p].lc != 0) {
p = t[p].lc;
}
rt = merge (x, y);
return t[p].w;
} void print (int p) {
if (t[p].lc) print (t[p].lc);
// cout << "p = " << p << " w = " << t[p].w << " sz = " << t[p].sz << endl;
if (t[p].rc) print (t[p].rc);
} int rank (int w) {
split (rt, x, y, w - 1);
int ret = t[x].sz + 1;
rt = merge (x, y);
// print (rt); cout << endl;
return ret;
}
}; struct Segment_Tree { FHQ_Treap tree[N << 2]; void modify (int pos, int wpre, int wnew, int l = 1, int r = n, int p = 1) {
// cout << "p = " << p << endl;
if (wpre != -1) {tree[p].Delete (wpre);/*cout << "tree[" << p << "].Delete (" << wpre << ");" << endl;*/}
if (wnew != -1) {tree[p].Insert (wnew);/*cout << "tree[" << p << "].Insert (" << wnew << ");" << endl;*/}
if (l == r) return;
if (pos <= mid) {
modify (pos, wpre, wnew, l, mid + 0, ls);
} else {
modify (pos, wpre, wnew, mid + 1, r, rs);
}
} int tot, sta[N]; void get_segment (int nl, int nr, int l = 1, int r = n, int p = 1) {
if (p == 1) tot = 0;
if (nl <= l && r <= nr) {
sta[++tot] = p; return;
}
if (nl <= mid) get_segment (nl, nr, l, mid + 0, ls);
if (mid < nr) get_segment (nl, nr, mid + 1, r, rs);
} int rank (int l, int r, int w) {
get_segment (l, r);
int ret = 1;
for (int i = 1; i <= tot; ++i) {
// cout << tree[sta[i]].rank (w) << endl;
ret += (tree[sta[i]].rank (w) - 1);
}
// cout << "val = " << w << " rank = " << ret << endl;
return ret;
} int kth (int l, int r, int k) {
int lval = 0, rval = INF;
// cout << "k = " << k << endl;
while (lval < rval) {
// getchar ();
int _mid = (lval + rval + 1) >> 1;
// cout << "lval = " << lval << " rval = " << rval << " mid = " << _mid << " rank " << k << " = " << rank (l, r, _mid) << endl;
if (rank (l, r, _mid) <= k) {
lval = _mid;
} else {
rval = _mid - 1;
}
}
return lval;
} int pre (int l, int r, int w) {
get_segment (l, r);
int maxw = -0x7fffffff;
for (int i = 1; i <= tot; ++i) {
maxw = max (maxw, tree[sta[i]].pre (w));
}
return maxw;
} int nxt (int l, int r, int w) {
get_segment (l, r);
int minw = +0x7fffffff;
for (int i = 1; i <= tot; ++i) {
minw = min (minw, tree[sta[i]].nxt (w));
}
return minw;
}
}tr; int read () {
int s = 0, ch = getchar ();
while (!isdigit (ch)) {
ch = getchar ();
}
while (isdigit (ch)) {
s = s * 10 + ch - '0';
ch = getchar ();
}
return s;
} int main () {
// freopen ("data.in", "r", stdin);
srand (time (NULL));
n = read (), m = read ();
for (int i = 1; i <= n; ++i) {
int w = read ();
tr.modify (i, -1, w);
arr[i] = w;
// cout << endl;
}
int opt, pos, l, r, k;
for (int i = 1; i <= m; ++i) {
opt = read ();
if (opt == 1) {
l = read (), r = read (), k = read ();
cout << tr.rank (l, r, k) << endl;
}
if (opt == 2) {
l = read (), r = read (), k = read ();
cout << tr.kth (l, r, k) << endl;
}
if (opt == 3) {
pos = read (), k = read ();
tr.modify (pos, arr[pos], k);
arr[pos] = k;
}
if (opt == 4) {
l = read (), r = read (), k = read ();
cout << tr.pre (l, r, k) << endl;
}
if (opt == 5) {
l = read (), r = read (), k = read ();
cout << tr.nxt (l, r, k) << endl;
}
}
}

【BZOJ3196】【Luogu P3380】 【模板】二逼平衡树(树套树)的更多相关文章

  1. 【bzoj3196】Tyvj 1730 二逼平衡树 线段树套Treap

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

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

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

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

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

  4. 【BZOJ3196】Tyvj 1730 二逼平衡树

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

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

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

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

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

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

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

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

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

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

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

  10. 洛谷 P3380 bzoj3196 Tyvj1730 【模板】二逼平衡树(树套树)

    [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在 ...

随机推荐

  1. Go package(2) strings 用法

    go version go1.10.3 Go中的字符串用法,可以在 godoc.org 上查看语法和用法. 最简单的语法就是获取字符串中的子串 s := "hello world" ...

  2. OpenStack组件——Nova计算资源管理

    1.nova介绍 Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源.OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是通过 Nova 来实现的. ...

  3. CAD常用命令、快捷键和命令说明大全

    CAD常用命令.快捷键和命令说明大全 一:常用功能键 F1: 获取帮助 F2: 实现作图窗和文本窗口的切换 F3: 控制是否实现对象自动捕捉 F4: 数字化仪控制 F5: 等轴测平面切换 F6: 控制 ...

  4. Serialize and Deserialize N-ary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  5. [转帖]Linux查找文件6个高效工具

    Linux查找文件6个高效工具 https://www.linuxrumen.com/rmxx/176.html 需要实操 -inname -type 等等. 1. 前言 我们使用Linux过程中,经 ...

  6. PostgreSQL SQL优化之NOT IN问题

    在我们平时写SQL时,如果遇到需要排除某些数据时,往往使用id <> xxx and id <> xxx,进而改进为id not in (xxx, xxx); 这样写没有问题, ...

  7. 医院医疗类报表免费用,提反馈,还能赢取P30!

    医院医疗类报表免费用,提反馈,还能赢取P30! “葡萄城报表模板库是一款免费的报表制作.学习和参考工具,包含了超过 200 张高质量报表模板,涵盖了 16 大行业和 50 多种报表类型,为 30 余万 ...

  8. python打印带颜色的字体

    在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了. ...

  9. L2-014. 列车调度(Dilworth定理)

    火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道进入,最后从出口 ...

  10. SpringMVC实现全局异常处理器 (转)

    出处:  SpringMVC实现全局异常处理器 我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手 ...