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/4_Silver/6_Silver_Psum.md

89 lines
4.5 KiB
Markdown
Raw Normal View History

2020-06-04 01:42:57 +00:00
---
slug: /silver/prefix-sums
2020-06-04 05:39:49 +00:00
title: "Prefix Sums"
2020-06-09 18:27:57 +00:00
author: Eric Wei
2020-06-04 05:39:49 +00:00
order: 6
2020-06-04 01:42:57 +00:00
---
2020-06-09 16:57:44 +00:00
> Given an array $A_1,A_2,\ldots,A_N$, answer $Q$ queries of the following form: compute $A_L+A_{L+1}+\cdots+A_R$.
2020-06-03 20:16:33 +00:00
2020-06-04 05:39:49 +00:00
<!-- END DESCRIPTION -->
2020-06-04 02:16:50 +00:00
2020-06-08 18:07:48 +00:00
## Standard
2020-06-04 02:16:50 +00:00
- [CSES Range Sum Queries I](https://cses.fi/problemset/task/1646)
2020-06-09 18:10:12 +00:00
- [LeetCode Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)
2020-06-04 02:16:50 +00:00
## Tutorials
2020-06-08 18:07:48 +00:00
This technique is also known as *cumulative sum* or *partial sums*.
2020-06-03 20:16:33 +00:00
2020-06-08 18:07:48 +00:00
- Intro to USACO 11
2020-06-04 02:16:50 +00:00
- CPH 9.1
2020-06-09 18:42:59 +00:00
## USACO Silver Problems
2020-06-04 02:16:50 +00:00
2020-06-09 18:42:59 +00:00
These problems are relatively straightforward.
- [USACO Breed Counting](http://www.usaco.org/index.php?page=viewproblem2&cpid=572)
- [USACO Subsequences Summing to Seven](http://www.usaco.org/index.php?page=viewproblem2&cpid=595)
Now we'll look at some extensions.
## Max Subarray Sum
- [Maximum Subarray Sum](https://cses.fi/problemset/task/1643)
2020-06-09 16:57:44 +00:00
2020-06-09 18:10:12 +00:00
(Note: This problem has a solution known as Kadane's Algorithm. Please *don't* use that solution; try to solve it with prefix sums.)
2020-06-09 18:56:48 +00:00
<details>
<summary>Why are the two methods equivalent?</summary>
Consider the desired maximum subarray. As you go along from left to right, the prefix sum solution will mark the start of that subarray as the "current minimum prefix sum". Kadane's Algorithm, on the other hand, will set the current value to 0 at that point. As both solutions iterate through the array, they eventually find the right side of the maximum sum, and they find the answer to the problem at that location. In essence, Kadane's Algorithm stores the maximum sum of a subarray that ends at the current location (which is what the prefix sum solution calculates on each iteration), but it calculates this value greedily instead.
</details>
2020-06-09 18:10:12 +00:00
2020-06-09 18:42:59 +00:00
Extension:
2020-06-09 16:57:44 +00:00
2020-06-09 18:42:59 +00:00
- [CSES Maximum Subarray Sum II](https://cses.fi/problemset/task/1644)
## 2D Prefix Sums
2020-06-04 02:16:50 +00:00
2020-06-09 18:56:48 +00:00
Given a 2-dimensional array of size $N\cdot M$, answer $Q$ queries of the following form: Find the sum of all elements within the rectangle of indices $(x1,y1)$ to $(x2,y2)$.
2020-06-04 02:16:50 +00:00
- [CSES Forest Queries](https://cses.fi/problemset/task/1652)
2020-06-09 18:42:59 +00:00
- [USACO Painting the Barn (Silver)](http://www.usaco.org/index.php?page=viewproblem2&cpid=919)
- [USACO Painting the Barn (Gold)](http://www.usaco.org/index.php?page=viewproblem2&cpid=923)
- combine with max subarray sum!
2020-06-04 02:16:50 +00:00
2020-06-09 18:42:59 +00:00
## Difference Array
2020-06-04 02:16:50 +00:00
2020-06-08 18:07:48 +00:00
**Task:** Given an array of size $N$, do the following operation $Q$ times: add $X$ to the values between $i$ and $j$. Afterwards, print the final array.
**Solution:** Consider the array formed by $a_i-a_{i-1}$. When processing a range addition, only two values in this difference array change! At the end, we can recover the original array using prefix sums. (The math is left as an exercise to the reader.)
2020-06-04 02:16:50 +00:00
2020-06-09 18:10:12 +00:00
- [USACO Haybale Stacking](http://www.usaco.org/index.php?page=viewproblem2&cpid=104)
2020-06-04 02:16:50 +00:00
2020-06-09 18:42:59 +00:00
## Prefix Minimum, XOR, etc.
2020-06-04 02:16:50 +00:00
2020-06-09 18:10:12 +00:00
Similar to prefix sums, you can also take prefix minimum or maximum; but *you cannot* answer min queries over an arbitrary range with prefix minimum. (This is because minimum doesn't have an inverse operation, the way subtraction is to addition.)
2020-06-09 18:34:04 +00:00
On the other hand, XOR is its own inverse operation, meaning that the XOR of any number with itself is zero.
2020-06-08 18:07:48 +00:00
2020-06-09 18:10:12 +00:00
- [USACO My Cow Ate My Homework](http://usaco.org/index.php?page=viewproblem2&cpid=762)
2020-06-05 18:30:28 +00:00
- [CSES Range XOR Queries](https://cses.fi/problemset/task/1650)
2020-06-09 18:42:59 +00:00
## More Complex Applications
2020-06-09 16:57:44 +00:00
2020-06-09 18:10:12 +00:00
Instead of storing just the values themselves, you can also take a prefix sum over $i\cdot a_i$, or $10^i \cdot a_i$, for instance. Some math is usually helpful for these problems; don't be afraid to get dirty with algebra!
2020-06-09 18:56:48 +00:00
For instance, let's see how to quickly answer the following type of query: Find $1\cdot a_l+2\cdot a_{l+1}+3\cdot a_{l+2}+\cdots+(r-l+1)\cdot a_{r}\linebreak$.
2020-06-09 18:10:12 +00:00
First, define the following:
2020-06-09 18:56:48 +00:00
$ps[i] = a_1+a_2+a_3+a_4+\cdots+a_i \linebreak$
$ips[i] = 1\cdot a_1+2\cdot a_2+\cdots+i\cdot a_i\linebreak$
2020-06-09 18:10:12 +00:00
Then, we have:
2020-06-09 18:56:48 +00:00
$l\cdot a_l + (l+1) \cdot a_{l+1} + \cdots + r \cdot a_r = ips[r]-ips[l-1]\linebreak$
$(l-1) \cdot a_l + (l-1) \cdot a_{l+1} + \cdot + (l-1) \cdot a_r = (l-1)(ps[r]-ps[l-1])\linebreak$
2020-06-09 18:10:12 +00:00
And so,
2020-06-09 18:56:48 +00:00
$1\cdot a_l + 2 \cdot a_{l+1} + \cdots + (r-l+1) \cdot a_r = ips[r]-ips[l-1]-(l-1)(ps[r]-ps[l-1])\linebreak$
2020-06-09 18:10:12 +00:00
Which is what we were looking for!
2020-06-08 18:07:48 +00:00
2020-06-05 18:30:28 +00:00
- [AtCoder Multiple of 2019](https://atcoder.jp/contests/abc164/tasks/abc164_d) (You may want to solve the below problem "Subsequences Summing to Seven" before doing this one.)
2020-06-09 18:56:48 +00:00
- [Google Kick Start Candies](https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/0000000000337b4d) (**only** Test Set 1.)