写的简单。主要是留给自己做复习资料。


「BZOJ1901」Dynamic Rankings.

给定一个含有 \(n\) 个数的序列 \(a_1,a_2 \dots a_n\),需要支持两种操作:

  • Q l r k 表示查询下标在区间 \([l,r]\) 中的第 \(k\) 小的数。
  • C x y 表示将 \(a_x\) 改为 \(y\)。

引入整体二分。

其实就是我们对于二分到的一个值域 mid,去离线针对所有的询问进行 check。

具体是利用一些数据结构。

然后根据 check 和 mid 的大小将询问分为两拨,再分治下去解决问题。

参考于 OI-wiki 的板子很精简且思路便于理解。

#include <cstdio>

int Abs (int x) { return x < 0 ? -x : x; }
int Max (int x, int y) { return x > y ? x : y; }
int Min (int x, int y) { return x < y ? x : y; } int Read () {
int x = 0, k = 1;
char s = getchar();
while (s < '0' || s > '9') {
if(s == '-')
k = -1;
s = getchar ();
}
while ('0' <= s && s <= '9')
x = (x << 3) + (x << 1) + (s ^ 48), s = getchar ();
return x * k;
} void Write (int x) {
if(x < 0)
x = -x, putchar ('-');
if(x > 9)
Write (x / 10);
putchar (x % 10 + '0');
} void Print (int x, char s) { Write (x), putchar (s); } const int MAXN = 2e5 + 5; char Opt[3];
int Ans[MAXN], BIT[MAXN], a[MAXN], n, m; int Low_Bit (int x) {
return x & -x;
} void Update (int k, int x) {
for (int i = k; i <= n; i += Low_Bit (i))
BIT[i] += x;
} int Query (int k) {
int Res = 0;
for (int i = k; i >= 1; i -= Low_Bit (i))
Res += BIT[i];
return Res;
} struct Node {
bool Type;
int Id, x, y, k;
Node () {}
Node (bool T, int I, int X, int Y, int K) {
Type = T, Id = I, x = X, y = Y, k = K;
}
} q[MAXN], Tmp[2][MAXN]; void Solve (int l, int r, int L, int R) {
if (l > r || L > R)
return ;
if (L == R) {
for (int i = l; i <= r; i++)
if (q[i].Type)
Ans[q[i].Id] = L;
return ;
}
int Mid = (L + R) >> 1, Cnt0 = 0, Cnt1 = 0;
for (int i = l; i <= r; i++)
if (q[i].Type) {
int Check = Query (q[i].y) - Query (q[i].x - 1);
if (q[i].k <= Check)
Tmp[0][++Cnt0] = q[i];
else
q[i].k -= Check, Tmp[1][++Cnt1] = q[i];
}
else {
if (q[i].y <= Mid)
Update (q[i].x, q[i].k), Tmp[0][++Cnt0] = q[i];
else
Tmp[1][++Cnt1] = q[i];
}
for (int i = 1; i <= Cnt0; i++)
if (!Tmp[0][i].Type)
Update (Tmp[0][i].x, -Tmp[0][i].k);
for (int i = 1; i <= Cnt0; i++)
q[l + i - 1] = Tmp[0][i];
for (int i = 1; i <= Cnt1; i++)
q[l + Cnt0 + i - 1] = Tmp[1][i];
Solve (l, l + Cnt0 - 1, L, Mid), Solve (l + Cnt0, r, Mid + 1, R);
} int main () {
n = Read (), m = Read ();
int p = 0, Tot = 0;
for (int i = 1, x; i <= n; i++)
x = Read (), q[++p] = Node (0, 0, i, x, 1), a[i] = x;
for (int i = 1, x, y; i <= m; i++) {
scanf ("%s", Opt + 1), x = Read (), y = Read ();
if (Opt[1] == 'Q') {
int k = Read ();
q[++p] = Node (1, ++Tot, x, y, k);
}
else
q[++p] = Node (0, 0, x, a[x], -1), q[++p] = Node (0, 0, x, y, 1), a[x] = y;
}
Solve (1, p, 0, 1e9);
for (int i = 1; i <= Tot; i++)
Print (Ans[i], '\n');
return 0;
}

