Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- problems/MA-01/Atcoder/abc190_f/editorial.mdAdded+18
- problems/MA-01/Atcoder/abc190_f/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc190_f/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc190_f/solution.cppAdded+31
- problems/MA-01/Atcoder/abc190_f/statement.mdAdded+67
- problems/MA-01/Atcoder/abc196_e/editorial.mdAdded+32
- problems/MA-01/Atcoder/abc196_e/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc196_e/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc196_e/solution.cppAdded+42
- problems/MA-01/Atcoder/abc196_e/statement.mdAdded+77
- problems/MA-01/Atcoder/abc219_f/editorial.mdAdded+85
- problems/MA-01/Atcoder/abc219_f/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc219_f/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc219_f/solution.cppAdded+75
- problems/MA-01/Atcoder/abc219_f/statement.mdAdded+81
- problems/MA-01/Atcoder/abc229_g/editorial.mdAdded+31
- problems/MA-01/Atcoder/abc229_g/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc229_g/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc229_g/solution.cppAdded+51
- problems/MA-01/Atcoder/abc229_g/statement.mdAdded+58
- problems/MA-01/Atcoder/abc240_f/editorial.mdAdded+46
- problems/MA-01/Atcoder/abc240_f/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc240_f/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc240_f/solution.cppAdded+51
- problems/MA-01/Atcoder/abc240_f/statement.mdAdded+75
- problems/MA-01/Atcoder/abc242_d/editorial.mdAdded+46
- problems/MA-01/Atcoder/abc242_d/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc242_d/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc242_d/solution.cppAdded+30
- problems/MA-01/Atcoder/abc242_d/statement.mdAdded+71
- problems/MA-01/Atcoder/abc256_f/editorial.mdAdded+15
- problems/MA-01/Atcoder/abc256_f/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc256_f/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc256_f/solution.cppAdded+52
- problems/MA-01/Atcoder/abc256_f/statement.mdAdded+72
- problems/MA-01/Atcoder/abc269_f/editorial.mdAdded+41
- problems/MA-01/Atcoder/abc269_f/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc269_f/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc269_f/solution.cppAdded+56
- problems/MA-01/Atcoder/abc269_f/statement.mdAdded+110
- problems/MA-01/Atcoder/abc272_e/editorial.mdAdded+15
- problems/MA-01/Atcoder/abc272_e/judge.jsonAdded+15
- problems/MA-01/Atcoder/abc272_e/meta.jsonAdded+16
- problems/MA-01/Atcoder/abc272_e/solution.cppAdded+32
- problems/MA-01/Atcoder/abc272_e/statement.mdAdded+62
- problems/MA-01/Atcoder/abc288_d/data/1.inAdded
- problems/MA-01/Atcoder/abc288_d/data/1.outAdded
- problems/MA-01/Atcoder/abc288_d/data/10.inAdded
- problems/MA-01/Atcoder/abc288_d/data/10.outAdded
- problems/MA-01/Atcoder/abc288_d/data/11.inAdded
123456789101112131415161718# F - Shift and Inversions (editorial)
Source: https://atcoder.jp/contests/abc190/editorial/690 by en_translator
The inversion number can be calculated in an \(O(N \log N)\) with a method like [Fenwick Tree](https://atcoder.github.io/ac-library/document_en/fenwicktree.html). However, if this is done for every \(B\), it will cost \(O(N^2 \log N)\) time, which will lead to TLE.
Observe that, when \(k = i\) is changed to \(k = i + 1\), \(B\) does not change so much; how the inversion number changes then?
Changing \(k = i\) to \(k = i + 1\) is equivalent to:
- removing \(a_i\), the first element of \(B\), and then- adding \(a_i\) as the last element of \(B\).
When \(a_i\) is removed from the first element of \(B\), the inversion number decreases by \(a_i\), and when \(a_i\) is added as the last element, the inversion number increases by \(N - 1 - a_i\). Therefore, when \(k = i\) is changed to \(k = i + 1\), the inversion number is changed by \(N - 1 - 2 \times a_i\).
Since the delta of the inversion numbers can be found in an \(O(1)\) time, the problem can be solved in a total of \(O(N \log N)\) time, where the process of finding the inversion number of \(A\) is the bottleneck.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 24, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc190_f", "contest": "abc190", "type": "ABC", "letter": "F", "title": "Shift and Inversions", "url": "https://atcoder.jp/contests/abc190/tasks/abc190_f", "difficulty": 1321, "score": 600, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 24, "editorial_url": "https://atcoder.jp/contests/abc190/editorial/690", "code_source": "model", "pid": "A1518"}12345678910111213141516171819202122232425262728293031// abc190_f: F - Shift and Inversions// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc190/editorial/690// Judged AC on all 24 tests, max 0.06 s of the 2 s limit (submission 1).
#include <bits/stdc++.h>#include <atcoder/fenwicktree>using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int N; cin >> N; vector<int> a(N); atcoder::fenwick_tree<int> bit(N); long long inversions = 0;
for (int i = 0; i < N; ++i) { cin >> a[i]; inversions += i - bit.sum(0, a[i] + 1); bit.add(a[i], 1); }
for (int i = 0; i < N; ++i) { cout << inversions << '\n'; inversions += N - 1 - 2LL * a[i]; }
return 0;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667# F - Shift and Inversions
## Score: 600 points
### Problem StatementGiven is a sequence \( A = [a_0, a_1, a_2, \dots, a_{N-1}] \) that is a permutation of \( 0, 1, 2, \dots, N - 1 \). For each \( k = 0, 1, 2, \dots, N - 1 \), find the inversion number of the sequence \( B = [b_0, b_1, b_2, \dots, b_{N-1}] \) defined as \( b_i = a_{i+k \bmod N} \).
<details><summary>What is inversion number?</summary>The inversion number of a sequence \( A = [a_0, a_1, a_2, \dots, a_{N-1}] \) is the number of pairs of indices \( (i, j) \) such that \( i < j \) and \( a_i > a_j \).</details>
### Constraints- All values in input are integers.- \( 2 ≤ N ≤ 3 \times 10^5 \)- \( a_0, a_1, a_2, \dots, a_{N-1} \) is a permutation of \( 0, 1, 2, \dots, N - 1 \).
### InputInput is given from Standard Input in the following format:```Na_0 a_1 a_2 ... a_{N-1}```
### OutputPrint \( N \) lines. The \( (i + 1) \)-th line should contain the answer for the case \( k = i \).
### Sample Input 1```40 1 2 3```
### Sample Output 1```0343```We have \( A = [0, 1, 2, 3] \). For \( k = 0 \), the inversion number of \( B = [0, 1, 2, 3] \) is \( 0 \). For \( k = 1 \), the inversion number of \( B = [1, 2, 3, 0] \) is \( 3 \). For \( k = 2 \), the inversion number of \( B = [2, 3, 0, 1] \) is \( 4 \). For \( k = 3 \), the inversion number of \( B = [3, 0, 1, 2] \) is \( 3 \).
### Sample Input 2```100 3 1 5 4 2 9 6 8 7```
### Sample Output 2```9182128272833242114```1234567891011121314151617181920212223242526272829303132# E - Filters (editorial)
Source: https://atcoder.jp/contests/abc196/editorial/953 by en_translator
A \(\Theta(NQ)\)-time solution of applying \(f_1, f_2, \dots, f_N\) to each of \(x_1, x_2, \dots, x_Q\) will lead to TLE, because \(NQ\) can be up to \(4 \times 10^{10}\). Now let us consider how the composite function \(F(x) = f_N(\dots f_2(f_1(x))\dots)\) looks like.
When \(t_i = 1\), \(f_i\) is an operation of shifting the graph vertically; when \(t_i = 2\), \(f_i\) is an operation of clamping the graph to at least \(a_i\); when \(t_i = 3\), \(f_i\) is an operation of clamping the graph to at most \(a_i\). Thus, we can expect that the composition of those operations forms like the graph below — namely, there exist numbers \(a, b, c\) such that \(F(x) = \min(c, \max(b, x + a))\).

We can prove the fact in the following way:
When \(t_i = 1\), \(f_i(x) = \min(\infty, \max(-\infty, x + a_i))\); when \(t_i = 2\), \(f_i(x) = \min(\infty, \max(a_i, x + 0))\); when \(t_i = 3\), \(f_i(x) = \min(a_i, \max(-\infty, x + 0))\); so we can always express \(f_i\) in the form of \(\min(c, \max(b, x + a))\).
Also, let \(f(x) = \min(c_1, \max(b_1, x + a_1)), g(x) = \min(c_2, \max(b_2, x + a_2))\); then their composite function \(g(f(x))\) satisfies
\[ \begin{aligned} g(f(x)) &= \min(c_2, \max(b_2, \min(c_1, \max(b_1, x + a_1)) + a_2))\\ &= \min(c_2, \max(b_2, \min(c_1 + a_2, \max(b_1 + a_2, x + a_1 + a_2))))\\ &= \min(\min(c_2, \max(b_2, c_1 + a_2)), \max(\max(b_2, \min(c_1 + a_2, b_1 + a_2)), x + (a_1 + a_2))),\\ \end{aligned} \]
so it can be represented as \(\min(c, \max(b, x + a))\).
Since \(F(x)\) are the composition of \(f_1, f_2, …, f_N\), it is proved that be represented in the form of \(\min(c, \max(b, x + a))\).
Next, let us find \(a, b, c\) when it is expressed as \(F(x) = \min(c, \max(b, x + a))\). These can be calculated by compositing based on the composition rule \(g(f(x)) = \min(\min(c_2, \max(b_2, c_1 + a_2)), \max(\max(b_2, \min(c_1 + a_2, b_1 + a_2)), x + (a_1 + a_2)))\) described above.
Once we have \(a, b,\) and \(c\), we can calculate \(F(x)\) in \(O(1)\) time each, so the problem has been solved in a total of \(O(N + Q)\) time.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 39, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc196_e", "contest": "abc196", "type": "ABC", "letter": "E", "title": "Filters", "url": "https://atcoder.jp/contests/abc196/tasks/abc196_e", "difficulty": 1650, "score": 500, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 39, "editorial_url": "https://atcoder.jp/contests/abc196/editorial/953", "code_source": "editorial", "pid": "A1553"}123456789101112131415161718192021222324252627282930313233343536373839404142// abc196_e: E - Filters// Code from the official editorial, https://atcoder.jp/contests/abc196/editorial/953 (by en_translator)// Judged AC on all 39 tests, max 0.42 s of the 2 s limit.
#include <iostream>#include <numeric>#include <algorithm>using namespace std;using ll = long long;const ll INF = numeric_limits<ll>::max() / 4;void chmin(ll& a, ll b){ if(a > b) a = b; }void chmax(ll& a, ll b){ if(a < b) a = b; }
int main(){ ll N; cin >> N; ll low = -INF, high = INF, add = 0; for(ll i = 0; i < N; i++){ ll A, T; cin >> A >> T; if(T == 1){ low += A; high += A; add += A; } else if(T == 2){ chmax(low, A); chmax(high, A); } else{ chmin(low, A); chmin(high, A); } } ll Q; cin >> Q; for(ll i = 0; i < Q; i++){ ll x; cin >> x; cout << clamp(x + add, low, high) << '\n'; }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677# E - Filters
## Score
500 points
## Problem Statement
Given are integer sequences \(A = (a_1, a_2, \dots, a_N)\), \(T = (t_1, t_2, \dots, t_N)\), and \(X = (x_1, x_2, \dots, x_Q)\). Let us define \(N\) functions \(f_1(x), f_2(x), \dots, f_N(x)\) as follows:
\[f_i(x) = \begin{cases} x + a_i & (t_i = 1) \\ \max(x, a_i) & (t_i = 2) \\ \min(x, a_i) & (t_i = 3) \end{cases}\]
For each \(i = 1, 2, \dots, Q\), find \(f_N( \dots f_2(f_1(x_i)) \dots )\).
## Constraints
- All values in input are integers.- \(1 \leq N \leq 2 \times 10^5\)- \(1 \leq Q \leq 2 \times 10^5\)- \(|a_i| \leq 10^9\)- \(1 \leq t_i \leq 3\)- \(|x_i| \leq 10^9\)
## Input
Input is given from Standard Input in the following format:
```Na_1 t_1a_2 t_2...a_N t_NQx_1 x_2 ... x_Q```
## Output
Print \(Q\) lines. The \(i\)-th line should contain \(f_N( \dots f_2(f_1(x_i)) \dots )\).
## Sample Input 1
```3-10 210 110 35-15 -10 -5 0 5```
## Sample Output 1
```0051010```
We have \(f_1(x) = \max(x, -10), f_2(x) = x + 10, f_3(x) = \min(x, 10)\), thus:
- \(f_3(f_2(f_1(-15))) = 0\)- \(f_3(f_2(f_1(-10))) = 0\)- \(f_3(f_2(f_1(-5))) = 5\)- \(f_3(f_2(f_1(0))) = 10\)- \(f_3(f_2(f_1(5))) = 10\)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# F - Cleaning Robot (editorial)
Source: https://atcoder.jp/contests/abc219/editorial/2665 by en_translator
For convenience, we interpret “executing a program consisting of a \(k\)-time concatenation of string \(S\)” as “executing string \(S\) for \(K\) times.”
Let us denote by \((0, 0) \rightarrow (x_1, y_1) \rightarrow (x_2, y_2) \rightarrow \cdots \rightarrow (x_{|S|}, y_{|S|})\) the transitions of Robot’s position. Here, \(|S|\) denotes the length of \(S\). Let \(a, b\) be \(x_{|S|}, y_{|S|}\), respectively.
Let \(V_1\) be a set of squares which robot visits during the first time of execution (including the initial position before the first execution \((0, 0)\)). In other words, \(V_1\) is a set consisting of \((0, 0), (x_1, y_1), (x_2, y_2), \cdots, (x_{|S|}, y_{|S|})\), without duplicates. Let us arbitrarily number the elements of \(V_1\), denoting
\(V_1 = \lbrace (X_1, Y_1), (X_2, Y_2), \ldots, (X_N, Y_N) \rbrace\).
Here, \(N\) is the number of elements of \(V_1\). Then, set \(V_2\), the set of squares which robot visits during the second time of execution (including the initial position before the second execution), can be obtained by shifting each element of \(V_1\) by \((a, b)\):
\(V_2 = \lbrace (X_1 + a, Y_1 + b), (X_2 + a, Y_2 + b), \ldots, (X_N + a, Y_N + b) \rbrace\).
Set \(V_3\), the set of squares which robot visits during the third time of execution (including the initial position before the third execution), can be obtained by again shifting each element of \(V_2\) by \((a, b)\):
\(V_3 = \lbrace (X_1 + 2a, Y_1 + 2b), (X_2 + 2a, Y_2 + 2b), \ldots, (X_N + 2a, Y_N+ 2b) \rbrace\).
Generally, set \(V_k\), the set of squares which robot visits during the \(K\)-th time of execution (including the initial position before the \(K\)-th execution), can be represented as:
\(V_k = \lbrace (X_1 + (k-1)a, Y_1 + (k-1)b), (X_2 + (k-1)a, Y_2 + (k-1)b), \ldots, (X_N + (k-1)a, Y_N+ (k-1)b) \rbrace\).
Especially note that, for \(i = 1, 2, \ldots, N\), \((X_i, Y_i) \in V_1\) corresponds to:
- \((X_i + a, Y_i + b)\) for the second execution;- \((X_i + 2a, Y_i + 2b)\) for the third execution;- \(\cdots\)- \((X_i + (k-1)a, Y_i + (k-1)b)\) for the \(k\)-th execution.
Here, for \(i = 1, 2, \ldots, N\), let
\(d_i\) := (minimum integer \(j\) such that \((X_i+ja, Y_i+jb) \in V_1\))
If there does not exist such integer \(j\), we define \(d_i := \infty\).
Then, focusing \((X_i, Y_i) \in V_1\), as for the first \(d_i\) executions,
- it visits Square \((X_i, Y_i)\) for the first time during the first execution;- it visits Square \((X_i + a, Y_i + b)\) for the first time during the second execution;- it visits Square \((X_i + 2a, Y_i + 2b)\) for the first time during the third execution;- \(\cdots\)- it visits Square \((X_i + (d_i-1)a, Y_i + (d_i-1)b)\) for the first time during the \(d_i\)-th execution.
However, for \((d_i+1)\)-th execution and later,
- it visits Square \((X_i + d_ia, Y_i + d_ib)\) during the \((d_i+1)\)-th execution, but **this is not his first visit there**. (By the definition of \(d_i\), \((X_i + d_ia, Y_i + d_ib) \in V_1\), so it already visited during the first execution)- it visits Square \((X_i + (d_i+1)a, Y_i + (d_i+1)b)\) during the \((d_i+2)\)-th execution, but **this is not his first visit there**. (\((X_i + (d_i+1)a, Y_i + (d_i+1)b) = ((X_i+d_ia) + a, (Y_i+d_ib) + b)\), so it already visited during the second execution)- it visits Square \((X_i + (d_i+2)a, Y_i + (d_i+2)b)\) during the \((d_i+3)\)-th execution, but **this is not his first visit there**. (\((X_i + (d_i+2)a, Y_i + (d_i+2)b) = ((X_i+d_ia) + 2a, (Y_i+d_ib) + 2b)\), so it already visited during the third execution)- \(\cdots\)
and so on. Therefore, while executing \(S\) for \(K\) times, this \((X_i, Y_i)\) changes an unvisited square to a visited one \(\min(d_i, K)\) times.
Therefore, the answer for this problem is
\(\displaystyle\sum_{i = 1}^N \min(d_i, K)\).
Thus, this problem boils down to finding \(d_i\) for all \(i = 1, 2, \ldots, N\). We will explain how to do it.
If \(a = b = 0\), we can see that \(d_1 = d_2 = \cdots = d_N = 1\). Also, if \(a = 0, b \neq 0\), we can swap the role of \(X\) coordinates and \(Y\) coordinates to assume that \(a \neq 0\), so we hereinafter assume \(a \neq 0\).
First, for each \(i = 1, 2, \ldots, N\), we calculate
\(q_i = \left\lfloor \displaystyle\frac{X_i}{a} \right\rfloor\)
\(s_i = X_i - q_i a\)
\(t_i = Y_i - q_i b\),
and group the elements of \(V_i\) depending on \((s_i, t_i)\). ( We define that \((X_i, Y_i)\) and \((X_j, Y_j)\) belongs to the same group if and only if \((s_i, t_i) = (s_j, t_j)\).)
For any \(1 \leq i, j \leq N\),
\((s_i, t_i) = (s_j, t_j) \Leftrightarrow (X_j, Y_j) = (X_i + (q_j-q_i)a, Y_i + (q_j-q_i)b)\),
so by sorting the elements of each group by \(q_i\), we can “find the value of \(d_i\) for each \(i = 1, 2, \ldots, N\) in a total of \(\mathrm{O}(|S|\log|S|)\) time.”
Therefore, we can solve this problem in a total of \(\mathrm{O}(|S|\log|S|)\) time.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 99, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc219_f", "contest": "abc219", "type": "ABC", "letter": "F", "title": "Cleaning Robot", "url": "https://atcoder.jp/contests/abc219/tasks/abc219_f", "difficulty": 2542, "score": 500, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 99, "editorial_url": "https://atcoder.jp/contests/abc219/editorial/2665", "code_source": "model", "pid": "A1706"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// abc219_f: F - Cleaning Robot// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc219/editorial/2665// Judged AC on all 99 tests, max 0.04 s of the 2 s limit (submission 1).
#include <bits/stdc++.h>using namespace std;
using ll = long long;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
string S; ll K; cin >> S >> K;
vector<pair<ll, ll>> points; points.reserve(S.size() + 1); ll x = 0, y = 0; points.emplace_back(x, y); for (char c : S) { if (c == 'L') --x; if (c == 'R') ++x; if (c == 'U') --y; if (c == 'D') ++y; points.emplace_back(x, y); } ll a = x, b = y;
if (a == 0 && b == 0) { sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); cout << points.size() << '\n'; return 0; }
if (a == 0) { swap(a, b); for (auto &p : points) swap(p.first, p.second); } if (a < 0) { a = -a; b = -b; for (auto &p : points) { p.first = -p.first; p.second = -p.second; } }
vector<array<ll, 3>> normalized; normalized.reserve(points.size()); for (auto [px, py] : points) { ll q = px / a; if (px % a < 0) --q; ll s = px - q * a; ll t = py - q * b; normalized.push_back({s, t, q}); } sort(normalized.begin(), normalized.end()); normalized.erase(unique(normalized.begin(), normalized.end()), normalized.end());
ll answer = 0; for (size_t i = 0; i < normalized.size(); ++i) { if (i + 1 < normalized.size() && normalized[i][0] == normalized[i + 1][0] && normalized[i][1] == normalized[i + 1][1]) { answer += min(K, normalized[i + 1][2] - normalized[i][2]); } else { answer += K; } } cout << answer << '\n'; return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081# F - Cleaning Robot
**Score:** 500 points
## Problem Statement
There is a cleaning robot on the square (0, 0) in an infinite two-dimensional grid.
The robot will be given a program represented as a string consisting of four kind of characters `L`, `R`, `U`, `D`. It will read the characters in the program from left to right and perform the following action for each character read.
1. Let (x, y) be the square where the robot is currently on.2. Make the following move according to the character read: - if `L` is read: go to (x-1, y). - if `R` is read: go to (x+1, y). - if `U` is read: go to (x, y-1). - if `D` is read: go to (x, y+1).
You are given a string `S` consisting of `L`, `R`, `U`, `D`. The program that will be executed by the robot is the concatenation of `K` copies of `S`.
Squares visited by the robot at least once, including the initial position (0, 0), will be cleaned. Print the number of squares that will be cleaned at the end of the execution of the program.
### Constraints- `S` is a string of length between `1` and `2 × 10^5` (inclusive) consisting of `L`, `R`, `U`, `D`.- `1 ≤ K ≤ 10^{12}`
---
### InputInput is given from Standard Input in the following format:```SK```
### OutputPrint the number of squares that will be cleaned at the end of the execution of the program.
---
### Sample Input 1```RDRUL2```
### Sample Output 1```7```The robot will execute the program `RDRULRDRUL`. It will start on (0, 0) and travel as follows:(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 0) → (1, 0) → (2, 0) → (2, 1) → (3, 1) → (3, 0) → (2, 0).In the end, seven squares will get cleaned: (0, 0), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1).
---
### Sample Input 2```LR1000000000000```
### Sample Output 2```2```
---
### Sample Input 3```UUURRDDDRRRUUUURDLLUURRRDDDDDDLLLLLLU31415926535```
### Sample Output 3```219911485785```12345678910111213141516171819202122232425262728293031# G - Longest Y (editorial)
Source: https://atcoder.jp/contests/abc229/editorial/2987 by en_translator
Let \(A\) be the sequence of indices of `Y`’s in \(S\) in the increasing order. (For Sample \(1\), \(A=\{1,2,6,8,10\}\).) Let us define sequence \(B\) by \(B_i = A_i -i\).
Then, the problem can be reworded to as follows.
You may \(+1\) or \(-1\) an element of \(B\) for \(K\) times. After these operation ended, how many same integers can be there at most? \(\cdots(1)\)
Since the answer has monotonicity, we will use binary search. Consider the following predicate.
You may \(+1\) or \(-1\) an element of \(B\) for \(K\) times. After these operation ended, is it possible to obtain \(k\) same integers? \(\cdots(2)\)
In order to solve \((2)\), we feel like considering the following problem.
How many times in total do we need to \(+1\) or \(-1\) in order to make all \(B_i,B_{i+1},\ldots,B_{i+m-1}\) the same integer?
In other words, what we want to know is:
Find the minimum value of the following function. \(\cdots(3)\) \(\displaystyle f(x) = \sum_{k=i}^{i+m-1} |B_k - x|\)
\((3)\) is a famous problem; \(f(x)\) is minimum at \(x = B_{i+\lfloor \frac{m}{2} \rfloor}\). (If you are not sure why, draw a graph of \(f(x)\).)
Therefore, if we precalculate the cumulative sums, \((3)\) can be found in an \(O(1)\) time. Hence, denoting \(N=|S|\), \((2)\) can be determined in an \(O(N)\) time, and \((1)\) in a total of \((O \log N)\) time, so the problem has been solved.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 60, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc229_g", "contest": "abc229", "type": "ABC", "letter": "G", "title": "Longest Y", "url": "https://atcoder.jp/contests/abc229/tasks/abc229_g", "difficulty": 2121, "score": 600, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 60, "editorial_url": "https://atcoder.jp/contests/abc229/editorial/2987", "code_source": "model", "pid": "A1787"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// abc229_g: G - Longest Y// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc229/editorial/2987// Judged AC on all 60 tests, max 0.01 s of the 2 s limit (submission 1).
#include <bits/stdc++.h>using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
string S; long long K; cin >> S >> K;
vector<long long> B; for (int i = 0; i < (int)S.size(); ++i) { if (S[i] == 'Y') { B.push_back((long long)i - (long long)B.size()); } }
int n = (int)B.size(); vector<long long> prefix(n + 1, 0); for (int i = 0; i < n; ++i) { prefix[i + 1] = prefix[i] + B[i]; }
auto feasible = [&](int len) -> bool { if (len == 0) return true; for (int l = 0; l + len <= n; ++l) { int r = l + len; int m = l + len / 2; long long x = B[m]; long long cost = x * (m - l) - (prefix[m] - prefix[l]); cost += (prefix[r] - prefix[m + 1]) - x * (r - m - 1); if (cost <= K) return true; } return false; };
int low = 0, high = n + 1; while (high - low > 1) { int mid = low + (high - low) / 2; if (feasible(mid)) low = mid; else high = mid; }
cout << low << '\n'; return 0;}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# G - Longest Y
**Score:** 600 points
## Problem Statement
Given is a string \( S \) consisting of `Y` and `.`.
You can do the following operation on \( S \) between \( 0 \) and \( K \) times (inclusive).- Swap two adjacent characters in \( S \).
What is the maximum possible number of consecutive `Y`s in \( S \) after the operations?
## Constraints- \( 2 \leq |S| \leq 2 \times 10^5 \)- Each character of \( S \) is `Y` or `.`.- \( 0 \leq K \leq 10^{12} \)- \( K \) is an integer.
---
## InputInput is given from Standard Input in the following format:```SK```
## OutputPrint the answer.
---
## Sample Input 1```YY...Y.Y.Y.2```
## Sample Output 1```3```After swapping the \( 6 \)-th, \( 7 \)-th characters, and \( 9 \)-th, \( 10 \)-th characters, we have `YY....YYY..`, which has three consecutive `Y`s at \( 7 \)-th through \( 9 \)-th positions. We cannot have four or more consecutive `Y`s, so the answer is \( 3 \).
---
## Sample Input 2```YYYY....YYY3```
## Sample Output 2```4```12345678910111213141516171819202122232425262728293031323334353637383940414243444546# F - Sum Sum Max (editorial)
Source: https://atcoder.jp/contests/abc240/editorial/3445 by en_translator
Let \(\displaystyle z_i = \sum_{k = 1}^i y_k\), where \(z_0 = 0\).
For \(z_{i - 1} \lt k \leq z_i\), it always holds that \(C_k = x_i\). We will use this property to find the maximum value of \(A_{z_{i - 1} + 1}, \dots, A_{z_{i}}\). For convenience, let \(A_0 = B_0 = 0\). Then, for \(1 \leq n \leq z_i - z_{i - 1}\),
\[ \begin{aligned} B_{z_{i - 1} + n} &= B_{z_{i - 1}} + \sum_{k = 1}^n C_{z_{i - 1} + k} \\ &= B_{z_{i - 1}} + \sum_{k = 1}^n x_i \\ &= B_{z_{i - 1}} + x_i n, \end{aligned} \]
\[ \begin{aligned} A_{z_{i - 1} + n} &= A_{z_{i - 1}} + \sum_{k = 1}^n B_{z_{i - 1} + k} \\ &= A_{z_{i - 1}} + \sum_{k = 1}^n (B_{z_{i - 1}} + x_i k) \\ &= A_{z_{i - 1}} + B_{z_{i - 1}}n + x_i \times \frac{n(n + 1)}{2}. \end{aligned} \]
therefore, we can express as \(A_{z_{i - 1} + n} = an^2 + bn + c\). If we denote the left hand side by \(f(n)\), the problem is boiled down to the problem of finding the maximum value of \(f(1), \dots, f(z_i - z_{i - 1})\).
#### [1] If \(a \geq 0\)
Since \(f(n + 1) - f(n) = 2an + a + b\) is monotonically increasing on \(n\), there exists an integer \(1 \leq m \leq z_i - z_{i - 1}\) such that
- \(f(1) \geq \dots \geq f(m) \),- \(f(m) \lt \dots \lt f(z_i - z_{i - 1})\).
Therefore, either \(f(1), f(z_i - z_{i - 1})\) is the maximum value.
#### [2] If \(a \lt 0\)
Since \(f(n + 1) - f(n) = 2an + a + b\) is monotonically decreasing on \(n\), there exists an integer \(1 \leq m \leq z_i - z_{i - 1}\) such that
- \(f(1) \leq \dots \leq f(m) \),- \(f(m) \gt \dots \gt f(z_i - z_{i - 1})\).
\(f(m)\) is the maximum value.
We describe how to find \(m\) fast using **ternary search**. Let \(g(n) = f(n + 1) - f(n)\), then \(f(m_2) - f(m_1) = g(m_1) + \dots + g(m_2 - 1)\), so
- if \(f(m_1) \leq f(m_2)\), then \(g(m_1) + \dots + g(m_2 - 1) \geq 0\), and as \(g(n)\) is monotonically decreasing, we have \(g(m_1) \geq 0\). Therefore, \(f(m_1) \leq f(m_1 + 1)\), hence \(m_1 \lt m\).- if \(f(m_1) \gt f(m_2)\), then \(g(m_1) + \dots + g(m_2 - 1) \lt 0\), and as \(g(n)\) is monotonically decreasing, we have \(g(m_2 - 1) \lt 0\). Therefore, \(f(m_2 - 1) \gt f(m_2)\), hence \(m \lt m_2\).
In either case, \(l < m < r\) is preserved. By choosing \(\displaystyle m_1 = \left\lfloor \frac{l + r}{2} \right\rfloor, m_2 = \left\lfloor \frac{l + r}{2} \right\rfloor + 1\), the value \(r - l\) is halved in each step, so \(m\) can be found in a total complexity of \(O(\log(z_{i} - z_{i - 1}))\).
Therefore, by finding the maximum value of \(A_{z_{i - 1} + 1}, \dots, A_{z_{i}}\) in the order of \(i = 1, \dots, N\), the problem has been solved in a total of \(O(N \log M)\) time. Here, the values of \(A_{z_{i - 1}}\) and \(B_{z_{i - 1}}\) are required, which can be computed from the equations\(A_{z_{i - 1} + n}\) and \(B_{z_{i - 1} + n}\) in a constant time.
### Postscript
For functions \(f : \mathbb{Z} \mapsto \mathbb{R}\) and \(g(n) = f(n + 1) - f(n)\), if \(g(n)\) is monotonically increasing, then \(f(n)\) is said to be **concave**; if \(g(n9\) is monotonically decreasing, then \(f(n)\) is said to be **convex**. In order to find the extremum of concave/convex function, techniques like **ternary search** and **golden-selection search** are frequently used.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 12, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc240_f", "contest": "abc240", "type": "ABC", "letter": "F", "title": "Sum Sum Max", "url": "https://atcoder.jp/contests/abc240/tasks/abc240_f", "difficulty": 1589, "score": 500, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 12, "editorial_url": "https://atcoder.jp/contests/abc240/editorial/3445", "code_source": "model", "pid": "A1875"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// abc240_f: F - Sum Sum Max// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc240/editorial/3445// Judged AC on all 12 tests, max 0.08 s of the 2 s limit (submission 1).
#include <bits/stdc++.h>using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T; while (T--) { int N; long long M; cin >> N >> M;
long long A = 0, B = 0; long long answer = LLONG_MIN; for (int i = 0; i < N; ++i) { long long x, y; cin >> x >> y;
auto value = [&](long long k) -> long long { return A + B * k + x * k * (k + 1) / 2; };
long long endA = value(y); if (x >= 0) { answer = max(answer, max(value(1), endA)); } else { long long left = 1, right = y; while (left < right) { long long mid = left + (right - left) / 2; if (value(mid) <= value(mid + 1)) { left = mid + 1; } else { right = mid; } } answer = max(answer, value(left)); }
A = endA; B += x * y; } cout << answer << '\n'; } return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475# F - Sum Sum Max
**Score**: 500 points
## Problem StatementThere are integer sequences \(A, B, C\) of length \(M\) each. \(C\) is represented by integers \(x_1, \dots, x_N, y_1, \dots, y_N\). The first \(y_1\) terms of \(C\) are \(x_1\), the subsequent \(y_2\) terms are \(x_2\), \( \ldots\), the last \(y_N\) terms are \(x_N\). \(B\) is defined by \(B_i = \sum_{k = 1}^i C_k \, (1 \leq i \leq M)\). \(A\) is defined by \(A_i = \sum_{k = 1}^i B_k \, (1 \leq i \leq M)\). Find the maximum value among \(A_1, \dots, A_M\). You will be given \(T\) test cases to solve.
## Constraints- \(1 \leq T \leq 2 \times 10^5\)- \(1 \leq N \leq 2 \times 10^5\)- The sum of \(N\) in a single file is at most \(2 \times 10^5\).- \(1 \leq M \leq 10^9\)- \(|x_i| \leq 4 \, (1 \leq i \leq N)\)- \(y_i > 0 \, (1 \leq i \leq N)\)- \(\sum_{k = 1}^N y_k = M\)- All values in input are integers.
## InputInput is given from Standard Input in the following format:```Tcase_1...case_T```Each case is in the following format:```N Mx_1 y_1...x_N y_N```
## OutputPrint \(T\) lines. The \(i\)-th line \((1 \leq i \leq T)\) should contain the answer to the \(i\)-th test case.
## Sample Input 1```33 7-1 22 3-3 210 472-4 121 292 77-1 860 513 813 17-2 31-4 654 231 10000000004 1000000000```
## Sample Output 1```4539102000000002000000000```In the first test case, we have:- \(C = (-1, -1, 2, 2, 2, -3, -3)\)- \(B = (-1, -2, 0, 2, 4, 1, -2)\)- \(A = (-1, -3, -3, -1, 3, 4, 2)\)
Thus, the maximum value among \(A_1, \dots, A_M\) is \(4\).12345678910111213141516171819202122232425262728293031323334353637383940414243444546# D - ABC Transform (editorial)
Source: https://atcoder.jp/contests/abc242/editorial/3528 by en_translator
### Preface
In this editorial, we use \(0\)-based index, instead of \(1\)-based index, for \(S\). In other words, when the length of \(N\) is \(S\), \(S = S_1 S_2 \ldots S_N\), not \(S = S_0 S_1 \ldots S_{N-1}\).
### Body
Let \(f(t,k)\) be “the function that returns the \(k\)-th character of \(S^{(t)}\).”
First, for \(t = 0\), \(f(t,k)\) is the \(k\)-th character of \(S\).
Next, we consider the case where \(k = 0\). Taking a closer look, we can see that the \(0\)-th characters of \(S^{(0)}, S^{(1)}, S^{(2)}, S^{(3)}, S^{(4)} \ldots\) are either
- `A`, `B`, `C`, `A`, `B`, \(\ldots\)- `B`, `C`, `A`, `B`, `C`, \(\ldots\)- `C`, `A`, `B`, `C`, `A`, \(\ldots\)
which are periodic.
If the successor of `A` is defined to be `B`, `B` is `C`, and `C` is `A`, then the \(0\)-th character of \(S^{(t)}\) is the \(t\)-th successor of the first character of \(S^{(0)}\).
Therefore, when \(k = 0\), \(f(t,k)\) is the \(k\)-th successor of \(S_0\).
Finally, let’s consider what \(f(t,k)\) returns when \(0 \lt t,k\).
Taking a closer look again, we can see that
- when \(k\) is even, \(f(t,k)\) is the first successor of \(f(t-1,\frac{k}{2})\);- when \(k\) is odd, \(f(t,k)\) is the second successor of \(f(t-1,\frac{k}{2})\).
Thus, for \(0 \lt t,k\), we can recursively obtain \(f(t,k)\) based on the value of \(f(t-1,\lfloor \frac{k}{2} \rfloor)\). Here, \(\lfloor \frac{k}{2} \rfloor\) is \(\frac{k}{2}\) rounded down.
Therefore, \(f(t, k)\) is
- the \(k\)-th character of \(S\) if \(t=0\);- the \(k\)-th successor of \(S_0\) if \(k=0\);- the (remainder of \(k\) by \(2\), plus \(1\))-th successor of \(f(t-1,\lfloor \frac{k}{2} \rfloor)\) if \(t=0\).
We can code the procedure as follows.
The time complexity is \(O(N + Q \log (\max(k)))\), because for each query the function \(f\) is called at most \(O(\log k)\) times.
This is very fast, even in PyPy3 where recursive function is generally said to be slow:123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 24, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc242_d", "contest": "abc242", "type": "ABC", "letter": "D", "title": "ABC Transform", "url": "https://atcoder.jp/contests/abc242/tasks/abc242_d", "difficulty": 1286, "score": 400, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 24, "editorial_url": "https://atcoder.jp/contests/abc242/editorial/3528", "code_source": "editorial", "pid": "A1888"}123456789101112131415161718192021222324252627282930// abc242_d: D - ABC Transform// Code from the official editorial, https://atcoder.jp/contests/abc242/editorial/3528 (by en_translator)// Judged AC on all 24 tests, max 0.24 s of the 2 s limit.
#include<bits/stdc++.h>using namespace std;
using ll = long long;
int main(){ string S; cin >> S; int Q; cin >> Q;
auto g = [&](char s, ll add){ return char('A'+(s-'A'+add)%3); };
std::function<char(ll,ll)> f = [&](ll t, ll k){ if(t == 0) return S[k]; if(k == 0) return g(S[0],t); return g(f(t-1,k/2),k%2+1); };
while(Q--){ ll t,k; cin >> t >> k; cout << f(t,k-1) << endl; }
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071# D - ABC Transform
**Score:** 400 points
## Problem Statement
You are given a string `S` consisting of `A`, `B`, `C`.Let \( S^{(0)}:=S \). For \( i=1,2,3,\ldots \), let \( S^{(i)} \) be the result of simultaneously replacing the characters of \( S^{(i-1)} \) as follows: `A` → `BC`, `B` → `CA`, `C` → `AB`.Answer `Q` queries. The `i`-th query is as follows:- Print the `k_i`-th character from the beginning of \( S^{(t_i)} \).
## Constraints- `S` is a string of length between \( 1 \) and \( 10^5 \) (inclusive) consisting of `A`, `B`, `C`.- \( 1 \leq Q \leq 10^5 \)- \( 0 \leq t_i \leq 10^{18} \)- \( 1 \leq k_i \leq \min(10^{18}, \text{ the length of } S^{(t_i)}) \)- `Q`, `t_i`, `k_i` are integers.
## InputInput is given from Standard Input in the following format:```SQt_1 k_1t_2 k_2...t_Q k_Q```
## OutputProcess the `Q` queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.
## Sample Input 1```ABC40 11 11 31 6```
## Sample Output 1```ABCB```We have \( S^{(0)}=\text{ABC} \), \( S^{(1)}=\text{BCCAAB} \).Thus, the answers to the queries are `A`, `B`, `C`, `B` in the given order.
## Sample Input 2```CBBAACCCCC557530144230160008 65927916484781484729622990657296329 861239705300265164509705228051901259 994708708957785197176678501072691541 655134104344481648827291290937314275 407121144297426665```
## Sample Output 2```AACAA```123456789101112131415# F - Cumulative Cumulative Cumulative Sum (editorial)
Source: https://atcoder.jp/contests/abc256/editorial/4143 by en_translator
First, let us consider how many times \(A_i (i\leq x)\) contributes to \(D_x\).
For each \(x\), \(A_i\) contributes to \(B_x\) once. For each \(x\), \(A_i\) contributes to \(C_x\) \((x-i+1)\) times. For each \(x\), \(A_i\) contributes to \(D_x\) \(\left( (x-i+1)(x-i+2)/2 \right)\) times.
Therefore, we have \(\displaystyle D_x=\sum_{i=1}^{x}\frac{(x-i+1)(x-i+2)}{2}A_i\), which can be transformed into
\(\displaystyle D_x=\frac{1}{2}\sum_{i=1}^{x}i^2A_i - \frac{2x+3}{2}\sum_{i=1}^{x}iA_i+\frac{(x+1)(x+2)}{2}\sum_{i=1}^{x}A_i.\)
With a data structure that enables updating \(i^2A_i,iA_i\), and \(A_i\), and obtaining the prefix sum, like a Fenwick tree or a segment tree, each query can be processed in an \(O(\log N)\) time.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 22, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc256_f", "contest": "abc256", "type": "ABC", "letter": "F", "title": "Cumulative Cumulative Cumulative Sum", "url": "https://atcoder.jp/contests/abc256/tasks/abc256_f", "difficulty": 2067, "score": 500, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 22, "editorial_url": "https://atcoder.jp/contests/abc256/editorial/4143", "code_source": "model", "pid": "A2003"}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// abc256_f: F - Cumulative Cumulative Cumulative Sum// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc256/editorial/4143// Judged AC on all 22 tests, max 0.09 s of the 2 s limit (submission 1).
#include <bits/stdc++.h>#include <atcoder/fenwicktree>#include <atcoder/modint>using namespace std;using mint = atcoder::modint998244353;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int N, Q; cin >> N >> Q; vector<mint> A(N + 1); atcoder::fenwick_tree<mint> f0(N), f1(N), f2(N); for (int i = 1; i <= N; ++i) { int v; cin >> v; A[i] = v; mint index = i; f0.add(i - 1, A[i]); f1.add(i - 1, A[i] * index); f2.add(i - 1, A[i] * index * index); }
const mint inv2 = mint(2).inv(); while (Q--) { int type, x; cin >> type >> x; if (type == 1) { int v; cin >> v; mint delta = mint(v) - A[x]; A[x] = v; mint index = x; f0.add(x - 1, delta); f1.add(x - 1, delta * index); f2.add(x - 1, delta * index * index); } else { mint s0 = f0.sum(0, x); mint s1 = f1.sum(0, x); mint s2 = f2.sum(0, x); mint answer = (s2 - mint(2 * x + 3) * s1 + mint(x + 1) * mint(x + 2) * s0) * inv2; cout << answer.val() << '\n'; } } return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172# F - Cumulative Cumulative Cumulative Sum
**Score**: 500 points
## Problem Statement
You are given \( N \), \( Q \), and \( A=(A_1,\ldots,A_N) \). Process \( Q \) queries, each of which is one of the following two kinds:
- `1 x v`: update \( A_x \) to \( v \).- `2 x`: let \( B_i=\sum_{j=1}^{i}A_j \), \( C_i=\sum_{j=1}^{i}B_j \), and \( D_i=\sum_{j=1}^{i}C_j \). Print \( D_x \) modulo \( 998244353 \).
## Constraints
- \( 1 \leq N \leq 2 \times 10^5 \)- \( 1 \leq Q \leq 2 \times 10^5 \)- \( 0 \leq A_i \leq 10^9 \)- \( 1 \leq x \leq N \)- \( 0 \leq v \leq 10^9 \)- All values in input are integers.
## Input
Input is given from Standard Input in the following format, where \( {\rm query}_i \) denotes the \( i \)-th query to be processed:```N QA_1 A_2 ... A_N{\rm query}_1{\rm query}_2...{\rm query}_Q```Each query is given in one of the following two formats:```1 x v``````2 x```
## Output
Print the answer to the queries, with newlines in between.
## Sample Input 1```3 31 2 32 31 2 02 3```
## Sample Output 1```159```When the 1-st query is given, \( A=(1,2,3) \), so \( B=(1,3,6) \), \( C=(1,4,10) \), and \( D=(1,5,15) \); thus, \( D_3=15 \). When the 3-rd query is given, \( A=(1,0,3) \), so \( B=(1,1,4) \), \( C=(1,2,6) \), and \( D=(1,3,9) \); thus, \( D_3=9 \).
## Sample Input 2```2 1998244353 9982443532 1```
## Sample Output 2```0```1234567891011121314151617181920212223242526272829303132333435363738394041# F - Numbered Checker (editorial)
Source: https://atcoder.jp/contests/abc269/editorial/4901 by en_translator
There are a lot of approaches to this problem, but the amount of implementation also varies. Let us consider a simple approach. Note the \(6\)-th query in Sample Input \(1\). Consider each row in the grid. The grid looks like as follows:
``` 1 0 3 0 0 6 0 8 9 0 11 0 0 14 0 16 17 0 19 0```
First, we compute the sum for each row. Since the non-zero element for each row forms an arithmetic sequence with common difference \(2\), so we can compute it using the formula of the sum of an arithmetic sequence. Also, since the grid forms a checker pattern, the leftmost non-zero element is either the \(1\)-st or \(2\)-nd element of the extracted row (if exists). (Same applies for the rightmost one.)
Next, let us consider what we should do after grouping rows. First, see the odd-indexed rows:
``` 1 0 3 0 9 0 11 0 17 0 19 0```
For the odd-indexed rows, the following holds: Let \(x\) be the number of non-zero elements in each row, then the sum is an arithmetic sequence with common difference \(2 \times x \times M\). (This also holds for \(x=0\).) This is because, for a sequence with a non-zero element, the values in each column forms an arithmetic sequence with common difference \(2 \times M\).
It is basically the same for the even-indexed rows:
``` 0 6 0 8 0 14 0 16```
Therefore, we can first compute the information for the \(A_i\)-th and \((A_i+1)\)-th row and from the \(C_i\)-th through \(D_i\)-th columns, and then find the sum from \(A_i\)-th through \(B_i\)-th rows.
One key for the implementation is that you need to find the sum of an arithmetic sequence many times, so such a repeated operation can be extracted to a function in order to reduce the implementation.123456789101112131415{ "time_limit_s": 3.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 43, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc269_f", "contest": "abc269", "type": "ABC", "letter": "F", "title": "Numbered Checker", "url": "https://atcoder.jp/contests/abc269/tasks/abc269_f", "difficulty": 1601, "score": 500, "time_limit_sec": 3.0, "memory_limit_mb": 1024, "n_tests": 43, "editorial_url": "https://atcoder.jp/contests/abc269/editorial/4901", "code_source": "editorial", "pid": "A2107"}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556// abc269_f: F - Numbered Checker// Code from the official editorial, https://atcoder.jp/contests/abc269/editorial/4901 (by en_translator)// Judged AC on all 43 tests, max 0.48 s of the 3 s limit.
#include<bits/stdc++.h>#define mod 998244353#define inv2 499122177 // inverse of 2
using namespace std;
// A_1 = fir// A_i = fir + (i-1) * d// return A_1 + A_2 + ... + A_{num}long long arithmetic_sum(long long fir,long long d,long long num){ long long las=(fir+(num-1)*d)%mod; long long res=(fir+las)%mod; res*=num;res%=mod; res*=inv2;res%=mod; return res;}
using pl=pair<long long,long long>;
long long n,m;pl row_data(long long x,long long l,long long r){ if((x+l)%2){l++;} if((x+r)%2){r--;} if(l>r){return {0,0};}
long long mi=((x-1)*m+l)%mod; long long num=1+(r-l)/2; long long sum=arithmetic_sum(mi,2,num);
num*=2;num%=mod; num*=m;num%=mod; return {sum,num};}
int main(){ int q; cin >> n >> m >> q; for(int i=0;i<q;i++){ long long a,b,c,d; cin >> a >> b >> c >> d;
pl r1=row_data(a,c,d); pl r2=row_data(a+1,c,d); long long c1=(b-a+1)/2 + (b-a+1)%2; long long c2=(b-a+1)/2; long long res=0; res+=arithmetic_sum(r1.first,r1.second,c1);res%=mod; res+=arithmetic_sum(r2.first,r2.second,c2);res%=mod; cout << res << "\n"; } return 0;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# F - Numbered Checker
**Score**: 500 points
## Problem StatementWe have a grid with `N` rows and `M` columns. The square `(i,j)` at the `i`-th row from the top and `j`-th column from the left has an integer `(i-1) × M + j` written on it. Let us perform the following operation on this grid.
- For every square `(i,j)` such that `i+j` is odd, replace the integer on that square with `0`.
Answer `Q` questions on the grid after the operation. The `i`-th question is as follows:
- Find the sum of the integers written on all squares `(p,q)` that satisfy all of the following conditions, modulo `998244353`. - `A_i ≤ p ≤ B_i`. - `C_i ≤ q ≤ D_i`.
## Constraints- All values in the input are integers.- `1 ≤ N,M ≤ 10^9`- `1 ≤ Q ≤ 2 × 10^5`- `1 ≤ A_i ≤ B_i ≤ N`- `1 ≤ C_i ≤ D_i ≤ M`
---
## InputThe input is given from Standard Input in the following format:```N MQA_1 B_1 C_1 D_1A_2 B_2 C_2 D_2...A_Q B_Q C_Q D_Q```
## OutputPrint `Q` lines. The `i`-th line should contain the answer to the `i`-th question as an integer.
---
## Sample Input 1```5 461 3 2 41 5 1 15 5 1 44 4 2 25 5 4 41 5 1 4```
## Sample Output 1```282736140104```The grid in this input is shown below.  This input contains six questions.- The answer to the first question is `0+3+0+6+0+8+0+11+0=28`.- The answer to the second question is `1+0+9+0+17=27`.- The answer to the third question is `17+0+19+0=36`.- The answer to the fourth question is `14`.- The answer to the fifth question is `0`.- The answer to the sixth question is `104`.
---
## Sample Input 2```1000000000 100000000031000000000 1000000000 1000000000 1000000000165997482 306594988 719483261 9923061471 1000000000 1 1000000000```
## Sample Output 2```716070898240994972536839100```For the first question, note that although the integer written on the square `(10^9,10^9)` is `10^{18}`, you are to find it modulo `998244353`.
---
## Sample Input 3```999999999 9999999993999999999 999999999 999999999 999999999216499784 840031647 84657913 4154487901 999999999 1 999999999```
## Sample Output 3```712559605648737448540261130```123456789101112131415# E - Add and Mex (editorial)
Source: https://atcoder.jp/contests/abc272/editorial/4990 by en_translator
First, let us consider, given a sequence \(A\) of length \(N\), a simple problem of finding the minimum non-negative integer not contained in \(A\).
Then, the answer is obviously between \(0\) and \(N\), so we can safely ignore the negative elements and the elements greater than \(N\) in \(A\), as it does not affect the answer.
Based on this observation, we can solve the original problem too. Let us call a value **important** if we cannot ignore it. How many important values are there?
There are at most \(N\) values for \(i=1\), at most \(\lceil\frac{N}{2}\rceil\) values for \(i=2\), … so, by the harmonic series, there are \(\mathrm{O}(N\log N)\) important values in total.
Therefore, the sum of the answer is also about \(\mathrm{O}(N\log N)\). Therefore, it is fast enough to find the answer for each output in a linear time of the answer.
Specifically, for each operation, we can check if there is \(0\), there is \(1\), …. See the sample code for how to store the data.123456789101112131415{ "time_limit_s": 2.0, "time_limit_source": "task page", "memory_mb": 1024, "memory_source": "task page", "mode": "exact", "validator_flags": "case_sensitive", "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)", "validator": null, "tests_dir": "data", "answer_suffix": ".out", "n_sample": null, "n_secret": 35, "note": "no sample/secret split: the statement's samples are among the numbered tests"}12345678910111213141516{ "task_id": "abc272_e", "contest": "abc272", "type": "ABC", "letter": "E", "title": "Add and Mex", "url": "https://atcoder.jp/contests/abc272/tasks/abc272_e", "difficulty": 1434, "score": 500, "time_limit_sec": 2.0, "memory_limit_mb": 1024, "n_tests": 35, "editorial_url": "https://atcoder.jp/contests/abc272/editorial/4990", "code_source": "editorial", "pid": "A2129"}1234567891011121314151617181920212223242526272829303132// abc272_e: E - Add and Mex// Code from the official editorial, https://atcoder.jp/contests/abc272/editorial/4990 (by en_translator)// Judged AC on all 35 tests, max 0.36 s of the 2 s limit.
#include <bits/stdc++.h>using namespace std;
int main() { int n, m; cin >> n >> m; vector<int> a(n); for(auto &v : a) cin >> v; vector<vector<int>> vals(m + 1); for(int i = 0; i < n; i++) { if(a[i] >= n) continue; int l = (a[i] >= 0 ? 1 : (-a[i] + i) / (i + 1)); int r = min(m + 1, (n - a[i] + i) / (i + 1)); for(int j = l; j < r; j++) { vals[j].push_back(a[i] + (i + 1) * j); } } for(int i = 1; i <= m; i++) { int sz = vals[i].size(); vector<bool> exi(sz + 1); for(auto v : vals[i]) { if(v < sz) exi[v] = true; } int res = 0; while(exi[res]) res++; cout << res << endl; }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162# E - Add and Mex
**Score:** 500 points
## Problem StatementYou are given an integer sequence \( A=(A_1,A_2,\ldots,A_N) \) of length \( N \). Perform the following operation \( M \) times:- For each \( i\ (1\leq i \leq N) \), add \( i \) to \( A_i \). Then, find the minimum non-negative integer not contained in \( A \).
## Constraints- \( 1\leq N,M \leq 2\times 10^5 \)- \( -10^9\leq A_i\leq 10^9 \)- All values in the input are integers.
## InputThe input is given from Standard Input in the following format:```N M A_1 A_2 \ldots A_N```
## OutputPrint \( M \) lines. The \( i \)-th \( (1\leq i \leq M) \) line should contain the minimum non-negative integer not contained in \( A \) after the \( i \)-th operation.
## Sample Input 1```3 3-1 -1 -6```
## Sample Output 1```220```The 1-st operation makes the sequence \( A \) \((-1 + 1, -1 +2 ,-6+3) = (0,1,-3).\) The minimum non-negative integer not contained in \( A \) is \( 2 \). The 2-nd operation makes the sequence \( A \) \((0 + 1, 1 +2 ,-3+3) = (1,3,0).\) The minimum non-negative integer not contained in \( A \) is \( 2 \). The 3-rd operation makes the sequence \( A \) \((1 + 1, 3 +2 ,0+3) = (2,5,3).\) The minimum non-negative integer not contained in \( A \) is \( 0 \).
## Sample Input 2```5 6-2 -2 -5 -7 -15```
## Sample Output 2```132000```problems/MA-01/Atcoder/abc190_f/editorial.mdAdded+18
problems/MA-01/Atcoder/abc190_f/judge.jsonAdded+15
problems/MA-01/Atcoder/abc190_f/meta.jsonAdded+16
problems/MA-01/Atcoder/abc190_f/solution.cppAdded+31
problems/MA-01/Atcoder/abc190_f/statement.mdAdded+67
problems/MA-01/Atcoder/abc196_e/editorial.mdAdded+32
problems/MA-01/Atcoder/abc196_e/judge.jsonAdded+15
problems/MA-01/Atcoder/abc196_e/meta.jsonAdded+16
problems/MA-01/Atcoder/abc196_e/solution.cppAdded+42
problems/MA-01/Atcoder/abc196_e/statement.mdAdded+77
problems/MA-01/Atcoder/abc219_f/editorial.mdAdded+85
problems/MA-01/Atcoder/abc219_f/judge.jsonAdded+15
problems/MA-01/Atcoder/abc219_f/meta.jsonAdded+16
problems/MA-01/Atcoder/abc219_f/solution.cppAdded+75
problems/MA-01/Atcoder/abc219_f/statement.mdAdded+81
problems/MA-01/Atcoder/abc229_g/editorial.mdAdded+31
problems/MA-01/Atcoder/abc229_g/judge.jsonAdded+15
problems/MA-01/Atcoder/abc229_g/meta.jsonAdded+16
problems/MA-01/Atcoder/abc229_g/solution.cppAdded+51
problems/MA-01/Atcoder/abc229_g/statement.mdAdded+58
problems/MA-01/Atcoder/abc240_f/editorial.mdAdded+46
problems/MA-01/Atcoder/abc240_f/judge.jsonAdded+15
problems/MA-01/Atcoder/abc240_f/meta.jsonAdded+16
problems/MA-01/Atcoder/abc240_f/solution.cppAdded+51
problems/MA-01/Atcoder/abc240_f/statement.mdAdded+75
problems/MA-01/Atcoder/abc242_d/editorial.mdAdded+46
problems/MA-01/Atcoder/abc242_d/judge.jsonAdded+15
problems/MA-01/Atcoder/abc242_d/meta.jsonAdded+16
problems/MA-01/Atcoder/abc242_d/solution.cppAdded+30
problems/MA-01/Atcoder/abc242_d/statement.mdAdded+71
problems/MA-01/Atcoder/abc256_f/editorial.mdAdded+15
problems/MA-01/Atcoder/abc256_f/judge.jsonAdded+15
problems/MA-01/Atcoder/abc256_f/meta.jsonAdded+16
problems/MA-01/Atcoder/abc256_f/solution.cppAdded+52
problems/MA-01/Atcoder/abc256_f/statement.mdAdded+72
problems/MA-01/Atcoder/abc269_f/editorial.mdAdded+41
problems/MA-01/Atcoder/abc269_f/judge.jsonAdded+15
problems/MA-01/Atcoder/abc269_f/meta.jsonAdded+16
problems/MA-01/Atcoder/abc269_f/solution.cppAdded+56
problems/MA-01/Atcoder/abc269_f/statement.mdAdded+110
problems/MA-01/Atcoder/abc272_e/editorial.mdAdded+15
problems/MA-01/Atcoder/abc272_e/judge.jsonAdded+15
problems/MA-01/Atcoder/abc272_e/meta.jsonAdded+16
problems/MA-01/Atcoder/abc272_e/solution.cppAdded+32
problems/MA-01/Atcoder/abc272_e/statement.mdAdded+62
problems/MA-01/Atcoder/abc288_d/data/1.inAdded
problems/MA-01/Atcoder/abc288_d/data/1.outAdded
problems/MA-01/Atcoder/abc288_d/data/10.inAdded
problems/MA-01/Atcoder/abc288_d/data/10.outAdded
problems/MA-01/Atcoder/abc288_d/data/11.inAdded