diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx index a6befe63f7..cdd8d691e4 100644 --- a/content/3_Silver/More_Prefix_Sums.mdx +++ b/content/3_Silver/More_Prefix_Sums.mdx @@ -3,7 +3,7 @@ id: more-prefix-sums redirects: - /silver/prefix-sums-2 title: 'More on Prefix Sums' -author: Darren Yao, Neo Wang, Qi Wang +author: Darren Yao, Neo Wang, Qi Wang, Mihnea Brebenel contributors: Jesse Choe, Kevin Sheng, Brad Ma, Juheon Rhee description: 'Max subarray sum, prefix sums in two dimensions, and a more complicated @@ -652,6 +652,85 @@ for _ in range(query_num): +## Difference Arrays + + + + + + + +### Explanation - Greg and Array + +Let's create an array $s$, where $s[i]$ is the number of times operation $i$ is applied. +The important step is how we update it. + +For an interval $[l, r]$, we can't loop through the interval and +increment each value, as that would be $\mathcal{O}(MK)$ and too slow. +Instead, we increment $s[l]$ by one and decrement $s[r+1]$ by one. + +Now, we get the *actual* array by computing its prefix sum array, +resulting in $\mathcal{O}(M)$ time complexity. +The second part, applying the operations, can be done exactly the same way. + +### Implementation - Greg and Array + +**Time Complexity:** $\mathcal{O}(N+M)$ + + + + +```cpp +#include +#include +#include + +using namespace std; + +int main() { + int n, m, k; + cin >> n >> m >> k; + vector a(n + 1); + for (int i = 1; i <= n; i++) { cin >> a[i]; } + vector> updates(m); + for (array &update : updates) { + cin >> update[0] >> update[1] >> update[2]; + } + + vector s(m + 2); + vector add(n + 2, 0); + for (int i = 0; i < k; i++) { + int x, y; + cin >> x >> y; + s[x]++; + s[y + 1]--; + } + + for (int i = 1; i <= m; i++) { + // Apply prefix sums + s[i] += s[i - 1]; + + // At the same time compute the second difference array + add[updates[i - 1][0]] += s[i] * updates[i - 1][2]; + add[updates[i - 1][1] + 1] -= s[i] * updates[i - 1][2]; + } + + for (int i = 1; i <= n; i++) { + // Apply prefix sums + add[i] += add[i - 1]; + cout << a[i] + add[i] << ' '; + } + cout << endl; +} +``` + + + + +### Problems + + + ## Quiz diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json index d17611573d..419c6f6603 100644 --- a/content/3_Silver/More_Prefix_Sums.problems.json +++ b/content/3_Silver/More_Prefix_Sums.problems.json @@ -30,6 +30,49 @@ } } ], + "sample3": [ + { + "uniqueId": "cf-295A", + "name": "Greg and Array", + "url": "https://codeforces.com/contest/295/problem/A", + "source": "CF", + "difficulty": "Normal", + "isStarred": false, + "tags": ["Difference Array"], + "solutionMetadata": { + "kind": "in-module", + "moduleId": "more-prefix-sums" + } + } + ], + + "difference-arrays-problemset": [ + { + "uniqueId": "abc179_d", + "name": " Leaping Tak", + "url": "https://atcoder.jp/contests/abc179/tasks/abc179_d", + "source": "AtCoder", + "difficulty": "Normal", + "isStarred": false, + "tags": ["Difference Array", "DP"], + "solutionMetadata": { + "kind": "none" + } + }, + { + "uniqueId": "cf-276C", + "name": "Little Girl and Maximum Sum", + "url": "https://codeforces.com/contest/276/problem/C", + "source": "CF", + "difficulty": "Normal", + "isStarred": false, + "tags": ["Difference Array"], + "solutionMetadata": { + "kind": "none" + } + } + ], + "cum2": [ { "uniqueId": "usaco-919",