「ZJOI2013」K 大数查询

需要维护 \(n\) 个可重整数集,集合的编号从 \(1\) 到 \(n\)。这些集合初始都是空集,有 \(m\) 个操作:

  • 1 l r c 表示将 \(c\) 加入到编号在 \([l,r]\) 内的集合中。
  • 2 l r c 表示查询编号在 \([l,r]\) 内的集合的并集中,第 \(c\) 大的数是多少。

注意可重集的并是不去除重复元素的,如 \(\{1,1,4\}\cup\{5,1,4\}=\{1,1,4,5,1,4\}\)。

放在这里是因为发现它比上道题的唯一差别在于区间修改。

也就是说整体二分的数据结构理论上可以百搭。比如线段树。

#include <cstdio>

typedef long long LL;
int Abs (int x) { return x < 0 ? -x : x; }
int Max (int x, int y) { return x > y ? x : y; }
int Min (int x, int y) { return x < y ? x : y; } int Read () {
int x = 0, k = 1;
char s = getchar();
while (s < '0' || s > '9') {
if(s == '-')
k = -1;
s = getchar ();
}
while ('0' <= s && s <= '9')
x = (x << 3) + (x << 1) + (s ^ 48), s = getchar ();
return x * k;
} LL Read_LL () {
LL x = 0, k = 1;
char s = getchar();
while (s < '0' || s > '9') {
if(s == '-')
k = -1;
s = getchar ();
}
while ('0' <= s && s <= '9')
x = (x << 3) + (x << 1) + (s ^ 48), s = getchar ();
return x * k;
} void Write (int x) {
if(x < 0)
x = -x, putchar ('-');
if(x > 9)
Write (x / 10);
putchar (x % 10 + '0');
} void Print (int x, char s) { Write (x), putchar (s); } const int MAXN = 5e4 + 5; int Ans[MAXN], n, m; struct Segment_Tree {
#define Lson p << 1
#define Rson p << 1 | 1 struct Segment_Node {
int l, r;
bool Clear;
LL Sum, Lazy;
Segment_Node () {}
Segment_Node (int L, int R, LL S, LL La, bool C) {
l = L, r = R, Sum = S, Lazy = La, Clear = C;
}
} Tr[MAXN * 4]; void Push (int p) {
if (Tr[p].Clear) {
Tr[Lson].Sum = Tr[Rson].Sum = 0;
Tr[Lson].Lazy = Tr[Rson].Lazy = 0;
Tr[Lson].Clear = Tr[Rson].Clear = true;
Tr[p].Clear = false;
}
if (Tr[p].Lazy) {
Tr[Lson].Sum += Tr[p].Lazy * (Tr[Lson].r - Tr[Lson].l + 1);
Tr[Rson].Sum += Tr[p].Lazy * (Tr[Rson].r - Tr[Rson].l + 1);
Tr[Lson].Lazy += Tr[p].Lazy, Tr[Rson].Lazy += Tr[p].Lazy;
Tr[p].Lazy = 0;
}
} void Pull (int p) {
Tr[p].Sum = Tr[Lson].Sum + Tr[Rson].Sum;
} void Make_Tree (int p, int l, int r) {
Tr[p].l = l, Tr[p].r = r;
if (Tr[p].l == Tr[p].r)
return ;
int Mid = (Tr[p].l + Tr[p].r) >> 1;
Make_Tree (Lson, l, Mid);
Make_Tree (Rson, Mid + 1, r);
} void Update (int p, int l, int r, LL x) {
if (l <= Tr[p].l && Tr[p].r <= r) {
Tr[p].Sum += x * (Tr[p].r - Tr[p].l + 1), Tr[p].Lazy += x;
return ;
}
Push (p);
int Mid = (Tr[p].l + Tr[p].r) >> 1;
if (l <= Mid)
Update (Lson, l, r, x);
if (r > Mid)
Update (Rson, l, r, x);
Pull (p);
} LL Query (int p, int l, int r) {
if (l <= Tr[p].l && Tr[p].r <= r)
return Tr[p].Sum;
Push (p);
int Mid = (Tr[p].l + Tr[p].r) >> 1;
LL Res = 0;
if (l <= Mid)
Res += Query (Lson, l, r);
if (r > Mid)
Res += Query (Rson, l, r);
Pull (p);
return Res;
} #undef Lson
#undef Rson
} Seg; struct Node {
LL k;
bool Type;
int Id, x, y;
Node () {}
Node (bool T, int I, int X, int Y, LL K) {
Type = T, Id = I, x = X, y = Y, k = K;
}
} q[MAXN], Tmp[2][MAXN]; void Solve (int l, int r, int L, int R) {
if (l > r || L > R)
return ;
if (L == R) {
for (int i = l; i <= r; i++)
if (q[i].Type)
Ans[q[i].Id] = L;
return ;
}
int Mid = (L + R) >> 1, Cnt0 = 0, Cnt1 = 0;
Seg.Tr[1].Clear = true, Seg.Tr[1].Sum = Seg.Tr[1].Lazy = 0;
for (int i = l; i <= r; i++)
if (q[i].Type) {
LL Check = Seg.Query(1, q[i].x, q[i].y);
if (q[i].k <= Check)
Tmp[1][++Cnt1] = q[i];
else
q[i].k -= Check, Tmp[0][++Cnt0] = q[i];
}
else {
if (q[i].k > Mid)
Seg.Update (1, q[i].x, q[i].y, 1ll), Tmp[1][++Cnt1] = q[i];
else
Tmp[0][++Cnt0] = q[i];
}
for (int i = 1; i <= Cnt0; i++)
q[l + i - 1] = Tmp[0][i];
for (int i = 1; i <= Cnt1; i++)
q[l + Cnt0 + i - 1] = Tmp[1][i];
Solve (l, l + Cnt0 - 1, L, Mid), Solve (l + Cnt0, r, Mid + 1, R);
} int main () {
LL k;
n = Read (), m = Read ();
int p = 0, Tot = 0;
Seg.Make_Tree (1, 1, n);
for (int i = 1, Opt, x, y; i <= m; i++) {
Opt = Read (), x = Read (), y = Read (), k = Read_LL ();
if (Opt == 1)
q[++p] = Node (0, i, x, y, k);
else
q[++p] = Node (1, ++Tot, x, y, k);
}
Solve (1, p, -n, n);
for (int i = 1; i <= Tot; i++)
Print (Ans[i], '\n');
return 0;
}

Template -「整体二分」的更多相关文章

  1. 「CF484E」Sign on Fence「整体二分」「线段树」

    题意 给定一个长度为\(n\)的正整数序列,第\(i\)个数为\(h_i\),\(m\)个询问,每次询问\((l, r, w)\),为\([l, r]\)所有长度为\(w\)的子区间最小值的最大值.( ...

  2. Template -「网络流 & 二分图」

    EK. 很少用到,知道思想即可. 懒得写封装的屑. queue<int> q; int Cap[MAXN][MAXN], Flow[MAXN][MAXN], Aug[MAXN], fa[M ...

  3. Template -「矩阵 - 行列式」

    #include <cstdio> int Abs(int x) { return x < 0 ? -x : x; } int Max(int x, int y) { return ...

  4. loj #535. 「LibreOJ Round #6」花火 树状数组求逆序对+主席树二维数点+整体二分

    $ \color{#0066ff}{ 题目描述 }$ 「Hanabi, hanabi--」 一听说祭典上没有烟火,Karen 一脸沮丧. 「有的哦-- 虽然比不上大型烟花就是了.」 还好 Shinob ...

  5. loj3161「NOI2019」I 君的探险(随机化,整体二分)

    loj3161「NOI2019」I 君的探险(随机化,整体二分) loj Luogu 题解时间 对于 $ N \le 500 $ 的点,毫无疑问可以直接 $ O(n^2) $ 暴力询问解决. 考虑看起 ...

  6. 【LOJ2402】「THUPC 2017」天天爱射击 / Shooting(整体二分)

    点此看题面 大致题意: 有\(n\)个区间,每个区间有一个权值,当权值变成\(0\)时消失.每个时刻将覆盖某一位置的所有区间权值减\(1\),求每个时刻有多少个区间在这一刻消失. 前言 整体二分裸题啊 ...

  7. 【BZOJ4009_洛谷3242】[HNOI2015] 接水果(整体二分)

    题目: 洛谷 3242 分析: 明确题意:在一棵树上给定若干权值为 \(w\) 的路径 \((u,v)\) (盘子),每次给定 \((a,b)\) (水果),询问所有满足 \((u,v)\) 被 \( ...

  8. 「DP 浅析」斜率优化

    #0.0 屑在前面 将结合经典例题 「HNOI2008」玩具装箱 以及 「NOI2007」货币兑换 进行讲解. #1.0 简述 #1.1 适用情况 斜率优化一般适用于状态转移方程如下的 DP \[f_ ...

  9. 「TJOI / HEOI2016」字符串

    「TJOI / HEOI2016」字符串 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为 \(n\) 的字符串 \(s\),和 ...

随机推荐

  1. c++:-2

    上节介绍C++的函数介绍:c++:-1,本节学习类与对象 类与对象 定义 类定义 class 类名称 { public: 公有成员(外部接口) private: 私有成员 protected: 保护型 ...

  2. 【ConcurrentHashMap】浅析ConcurrentHashMap的构造方法及put方法(JDK1.7)

    目录 引言 代码讲解 构造方法 put方法 ensureSegment Segment.put 引言 ConcurrentHashMap的数据结构如下. 和HashMap的最大区别在于多了一层Segm ...

  3. [题解] trip

    题目大意 给定一颗大小为 \(N\) 的树, \(1\)的度数不小于 \(2\) .每个点有一个颜色,要么为黑色要么为白色. 从 \(1\) 号点开始游走,计数器初始为 \(0\). 如果当前为黑点计 ...

  4. 初始C语言作业一

    1.下面哪个不是C语言内置的数据类型:( ) A.char B.double C.struct Stu D.short 解析: C语言中内置类型包括 char //字符数据类型 short //短整型 ...

  5. VsCode[Git] | 配置Gitee和Github | 不使用全局用户名和邮箱

    (VsCode[Git] | 配置Gitee和Github | 不使用全局用户名和邮箱 | 2021-04-11) 目录 一 .安装Git / VsCode配置Git / Win10系统 二.Git配 ...

  6. zabbix5.0报错PHP时区未设置(配置参数"date.timezone")

    解决办法 : #1.编辑文件/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,取消注释并设置为所在地时区 vim /etc/opt/rh/rh-php72/php- ...

  7. 2021夏季学期华清大学EE数算OJ1:算数问题

    第一次写博客,有点紧张... 也许格式也没有特别丑吧 先看原题( 此题做法众多,这里仅仅介绍蒟蒻的一种很复杂的思路(但最后还是喜提0ms的好成绩) 读完这道题,不难发现,此题不过是一个质因数分解+一堆 ...

  8. Java高并发-概念

    一.为什么需要并行 业务要求 http处理多个客户端请求 java虚拟机启动多个线程 进程开销比线程大的多 性能 多线程在多核系统比单线程要好的多 摩尔定律失效 二.几个重要概念 2.1 同步和异步 ...

  9. QTP——功能测试

    一.前言(课设目的及内容) QTP是quicktest Professional的简称,是一种自动测试工具.使用QTP的目的是想用它来执行重复的手动测试,主要是用于回归测试和测试同一软件的新版本.因此 ...

  10. 实验二——以点类 Point 为基类设计圆类 Circle

    学习内容:以点类 Point 为基类设计圆类 Circle 示例代码: package 实验二; import java.util.Scanner; class Point{//父类Point pri ...