This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/6_Plat/2DRQ.mdx

71 lines
3.4 KiB
Text
Raw Normal View History

2020-06-04 02:09:42 +00:00
---
2020-06-15 23:19:07 +00:00
id: 2DRQ
2020-06-04 05:39:49 +00:00
title: "2D Range Queries"
2020-06-04 03:26:53 +00:00
author: Benjamin Qi
2020-06-05 00:21:03 +00:00
prerequisites:
2020-06-22 20:51:12 +00:00
- Platinum - Range Update Range Query
2020-06-22 17:56:41 +00:00
description: "Extending Range Queries to 2D (and beyond)."
2020-06-04 02:09:42 +00:00
---
2020-06-04 03:26:53 +00:00
See [my implementations](https://github.com/bqi343/USACO/tree/master/Implementations/content/data-structures/2D%20Range%20Queries%20(15.2)).
## Static Array Queries
- [Multi-Dimensional RMQ (retrograd)](https://codeforces.com/blog/entry/53810)
- GP of Serbia 2020 B
## 2D BIT
### Tutorials
- [GFG 2D BIT](https://www.geeksforgeeks.org/two-dimensional-binary-indexed-tree-or-fenwick-tree/)
- [TopCoder BIT](https://www.topcoder.com/community/competitive-programming/tutorials/binary-indexed-trees/)
2020-06-08 18:07:48 +00:00
- Suppose that you want to update $N\approx 10^5$ points with both coordinates in the range $[1,N]$.
- The 2D BITs mentioned above use $O(N^2)$ memory, which is too much.
- Can be reduced to $O(N\log^2N)$ memory with unordered map, but this might also be too much (and too slow).
- If you know the points to be updated beforehand then you can reduce to $O(N\log N)$ memory (and no maps).
- [my 1D offline BIT](https://github.com/bqi343/USACO/blob/master/Implementations/content/data-structures/1D%20Range%20Queries%20(9.2)/BIToff.h)
- [my 2D offline BIT](https://github.com/bqi343/USACO/blob/master/Implementations/content/data-structures/2D%20Range%20Queries%20(15.2)/BIT2DOff%20(15.2).h)
2020-06-04 03:26:53 +00:00
2020-06-22 14:26:06 +00:00
<optional-content title="Range Update and Range Query in Higher Dimensions">
2020-06-22 17:56:41 +00:00
2020-06-22 14:26:06 +00:00
You can extend the 1D BIT solution for range update range query to higher dimensions as well
2020-06-05 00:21:03 +00:00
- [Paper](https://arxiv.org/pdf/1311.6093.pdf)
2020-06-22 14:26:06 +00:00
- USACO Camp - "Cows Play Global Thermonuclear War" (2D case)
2020-06-22 17:56:41 +00:00
2020-06-22 14:26:06 +00:00
</optional-content>
2020-06-05 00:21:03 +00:00
2020-06-04 03:26:53 +00:00
### Problems
- [CSES Forest Queries II](https://cses.fi/problemset/task/1739)
- [thecodingwizard's implementation](https://github.com/thecodingwizard/competitive-programming/blob/master/cses/Forest%20Queries%20II.cpp)
2020-06-04 03:26:53 +00:00
- [DMOJ Soriya's Programming Project](https://dmoj.ca/problem/dmopc19c7p5)
2020-06-08 18:07:48 +00:00
- intended complexity is $O(N\log^2 N)$
2020-06-04 03:26:53 +00:00
- compressed 2D BIT
2020-06-08 18:07:48 +00:00
- rather difficult to pass within time and memory limits
- Make sure to use `\n` instead of `endl`!
- [thecodingwizard's implementation with Benq's 2d offline bit](https://github.com/thecodingwizard/competitive-programming/blob/master/DMOJ/Soriyas%20Programming%20Project.cpp)
2020-06-04 03:26:53 +00:00
- or do divide & conquer with a 1D BIT
2020-06-08 18:07:48 +00:00
- same as first problem [here](https://robert1003.github.io/2020/01/31/cdq-divide-and-conquer.html)
- [thecodingwizard's (messy) implementation](https://github.com/thecodingwizard/competitive-programming/blob/master/DMOJ/Soriya%20Programming%20Project%201d%20BIT%20cdq%20dnc.cpp) based off of article above
2020-06-04 03:26:53 +00:00
- [IOI 2007 Pairs](https://wcipeg.com/problem/ioi0722)
- [DMOJ Crowded Cities](https://dmoj.ca/problem/bfs17p6)
2020-06-08 18:07:48 +00:00
- [USACO Plat Friendcross](http://www.usaco.org/index.php?page=viewproblem2&cpid=722)
- [USACO Plat Mowing](http://www.usaco.org/index.php?page=viewproblem2&cpid=601)
2020-06-04 03:26:53 +00:00
## 2D Segment Tree
2020-06-05 00:21:03 +00:00
Note: no lazy propagation in 2D.
2020-06-08 18:07:48 +00:00
### Short Description
2020-06-23 01:00:35 +00:00
- CSES 28.2 (Sparse Segment Tree), 28.4
2020-06-22 14:26:06 +00:00
- Segment Tree (or BIT) nested inside segment tree
- use 2D offline BIT instead whenever possible (faster, lower memory)
2020-06-08 18:07:48 +00:00
### Problems
2020-06-22 14:26:06 +00:00
- [POI Tetris 3D](https://szkopul.edu.pl/problemset/problem/OQjANSOOD_-c38gh8p6g3Gxp/site/?key=statement)
2020-06-24 20:50:30 +00:00
- [IOI 2013 Game](http://wcipeg.com/problem/ioi1323)
- good implementation?