2020-06-22 01:45:24 +00:00
---
2020-06-22 14:26:06 +00:00
id: SRQ
2020-06-22 19:59:16 +00:00
title: "Static Range Queries"
2020-06-22 01:45:24 +00:00
author: Benjamin Qi
prerequisites:
2020-06-22 19:59:16 +00:00
description: Range queries for any associative operation over a static array.
2020-06-22 01:45:24 +00:00
---
2020-06-22 17:56:41 +00:00
Given a static array $A[1],A[2],\ldots,A[N]$, you want to answer queries in the form $A[l]\ominus A[l+1]\ominus \cdots \ominus A[r]$ where $\ominus$ denotes any associative operation.
2020-06-22 14:26:06 +00:00
This can be done in $O(1)$ time each with $O(N\log N)$ time preprocessing.
2020-06-22 03:32:32 +00:00
## [Range Minimum Query](https://en.wikipedia.org/wiki/Range_minimum_query)
2020-06-22 14:26:06 +00:00
First we'll consider the special case when $\ominus$ denotes `min` .
2020-06-22 01:45:24 +00:00
- [CSES Range Minimum Queries I ](https://cses.fi/problemset/task/1647 )
### Tutorial
2020-06-22 03:32:32 +00:00
- CPH 9.1
- [cp-algorithms RMQ ](https://cp-algorithms.com/sequences/rmq.html )
2020-06-22 14:26:06 +00:00
< optional-content title = "Preprocessing in $O(N)$ Time" >
2020-06-22 03:51:48 +00:00
2020-06-22 14:26:06 +00:00
- [CF: $O(1)$ Query RMQ with $O(N)$ build ](https://codeforces.com/blog/entry/78931 )
2020-06-22 03:51:48 +00:00
< / optional-content >
2020-06-22 01:45:24 +00:00
2020-06-22 14:26:06 +00:00
## Divide & Conquer
2020-06-22 01:45:24 +00:00
**Divide & conquer** can refer to many different techniques. In this case, we use it to answer $Q$ queries offline in $O((N+Q)\log N)$ time.
Suppose that all queries satisfiy $L\le l\le r\le R$ (initially, $L=1$ and $R=N$). Letting $M=\left\lfloor \frac{L+R}{2}\right\rfloor$, we can compute
2020-06-22 03:32:32 +00:00
$$
lef[l]=A[l]\ominus A[l+1]\ominus \cdots \ominus A[M]
$$
2020-06-22 01:45:24 +00:00
for all $L\le l\le M$ and
2020-06-22 03:32:32 +00:00
$$
rig[r]=A[M+1]\ominus A[M+2] \ominus \cdots\ominus A[r]
$$
2020-06-22 01:45:24 +00:00
for each $M< r \le R $. Then the answer for all queries satisfying $ l \le M < r $ is simply $ lef [ l ] \ominus rig [ r ]$ due to the associativity condition . After that , we recurse on all query intervals completely contained within $[ L , M ]$ and $[ M + 1 , R ]$ independently .
Actually, this can be adjusted to answer queries online in $O(1)$ time each. See my implementation [here ](https://github.com/bqi343/USACO/blob/master/Implementations/content/data-structures/Static%20Range%20Queries%20(9.1 )/RangeQuery.h).
2020-06-22 19:59:16 +00:00
< optional-content title = "Faster Preprocessing" >
2020-06-22 14:26:06 +00:00
2020-06-22 19:59:16 +00:00
A data structure known as **sqrt-tree** can speed up preprocessing time to $O(N\log \log N)$.
2020-06-22 14:26:06 +00:00
2020-06-22 19:59:16 +00:00
* [CF Blog Pt 1 ](http://codeforces.com/blog/entry/57046 )
* [CF Blog Pt 2 ](http://codeforces.com/blog/entry/59092 )
2020-06-22 14:26:06 +00:00
< / optional-content >
## Problems
2020-06-22 01:45:24 +00:00
2020-06-22 03:32:32 +00:00
- [Codechef - Product on Segment ](https://www.codechef.com/problems/SEGPROD )
- [DMOJ - Continued Fractions ](https://dmoj.ca/problem/dmopc19c7p4 )
- [USACO Plat - Non-Decreasing Subsequences ](http://www.usaco.org/index.php?page=viewproblem2&cpid=997 )
- [JOI - Secret ](https://oj.uz/problem/view/JOI14_secret )