Prefix Sum
1D Prefix Sum
Given an array of $n$ integers $a_1,a_2,\ldots,a_n$, the prefix sum array $\text{pre}$ is defined as:
\[\text{pre}_i = \sum_{j = 1}^ia_j, \quad \forall \; 1 \le i \le n\]That is, $\text{pre}_i$ stores the sum of the first $i$ elements of the array.
This array can be computed iteratively in $O(n)$ time using the following relations:
\[\begin{aligned} & \text{pre}_0 = 0 \\[6pt] & \text{pre}_i = \text{pre}_{i-1} + a_i, \quad \forall \; 1 \le i \le n \end{aligned}\]The most common application of this array is computing the sum of elements in a subarray $[l,r]$ of the array $a$ in $O(1)$ time using the fact:
\[\sum_{j=l}^ra_j \\[6pt] = \sum_{j=1}^ra_j - \sum_{j=1}^{l-1}a_j \\[6pt] = \text{pre}_r - \text{pre}_{l-1}\]Learn Through Problems
CSES - Static Range Sum Queries
Input
The first line contains integers $n \left(1 \le n \le 2\cdot10^5\right)$ and $q \left(1 \le q \le 2\cdot10^5\right)$.
The next line contains $n$ integers $a_1,a_2,\ldots,a_n \left(1 \le a_i \le 10^9\right)$.
The next $q$ lines contain integers $l_i$ and $r_i$ $(1 \le l_i \le r_i \le n)$.
Output
For each query, print the sum of values in range $[l_i,r_i]$.
8 4 3 2 4 5 1 1 5 3 2 4 5 6 1 8 3 3
11 2 24 4
Solution
Time Complexity: $O(n)$
Code (C++)
CSES - Subarray Sums II
Count the number of subarrays of $a$ having sum $x$.
Input
The first line contains integers $n \left(1 \le n \le 2\cdot10^5\right)$ and $x \left(-10^9 \le x \le 10^9\right)$.
The next line contains $n$ integers $a_1,a_2,\ldots,a_n \left(-10^9 \le a_i \le 10^9\right)$.
Output
Print the required number of subarrays.
5 7 2 -1 3 5 -2
2
Solution
- $1 \le l \le r \le n$
- $\displaystyle \sum_{i = l}^ra_i = x$
Build the prefix sum array $\text{pre}$ of the array $a$. Now the second condition can be written as: $$ \begin{aligned} & \text{pre}_r-\text{pre}_{l-1}=x \\[6pt] \Rightarrow &\; \text{pre}_{l-1} = \text{pre}_r-x \end{aligned} $$ Replacing $l \rightarrow l+1$, we can rewrite both the conditions as:
- $0 \le l < r \le n$
- $\text{pre}_l = \text{pre}_r-x$
To count the number of pairs $(l,r)$ satisfying this, we can iterate on $r$ from $1$ to $n$ and keep track of the frequencies of $\text{pre}_l$ for $0 \le l < r$.
Time Complexity: $O(n\log n)$
Code (C++)
CSES - Maximum Subarray Sum
Find the maximum sum of values in a contiguous non-empty subarray of $a$.
Input
The first line contains integer $n \left(1 \le n \le 2\cdot10^5\right)$.
The next line contains $n$ integers $a_1,a_2,\ldots,a_n \left(-10^9 \le a_i \le 10^9\right)$.
Output
Print the maximum subarray sum.
8 -1 3 -2 5 3 -5 2 2
9
Solution
Time Complexity: $O(n)$
Code (C++)
Codeforces - Array Splitting
Divide the array $a$ into $k$ non-empty consecutive subarrays. The subarrays are indexed from left to right as $1,2,\ldots,k$. The cost of the division is given as: $$\sum_{i = 1}^n\left(a_i \cdot f(i)\right)$$ where $f(i)$ is the index of the subarray containing $a_i$.
Find the maximum cost that can be obtained by dividing the array $a$ into $k$ non-empty consecutive subarrays.
Input
The first line contains integers $n$ and $k \left(1 \le k \le n \le 3\cdot10^5\right)$.
The next line contains integers $a_1,a_2,\ldots,a_n \left(-10^6 \le a_i \le 10^6\right)$.
Output
Print the maximum cost that can be obtained by dividing the array $a$ into $k$ non-empty consecutive subarrays.
5 2 -1 -2 5 -4 8
15
Solution
Build the prefix sum array $\text{pre}$ of the array $a$. We can rewrite the cost of the division as: $$ \begin{aligned} & \sum_{l = 1}^k\sum_{r = i_{l - 1} + 1}^{i_l}\left(a_r \cdot l\right) \\ =&\; \sum_{l = 1}^kl\cdot\sum_{r = i_{l - 1} + 1}^{i_l}a_r \\ =&\; \sum_{l = 1}^kl\cdot\left(\text{pre}_{i_l} - \text{pre}_{i_{l - 1}}\right) \\ =&\; \sum_{l = 1}^kl\cdot\text{pre}_{i_l}-\sum_{l = 1}^kl\cdot\text{pre}_{i_{l-1}} \\ =&\; \sum_{l = 1}^kl\cdot\text{pre}_{i_l}-\sum_{l = 0}^{k-1}\left(l+1\right)\cdot\text{pre}_{i_{l}} \\ =&\; \sum_{l = 1}^kl\cdot\text{pre}_{i_l}-\sum_{l = 0}^{k-1}l\cdot\text{pre}_{i_{l}}-\sum_{l = 0}^{k-1}\text{pre}_{i_{l}} \\ =&\; k\cdot\text{pre}_{i_k}-\sum_{l = 0}^{k-1}\text{pre}_{i_{l}} \\ =&\; k\cdot\text{pre}_{n}-\sum_{l = 1}^{k-1}\text{pre}_{i_{l}} \left(\because i_k = n,\text{pre}_{i_0} = \text{pre}_0 = 0\right) \end{aligned} $$ Formally, we have to compute the value of the expression: $$ \begin{aligned} & \max \left(k\cdot\text{pre}_{n}-\sum_{l = 1}^{k-1}\text{pre}_{i_{l}}\right) \\ =&\; k\cdot\text{pre}_{n}-\min\sum_{l = 1}^{k-1}\text{pre}_{i_{l}} \end{aligned} $$ To compute $\displaystyle \min\sum_{l = 1}^{k-1}\text{pre}_{i_{l}}$, we can take the $k-1$ smallest elements from $\left(\text{pre}_1, \text{pre}_2, \ldots, \text{pre}_{n-1}\right)$ since $0 < i_1 < i_2 < \ldots < i_{k-1} < n$.
Time Complexity: $O(n\log n)$
Code (C++)
Codeforces - Karen and Coffee
A temperature is called admissible if it belongs to at least $k$ of the given intervals.
You are also given $q$ queries. In each query, two integers $a$ and $b$ are given, and you have to determine the number of admissible integer temperatures in the range $[a,b]$.
Input
The first line contains integers $n$, $k \left(1 \le k \le n \le 2\cdot10^5\right)$ and $q \left(1 \le q \le 2\cdot10^5\right)$.
The next $n$ lines contain integers $l_i$ and $r_i \left(1 \le l_i \le r_i \le 2\cdot10^5\right)$.
The next $q$ lines contain integers $a$ and $b \left(1 \le a \le b \le 2\cdot10^5\right)$.
Output
For each query, print the number of admissible integer temperatures in the range $[a,b]$.
3 2 4 91 94 92 97 97 99 92 94 93 97 95 96 90 100
3 3 0 4
Solution
- $\displaystyle \text{freq}_i = \sum_{\substack{j=1 \\[3pt] l_j \le i \le r_j}}^n1$: the number of intervals to which the temperature $i$ belongs, $\quad \forall \; 1 \le i \le N$ where $N = 2\cdot 10^5$.
To efficiently build the $\text{freq}$ array, we use the technique of Difference Array:
- Initialize with all zeros.
- For every interval $[l_i,r_i]$, we update:
- $\text{freq}_{l_i} \gets \text{freq}_{l_i}+1$
- $\text{freq}_{r_i+1} \gets \text{freq}_{r_i+1}-1$
- After processing all the intervals, replace the $\text{freq}$ array with its prefix sum array.
The logic behind this technique is explained below:
- When we update $\text{freq}_{l_i} \gets \text{freq}_{l_i}+1$ and later replace the $\text{freq}$ array by its prefix sum array, the frequency of all temperatures $\ge l_i$ gets increased by $1$.
- However, for an interval $[l_i,r_i]$, we want to increase the frequency only for temperatures lying in the range $[l_i,r_i]$.
- To stop this increment after $r_i$, we update $\text{freq}_{r_i+1} \gets \text{freq}_{r_i+1}-1$ and later replacing the $\text{freq}$ array by its prefix sum cancels the earlier increment for all temperatures $\ge r_i+1$.
- As a result, the net effect is that only temperatures in the range $[l_i,r_i]$ have their frequency increased by $1$, while other temperatures remain unaffected.
Let us define: $$ c_i = \begin{cases} 1, & \text{if } \text{freq}_i \ge k \\ 0, & \text{otherwise} \end{cases} ,\quad \forall \;1 \le i \le N. $$ Build the prefix sum array $\text{pre}$ of the array $c$. The number of admissible integer temperatures in the range $[a,b]$ is given by $\text{pre}_b - \text{pre}_{a-1}$.
Time Complexity: $O(N + n + q)$
Code (C++)
CodeChef - Angry Cyborg
The process lasts for $q$ days. On the $j^{th}$ day, two integers $l_j$ and $r_j$ are chosen $\left(1 \le l_j \le r_j \le n\right)$, and the following events occur:
- In city $l_j$, $1$ statue is destroyed.
- In city $l_j+1$, $2$ statues are destroyed.
- $\ldots$
- In city $r_j$, $r_j-l_j+1$ statues are destroyed.
Formally, for each $i$ such that $l_j \le i \le r_j$, the number of statues destroyed in city $i$ is $i-l_j+1$.
After $q$ days, determine the total number of statues destroyed in each city.
Input
The first line contains an integer $t \left(1 \le t \le 10^3\right)$.
The first line of each test case contains integers $n \left(1 \le n \le 10^5\right)$ and $q \left(1 \le q \le 10^5\right)$.
The next $q$ lines of each test case contain integers $l_j$ and $r_j \left(1 \le l_j \le r_j \le n\right)$.
Additionally, the following holds true:
- The sum of $n$ over all test cases doesn't exceed $10^6$.
- The sum of $q$ over all test cases doesn't exceed $10^6$.
Output
For each test case, print $n$ integers, where the $i^{th}$ integer represents the total number of statues destroyed in city $i$ after $q$ days.
2 5 3 1 3 1 2 4 5 2 1 1 1
2 4 3 1 2 1 0
Solution
- $\displaystyle \text{val}_i = \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} (i - l_j + 1)$: the total number of statues destroyed in city $i$ after $q$ days, $\quad \forall \; 1 \le i \le n$.
We can rewrite the expression for $\text{val}_i$ as: $$ \begin{aligned} & \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} (i+1) - \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} l_j \\[6pt] =\; & (i+1)\cdot\sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} 1 - \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} l_j \end{aligned} $$ Let us define:
- $\displaystyle \text{cnt}_i = \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} 1$ : the number of intervals $[l_j,r_j]$ such that $l_j \le i \le r_j, \quad \forall \; 1 \le i \le n$.
- $\displaystyle \text{sum}_i = \sum_{\substack{j = 1 \\[3pt] l_j \le i \le r_j}}^{q} l_j$ : the sum of $l_j$ over all intervals $[l_j,r_j]$ such that $l_j \le i \le r_j, \quad \forall \; 1 \le i \le n$.
We can rewrite the expression for $\text{val}_i$ as: $$(i+1)\cdot\text{cnt}_i - \text{sum}_i$$ To efficiently build the $\text{cnt}$ and $\text{sum}$ arrays, we use the technique of Difference Array:
- Initialize with all zeros.
- For every interval $[l_j,r_j]$, we update:
- $\text{cnt}_{l_j} \gets \text{cnt}_{l_j}+1$
- $\text{cnt}_{r_j+1} \gets \text{cnt}_{r_j+1}-1$
- $\text{sum}_{l_j} \gets \text{sum}_{l_j}+l_j$
- $\text{sum}_{r_j+1} \gets \text{sum}_{r_j+1}-l_j$
- After processing all the intervals, replace the $\text{cnt}$ and $\text{sum}$ arrays by their prefix sum arrays.
Time Complexity: $O\left(\sum (n+q)\right)$
Code (C++)
AtCoder - Snuke Prime
There are $n$ services. For the $i^{th}$ service:
- It is used from day $a_i$ to day $b_i$, inclusive.
- If not subscribed, using this service costs $c_i$ yen per day.
There is a subscription plan that costs $C$ yen per day and allows unlimited use of all services during the subscribed days. You may start the subscription to this plan at the beginning of any day and cancel it at the end of any day.
Determine the minimum total cost required to use all services.
Input
The first line contains integers $n \left(1 \le n \le 2\cdot10^5 \right)$ and $C \left(1 \le C \le 10^9 \right)$.
The next $n$ lines contain integers $a_i$, $b_i \left(1 \le a_i \le b_i \le 10^9\right)$ and $c_i \left(1 \le c_i \le 10^9\right)$.
Output
Print the minimum total cost required to use all services.
2 6 1 2 4 2 2 4
10
Solution
- Initialize an empty vector $\text{all}$.
- Insert all values of $a_i$ and $b_i$ in $\text{all}$.
- Sort $\text{all}$ and erase duplicates from it.
- Let $m$ be the final size of the vector $\text{all}$
For each compressed index $i$, there are two distinct parts of days to consider:
- The exact day $\text{all}_i$.
- The continuous block of days $[\text{all}_i+1, \text{all}_{i+1}-1]$.
We will handle these two parts separately using arrays $\text{end}$ and $\text{mid}$ respectively:
- $\displaystyle \text{end}_i = \sum_{\substack{j = 1 \\[3pt] l_j \le \text{all}_i \le r_j}}^{n} c_j$ : the total cost of all services active on day $\text{all}_i, \quad \forall \; 0 \le i < m$.
- $\displaystyle \text{mid}_i = \sum_{\substack{j = 1 \\[3pt] l_j \le \text{all}_i+1 \\[3pt] \text{all}_{i+1}-1 \le r_j}}^{n} c_j$ : the total cost of all services active on every day in $[\text{all}_i+1, \text{all}_{i+1}-1], \quad \forall \; 0 \le i < m-1$.
Basically, the array $\text{end}$ handles individual important days (i.e., days that are either start or end of a service interval) and the array $\text{mid}$ handles continuous stretches (i.e., days that are neither start nor end of any service interval and hence have identical cost).
To efficiently build the $\text{end}$ and $\text{mid}$ arrays, we use the technique of Difference Array:
- Initialize with all zeros.
- For every service $a_i,b_i,c_i$, find the indices of $a_i$ and $b_i$ in the array $\text{all}$ using binary search to find their compressed values. Let these values be $l_i$ and $r_i$ respectively, we update:
- $\text{end}_{l_i} \gets \text{end}_{l_i}+c_i$
- $\text{end}_{r_i+1} \gets \text{end}_{r_i+1}-c_i$
- $\text{mid}_{l_i} \gets \text{mid}_{l_i}+c_i$
- $\text{mid}_{r_i} \gets \text{mid}_{r_i}-c_i$
- After processing all the services, replace the $\text{end}$ and $\text{mid}$ arrays by their prefix sum arrays.
Without considering the subscription plan that costs $C$ yen per day, the total cost required to use all services is given by: $$\sum_{i=0}^{m-1}\text{end}_i + \sum_{i=0}^{m-2}\text{mid}_i\cdot\left(\text{all}_{i+1}-\text{all}_i-1\right)$$ Considering the subscription plan that costs $C$ yen per day, the minimum total cost required to use all services is given by: $$\sum_{i=0}^{m-1}\min\left(\text{end}_i, C\right) + \sum_{i=0}^{m-2}\min\left(\text{mid}_i, C\right)\cdot\left(\text{all}_{i+1}-\text{all}_i-1\right)$$ Time Complexity: $O(n\log n)$
Code (C++)
CSAcademy - Subarray Medians
Input
The first line contains integer $n \left(1 \le n \le 10^4 \right)$.
The next line contains $n$ integers $a_1,a_2,\ldots,a_n \left(1 \le a_i \le n \right)$.
Output
Print the value of the given expression.
5 1 5 2 4 3
276
Solution
Build the prefix sum array $\text{pre}$ of the array $b$. We can rewrite the expression for the inner summation as: $$ \begin{aligned} & \sum_{\substack{1 \le l \le i \le r \le n \\[3pt] \text{pre}_{l-1} = \text{pre}_r}}l \cdot r \\[6pt] =\; & \sum_{r=i}^nr\cdot\sum_{\substack{l=1 \\[3pt] \text{pre}_{l-1} = \text{pre}_r}}^il \\[6pt] =\; & \sum_{r=i}^nr\cdot\text{sum}_{\text{pre}_r} \end{aligned} $$ where $\displaystyle \text{sum}_j = \sum_{\substack{l=1 \\[3pt] \text{pre}_{l-1} = j}}^il$, which can be computed by iterating on $l$ from $1$ to $i$.
Also, $-n \le \text{pre}_k \le n \left(\because \left|b_k\right| \leq 1\right)$ implies that we have to compute $\text{sum}_j$ for $-n \le j \le n$. To avoid using a map, we can do the following modification to the expression for the inner summation: $$\sum_{r=i}^nr\cdot\text{sum}_{\text{pre}_r+n}$$ where $\displaystyle \text{sum}_j = \sum_{\substack{l=1 \\[3pt] \text{pre}_{l-1}+n = j}}^il$. Thus, we have to compute $\text{sum}_j$ for $0 \le j \le 2\cdot n$ which can be stored in an array.
The final expression for the answer is given by: $$\sum_{i=1}^na_i\cdot \sum_{r=i}^nr\cdot\text{sum}_{\text{pre}_r+n}$$ Time Complexity: $O(n^2)$
Code (C++)
CodeChef - Sum of Cube
Input
The first line contains integer $t \left(1 \le t \le 10^5\right)$.
The first line of each test case contains integer $n \left(1 \le n \le 5\cdot10^5 \right)$.
The next line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n \left(1 \le a_i \le 10^6\right)$.
Additionally, the following holds true:
- The sum of $n$ over all test cases doesn't exceed $5\cdot10^5$.
Output
For each test case, print the value of the given expression modulo $998244353$.
3 2 1 1 3 1 2 1 5 8 5 6 2 3
10 128 42621
Solution
Build the prefix sum array $\text{pre}$ of the array $a$. Also build the prefix sum array $\text{pre2}$ of the array $\text{pre}$.
We can rewrite the given expression as: $$ \begin{aligned} & \sum_{i = 1}^n\sum_{j = i}^n\left(\text{pre}_j - \text{pre}_{i-1}\right)^3 \\[6pt] =\; & \sum_{i = 1}^n\sum_{j = i}^n\left(\text{pre}_j^3 - \text{pre}_{i-1}^3 - 3\cdot \text{pre}_j^2 \cdot \text{pre}_{i-1} + 3\cdot \text{pre}_j \cdot \text{pre}_{i-1}^2\right) \\[6pt] =\; & S_1 - S_2 - 3\cdot S_3 + 3\cdot S_4 \end{aligned} $$ Simplifying the expression for $S_1$: $$ \sum_{i = 1}^n\sum_{j = i}^n\text{pre}_j^3 = \sum_{j = 1}^n\sum_{i = 1}^j\text{pre}_j^3 = \sum_{j=1}^n\text{pre}_j^3\cdot\sum_{i=1}^j1 = \sum_{j=1}^n\text{pre}_j^3\cdot j $$ Simplifying the expression for $S_2$: $$ \sum_{i = 1}^n\sum_{j = i}^n\text{pre}_{i-1}^3 = \sum_{i=1}^n\text{pre}_{i-1}^3\cdot\sum_{j=i}^n1 = \sum_{i=1}^n\text{pre}_{i-1}^3\cdot(n-i+1) $$ Simplifying the expression for $S_3$: $$ \sum_{i = 1}^n\sum_{j = i}^n\text{pre}_j^2\cdot\text{pre}_{i-1} = \sum_{j = 1}^n\sum_{i = 1}^j\text{pre}_j^2\cdot\text{pre}_{i-1} = \sum_{j = 1}^n\text{pre}_j^2\cdot\sum_{i = 1}^j\text{pre}_{i-1} = \sum_{j = 1}^n\text{pre}_j^2\cdot\text{pre2}_{j-1} $$ Simplifying the expression for $S_4$: $$ \sum_{i = 1}^n\sum_{j = i}^n\text{pre}_j\cdot\text{pre}_{i-1}^2 = \sum_{i = 1}^n\text{pre}_{i-1}^2\cdot\sum_{j = i}^n\text{pre}_j = \sum_{i = 1}^n\text{pre}_{i-1}^2\cdot(\text{pre2}_n - \text{pre2}_{i-1}) $$ Time Complexity: $O(\sum n)$
Code (C++)
AtCoder - Manhattan Multifocal Ellipse
Find the number of integer pairs $(x,y)$ such that: $$\sum_{i = 1}^n\left(\left|x - x_i\right| + \left|y - y_i\right|\right) \leq D$$
Input
The first line contains integers $n \left(1 \le n \le 2\cdot10^5\right)$ and $D \left(0 \le D \le 10^6 \right)$.
The next $n$ lines contain integers $x_i$ and $y_i \left(-10^6 \le x_i,y_i \le 10^6\right)$.
Additionally, the following holds true:
- $(x_i,y_i) \neq (x_j,y_j)$ for $i \neq j$.
Output
Print the number of integer pairs $(x,y)$ satisfying the given condition.
2 3 0 0 1 0
8
Solution
- $\displaystyle f(x) = \sum_{i=1}^n\left|x-x_i\right|$, $\quad \forall \; 1 \le i \le n$.
- $\displaystyle g(y) = \sum_{i=1}^n\left|y-y_i\right|$, $\quad \forall \; 1 \le i \le n$.
Also, $\left|x-x_i\right| \le D \Rightarrow x_i - D \le x \le x_i + D$. Since $\min x_i = -10^6$, $\max x_i = 10^6$ and $\max D = 10^6$, we have $-2\cdot 10^6 \le x \le 2\cdot 10^6$. To make our computations simpler, we add a value $2\cdot 10^6$ to each $x_i$ so that $0 \le x \le 4\cdot 10^6$. Similarly we add a value $2\cdot 10^6$ to each $y_i$ so that $0 \le y \le 4\cdot 10^6$.
We have to count the number of integer pairs $(x,y)$ such that:
- $0 \le x, y \le N$ where $N= 4\cdot10^6$
- $f(x)+g(y) \le D$
Let us understand how to compute $f(x)$ efficiently. Build the arrays $\text{cnt}$ and $\text{sum}$ as:
- $\displaystyle \text{cnt}_j = \sum_{\substack{i = 1 \\[3pt] x_i \le j}}^n1$ : the number of $x_i \le j$, $\quad \forall\; 0 \le j \le N$.
- $\displaystyle \text{sum}_j = \sum_{\substack{i = 1 \\[3pt] x_i \le j}}^nx_i$ : the sum of $x_i \le j$, $\quad \forall\; 0 \le j \le N$.
We can rewrite the expression for $f(x)$ as: $$ \begin{aligned} & \sum_{\substack{i=1 \\[3pt] x_i \le x}}^n(x-x_i) + \sum_{\substack{i=1 \\[3pt] x_i > x}}^n(x_i-x) \\[6pt] =\; & \sum_{\substack{i=1 \\[3pt] x_i \le x}}^nx - \sum_{\substack{i=1 \\[3pt] x_i \le x}}^nx_i + \sum_{\substack{i=1 \\[3pt] x_i > x}}^nx_i - \sum_{\substack{i=1 \\[3pt] x_i > x}}^nx \\[6pt] =\; & x\cdot\sum_{\substack{i=1 \\[3pt] x_i \le x}}^n1 - \sum_{\substack{i=1 \\[3pt] x_i \le x}}^nx_i + \sum_{\substack{i=1 \\[3pt] x_i > x}}^nx_i - x\cdot\sum_{\substack{i=1 \\[3pt] x_i > x}}^n1 \\[6pt] =\; & x\cdot\text{cnt}_x - \text{sum}_x + (\text{sum}_N - \text{sum}_x) - x\cdot(\text{cnt}_N - \text{cnt}_x) \\[6pt] =\; & \text{sum}_N - 2\cdot\text{sum}_x + 2\cdot x\cdot\text{cnt}_x - x\cdot\text{cnt}_N \\[6pt] =\; & \text{sum}_N - 2\cdot\text{sum}_x + 2\cdot x\cdot\text{cnt}_x - x\cdot n \end{aligned} $$ Thus, we can compute $f(x)$ for some fixed $x$ in $O(1)$ using the arrays $\text{cnt}$ and $\text{sum}$. Similarly, we can build the arrays $\text{cnt}$ and $\text{sum}$ over $y_i$ for computing $g(y)$.
To count the number of integer pairs $(x,y)$ satisfying the given conditions, follow the steps:
- Store the values of $g(y)$ for $0 \le y \le N$ in a vector $\text{all}$ and sort it.
- Iterate on $x$ from $0$ to $N$, and compute the value of $f(x)$. The inequality $f(x)+g(y) \le D$ implies that $g(y) \le D-f(x)$. Use binary search in the vector $\text{all}$ to count the number of elements $\le D-f(x)$. This count gives us the number of integers $y$ for a fixed $x$ such that $f(x)+g(y) \le D$. Final answer is the sum of this count over all $x$.
Time Complexity: $O(N\log N)$
Code (C++)
2D Prefix Sum
Given a 2D array $a$ of size $n \times m$, the prefix sum array $\text{pre}$ is defined as:
\[\text{pre}_{i,j} = \sum_{x=1}^i\sum_{y=1}^ja_{x,y}, \quad \forall \; 1 \le i \le n, 1 \le j \le m\]That is, $\text{pre}_{i,j}$ stores the sum of all elements in the submatrix with top-left corner $(1,1)$ and bottom-right corner $(i,j)$.
This array can be computed iteratively in $O(n\cdot m)$ time using the following relations:
\[\begin{aligned} & \text{pre}_{0,j} = 0, \quad \forall \; 0 \le j \le m \\[6pt] & \text{pre}_{i,0} = 0, \quad \forall \; 0 \le i \le n \\[6pt] & \text{pre}_{i,j} = \text{pre}_{i-1,j} + \text{pre}_{i,j-1} - \text{pre}_{i-1,j-1} + a_{i,j}, \quad \forall \; 1 \le i \le n, 1 \le j \le m \end{aligned}\]The most common application of this array is computing the sum of elements in a submatrix with top-left corner $(x_1,y_1)$ and bottom-right corner $(x_2,y_2)$ in $O(1)$ time using the fact:
\[\sum_{x=x_1}^{x_2}\sum_{y=y_1}^{y_2}a_{x,y} = \text{pre}_{x_2,y_2} - \text{pre}_{x_1-1,y_2} - \text{pre}_{x_2, y_1-1} + \text{pre}_{x_1-1,y_1-1}\]To gain a much better understanding of this visually, do check out the interactive widget on the USACO Guide.
For completeness, the mathematical derivation is provided below:
\[\begin{aligned} & \sum_{x=x_1}^{x_2}\sum_{y=y_1}^{y_2}a_{x,y} \\[6pt] =\; & \sum_{x=x_1}^{x_2}\left(\sum_{y=1}^{y_2}a_{x,y} - \sum_{y=1}^{y_1-1}a_{x,y}\right) \\[6pt] =\; & \sum_{x=x_1}^{x_2}\sum_{y=1}^{y_2}a_{x,y} - \sum_{x=x_1}^{x_2}\sum_{y=1}^{y_1-1}a_{x,y} \\[6pt] =\; & \left(\sum_{x=1}^{x_2}\sum_{y=1}^{y_2}a_{x,y} - \sum_{x=1}^{x_1-1}\sum_{y=1}^{y_2}a_{x,y}\right) - \left(\sum_{x=1}^{x_2}\sum_{y=1}^{y_1-1}a_{x,y} - \sum_{x=1}^{x_1-1}\sum_{y=1}^{y_1-1}a_{x,y}\right) \\[6pt] =\; & \text{pre}_{x_2,y_2} - \text{pre}_{x_1-1,y_2} - \text{pre}_{x_2,y_1-1} + \text{pre}_{x_1-1,y_1-1} \end{aligned}\]Learn Through Problems
CSES - Forest Queries
You have to answer $q$ queries. In each query, you are given a rectangle defined by its corners $(y_1, x_1)$ and $(y_2, x_2)$, and you have to determine the number of cells containing a tree inside the rectangle.
Input
The first line contains integers $n \left(1 \le n \le 10^3 \right)$ and $q \left(1 \le q \le 2\cdot10^5 \right)$.
The next $n$ lines contain a string of length $n$ where an empty cell is defined by $.$ and a tree is defined by $*$.
The next $q$ lines contain integers $y_1$, $x_1$, $y_2$, $x_2 \left(1 \le y_1 \le y_2 \le n, 1 \le x_1 \le x_2 \le n \right)$.
Output
For each query, print the number of cells containing a tree inside the rectangle.
4 3 .*.. *.** **.. **** 2 2 3 4 3 1 3 1 1 1 2 2
3 1 2
Solution
The number of trees inside the rectangle defined by $(y_1,x_1)$ and $(y_2,x_2)$ is given by: $$\text{pre}_{y_2,x_2} - \text{pre}_{y_1-1,x_2} - \text{pre}_{y_2,x_1-1} + \text{pre}_{y_1-1,x_1-1}$$ Time Complexity: $O(n^2 + q)$
Code (C++)
Codeforces - 2D Difference Array
Perform $q$ operations on this grid:
- The $i^{th}$ operation is described by integers $a_i,b_i,c_i,d_i,w_i$.
- In the $i^{th}$ operation, for every $j,k$ such that $a_i \le j \le b_i$ and $c_i \le k \le d_i$, update $a_{j,k} \gets a_{j,k}+w_i$.
Find the final state of the grid after performing all $q$ operations.
Input
The first line contains integers $n$, $m \left(1 \le n,m \le 10^3 \right)$ and $q \left(1 \le q \le 10^5 \right)$.
The next $q$ lines contain integers $a_i$, $b_i \left(1 \le a_i,b_i \le n \right)$, $c_i$, $d_i \left(1 \le c_i,d_i \le m \right)$ and $w_i \left(0 \le w_i \le 10^9 \right)$.
Output
Print the final state of the grid after performing all $q$ operations.
3 3 3 1 2 1 3 5 2 3 2 3 4 2 3 1 2 3
5 5 5 8 12 9 3 7 4
Solution
- $\displaystyle \text{val}_{i,j} = \sum_{\substack{k=1 \\ a_k \le i \le b_k \\ c_k \le j \le d_k}}^q{w_k}$: the value of $a_{i,j}$ after all $q$ operations, $\quad \forall \; 1 \le i \le n, 1 \le j \le m$.
To efficiently build the $\text{val}$ array, we use the technique of Difference Array:
- Initialize with all zeros.
- For every query $a_i,b_i,c_i,d_i,w_i$, we update:
- $\text{val}_{a_i,c_i} \gets \text{val}_{a_i,c_i} + w_i$
- $\text{val}_{a_i,d_i+1} \gets \text{val}_{a_i,d_i+1} - w_i$
- $\text{val}_{b_i+1,c_i} \gets \text{val}_{b_i+1,c_i} - w_i$
- $\text{val}_{b_i+1,d_i+1} \gets \text{val}_{b_i+1,d_i+1} + w_i$
- After processing all the queries, replace the $\text{val}$ array with its prefix sum array.
The logic of this technique extended to 2D is left as an exercise for you.
Time Complexity: $O(n\cdot m + q)$
Code (C++)
Practice Problems
- CSES - Range XOR Queries
- CSES - Subarray Divisibility
- Codeforces - Good Subarrays
- AtCoder - Not All Covered
- AtCoder - Lamps
- Codeforces - Constant Palindrome Sum
- Codeforces - Greg and Array
- Codeforces - Little Girl and Maximum Sum
- Codeforces - Number of Ways
- Codeforces - Matrix Cascade
- Codeforces - Unique Median
- Codeforces - Yamakasi
- Codeforces - Gangsta
- Codeforces - Running Miles
- Codeforces - The Union of k-Segments
- Codeforces - Mike and Geometry Problem
- Codeforces - Blackslex and Plants
- AtCoder - Tail of Snake
- AtCoder - Max/Min
- AtCoder - Divisible Substring
- Codeforces - Star Sky
- AtCoder - Balanced Rectangles
- CodeChef - Cherry and Bits