From 11e3278722cfe7552d988fa373d75bf08dabf504 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 14:18:29 +0300
Subject: [PATCH 01/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 14 ++++++++++++++
content/3_Silver/More_Prefix_Sums.problems.json | 15 +++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index a6befe63f7..8bde55c318 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -652,6 +652,20 @@ for _ in range(query_num):
+## Difference Arrays
+
+
+
+
+
+
+```cpp
+
+```
+
+
+
+
## Quiz
diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json
index d17611573d..0cb4eb3b46 100644
--- a/content/3_Silver/More_Prefix_Sums.problems.json
+++ b/content/3_Silver/More_Prefix_Sums.problems.json
@@ -30,6 +30,21 @@
}
}
],
+ "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"
+ }
+ }
+ ],
"cum2": [
{
"uniqueId": "usaco-919",
From 5dbd1304ab0cb238271be67727ea6f14251aad73 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 14:43:50 +0300
Subject: [PATCH 02/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 33 +++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 8bde55c318..91ebde59f4 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -660,7 +660,40 @@ for _ in range(query_num):
```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(tuple &update : updates) {
+ cin >> get<0>(update) >> get<1>(update) >> get<2>(update);
+ }
+ vector s(m + 2, 0), 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++) {
+ s[i] += s[i - 1];
+ add[get<0>(updates[i - 1])] += s[i] * get<2>(updates[i - 1]);
+ add[get<1>(updates[i - 1]) + 1] -= s[i] * get<2>(updates[i - 1]);
+ }
+ for(int i = 1; i <= n; i++) {
+ add[i] += add[i - 1];
+ cout << a[i] + add[i] << ' ';
+ }
+}
```
From 7acd5093ff3b10e5504334ba96c2b23ab02b30ec Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 15:11:06 +0300
Subject: [PATCH 03/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 91ebde59f4..2b989a6584 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,8 +654,26 @@ for _ in range(query_num):
## Difference Arrays
+
+
+
+
+### Explanation
+
+We need to know how many times each operation is applied i.e. how many times is contained within a query interval.
+For this, maintain a frequency array $s$; $s[i]$ is the number of times operation $i$ is applied. The important
+step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and increase
+each value by $1$, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just simply add $1$
+to $s[l]$, the left bound, and subtract $1$ from $s[r+1]$, the next cell after the right bound. After the queries,
+we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
+applying the operatios, can be done exactly the same way. This technique is called difference array and has linear time complexity.
+
+### Implementation
+
+**Time Complexity:** $\mathcal{O}(N+M)$
+
From d9079822d7cc4b19859bd13af9cbff0c2dbb8035 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 15:19:45 +0300
Subject: [PATCH 04/22] upd
---
.../3_Silver/More_Prefix_Sums.problems.json | 28 +++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json
index 0cb4eb3b46..1527c74731 100644
--- a/content/3_Silver/More_Prefix_Sums.problems.json
+++ b/content/3_Silver/More_Prefix_Sums.problems.json
@@ -45,6 +45,34 @@
}
}
],
+
+ "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",
From 3f9d56e4a98a970da2d78fa6ae5c61738c880833 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 16:19:29 +0300
Subject: [PATCH 05/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 2b989a6584..6b2d115073 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
@@ -664,11 +664,12 @@ for _ in range(query_num):
We need to know how many times each operation is applied i.e. how many times is contained within a query interval.
For this, maintain a frequency array $s$; $s[i]$ is the number of times operation $i$ is applied. The important
-step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and increase
-each value by $1$, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just simply add $1$
-to $s[l]$, the left bound, and subtract $1$ from $s[r+1]$, the next cell after the right bound. After the queries,
+step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and increment
+each value by one, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just increment
+$s[l]$ by one, the left bound, and decrement by one $s[r+1]$, the cell next to the right bound. After processing the queries
we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
-applying the operatios, can be done exactly the same way. This technique is called difference array and has linear time complexity.
+applying the operations, can be done exactly the same way. This technique is called difference array and has linear time complexity
+coming from interating through the array for making prefix sums.
### Implementation
@@ -717,6 +718,10 @@ int main() {
+### Problems
+
+
+
## Quiz
From a4b9c5e6aff20dfc1fc30f4ff1422b57199c2110 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Thu, 27 Jun 2024 13:32:20 +0000
Subject: [PATCH 06/22] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/More_Prefix_Sums.mdx | 24 +++++++++----------
.../3_Silver/More_Prefix_Sums.problems.json | 20 ++++++++--------
2 files changed, 21 insertions(+), 23 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 6b2d115073..9bdfcf1f8b 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -652,7 +652,7 @@ for _ in range(query_num):
-## Difference Arrays
+## Difference Arrays
@@ -668,7 +668,7 @@ step is how the frequency array is updated. Instead of iterationg through the in
each value by one, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just increment
$s[l]$ by one, the left bound, and decrement by one $s[r+1]$, the cell next to the right bound. After processing the queries
we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
-applying the operations, can be done exactly the same way. This technique is called difference array and has linear time complexity
+applying the operations, can be done exactly the same way. This technique is called difference array and has linear time complexity
coming from interating through the array for making prefix sums.
### Implementation
@@ -678,37 +678,35 @@ coming from interating through the array for making prefix sums.
-```cpp
+```cpp
#include
-#include
#include
+#include
using namespace std;
-int main() {
+int main() {
int n, m, k;
cin >> n >> m >> k;
vector a(n + 1);
- for(int i = 1; i <= n; i++) {
- cin >> a[i];
- }
+ for (int i = 1; i <= n; i++) { cin >> a[i]; }
vector> updates(m);
- for(tuple &update : updates) {
+ for (tuple &update : updates) {
cin >> get<0>(update) >> get<1>(update) >> get<2>(update);
}
vector s(m + 2, 0), add(n + 2, 0);
- for(int i = 0; i < k; i++) {
+ 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++) {
+ for (int i = 1; i <= m; i++) {
s[i] += s[i - 1];
add[get<0>(updates[i - 1])] += s[i] * get<2>(updates[i - 1]);
add[get<1>(updates[i - 1]) + 1] -= s[i] * get<2>(updates[i - 1]);
}
- for(int i = 1; i <= n; i++) {
+ for (int i = 1; i <= n; i++) {
add[i] += add[i - 1];
cout << a[i] + add[i] << ' ';
}
@@ -718,7 +716,7 @@ int main() {
-### Problems
+### Problems
diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json
index 1527c74731..419c6f6603 100644
--- a/content/3_Silver/More_Prefix_Sums.problems.json
+++ b/content/3_Silver/More_Prefix_Sums.problems.json
@@ -60,16 +60,16 @@
}
},
{
- "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"
- }
+ "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"
+ }
}
],
From 17a08a78fd4cb7d2234d5b3fb055f5e6e29c5345 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 16:47:13 +0300
Subject: [PATCH 07/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 9bdfcf1f8b..83cc71ce7f 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -664,7 +664,7 @@ for _ in range(query_num):
We need to know how many times each operation is applied i.e. how many times is contained within a query interval.
For this, maintain a frequency array $s$; $s[i]$ is the number of times operation $i$ is applied. The important
-step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and increment
+step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and incrementing
each value by one, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just increment
$s[l]$ by one, the left bound, and decrement by one $s[r+1]$, the cell next to the right bound. After processing the queries
we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
From 0662c299f337992458166276df8efedb097a53e4 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 20:39:21 +0300
Subject: [PATCH 08/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 83cc71ce7f..d272f12072 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -655,7 +655,7 @@ for _ in range(query_num):
## Difference Arrays
-
+
From 06d58cc1c3b94fcc234c888718c2fc4b165e9e8f Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 20:40:33 +0300
Subject: [PATCH 09/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index d272f12072..5ec2dbdcb2 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -662,14 +662,14 @@ for _ in range(query_num):
### Explanation
-We need to know how many times each operation is applied i.e. how many times is contained within a query interval.
+We need to know how many times each operation is applied, i.e. how many times is contained within a query interval.
For this, maintain a frequency array $s$; $s[i]$ is the number of times operation $i$ is applied. The important
-step is how the frequency array is updated. Instead of iterationg through the interval $[l, r]$ and incrementing
+step is how the frequency array is updated. Instead of iterating through the interval $[l, r]$ and incrementing
each value by one, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just increment
$s[l]$ by one, the left bound, and decrement by one $s[r+1]$, the cell next to the right bound. After processing the queries
we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
applying the operations, can be done exactly the same way. This technique is called difference array and has linear time complexity
-coming from interating through the array for making prefix sums.
+coming from iterating through the array for making prefix sums.
### Implementation
From 4f4de4ca148477edcfd3ef10d052e8712df49cdf Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 20:58:16 +0300
Subject: [PATCH 10/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 5ec2dbdcb2..0453781720 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -680,7 +680,7 @@ coming from iterating through the array for making prefix sums.
```cpp
#include
-#include
+#include
#include
using namespace std;
@@ -690,9 +690,9 @@ int main() {
cin >> n >> m >> k;
vector a(n + 1);
for (int i = 1; i <= n; i++) { cin >> a[i]; }
- vector> updates(m);
- for (tuple &update : updates) {
- cin >> get<0>(update) >> get<1>(update) >> get<2>(update);
+ vector> updates(m);
+ for (array &update : updates) {
+ cin >> update[0] >> update[1] >> update[2];
}
vector s(m + 2, 0), add(n + 2, 0);
for (int i = 0; i < k; i++) {
@@ -702,11 +702,14 @@ int main() {
s[y + 1]--;
}
for (int i = 1; i <= m; i++) {
+ // Apply prefix sums
s[i] += s[i - 1];
- add[get<0>(updates[i - 1])] += s[i] * get<2>(updates[i - 1]);
- add[get<1>(updates[i - 1]) + 1] -= s[i] * get<2>(updates[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] << ' ';
}
From 873c245b6517077f5595acccde52d827e498f89c Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Thu, 27 Jun 2024 17:59:28 +0000
Subject: [PATCH 11/22] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
content/3_Silver/More_Prefix_Sums.mdx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 0453781720..a1539dd966 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -679,8 +679,8 @@ coming from iterating through the array for making prefix sums.
```cpp
-#include
#include
+#include
#include
using namespace std;
@@ -704,7 +704,7 @@ int main() {
for (int i = 1; i <= m; i++) {
// Apply prefix sums
s[i] += s[i - 1];
- // At the same time compute the second difference array
+ // 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];
}
From c6568ac7533fd2bc8447c4e4da8778c48d0fc1a8 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 21:46:12 +0300
Subject: [PATCH 12/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 4 ----
1 file changed, 4 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index a1539dd966..a3954512f1 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,10 +654,6 @@ for _ in range(query_num):
## Difference Arrays
-
-
-
-
### Explanation
From 67d10058bd207670ceb4109d06b43cc75a83bfbe Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Thu, 27 Jun 2024 21:54:37 +0300
Subject: [PATCH 13/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index a3954512f1..a1539dd966 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,6 +654,10 @@ for _ in range(query_num):
## Difference Arrays
+
+
+
+
### Explanation
From f2133229f3e4f5fb37e3bbd0a3f34f03b8a3be73 Mon Sep 17 00:00:00 2001
From: SansPapyrus683 <55369003+SansPapyrus683@users.noreply.github.com>
Date: Thu, 27 Jun 2024 15:41:58 -0700
Subject: [PATCH 14/22] Update More_Prefix_Sums.mdx
---
content/3_Silver/More_Prefix_Sums.mdx | 26 +++++++++++++++++---------
1 file changed, 17 insertions(+), 9 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index a1539dd966..448d6de597 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -662,14 +662,16 @@ for _ in range(query_num):
### Explanation
-We need to know how many times each operation is applied, i.e. how many times is contained within a query interval.
-For this, maintain a frequency array $s$; $s[i]$ is the number of times operation $i$ is applied. The important
-step is how the frequency array is updated. Instead of iterating through the interval $[l, r]$ and incrementing
-each value by one, which would result in $\mathcal{O}(K \cdot M)$ time complexity, we could just increment
-$s[l]$ by one, the left bound, and decrement by one $s[r+1]$, the cell next to the right bound. After processing the queries
-we obtain the frequency array by doing prefix sums on $s$, resulting in $\mathcal{O}(M)$ time complexity. The second part,
-applying the operations, can be done exactly the same way. This technique is called difference array and has linear time complexity
-coming from iterating through the array for making prefix sums.
+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
@@ -694,25 +696,31 @@ int main() {
for (array &update : updates) {
cin >> update[0] >> update[1] >> update[2];
}
- vector s(m + 2, 0), add(n + 2, 0);
+
+ 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;
}
```
From 0b1e8ab30aa44b85bc891456d4fa812c4a650e06 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 11:45:00 +0300
Subject: [PATCH 15/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 79 ---------------------------
1 file changed, 79 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 448d6de597..8e1772cbe8 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -652,85 +652,6 @@ for _ in range(query_num):
-## Difference Arrays
-
-
-
-
-
-
-
-### Explanation
-
-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
-
-**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
From fa75b5c6d530e1516866128b1d932a37b5a8d2da Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 11:55:26 +0300
Subject: [PATCH 16/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 78 +++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 8e1772cbe8..6132711f00 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -652,6 +652,84 @@ for _ in range(query_num):
+## Difference Arrays
+
+
+
+
+
+
+
+### Explanation
+
+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
+
+**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
From f65e6cb02e1bd480513b83c0f4ef8f2170dc603b Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 12:09:24 +0300
Subject: [PATCH 17/22] try
---
content/3_Silver/More_Prefix_Sums.mdx | 53 ---------------------------
1 file changed, 53 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 6132711f00..fc443603de 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,10 +654,6 @@ for _ in range(query_num):
## Difference Arrays
-
-
-
-
### Explanation
@@ -677,55 +673,6 @@ The second part, applying the operations, can be done exactly the same way.
**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
From a033515cc3b88df7ea2c2216003ec7bcb7573aed Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 12:15:05 +0300
Subject: [PATCH 18/22] try
---
content/3_Silver/More_Prefix_Sums.mdx | 2 --
1 file changed, 2 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index fc443603de..25c4044f74 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,8 +654,6 @@ for _ in range(query_num):
## Difference Arrays
-
-
### Explanation
Let's create an array $s$, where $s[i]$ is the number of times operation $i$ is applied.
From 04e564b977972e690c0276d2a948a06206a25912 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 12:23:07 +0300
Subject: [PATCH 19/22] upd
---
.../3_Silver/More_Prefix_Sums.problems.json | 42 -------------------
1 file changed, 42 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json
index 419c6f6603..40c5cf6b89 100644
--- a/content/3_Silver/More_Prefix_Sums.problems.json
+++ b/content/3_Silver/More_Prefix_Sums.problems.json
@@ -30,48 +30,6 @@
}
}
],
- "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": [
{
From 430080be1e8495968acb3a97c022da1d1ea46fa4 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 12:34:36 +0300
Subject: [PATCH 20/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 58 ++++++++++++++++++-
.../3_Silver/More_Prefix_Sums.problems.json | 42 ++++++++++++++
2 files changed, 98 insertions(+), 2 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 25c4044f74..0200623ee7 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -654,6 +654,12 @@ for _ in range(query_num):
## Difference Arrays
+
+
+
+
+
+
### Explanation
Let's create an array $s$, where $s[i]$ is the number of times operation $i$ is applied.
@@ -667,13 +673,61 @@ 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
-
**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 40c5cf6b89..419c6f6603 100644
--- a/content/3_Silver/More_Prefix_Sums.problems.json
+++ b/content/3_Silver/More_Prefix_Sums.problems.json
@@ -30,6 +30,48 @@
}
}
],
+ "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": [
{
From d539e164de139eb56bcd7d4c364f9895fb8e4d45 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Fri, 28 Jun 2024 12:44:24 +0300
Subject: [PATCH 21/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 0200623ee7..5082bc92d6 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -660,7 +660,7 @@ for _ in range(query_num):
-### Explanation
+### Explanation 2
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.
@@ -673,6 +673,8 @@ 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 2
+
**Time Complexity:** $\mathcal{O}(N+M)$
From 14850c38a6e3fbf157a31a2a5fb8280ce3959018 Mon Sep 17 00:00:00 2001
From: Brebenel Mihnea <59292725+brebenelmihnea@users.noreply.github.com>
Date: Sat, 29 Jun 2024 11:04:30 +0300
Subject: [PATCH 22/22] upd
---
content/3_Silver/More_Prefix_Sums.mdx | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/content/3_Silver/More_Prefix_Sums.mdx b/content/3_Silver/More_Prefix_Sums.mdx
index 5082bc92d6..cdd8d691e4 100644
--- a/content/3_Silver/More_Prefix_Sums.mdx
+++ b/content/3_Silver/More_Prefix_Sums.mdx
@@ -660,7 +660,7 @@ for _ in range(query_num):
-### Explanation 2
+### 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.
@@ -673,7 +673,7 @@ 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 2
+### Implementation - Greg and Array
**Time Complexity:** $\mathcal{O}(N+M)$