2D RMQ
| Resources | |||||
|---|---|---|---|---|---|
| CF | |||||
这种技巧相当少见,我只用到过一次。
2D BIT
重点例题 – 继续学习之前,请先尽力独立解决这道题!
教程
| Resources | |||||
|---|---|---|---|---|---|
| GFG | |||||
| TC | |||||
实现
本质上,只需嵌套一维树状数组中的循环,就能得到 N 维树状数组。随后可以利用 容斥原理 查询子矩形。
#include <iostream>#include <vector>using namespace std;/*** 2D Fenwick Tree implementation.* Note that all cell locations are zero-indexed* in this implementation.*/template <typename T> class BIT2D {
另一种实现
使用这里提到的多维实现。
template <class T, int... Ns> struct BIT {T val = 0;void upd(T v) { val += v; }T query() { return val; }};template <class T, int N, int... Ns> struct BIT<T, N, Ns...> {BIT<T, Ns...> bit[N + 1];template <typename... Args> void upd(int pos, Args... args) {for (; pos <= N; pos += (pos & -pos)) bit[pos].upd(args...);
另见 Benq 的实现。
习题
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Back From Summer | Medium | Show Tags2DRQ, BIT | ||||
| IOI | Medium | Show Tags3DRQ, BIT | ||||
线段树的懒标记无法推广到高维。不过,可以推广一维树状数组解法,在高维中同样支持 区间加与区间求和。详情见这篇论文。
重点例题 – 继续学习之前,请先尽力独立解决这道题!
离线二维树状数组
目标是以较小常数实现 的复杂度。这需要对坐标范围 内的点执行 次单点更新与矩形求和查询。然而,上述二维树状数组需要 空间,无法接受。
由于事先知道全部更新与查询,可以在保持较小常数的同时降低空间占用。
可以用哈希表代替二维数组,但其时间和空间均为 ,而且常数 很大。更好的方法是压缩会被更新的点,使空间降至 。
核心思想是先确定每次更新会影响树状数组某一维上的哪些值。下表中的更新为 ,受其影响的单元格分别标为蓝、红、绿三色。
| (1, 1) | (1, 2) | (1, 3) | (1, 4) |
| (2, 1) | (2, 2) | (2, 3) | (2, 4) |
| (3, 1) | (3, 2) | (3, 3) | (3, 4) |
| (4, 1) | (4, 2) | (4, 3) | (4, 4) |
现在可以像离线一维树状数组一样压缩每一行(别忘了,二维树状数组的每一行本身又是 一个一维树状数组)。例如,可把第二行压缩为大小为 的树状数组:当 时,把区间查询 映射为 ;当 时, 映射为 。
类似地,对压缩后大小为 的第 4 行:
- → 查询
- → 查询
- → 查询
这只要求预先知道更新,并不要求预先知道查询。
实现
下面是上述离线二维树状数组的一份实现。它可能更容易理解,但常数较大,速度会慢很多:
#include <algorithm>#include <array>#include <iostream>#include <vector>using namespace std;/*** Offline 2D Fenwick Tree implementation.* Note that all the update and query indices* are zero-indexed, and that the rows are not
可以这样使用它:
#include <algorithm>#include <array>#include <iostream>#include <vector>using namespace std;using ll = long long;Code Snippet: Offline 2D BIT (Click to expand)int main() {
如前所述,上面的 OfflineBIT2D 实现远慢于 Benq 的 OffBIT2D,实际上会在
Soriya's Programming Project 一题中超时。
要在时限内通过上题并不容易。务必使用快速输入,并避免使用 endl。
一维树状数组 + 分治
这是最快的方法。
- 见 这篇文章
习题
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| Platinum | Medium | Show Tags2DRQ, BIT | ||||
| Platinum | Medium | Show Tags2DRQ, BIT | ||||
| APIO | Medium | Show Tags2DRQ, BIT | ||||
2D Segment Tree
A segment tree of (maybe sparse) segment trees.
Pro Tip
This is not the same as Quadtree. If the coordinates go up to , then 2D segment tree queries run in time each but some queries make Quadtree take time!
| Resources | |||||
|---|---|---|---|---|---|
| CPH | Brief description | ||||
Implementation
| Resources | |||||
|---|---|---|---|---|---|
| USACO | Code | ||||
| cp-algo | More code | ||||
Note - Memory Usage
Naively, inserting elements into a sparse segment tree requires memory, giving a bound of on 2D segment tree memory. This is usually too much for and 256 MB (although it sufficed for "Mowing the Field" due to the 512MB memory limit). Possible ways to get around this:
- Use arrays of fixed size rather than pointers.
- Reduce the memory usage of sparse segment tree to while maintaining the same insertion time (see the solution for IOI Game below for details).
Problems
Can also try the USACO problems from above.
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| POI | Hard | Show Tags2DRQ, Lazy SegTree | ||||
| IOI | Hard | Show Tags2DRQ, Sparse SegTree, Treap | ||||
| JOI | Very Hard | Show Tags2DRQ, SegTree | ||||