Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editorial] Difference Arrays #4558

Merged
merged 26 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion content/3_Silver/More_Prefix_Sums.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -652,6 +652,85 @@ for _ in range(query_num):

<Problems problems="cum2" hideSuggestProblemButton />

## Difference Arrays

<Resources>
<Resource source="Codeforces" title="An Introduction To Difference Arrays" url="https://codeforces.com/blog/entry/78762"/>
</Resources>

<FocusProblem problem="sample3" />

### 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)$

<LanguageSection>
<CPPSection>

```cpp
#include <array>
#include <iostream>
#include <vector>

using namespace std;

int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) { cin >> a[i]; }
vector<array<int, 3>> updates(m);
for (array<int, 3> &update : updates) {
cin >> update[0] >> update[1] >> update[2];
}

vector<long long> s(m + 2);
vector<long long> 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;
}
```

</CPPSection>
</LanguageSection>

### Problems

<Problems problems="difference-arrays-problemset" />

## Quiz

<Quiz>
Expand Down
43 changes: 43 additions & 0 deletions content/3_Silver/More_Prefix_Sums.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading