From 1b59408c1ca711d66a09d66f7b43760e1d0a781b Mon Sep 17 00:00:00 2001 From: summitwei Date: Fri, 5 Jun 2020 14:30:28 -0400 Subject: [PATCH] Update 6_Silver_Psum.md --- content/2_Silver/6_Silver_Psum.md | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/content/2_Silver/6_Silver_Psum.md b/content/2_Silver/6_Silver_Psum.md index 9b2e2ee..5d6f310 100644 --- a/content/2_Silver/6_Silver_Psum.md +++ b/content/2_Silver/6_Silver_Psum.md @@ -16,6 +16,7 @@ order: 6 Given an array of size $N$, answer $Q$ queries of the following form: Find the sum of all elements between indices $i$ and $j$. - [CSES Range Sum Queries I](https://cses.fi/problemset/task/1646) + - [USACO Breed Counting](http://www.usaco.org/index.php?page=viewproblem2&cpid=572) ## Tutorials @@ -29,16 +30,30 @@ See Ch 11 of https://www.overleaf.com/project/5e73f65cde1d010001224d8a ### 2D Prefix Sums -Given a 2-Dimension array of size $NxM$, answer $Q$ queries of the following form: Find the sum of all elements within the rectangle of indices $(x1,y1)$ to $(x2,y2)$. +Given a 2-dimensional array of size $NxM$, answer $Q$ queries of the following form: Find the sum of all elements within the rectangle of indices $(x1,y1)$ to $(x2,y2)$. - [CSES Forest Queries](https://cses.fi/problemset/task/1652) ### Difference Array -Consider the array formed by $a_i-a_{i-1}$. +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. +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.) - - [USACO Painting the Barn](http://www.usaco.org/index.php?page=viewproblem2&cpid=919) (i should find an easier one because this is hard) + - (find 1d difference array problem) -### Prefix Minimum, Etc. +### Prefix Minimum, XOR, etc. -Using \ No newline at end of file +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.) +On the other hand, XOR is its own inverse operation... + - (find range min problem. homework?) + - [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. + - (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.) + +## 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)