Update 6_Silver_Psum.md

This commit is contained in:
summitwei 2020-06-09 14:10:12 -04:00 committed by GitHub
parent 137851babe
commit 242f872fb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,7 @@ order: 6
- [CSES Range Sum Queries I](https://cses.fi/problemset/task/1646)
- [USACO Breed Counting](http://www.usaco.org/index.php?page=viewproblem2&cpid=572)
- [LeetCode Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)
## Tutorials
@ -26,6 +27,8 @@ This technique is also known as *cumulative sum* or *partial sums*.
### Max Subarray Sum
[Maximum Subarray Sum](https://cses.fi/problemset/task/1643)
(Note: This problem has a solution known as Kadane's Algorithm. Please *don't* use that solution; try to solve it with prefix sums.)
### 2D Prefix Sums
@ -39,25 +42,36 @@ Given a 2-dimensional array of size $NxM$, answer $Q$ queries of the following f
**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.)
- (find 1d difference array problem)
- [USACO Haybale Stacking](http://www.usaco.org/index.php?page=viewproblem2&cpid=104)
### Prefix Minimum, XOR, etc.
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, like subtraction is to addition.)
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.)
On the other hand, XOR is its own inverse operation...
- (find range min problem. homework?)
- [USACO My Cow Ate My Homework](http://usaco.org/index.php?page=viewproblem2&cpid=762)
- [CSES Range XOR Queries](https://cses.fi/problemset/task/1650)
### More Complex Applications
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.
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!
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}$.
First, define the following:
$ps[i] = a_1+a_2+a_3+a_4+\cdots+a_i$
$ips[i] = 1\cdot a_1+2\cdot a_2+\cdots+i\cdot a_i$
Then, we have:
$l\cdot a_l + (l+1) \cdot a_{l+1} + \cdots + r \cdot a_r = ips[r]-ips[l-1]$
$(l-1) \cdot a_l + (l-1) \cdot a_{l+1} + \cdot + (l-1) \cdot a_r = (l-1)(ps[r]-ps[l-1])$
And so,
$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])$
Which is what we were looking for!
- (find iota ps problem)
- [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.)
- [Google Kick Start Candies](https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff43/0000000000337b4d) (**only** Test Set 1.)
## Additional Problems
- [USACO Subsequences Summing to Seven](http://www.usaco.org/index.php?page=viewproblem2&cpid=595)
- [USACO My Cow Ate My Homework](http://usaco.org/index.php?page=viewproblem2&cpid=762)
- [USACO Painting the Barn](http://www.usaco.org/index.php?page=viewproblem2&cpid=919)