Codeforces Round 546 (Div. 2) - A-E Editorial
- 1Codeforces Round 162 (Div. 2) - A-E Editorial
- 2Codeforces Round 250 (Div. 1) - A-E Editorial
- 3Codeforces Round 256 (Div. 2) - A-E Editorial
- 4Codeforces Round 267 (Div. 2) - A-E Editorial
- 5Codeforces Round 271 (Div. 2) - A-F Editorial
- 6Codeforces Round 277.5 (Div. 2) - A-F Editorial
- 7Codeforces Round 316 (Div. 2) - A-E Editorial
- 8Codeforces Round 324 (Div. 2) - A-E Editorial
- 9Codeforces Round 388 (Div. 2) - A-E Editorial
- 10Codeforces Round 546 (Div. 2) - A-E EditorialThis article

The key transformation in the final problem subtracts a fixed prefix offset. Once that fixed part is gone, a chain of gap constraints becomes an ordinary nondecreasing sequence. The same habit appears earlier when a matrix operation is reduced to the anti-diagonal it preserves.
This post covers every problem, A-E, from Codeforces Round 546 (Div. 2). Each section contains a self-contained C++17 solution and its reasoning. The shorter opening problems are included so that the round is complete; the later sections carry the deeper techniques.
Round map
| Problem | Focus |
|---|---|
| A. Nastya Is Reading a Book | implementation |
| B. Nastya Is Playing Computer Games | constructive algorithms, math |
| C. Nastya Is Transposing Matrices | constructive algorithms, sortings |
| D. Nastya Is Buying Lunch | greedy |
| E. Nastya Hasn’t Written a Legend | binary search, data structures |
A picture of the main reduction
A. Nastya Is Reading a Book
Problem statement · Download the complete C++17 solution
Derive the algorithm
Find the first chapter whose ending page is at least the current page k. Every chapter from there to the last is still unread, including the current chapter.
Why it works
Chapter intervals appear in reading order. An ending page below k certifies a completed chapter, whereas an ending page at least k certifies a chapter that still contains an unread page. Counting the latter intervals is exactly the requested quantity.
Complexity. O(n) time and O(n) space in this implementation.
Implementation check. Use >= for the last page of a chapter.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { int n; cin >> n; vector<int> end(n); for (int i = 0, l; i < n; i++) cin >> l >> end[i]; int k; cin >> k; cout << count_if(end.begin(), end.end(), [&](int r) { return r >= k; }) << '\n';}B. Nastya Is Playing Computer Games
Problem statement · Download the complete C++17 solution
Derive the algorithm
There are n coin-picking actions. Every stone must be moved at least once, and the first moved stone lands on a manhole that has not yet been opened, so that stone must be moved again. This gives at least n+1 throws. Visiting both ends requires walking moves. The total lower bound is .
Why it works
Move toward the nearer end first, then sweep to the farther end. Initially throw the starting stone onto that final manhole and pick the starting coin. Whenever another manhole must be cleared, throw its stones onto the already opened starting manhole. Every stone moves once except the first stone, which moves twice. Thus the route attains the walking lower bound, n+1 throws, and exactly n coin picks simultaneously.
Complexity. O(1) time and space.
Implementation check. Use 64-bit arithmetic for the formula, and count distance in edges rather than positions.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { ll n, k; cin >> n >> k; cout << 3 * n + min(k - 1, n - k) << '\n';}C. Nastya Is Transposing Matrices
Problem statement · Download the complete C++17 solution
Derive the algorithm
A transpose sends a cell at offset to , preserving the sum of its global row and column. Therefore compare the multisets on every anti-diagonal, indexed by .
The invariant is sufficient too: transposing an adjacent 2 by 2 square swaps its two off-diagonal entries, which are neighboring cells along one anti-diagonal, while leaving the other two entries unchanged. These adjacent swaps generate every permutation of a diagonal.
Why it works
Necessity follows from the preserved index sum. For sufficiency, sort one anti-diagonal with adjacent swaps implemented by the corresponding 2 by 2 transposes. Such an operation changes no other anti-diagonal, so all diagonals can be independently transformed into their targets.
Complexity. O(nm log(min(n,m))) time and O(nm) space.
Implementation check. Compare multisets, including duplicate values, rather than only sums.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> a(n + m), b(n + m); for (int i = 0; i < n; i++) for (int j = 0, x; j < m; j++) { cin >> x; a[i + j].push_back(x); } for (int i = 0; i < n; i++) for (int j = 0, x; j < m; j++) { cin >> x; b[i + j].push_back(x); } for (int i = 0; i < n + m; i++) { sort(a[i].begin(), a[i].end()); sort(b[i].begin(), b[i].end()); } cout << (a == b ? "YES" : "NO") << '\n';}D. Nastya Is Buying Lunch
Problem statement · Download the complete C++17 solution
Derive the algorithm
Scan the queue backward. Keep a set of people who must remain in front of the final person. A person x can be crossed precisely when x has a permitted directed swap with the final person and with every already established blocker. Count those edges by scanning x’s outgoing adjacency list.
Why it works
The final person cannot pass a blocker, so it can never reach an earlier x unless x can first move behind every blocker and then behind the final person. Those are exactly the tested swaps. If x cannot do this, x itself becomes an additional permanent blocker. Backward induction proves that the greedy classification is both necessary and achievable.
Complexity. O(n+m) time and O(n+m) space.
Implementation check. The allowed pair is directed. A person already classified as crossable is not another blocker.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> p(n); for (int &x : p) { cin >> x; --x; } vector<vector<int>> g(n); for (int i = 0, a, b; i < m; i++) { cin >> a >> b; g[a - 1].push_back(b - 1); } vector<bool> blocked(n); blocked[p.back()] = true; int need = 1, answer = 0; for (int i = n - 2; i >= 0; i--) { int x = p[i], cnt = 0; for (int y : g[x]) cnt += blocked[y]; if (cnt == need) answer++; else blocked[x] = true, need++; } cout << answer << '\n';}E. Nastya Hasn’t Written a Legend
Problem statement · Download the complete C++17 solution
Derive the algorithm
Define , , and . The original inequalities become simply . Increasing position i makes a flat plateau: set every following b below the new value to that value, stopping at the first b already large enough.
A lazy segment tree stores sums and maxima of b, supports range assignment, and searches for the first index at least i with value at least the plateau height. A sum query adds back the precomputed sum of p over its interval.
Why it works
Subtracting p removes exactly the fixed gap from each neighboring inequality. Since b is nondecreasing, all affected elements form one contiguous interval. The propagation rule assigns the smallest legal value at every affected position, which is the plateau height; the first already-large value terminates propagation. The tree operations therefore reproduce the update exactly.
Complexity. O((n+q) log n) time and O(n) space.
Implementation check. Assignments can be negative or zero. Use a separate lazy flag instead of treating a numerical value as the absence of an assignment.
C++17
#include <bits/stdc++.h>using namespace std;using ll = long long;
struct Tree { int n; vector<ll> s, mx, lazy; vector<bool> has; Tree(vector<ll> &a) : n(a.size()), s(4 * n), mx(4 * n), lazy(4 * n), has(4 * n) { build(1, 0, n, a); } void pull(int v) { s[v] = s[v * 2] + s[v * 2 + 1]; mx[v] = max(mx[v * 2], mx[v * 2 + 1]); } void build(int v, int l, int r, vector<ll> &a) { if (r - l == 1) { s[v] = mx[v] = a[l]; return; } int m = (l + r) / 2; build(v * 2, l, m, a); build(v * 2 + 1, m, r, a); pull(v); } void put(int v, int l, int r, ll x) { s[v] = x * (r - l); mx[v] = lazy[v] = x; has[v] = true; } void push(int v, int l, int r) { if (!has[v] || r - l == 1) return; int m = (l + r) / 2; put(v * 2, l, m, lazy[v]); put(v * 2 + 1, m, r, lazy[v]); has[v] = false; } void assign(int v, int l, int r, int a, int b, ll x) { if (b <= l || r <= a) return; if (a <= l && r <= b) { put(v, l, r, x); return; } push(v, l, r); int m = (l + r) / 2; assign(v * 2, l, m, a, b, x); assign(v * 2 + 1, m, r, a, b, x); pull(v); } ll sum(int v, int l, int r, int a, int b) { if (b <= l || r <= a) return 0; if (a <= l && r <= b) return s[v]; push(v, l, r); int m = (l + r) / 2; return sum(v * 2, l, m, a, b) + sum(v * 2 + 1, m, r, a, b); } int first(int v, int l, int r, int start, ll x) { if (r <= start || mx[v] < x) return n; if (r - l == 1) return l; push(v, l, r); int m = (l + r) / 2; int z = first(v * 2, l, m, start, x); return z < n ? z : first(v * 2 + 1, m, r, start, x); }};int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<ll> a(n), p(n), pref(n + 1); for (ll &x : a) cin >> x; for (int i = 1; i < n; i++) { ll k; cin >> k; p[i] = p[i - 1] + k; } for (int i = 0; i < n; i++) { a[i] -= p[i]; pref[i + 1] = pref[i] + p[i]; } Tree st(a); int q; cin >> q; while (q--) { char op; int l; ll r; cin >> op >> l >> r; --l; if (op == '+') { ll x = st.sum(1, 0, n, l, l + 1) + r; int end = st.first(1, 0, n, l, x); st.assign(1, 0, n, l, end, x); } else cout << st.sum(1, 0, n, l, (int)r) + pref[(int)r] - pref[l] << '\n'; }}Support & Share
If this article helped you, please share or support!




























































Conversation
Comments powered by Waline