Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long BIG = 1446803456761533460;
const int Big = 336860180;
stringstream sss;
const int maxn = 100010;
const int SQ = 330;
int n, k;
int A[maxn], P[maxn];
map<int, int> lst;
int block[SQ][maxn];
int lazy[SQ], val[maxn];
int dp[maxn];
int sum = 0;
void inc(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
sum = ((sum) + (MOD - block[l / SQ][k - lazy[l / SQ]])) % MOD;
++lazy[l / SQ];
l += SQ;
} else {
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (MOD - dp[l])) % MOD;
block[l / SQ][val[l]] = ((block[l / SQ][val[l]]) + (MOD - dp[l])) % MOD;
++val[l];
block[l / SQ][val[l]] = ((block[l / SQ][val[l]]) + (dp[l])) % MOD;
++l;
}
}
}
void dec(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
--lazy[l / SQ];
sum = ((sum) + (block[l / SQ][k - lazy[l / SQ]])) % MOD;
l += SQ;
} else {
block[l / SQ][val[l]] = ((block[l / SQ][val[l]]) + (MOD - dp[l])) % MOD;
--val[l];
block[l / SQ][val[l]] = ((block[l / SQ][val[l]]) + (dp[l])) % MOD;
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (dp[l])) % MOD;
++l;
}
}
}
void MAIN() {
cin >> n >> k;
for (int i = (0); i < (n); ++i) {
cin >> A[i];
P[i + 1] = lst.count(A[i]) ? lst[A[i]] : 0;
lst[A[i]] = i + 1;
}
dp[0] = 1;
sum = 1;
block[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
inc(P[i], i);
dec(P[P[i]], P[i]);
dp[i] = sum;
sum = ((sum) + (dp[i])) % MOD;
val[i] = -lazy[i / SQ];
block[i / SQ][val[i]] = ((block[i / SQ][val[i]]) + (dp[i])) % MOD;
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
sss << R"(
5 2
1 1 2 1 3
)";
MAIN();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
const int N = 100005;
const int BLK = 405;
const int K = 255;
int L[K], R[K], tg[K];
int top[K], pos[K];
int id[N], f[N], s[N];
int v[N], V[N], LIM;
int a[N], la[N], pre[N], n;
void pushdown(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) s[i] += tg[k];
tg[k] = 0;
}
void pushup(int k) {
top[k] = L[k];
v[top[k]] = f[id[L[k]]];
V[top[k]] = s[id[L[k]]];
for (int i = (int)(L[k] + 1); i <= (int)(R[k]); i++) {
if (s[id[i]] != s[id[i - 1]]) {
V[++top[k]] = s[id[i]];
v[top[k]] = v[top[k] - 1];
}
v[top[k]] = (v[top[k]] + f[id[i]]) % mo;
}
pos[k] = L[k] - 1;
for (; pos[k] != top[k]; ++pos[k])
if (V[pos[k] + 1] > LIM) break;
}
bool cmp(int x, int y) { return s[x] < s[y]; }
void build(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) id[i] = i;
sort(id + L[k], id + R[k] + 1, cmp);
pushup(k);
}
int q1[BLK + 5], q2[BLK + 5];
void add(int k, int l, int r, int v) {
pushdown(k);
int p1 = 0, p2 = 0, p3 = L[k] - 1;
for (int i = (int)(L[k]); i <= (int)(R[k]); i++)
if (l <= id[i] && id[i] <= r)
q1[++p1] = id[i], s[id[i]] += v;
else
q2[++p2] = id[i];
int pp1 = 1, pp2 = 1;
for (; pp1 <= p1 || pp2 <= p2;)
if (pp2 > p2 || (pp1 <= p1 && s[q1[pp1]] < s[q2[pp2]]))
id[++p3] = q1[pp1++];
else
id[++p3] = q2[pp2++];
pushup(k);
}
void add(int k, int val) {
tg[k] += val;
for (; pos[k] != top[k] && V[pos[k] + 1] + tg[k] <= LIM; ++pos[k])
;
for (; pos[k] != L[k] - 1 && V[pos[k]] + tg[k] > LIM; --pos[k])
;
}
int query(int k) { return pos[k] == L[k] - 1 ? 0 : v[pos[k]]; }
int ed;
void change(int l, int r, int v) {
for (; r >= l && r >= ed; r--) s[r] += v;
if (r < l) return;
int bl = l / BLK, br = r / BLK;
if (bl == br)
add(bl, l, r, v);
else {
for (int i = (int)(bl + 1); i <= (int)(br - 1); i++) add(i, v);
add(bl, l, R[bl], v);
add(br, L[br], r, v);
}
}
void update(int x) {
change(pre[x], x - 1, 1);
if (pre[x]) change(pre[pre[x]], pre[x] - 1, -1);
}
int main() {
scanf("%d%d", &n, &LIM);
for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
for (int i = (int)(1); i <= (int)(n); i++) la[a[i]] = 0;
for (int i = (int)(1); i <= (int)(n); i++) pre[i] = la[a[i]], la[a[i]] = i;
for (int i = (int)(0); i <= (int)(n / BLK); i++)
L[i] = i * BLK, R[i] = min(i * BLK + BLK - 1, n);
f[0] = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
update(i);
int be = i / BLK;
for (int j = (int)(0); j <= (int)(be - 1); j++)
f[i] = (f[i] + query(j)) % mo;
for (int j = (int)(be * BLK); j <= (int)(i - 1); j++)
if (s[j] <= LIM) f[i] = (f[i] + f[j]) % mo;
if (i % BLK == BLK - 1 || i == n) build(i / BLK), ed = i;
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int n, k;
int a[100055];
int dp[100055];
int l[100055];
int r[100055];
int last[100055];
int dat[400055];
int high[400055];
int low[400055];
int lazy[400055];
vector<int> out[100055];
int d[100055];
void push(int from, int to) {
high[to] += lazy[from];
low[to] += lazy[from];
lazy[to] += lazy[from];
}
void add(int id, int x, int y, int w, int val) {
if (x == y) {
dat[id] = val;
return;
}
int mid = (x + y) >> 1;
if (lazy[id]) {
push(id, id + id);
push(id, id + id + 1);
lazy[id] = 0;
}
if (w <= mid) {
add(id + id, x, mid, w, val);
} else {
add(id + id + 1, mid + 1, y, w, val);
}
dat[id] = dat[id + id] + dat[id + id + 1];
high[id] = max(high[id + id], high[id + id + 1]);
low[id] = min(low[id + id], low[id + id + 1]);
}
void update(int id, int x, int y, int l, int r, int delta) {
if (l > y || r < x) return;
if (l <= x && y <= r && high[id] == low[id]) {
lazy[id] += delta;
high[id] += delta;
low[id] += delta;
if (delta == 1 && high[id] == k + 1) {
dat[id] = 0;
} else if (delta == -1 && high[id] == k) {
dat[id] = (d[y] - d[x - 1] + MOD) % MOD;
}
return;
}
int mid = (x + y) >> 1;
if (lazy[id]) {
push(id, id + id);
push(id, id + id + 1);
lazy[id] = 0;
}
update(id + id, x, mid, l, r, delta);
update(id + id + 1, mid + 1, y, l, r, delta);
dat[id] = dat[id + id] + dat[id + id + 1];
high[id] = max(high[id + id], high[id + id + 1]);
low[id] = min(low[id + id], low[id + id + 1]);
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
dp[0] = 1;
{
memset(last, 0, sizeof(last));
for (int i = 1; i <= n; i++) {
l[i] = last[a[i]] + 1;
last[a[i]] = i;
}
}
{
for (int i = 1; i <= n; i++) {
last[i] = n + 1;
}
for (int i = n; i >= 1; i--) {
r[i] = last[a[i]] - 1;
last[a[i]] = i;
}
}
dp[0] = 1;
d[0] = 1;
add(1, 0, n, 0, 1);
for (int i = 1; i <= n; i++) {
for (auto j : out[i]) {
update(1, 0, n, l[j] - 1, j - 1, -1);
}
update(1, 0, n, l[i] - 1, i - 1, +1);
out[r[i] + 1].push_back(i);
dp[i] = dat[1];
d[i] = (d[i - 1] + dp[i]);
add(1, 0, n, i, dp[i]);
}
printf("%d\n", dp[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int M = 11;
const double PI = acos(-1.0);
const int blo = 320;
inline void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
int v[N];
int cnt[blo][(blo << 2) + 10], sum[blo];
int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0};
void up(int x, int y) {
vc[x] = y;
int id = x / blo;
memset(cnt[id], 0, sizeof(cnt[id]));
sum[id] = 0;
for (int i = blo - 1, be = id * blo; i >= 0; --i) {
add(cnt[id][blo + sum[id]], w[be + i]);
sum[id] += vc[be + i];
}
for (int i = 1; i <= blo * 2; ++i) add(cnt[id][i], cnt[id][i - 1]);
}
void qu(int x, int k) {
int id = x / blo;
for (int i = id; i >= 0; --i) {
if (k + blo >= 0) add(w[x], cnt[i][min(blo + k, blo * 2)]);
k -= sum[i];
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
w[0] = 1;
for (int i = 1; i <= n; ++i) {
pre[i] = l[v[i]], l[v[i]] = i;
for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]);
qu(i, k);
}
printf("%d\n", w[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int block_size = 300;
int arr[100005], cum[100005], sh[100005], sp[355][605];
int last[100005], pre[100005], n, k, dp[100005];
void update(int i, int val) {
int st = (i / block_size) * block_size;
int ed = ((i / block_size) + 1) * block_size;
if (ed > n) ed = n;
sh[i] = val;
for (int j = st; j < ed; j++) cum[j] = sh[j];
for (int j = ed - 2; j >= st; j--) {
cum[j] += cum[j + 1];
}
int dx = 302;
memset(sp[i / block_size], 0, sizeof sp[i / block_size]);
for (int j = st; j < ed; j++) {
sp[i / block_size][cum[j] + dx] += j ? dp[j - 1] : 1;
if (sp[i / block_size][cum[j] + dx] >= 998244353)
sp[i / block_size][cum[j] + dx] -= 998244353;
}
for (int j = -300; j <= 300; j++) {
sp[i / block_size][j + dx] += sp[i / block_size][j + dx - 1];
if (sp[i / block_size][j + dx] >= 998244353)
sp[i / block_size][j + dx] -= 998244353;
}
}
void add(int i) {
pre[i] = last[arr[i]];
last[arr[i]] = i;
int pr = pre[i];
int prpr = (~pr) ? pre[pr] : pr;
update(i, 1);
if (~pr) update(pr, -1);
if (~prpr) update(prpr, 0);
}
void query(int x) {
int dx = 302;
int res = 0;
int car = 0;
for (int i = x / block_size; i >= 0; i--) {
if (k - car >= -300) res += sp[i][k - car + dx];
if (res >= 998244353) res -= 998244353;
car += cum[i * block_size];
}
dp[x] = res;
}
void solve() {
memset(last, -1, sizeof last);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
add(i);
query(i);
}
cout << dp[n - 1] << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
void add(int &a, int b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
struct FenwickTree {
int dat[100055];
FenwickTree() { memset(dat, 0, sizeof(dat)); }
void add(int id, int val) {
while (id <= (int)1e5) {
::add(dat[id], val);
id |= (id + 1);
}
}
int get(int id) {
int res = 0;
while (id >= 0) {
::add(res, dat[id]);
id = (id & (id + 1)) - 1;
}
return res;
}
};
const int B = 317;
int n, k;
int a[100055];
FenwickTree dat[B];
int dp[100055];
vector<int> occ[100055];
int offset[B];
int cnt[100055];
void change(int l, int r, int val) {
if (l / B == r / B) {
for (int i = l; i <= r; i++) {
dat[i / B].add(cnt[i], -dp[i]);
cnt[i] += val;
dat[i / B].add(cnt[i], +dp[i]);
}
return;
}
while (l % B != 0) {
dat[l / B].add(cnt[l], -dp[l]);
cnt[l] += val;
dat[l / B].add(cnt[l], +dp[l]);
l++;
}
while (r % B != B - 1) {
dat[r / B].add(cnt[r], -dp[r]);
cnt[r] += val;
dat[r / B].add(cnt[r], +dp[r]);
r--;
}
if (l > r) return;
while (l <= r) {
offset[l / B] += val;
l += B;
}
}
int query() {
int res = 0;
for (int i = 0; i < B; i++) {
int ub = k - offset[i];
add(res, dat[i].get(ub));
}
return res;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i <= n; i++) {
occ[i].push_back(0);
}
dat[0].add(0, 1);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (occ[a[i]].size() > 1) {
change(occ[a[i]].end()[-2], occ[a[i]].back() - 1, -1);
}
occ[a[i]].push_back(i);
change(occ[a[i]].end()[-2], occ[a[i]].back() - 1, 1);
dp[i] = query();
dat[i / B].add(0, dp[i]);
}
cout << dp[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int t = 399, mod = 998244353;
inline int mo(const register int x) { return x >= mod ? x - mod : x; }
int a[100010], n, k, bl[100010], p[100010], pre[100010], sum[400][100010],
f[400], S[400], dp[100010], lst[100010];
inline void ad(const register int l, const register int r,
const register int x) {
if (bl[l] == bl[r]) {
for (int i = (bl[l] - 1) * t + 1; i <= min(n, bl[l] * t); i++)
sum[bl[l]][p[i]] = 0, p[i] += k - f[bl[l]];
for (int i = l; i <= r; i++) p[i] += x;
S[bl[l]] = 0;
for (int i = (bl[l] - 1) * t + 1; i <= min(n, bl[l] * t); i++)
sum[bl[l]][p[i]] = mo(sum[bl[l]][p[i]] + dp[i]),
S[bl[l]] = mo(S[bl[l]] + (p[i] <= k) * dp[i]);
f[bl[l]] = k;
return;
}
for (int i = (bl[l] - 1) * t + 1; i <= bl[l] * t; i++)
sum[bl[l]][p[i]] = 0, p[i] += k - f[bl[i]];
for (int i = l; i <= bl[l] * t; i++) p[i] += x;
S[bl[l]] = 0;
for (int i = (bl[l] - 1) * t + 1; i <= bl[l] * t; i++)
sum[bl[l]][p[i]] = mo(sum[bl[l]][p[i]] + dp[i]),
S[bl[l]] = mo(S[bl[l]] + (p[i] <= k) * dp[i]);
for (int i = (bl[r] - 1) * t + 1; i <= min(n, bl[r] * t); i++)
sum[bl[r]][p[i]] = 0, p[i] += k - f[bl[r]];
for (int i = (bl[r] - 1) * t + 1; i <= r; i++) p[i] += x;
S[bl[r]] = 0;
for (int i = (bl[r] - 1) * t + 1; i <= min(n, bl[r] * t); i++)
sum[bl[r]][p[i]] = mo(sum[bl[r]][p[i]] + dp[i]),
S[bl[r]] = mo(S[bl[r]] + (p[i] <= k) * dp[i]);
f[bl[l]] = f[bl[r]] = k;
for (int i = bl[l] + 1; i < bl[r]; i++) {
f[bl[i]] -= x;
if (x > 0)
S[i] = mo(S[i] + sum[i][f[bl[i]]]);
else
S[i] = mo(S[i] - sum[i][f[bl[i]]] + mod);
}
}
int main() {
scanf("%d%d", &n, &k);
n++;
for (int i = 1; i <= n; i++) bl[i] = (i - 1) / t + 1;
for (int i = 1; i <= bl[n]; i++) f[i] = k;
for (int i = 2; i <= n; i++) scanf("%d", &a[i]), pre[a[i]] = 1;
dp[1] = 1;
ad(1, 1, 0);
for (int i = 2; i <= n; i++) {
if (lst[pre[a[i]]])
ad(lst[pre[a[i]]], pre[a[i]] - 1, -1), ad(pre[a[i]], i - 1, 1);
else
ad(1, i - 1, 1);
lst[i] = pre[a[i]];
pre[a[i]] = i;
for (int j = 1; j <= bl[n]; j++) dp[i] += S[j];
ad(i, i, 0);
}
cout << dp[n] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
const int N = 100005;
const int BLK = 405;
const int K = 255;
int L[K], R[K], tg[K];
int top[K], pos[K];
int id[N], f[N], s[N];
int v[N], V[N], LIM;
int a[N], la[N], pre[N], n;
void pushdown(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) s[i] += tg[k];
tg[k] = 0;
}
void pushup(int k) {
top[k] = L[k];
v[top[k]] = f[id[L[k]]];
V[top[k]] = s[id[L[k]]];
for (int i = (int)(L[k] + 1); i <= (int)(R[k]); i++) {
if (s[id[i]] != s[id[i - 1]]) {
V[++top[k]] = s[id[i]];
v[top[k]] = v[top[k] - 1];
}
v[top[k]] = (v[top[k]] + f[id[i]]) % mo;
}
pos[k] = L[k] - 1;
for (; pos[k] != top[k]; ++pos[k])
if (V[pos[k] + 1] > LIM) break;
}
bool cmp(int x, int y) { return s[x] < s[y]; }
void build(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) id[i] = i;
sort(id + L[k], id + R[k] + 1, cmp);
pushup(k);
}
int q1[BLK], q2[BLK];
void add(int k, int l, int r, int v) {
pushdown(k);
int p1 = 0, p2 = 0, p3 = L[k] - 1;
for (int i = (int)(L[k]); i <= (int)(R[k]); i++)
if (l <= id[i] && id[i] <= r)
q1[++p1] = id[i], s[id[i]] += v;
else
q2[++p2] = id[i];
int pp1 = 1, pp2 = 1;
for (; pp1 <= p1 || pp2 <= p2;)
if (pp2 > p2 || (pp1 <= p1 && s[q1[pp1]] < s[q2[pp2]]))
id[++p3] = q1[pp1++];
else
id[++p3] = q2[pp2++];
pushup(k);
}
void add(int k, int val) {
tg[k] += val;
for (; pos[k] != top[k] && V[pos[k] + 1] + tg[k] <= LIM; ++pos[k])
;
for (; pos[k] != L[k] - 1 && V[pos[k]] + tg[k] > LIM; --pos[k])
;
}
int query(int k) { return pos[k] == L[k] - 1 ? 0 : v[pos[k]]; }
int ed;
void change(int l, int r, int v) {
for (; r >= l && r >= ed; r--) s[r] += v;
if (r < l) return;
int bl = l / BLK, br = r / BLK;
if (bl == br)
add(bl, l, r, v);
else {
for (int i = (int)(bl + 1); i <= (int)(br - 1); i++) add(i, v);
add(bl, l, R[bl], v);
add(br, L[br], r, v);
}
}
void update(int x) {
change(pre[x], x - 1, 1);
if (pre[x]) change(pre[pre[x]], pre[x] - 1, -1);
}
int main() {
scanf("%d%d", &n, &LIM);
for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
for (int i = (int)(1); i <= (int)(n); i++) la[a[i]] = 0;
for (int i = (int)(1); i <= (int)(n); i++) pre[i] = la[a[i]], la[a[i]] = i;
for (int i = (int)(0); i <= (int)(n / BLK); i++)
L[i] = i * BLK, R[i] = min(i * BLK + BLK - 1, n);
f[0] = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
update(i);
int be = i / BLK;
for (int j = (int)(0); j <= (int)(be - 1); j++)
f[i] = (f[i] + query(j)) % mo;
for (int j = (int)(be * BLK); j <= (int)(i - 1); j++)
if (s[j] <= LIM) f[i] = (f[i] + f[j]) % mo;
if (i % BLK == BLK - 1 || i == n) build(i / BLK), ed = i;
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int ans[100010], sum[100010], cnt[100010];
int dp[321][100010];
int val[100010];
int a[100010], pre[100010];
int L[100010], R[100010], id[100010];
int ID[100010];
int n, k;
void build(int n) {
for (int i = 1; i < 100010; i++) id[i] = sqrt(i);
for (int i = 1; i < 100010; i++) {
if (!L[id[i]]) L[id[i]] = i;
R[id[i]] = i;
}
L[1] = 0, id[0] = 1;
for (int i = 1; i <= n; i++) {
pre[i] = ID[a[i]];
ID[a[i]] = i;
}
for (int i = 0; i < 100010; i++) cnt[i] = -1;
}
inline int add_val(int &u, int v) {
u += v;
if (u >= 998244353) u -= 998244353;
}
inline int add(int st, int ed, int s) {
if (st > ed) return 0;
int u = id[st];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
sum[u] = 0;
if (id[st] == id[ed]) return 0;
u = id[ed];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
sum[u] = 0;
for (int i = id[st] + 1; i < id[ed]; i++) {
if (s == 1 and k >= sum[i]) add_val(val[i], 998244353 - dp[i][k - sum[i]]);
if (s == -1 and k + 1 >= sum[i]) add_val(val[i], dp[i][k + 1 - sum[i]]);
sum[i] += s;
}
return 0;
}
int main() {
gn(n, k);
for (int i = 1; i <= n; i++) gn(a[i]);
build(n);
cnt[0] = 0;
dp[1][0] = ans[0] = val[1] = 1;
for (int i = 1; i <= n; i++) {
add(pre[i], i - 1, 1);
add(pre[pre[i]], pre[i] - 1, -1);
ans[i] = 0;
cnt[i] = 0;
int u = id[i];
for (int j = 1; j <= u; j++) {
add_val(ans[i], val[j]);
}
add_val(val[u], ans[i]);
add_val(dp[u][cnt[i]], ans[i]);
}
cout << ans[n] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int Mod = 998244353;
int add(int a, int b) { return (a += b) >= Mod ? a - Mod : a; }
int sub(int a, int b) { return (a -= b) < 0 ? a + Mod : a; }
int mul(int a, int b) { return 1ll * a * b % Mod; }
int n, k, a[N], siz, cntblo;
int pre[N], lst[N], dp[N], f[N], sum[400][N], ans[400];
int tag[400], L[400], R[400], bel[N];
void rebuild(int id) {
for (int i = L[id]; i <= R[id]; i++) {
sum[id][f[i]] = 0;
f[i] += tag[i];
}
tag[id] = ans[id] = 0;
for (int i = L[id]; i <= R[id]; i++) {
sum[id][f[i]] = add(sum[id][f[i]], dp[i]);
if (f[i] <= k) {
ans[id] = add(ans[id], dp[i]);
}
}
}
void update(int x, int vl) {
int id = bel[x];
sum[id][f[x]] = sub(sum[id][f[x]], dp[x]);
if (f[x] <= k) {
ans[id] = sub(ans[id], dp[x]);
}
f[x] += vl;
sum[id][f[x]] = add(sum[id][f[x]], dp[x]);
if (f[x] <= k) {
ans[id] = add(ans[id], dp[x]);
}
}
void modify(int l, int r, int vl) {
if (l > r) {
return;
}
if (bel[l] == bel[r]) {
rebuild(bel[l]);
for (int i = l; i <= r; i++) {
update(i, vl);
}
return;
}
rebuild(bel[l]);
for (int i = l; i <= R[bel[l]]; i++) {
update(i, vl);
}
rebuild(bel[r]);
for (int i = L[bel[r]]; i <= r; i++) {
update(i, vl);
}
for (int id = bel[l] + 1; id < bel[r]; id++) {
if (vl > 0) {
ans[id] = sub(ans[id], sum[id][k - tag[id]]);
++tag[id];
} else {
--tag[id];
ans[id] = add(ans[id], sum[id][k - tag[id]]);
}
}
}
int query(int x) {
int res = 0;
for (int i = 1; i < bel[x]; i++) {
res = add(res, ans[i]);
}
for (int i = L[bel[x]]; i <= x; i++) {
if (f[i] <= k) {
res = add(res, dp[i]);
}
}
return res;
}
int main() {
scanf("%d %d", &n, &k);
siz = sqrt(n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pre[i] = lst[a[i]];
lst[a[i]] = i;
bel[i] = (i - 1) / siz + 1;
}
cntblo = bel[n];
for (int i = 1; i <= cntblo; i++) {
L[i] = R[i - 1] + 1;
R[i] = min(n, L[i] + siz - 1);
}
dp[1] = 1;
for (int i = 1; i <= n; i++) {
modify(pre[i] + 1, i - 1, 1);
modify(pre[pre[i]] + 1, pre[i], -1);
dp[i + 1] = query(i);
rebuild(bel[i]);
update(i, 1);
}
printf("%d", dp[n + 1]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define PB push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll, int>
#define pi4 pair<pii, pii>
#define pib pair<int, bool>
#define ft first
#define sd second
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<class T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N = 100100;
//const int N = 2010;
const int M = (1 << 10);
const int oo = 2e9;
const ll OO = 1e18;
const int PW = 20;
const int md = int(1e9) + 7;
const int BLOCK = 750;
//const int BLOCK = 2;
const int K = N / BLOCK;
int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i]; a[i]--;
}
fill(mid, mid + K, N);
for (int i = 0; i < n; i++){
ppr[i] = pr[i] = -1;
}
for (int i = 0; i < n; i++){
int l = ppr[a[i]], r = pr[a[i]];
swap(ppr[a[i]], pr[a[i]]);
pr[a[i]] = i;
int nm = i / BLOCK;
for (int j = max(r + 1, BLOCK * nm); j <= i; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
if ((r + 1) / BLOCK != nm){
nm--;
int nd = (r + 1) / BLOCK;
for (; nm > nd; nm--){
sum[nm] -= SQ[nm][mid[nm] + k];
mid[nm]--;
}
for (int j = r + 1; j < (nm + 1) * BLOCK; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
}
if (r >= 0){
int nd = (l + 1) / BLOCK;
if (nd < nm){
for (int j = BLOCK * nm; j < r + 1; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
nm--;
}
for (; nm > nd; nm--){
sum[nm] += SQ[nm][mid[nm] + k + 1];
mid[nm]++;
}
for (int j = l + 1; j <= r; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
}
for (int j = 0; j * BLOCK <= i; j++)
f[i] += sum[j];
f[i] += (i ? f[i - 1] : 1);
nm = i / BLOCK;
SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1);
if (vl[i] <= k)
sum[nm] += (i ? f[i - 1] : 1);
}
cout << f[n - 1];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int MAXN = 1e5 + 5;
const int MAXM = 320 + 5;
const int ha = 998244353;
int BLO;
int bel[MAXN];
int n, k;
int g[MAXM][MAXN], val[MAXN], f[MAXN], sm[MAXN];
int tag[MAXN];
inline void add(int &x, int y) {
x += y;
if (x >= ha) x -= ha;
}
inline void build(int x) {
for (int i = (x - 1) * BLO + 1; i <= x * BLO; ++i) val[i] += tag[x];
tag[x] = 0;
sm[x] = 0;
for (int i = (x - 1) * BLO + 1; i <= x * BLO; ++i) {
add(g[x][val[i]], f[i - 1]);
if (val[i] <= k) add(sm[x], f[i - 1]);
}
}
inline void modify(int l, int r, int d) {
if (l > r) return;
++l;
++r;
if (bel[l] == bel[r]) {
for (int i = l; i <= r; ++i) val[i] += d;
build(bel[l]);
return;
}
for (int i = l; i <= bel[l] * BLO; ++i) val[i] += d;
build(bel[l]);
for (int i = bel[l] + 1; i <= bel[r - 1]; ++i) {
if (d == 1)
add(sm[i], ha - g[i][k - tag[i]]);
else
add(sm[i], g[i][k - tag[i] + 1]);
tag[i] += d;
}
for (int i = (bel[r] - 1) * BLO + 1; i <= r; ++i) val[i] += d;
build(bel[r]);
}
inline void upd(int p) {
add(g[bel[p + 1]][0], f[p]);
add(sm[bel[p + 1]], f[p]);
}
int pre[MAXN], lst[MAXN], a[MAXN];
int main() {
scanf("%d%d", &n, &k);
BLO = std::sqrt(1.0 * n);
for (int i = 1; i <= n + 1; ++i) bel[i] = (i - 1) / BLO + 1;
for (int i = 1; i <= n; ++i)
scanf("%d", a + i), pre[i] = lst[a[i]], lst[a[i]] = i;
for (int i = 1; i <= bel[n + 1]; ++i) build(i);
f[0] = 1;
upd(0);
for (int i = 1; i <= n; ++i) {
int p1 = pre[i], p2 = pre[p1];
modify(p1, i - 1, 1);
if (p1) modify(p2, p1 - 1, -1);
for (int j = 1; j <= bel[n + 1]; ++j) add(f[i], sm[j]);
upd(i);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
const int N = 1.1e5, M = 400;
int n, k, q, x;
int b[N], l1[N], l2[N], dp[N];
int s[M][M], mi[M], sum[M];
inline void add(int &x, int y) {
x = x + y;
if (x > P) x -= P;
}
void upd(int x, int val) {
int y = x / q;
b[x] = val;
sum[y] = mi[y] = 0;
for (int i = (y * q + q - 1); i >= (y * q); --i)
mi[y] = min(mi[y], sum[y]), sum[y] += b[i];
sum[y] = 0;
for (int i = (0); i <= (q); ++i) s[y][i] = 0;
for (int i = (y * q + q - 1); i >= (y * q); --i) {
add(s[y][sum[y] - mi[y]], dp[i]);
sum[y] += b[i];
}
for (int i = (1); i <= (q); ++i) add(s[y][i], s[y][i - 1]);
}
inline int get(int y, int up) {
return up >= mi[y] ? s[y][min(q, up - mi[y])] : 0;
}
int qry(int x) {
int y = x / q, ans = 0, S = 0;
for (; ~y; --y) add(ans, get(y, k - S)), S += sum[y];
return ans;
}
int main() {
scanf("%d%d", &n, &k);
q = sqrt(n), dp[0] = 1;
upd(0, 0);
for (int i = (1); i <= (n); ++i) {
scanf("%d", &x);
if (l2[x]) upd(l2[x], 0);
if (l1[x]) upd(l1[x], -1);
dp[i] = qry(i);
upd(i, 1);
if (l1[x]) l2[x] = l1[x];
l1[x] = i;
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100002;
const int MOD = 1000000007;
const int MAGIC = 500;
const int INF = 1e9;
int n, k, prv[MAX_N], pos[MAX_N], a[MAX_N];
int nBlock, L[MAGIC + 2], R[MAGIC + 2], blockID[MAGIC + 2];
int v[MAX_N], offset[MAGIC + 2], head[MAGIC + 2], ps[MAGIC + 2][MAX_N];
int f[MAX_N];
vector<pair<int, int> > all, comp[MAGIC + 2];
void readInput() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
}
void init() {
for (int i = 1; i <= n; ++i) {
prv[i] = pos[a[i]];
pos[a[i]] = i;
}
}
void sqrtDecompostion() {
for (int i = 1; i <= n; ++i) {
if (i % MAGIC == 1) {
R[nBlock] = i - 1;
L[++nBlock] = i;
}
}
R[nBlock] = n;
for (int i = 1; i <= nBlock; ++i) {
for (int j = L[i]; j <= R[i]; ++j) blockID[j] = i;
}
}
void upd(int idx, int l, int r, int delta) {
for (int i = l; i <= r; ++i) {
v[i] += offset[idx];
if (l <= i && i <= r) v[i] += delta;
}
offset[idx] = 0;
all.clear();
comp[idx].clear();
for (int i = L[idx]; i <= R[idx]; ++i) all.push_back({v[i], f[i - 1]});
sort(all.begin(), all.end());
comp[idx].push_back({-INF, 0});
for (auto x : all) {
if (x.first != comp[idx].back().first)
comp[idx].push_back(x);
else
comp[idx].back().second = (comp[idx].back().second + x.second) % MOD;
}
for (int i = 1; i < comp[idx].size(); ++i)
ps[idx][i] = (comp[idx][i].second + ps[idx][i - 1]) % MOD;
for (int i = 0; i < comp[idx].size(); ++i) {
if (comp[idx][i].first <= k) head[idx] = i;
}
}
void upd(int l, int r, int delta) {
if (l > r) return;
if (blockID[l] == blockID[r]) return upd(blockID[l], l, r, delta);
for (int i = blockID[l] + 1; i < blockID[r]; ++i) {
offset[i] += delta;
if (delta == -1) {
while (head[i] + 1 < comp[i].size() &&
comp[i][head[i] + 1].first + offset[i] <= k)
++head[i];
} else {
while (head[i] > 0 && comp[i][head[i]].first + offset[i] > k) --head[i];
}
}
upd(l, R[blockID[l]], delta);
upd(L[blockID[r]], r, delta);
}
int get() {
int res = 0;
for (int i = 1; i <= nBlock; ++i) res = (res + ps[i][head[i]]) % MOD;
return res;
}
void solve() {
for (int i = 1; i <= nBlock; ++i) upd(L[i], R[i], 0);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int x1 = prv[i];
int x2 = prv[x1];
upd(x1 + 1, i, 1);
upd(x2 + 1, x1, -1);
f[i] = get();
upd(L[blockID[i]], R[blockID[i]], 0);
}
cout << f[n];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
readInput();
init();
sqrtDecompostion();
solve();
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 100 + 1;
const int Q = 100;
const int MOD = 998244353;
const int UNDEF = -10;
int a[MAXN];
int b[MAXN];
int pr[MAXN];
int prpr[MAXN];
int dp[MAXN];
int sum_dp[MAXN / Q + 10][2 * Q + 1];
int sum[MAXN / Q + 10];
int n, k;
void relax(int& x) {
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
}
void up(int ind) {
int l = ind * Q;
int r = (ind + 1) * Q;
for (int i = 0; i < 2 * Q + 1; i++) {
sum_dp[ind][i] = 0;
}
int& sum = ::sum[ind] = 0;
for (int i = r - 1; i >= l; i--) {
if (b[i] != UNDEF) {
sum_dp[ind][sum + Q] += dp[i];
sum += b[i];
}
}
for (int i = 1; i < 2 * Q + 1; i++) {
sum_dp[ind][i] += sum_dp[ind][i - 1];
relax(sum_dp[ind][i]);
}
}
int get(int ind, int cur_sum) {
int up = max(-Q, min(Q, k - cur_sum));
return sum_dp[ind][up + Q];
}
int main(int argc, const char* argv[]) {
for (int i = 0; i < MAXN / Q + 10; i++) {
sum[i] = 0;
}
for (int i = 0; i < MAXN; i++) {
b[i] = UNDEF;
pr[i] = -1;
prpr[i] = -1;
}
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%i", a + i);
a[i]--;
}
dp[0] = 1;
b[0] = 0;
up(0);
for (int i = 1; i <= n; i++) {
if (prpr[a[i]] != -1) {
b[prpr[a[i]]] = 0;
up(prpr[a[i]] / Q);
}
if (pr[a[i]] != -1) {
b[pr[a[i]]] = -1;
up(pr[a[i]] / Q);
}
prpr[a[i]] = pr[a[i]];
pr[a[i]] = i;
int sum = 1;
dp[i] = 0;
for (int j = (i - 1) / Q; j >= 0; j--) {
dp[i] += get(j, sum);
relax(dp[i]);
sum += ::sum[j];
}
b[i] = 1;
up(i / Q);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline void open(const char *s) {}
inline int rd() {
static int x, f;
x = 0;
f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return f > 0 ? x : -x;
}
const int N = 100010, M = 410, mod = 998244353;
int n, K, a[N], B;
int head[N], nxt[N];
int bl[N], mn[N], L[N], R[N], sum[M][M], tag[M], f[N], b[N];
inline int pls(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int add(int &a, int b) { return a = pls(a, b); }
inline void rebuild(int x) {
mn[x] = 1e9;
memset(sum[x], 0, sizeof sum[x]);
for (int i = L[x]; i <= R[x]; i++) b[i] += tag[x], mn[x] = min(mn[x], b[i]);
tag[x] = 0;
for (int i = L[x]; i <= R[x]; i++) add(sum[x][b[i] - mn[x]], f[i]);
for (int i = 1; i <= 400; i++) add(sum[x][i], sum[x][i - 1]);
}
void modify(int ql, int qr, int d) {
if (ql > qr) return;
int l = bl[ql], r = bl[qr];
for (int i = l + 1; i <= r - 1; i++) tag[i] += d;
if (l == r) {
for (int i = ql; i <= qr; i++) b[i] += d;
rebuild(l);
return;
}
for (int i = ql; i <= R[l]; i++) b[i] += d;
rebuild(l);
for (int i = L[r]; i <= qr; i++) b[i] += d;
rebuild(r);
}
int query(int x) {
int res = 0;
for (int i = 1; i <= bl[x]; i++)
add(res, sum[i][min(K - tag[i] - mn[i], 400)]);
return res;
}
int main() {
open("hh");
n = rd();
K = rd();
B = 320;
for (int i = 1; i <= n; i++) a[i] = rd(), nxt[i] = head[a[i]], head[a[i]] = i;
for (int i = 0; i <= n; i++) bl[i] = i / B + 1, R[bl[i]] = i;
for (int i = n; i >= 0; i--) L[bl[i]] = i, mn[i] = 1e9;
f[0] = 1;
rebuild(1);
for (int i = 1; i <= n; i++) {
int p1 = nxt[i], p2 = nxt[p1];
modify(p1, i - 1, 1);
modify(p2, p1 - 1, -1);
f[i] = query(i - 1);
rebuild(bl[i]);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxm = 320, tt = 1e9 + 7;
int n, m, n1, a[maxn], f[maxn], g[maxm][maxn], bl[maxn], sq, val[maxn],
tag[maxm];
int L[maxn], R[maxn];
int las[maxn], las1[maxn];
void build(int x, int l, int r) {
for (int i = l; i <= r; i++) {
g[x][val[i]] = (g[x][val[i]] + f[i - 1]) % tt;
}
for (int i = 1; i <= n1; i++) g[x][i] = (g[x][i] + g[x][i - 1]) % tt;
}
void change(int l, int r, int p, int now) {
int st = bl[l] + 1, ed = bl[r] - 1;
if (st <= ed)
for (int i = st; i <= ed; i++) tag[i] += p;
if (bl[l] != bl[r]) {
for (int i = l; i <= R[bl[l]]; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
if (bl[r] != bl[now] || (bl[r] == bl[now] && R[bl[now]] == now)) {
for (int i = L[bl[r]]; i <= r; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
} else {
for (int i = L[bl[r]]; i <= r; i++)
if (p == -1)
val[i]--;
else
val[i]++;
}
} else {
if (bl[r] != bl[now] || (bl[r] == bl[now] && R[bl[now]] == now)) {
for (int i = l; i <= r; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
} else {
for (int i = l; i <= r; i++)
if (p == -1)
val[i]--;
else
val[i]++;
}
}
}
int query(int l, int r) {
int ret = 0;
for (int i = bl[l]; i < bl[r]; i++) ret = (ret + g[i][m - tag[i]]) % tt;
for (int i = L[bl[r]]; i <= r; i++)
if (val[i] + tag[i] <= m) ret = (ret + f[i - 1]) % tt;
return ret;
}
int main() {
scanf("%d%d", &n, &m);
n1 = m;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), n1 = max(n1, a[i]);
int ls = 0, bls = 0;
sq = sqrt(n);
for (int i = 1; i <= n; i++) {
bl[i] = bls + 1;
if (i - ls == sq || i == n) L[++bls] = ls + 1, R[bls] = i, ls = i;
}
ls = 0, bls = 0;
f[0] = 1;
for (int i = 1; i <= n; i++) {
if (i == ls + sq) {
build(++bls, ls + 1, i);
ls = i;
}
if (las[a[i]]) change(las1[a[i]] + 1, las[a[i]], -1, i);
change(las[a[i]] + 1, i, 1, i);
las1[a[i]] = las[a[i]], las[a[i]] = i;
f[i] = query(1, i);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
const int mod = 998244353;
using namespace std;
int n, k, sq;
int a[100050], pre[100050], las[100050], p[100050], dp[100050], bl[100050],
pd[100050];
inline void modd(int &x) {
if (x >= mod) x -= mod;
}
struct node {
int l, r, tag;
int b[1555], mi, ma;
inline void build() {
memset(b, 0, sizeof(int) * (ma + 1 - mi));
mi = n;
for (int i = l; i <= r; ++i)
p[i] += tag, mi = min(mi, p[i]), ma = max(ma, p[i] + tag);
tag = 0;
for (int i = l; i <= r; ++i) b[p[i] - mi] += dp[i], modd(b[p[i] - mi]);
for (int i = 1; i <= ma - mi; ++i) b[i] += b[i - 1], modd(b[i]);
}
inline int ans() {
if (k < mi + tag) return 0;
if (k > ma + tag) return b[ma - mi];
return b[k - tag - mi];
}
} q[1555];
inline void add(int l, int r, int t) {
if (l > r) return;
if (bl[l] == bl[r]) {
for (int i = l; i <= r; ++i) p[i] += t;
if (pd[bl[l]]) q[bl[l]].build();
return;
}
for (int i = l; bl[i] == bl[l]; ++i) ++p[i];
if (pd[bl[l]]) q[bl[l]].build();
for (int i = r; bl[i] == bl[r]; --i) ++p[i];
if (pd[bl[r]]) q[bl[r]].build();
for (int i = bl[l] + 1; i < bl[r]; ++i) q[i].tag += t;
}
inline int getans(int r) {
int ret = 0;
for (int i = 1; i < bl[r]; ++i) {
ret += q[i].ans(), modd(ret);
}
for (int i = r; bl[i] == bl[r]; --i)
if (p[i] + q[bl[i]].tag <= k) ret += dp[i], modd(ret);
return ret;
}
int main() {
scanf("%d%d", &n, &k);
sq = sqrt(n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) bl[i] = i / sq + 1;
for (int i = 1; i <= bl[n - 1]; ++i)
q[i].l = i * sq - sq, q[i].r = min(i * sq - 1, n - 1);
dp[0] = 1;
p[0] = 0;
for (int i = 1; i <= n; ++i) {
pre[i] = las[a[i]];
las[a[i]] = i;
add(pre[i], i - 1, 1);
add(pre[pre[i]], pre[i] - 1, -1);
dp[i] = getans(i - 1);
if (i - 1 == q[bl[i - 1]].r) {
pd[bl[i - 1]] = 1;
q[bl[i - 1]].build();
}
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int f[100010], val[100010], sep[100010], cnt, k, h[100010], last[100010], n;
int read() {
int tmp = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
tmp = tmp * 10 + c - '0';
c = getchar();
}
return tmp;
}
struct block {
int l, r, tag, xs[320], val[320], diff[320], pre[320], cur, cnt;
int id[2][320], merge[320];
void change(int tl, int tr, int c) {
int sum[2], p[2], now = 0;
sum[0] = sum[1] = 0;
p[0] = p[1] = 1;
for (int i = 1; i <= r - l + 1; i++)
if (merge[i] >= tl - l + 1 && merge[i] <= tr - l + 1) {
id[1][++sum[1]] = merge[i];
xs[merge[i]] += c;
} else
id[0][++sum[0]] = merge[i];
while (p[0] <= sum[0] || p[1] <= sum[1]) {
int ch;
if (p[0] > sum[0])
ch = 1;
else if (p[1] > sum[1])
ch = 0;
else if (xs[id[0][p[0]]] < xs[id[1][p[1]]])
ch = 0;
else
ch = 1;
merge[++now] = id[ch][p[ch]];
p[ch]++;
}
cnt = 0;
for (int i = 1; i <= r - l + 1; i++)
if (i != 1 && xs[merge[i]] == xs[merge[i - 1]])
pre[cnt] = (pre[cnt] + val[merge[i]]) % 998244353;
else {
cnt++;
diff[cnt] = xs[merge[i]];
pre[cnt] = val[merge[i]];
}
cur = 0;
for (int i = 1; i <= cnt; i++) {
if (diff[i] + tag <= k) cur = i;
pre[i] = (pre[i - 1] + pre[i]) % 998244353;
}
}
void update() {
if (cur && diff[cur] + tag > k) cur--;
if (cur != cnt && diff[cur + 1] + tag <= k) cur++;
}
} B[320];
void modify(int l, int r, int x) {
if (sep[l] == sep[r]) {
B[sep[l]].change(l, r, x);
return;
}
for (int i = sep[l] + 1; i <= sep[r] - 1; i++) {
B[i].tag++;
B[i].update();
}
B[sep[l]].change(l, B[sep[l]].r, x);
B[sep[r]].change(B[sep[r]].l, r, x);
}
int main() {
n = read();
k = read();
int S = sqrt(n);
for (int i = 1; i <= n; i++) val[i] = read();
for (int cur = 0; cur <= n; cur += S) {
int nxt = min(cur + S - 1, n);
cnt++;
B[cnt].l = cur;
B[cnt].r = nxt;
for (int i = cur; i <= nxt; i++) {
sep[i] = cnt;
B[cnt].merge[i - cur + 1] = i - cur + 1;
}
B[cnt].cnt = B[cnt].cur = 1;
}
f[0] = 1;
B[sep[0]].val[1] = f[0];
B[1].change(1, 0, 0);
for (int i = 1; i <= n; i++) {
last[i] = h[val[i]];
h[val[i]] = i;
int tmp = last[i];
modify(tmp, i - 1, 1);
if (tmp != 0) modify(last[tmp], tmp - 1, -1);
for (int j = 1; j <= sep[i] - 1; j++)
f[i] = (f[i] + B[j].pre[B[j].cur]) % 998244353;
int c = sep[i];
for (int j = B[c].l; j <= i - 1; j++)
if (B[c].tag + B[c].xs[j - B[c].l + 1] <= k)
f[i] = (f[i] + f[j]) % 998244353;
B[c].val[i - B[c].l + 1] = f[i];
B[c].change(i, i, 0);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, tot, a[100005], vis[100005];
long long f[100005];
int main() {
f[0] = 1ll;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) {
memset(vis, 0, sizeof vis);
++vis[a[i]], tot = 1;
for (int j = i - 1; j >= 0; --j) {
if (tot <= k) f[i] = (f[i] + f[j]) % 998244353ll;
if (!vis[a[j]]) ++tot;
++vis[a[j]];
if (vis[a[j]] > 1) --tot;
}
}
printf("%lld\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | //~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
//~ #pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x))));
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;
//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbΔ elementΓ³w ostro mniejszych niΕΌ k
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...);
}
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#define cerr if(0)cout
#endif
const int maxn = (1e5)+7;
const int maxk = 20;
const int inf = (1e9)+7;
const LL LLinf = ((LL)1e18)+7LL;
const LD eps = 1e-9;
const LL mod = 998244353LL;
// ***************************** CODE ***************************** //
const int pier = 300;
int n, k;
int val[maxn];
VI pos[maxn];
LL dp[maxn];
struct kubel
{
int l, p;
int tab[pier * 2 + 10];
int suma = 0;
void update()
{
if(l == 0)
return;
// dbg(l);
memset(tab, 0, sizeof tab);
suma = pier;
for(int i = p;i >= l;i--)
{
suma += val[i];
tab[suma] += dp[i - 1];
tab[suma] %= mod;
}
suma -= pier;
for(int i = 1;i < 2 * pier + 2;i++)
tab[i] = (tab[i] + tab[i - 1]) % mod;
}
LL pyt(int prawo)
{
int xd = k - prawo;
xd += pier;
xd = min(xd, pier * 2);
dbg(xd, (xd > 0 ? tab[xd] : 0));
if(xd < 0)
return 0;
return tab[xd];
}
};
int tab[maxn];
kubel kub[maxn / pier + 10];
int numer[maxn];
int main()
{
_upgrade
cin>>n>>k;
for(int i = 1;i <= n;i++)
cin>>tab[i];
dp[0] = 1;
int num = 0;
for(int i = 1;i <= n;i += pier)
{
kub[i / pier + 1].l = i;
kub[i / pier + 1].p = i + pier - 1;
for(int j = i;j < i + pier;j++)
numer[j] = i / pier + 1;
// dbg(i, kub[i].l, kub[i].p);
kub[i / pier + 1].update();
num++;
}
for(int i = 1;i <= n;i++)
{
int ost = (SZ(pos[tab[i]]) > 0 ? pos[tab[i]].back() : 0);
int przed = (SZ(pos[tab[i]]) > 1 ? pos[tab[i]][SZ(pos[tab[i]]) - 2] : 0);
val[przed] = 0;
val[ost] = -1;
val[i] = 1;
kub[numer[przed]].update();
kub[numer[ost]].update();
kub[numer[i]].update();
pos[tab[i]].pb(i);
// for(int j = 1;j <= n;j++)
// cerr<<val[j]<<" ";
// cerr<<endl;
for(int j = -3;j <= 3;j++)
dbg(j, kub[1].tab[pier + j]);
for(int j = -3;j <= 3;j++)
dbg(j, kub[2].tab[pier + j]);
int suma = 0;
for(int j = num;j > 0;j--)
{
dp[i] += (LL)kub[j].pyt(suma);
// dbg(i, suma, j, dp[i]);
suma += kub[j].suma;
}
dp[i] %= mod;
dbg(i, dp[i]);
kub[numer[i + 1]].update();
}
cout<<dp[n];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
using namespace std;
template <class T1, class T2, class T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple() : a(T1()), b(T2()), c(T3()){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else if (t1.b != t2.b)
return t1.b < t2.b;
else
return t1.c < t2.c;
}
template <class T1, class T2, class T3>
bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a > t2.a;
else if (t1.b != t2.b)
return t1.b > t2.b;
else
return t1.c > t2.c;
}
inline int countBits(unsigned int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int countBits(unsigned long long v) {
unsigned int t = v >> 32;
unsigned int p = (v & ((1ULL << 32) - 1));
return countBits(t) + countBits(p);
}
inline int countBits(long long v) { return countBits((unsigned long long)v); }
inline int countBits(int v) { return countBits((unsigned int)v); }
unsigned int reverseBits(unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
template <class T>
inline int sign(T x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); }
constexpr long long power(long long x, int p) {
return p == 0 ? 1 : (x * power(x, p - 1));
}
template <class T>
inline bool inRange(const T& val, const T& min, const T& max) {
return min <= val && val <= max;
}
unsigned long long rdtsc() {
unsigned long long ret = 0;
return __builtin_readcyclecounter();
asm volatile("rdtsc" : "=A"(ret) : :);
return ret;
}
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template <class T = int>
T readInt() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir;
c();
bool m = false;
if (*__ir == '-') ++__ir, c(), m = true;
T r = 0;
while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
++__ir;
return m ? -r : r;
}
string readString() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
string r;
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c();
++__ir;
return r;
}
char readChar() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
return *__ir++;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template <class T>
void writeInt(T x, char endc = '\n') {
if (x < 0) *__iw++ = '-', x = -x;
if (x == 0) *__iw++ = '0';
char* s = __iw;
while (x) {
T t = x / 10;
char c = x - 10 * t + '0';
*__iw++ = c;
x = t;
}
char* f = __iw - 1;
while (s < f) swap(*s, *f), ++s, --f;
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
if (endc) {
*__iw++ = endc;
}
}
template <class T>
void writeStr(const T& str) {
int i = 0;
while (str[i]) {
*__iw++ = str[i++];
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
}
}
struct __FL__ {
~__FL__() {
if (__iw != __buw) cout.write(__buw, __iw - __buw);
}
};
static __FL__ __flushVar__;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p);
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t);
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
bool f = 1;
os << "[";
while (!v.empty()) {
auto x = v.top();
v.pop();
if (!f) os << ", ";
f = 0;
os << x;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
template <class T1, class T2>
void printarray(const T1& a, T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t);
template <class T1, class T2>
inline istream& operator>>(istream& os, pair<T1, T2>& p) {
return os >> p.first >> p.second;
}
template <class T>
inline istream& operator>>(istream& os, vector<T>& v) {
if (v.size())
for (T& t : v) os >> t;
else {
string s;
T obj;
while (s.empty()) {
getline(os, s);
if (!os) return os;
}
stringstream ss(s);
while (ss >> obj) v.push_back(obj);
}
return os;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) {
return os >> t.a >> t.b >> t.c;
}
template <class T1, class T2>
inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first += p2.first, p1.second += p2.second;
return p1;
}
template <class T1, class T2>
inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first -= p2.first, p1.second -= p2.second;
return p1;
}
const int USUAL_MOD = 1000000007;
template <class T>
inline void normmod(T& x, T m = USUAL_MOD) {
x %= m;
if (x < 0) x += m;
}
template <class T1, class T2>
inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) {
T2 res = x + y;
if (res >= m) res -= m;
return res;
}
template <class T1, class T2, class T3 = int>
inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x += y;
if (x >= m) x -= m;
}
template <class T1, class T2, class T3 = int>
inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x -= y;
if (x < 0) x += m;
}
inline long long mulmod(long long x, long long n, long long m) {
x %= m;
n %= m;
long long r =
x * n - long long(long double(x) * long double(n) / long double(m)) * m;
while (r < 0) r += m;
while (r >= m) r -= m;
return r;
}
inline long long powmod(long long x, long long n, long long m) {
long long r = 1;
normmod(x, m);
while (n) {
if (n & 1) r *= x;
x *= x;
r %= m;
x %= m;
n /= 2;
}
return r;
}
inline long long powmulmod(long long x, long long n, long long m) {
long long res = 1;
normmod(x, m);
while (n) {
if (n & 1) res = mulmod(res, x, m);
x = mulmod(x, x, m);
n /= 2;
}
return res;
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <class T>
T fast_gcd(T u, T v) {
int shl = 0;
while (u && v && u != v) {
T eu = u & 1;
u >>= eu ^ 1;
T ev = v & 1;
v >>= ev ^ 1;
shl += (~(eu | ev) & 1);
T d = u & v & 1 ? (u + v) >> 1 : 0;
T dif = (u - v) >> (sizeof(T) * 8 - 1);
u -= d & ~dif;
v -= d & dif;
}
return std::max(u, v) << shl;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
template <class T>
inline T gcd(T a, T b, T c) {
return gcd(gcd(a, b), c);
}
long long gcdex(long long a, long long b, long long& x, long long& y) {
if (!a) {
x = 0;
y = 1;
return b;
}
long long y1;
long long d = gcdex(b % a, a, y, y1);
x = y1 - (b / a) * y;
return d;
}
bool isPrime(long long n) {
if (n <= (1 << 14)) {
int x = (int)n;
if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
for (int i = 5; i * i <= x; i += 6)
if (x % i == 0 || x % (i + 2) == 0) return 0;
return 1;
}
long long s = n - 1;
int t = 0;
while (s % 2 == 0) {
s /= 2;
++t;
}
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (!(a %= n)) return true;
long long f = powmulmod(a, s, n);
if (f == 1 || f == n - 1) continue;
for (int i = 1; i < t; ++i)
if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
return false;
nextp:;
}
return true;
}
int32_t solve();
int32_t main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
return solve();
}
int a[101001];
int b[101010];
int dp[101010];
int p1[101010];
int p2[101010];
long long getSum(int* __restrict b, int* __restrict dp, int n) {
long long res = 0;
for (int j = 0; j < n; ++j) {
res += b[j] > 0 ? 0 : dp[j];
}
return res;
}
template <int val>
void add(int* a, int n) {
for (int i = 0; i < n; ++i) {
a[i] += val;
}
}
int solve() {
int n = readInt();
int k = readInt();
for (int i = 0; i < (n); ++i) {
b[i] -= k;
}
for (int i = 0; i < (n); ++i) {
a[i] = readInt() - 1;
p1[i] = p2[i] = -1;
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
add<-1>(b + p2[x] + 1, p1[x] - p2[x] + 1 + 1);
add<1>(b + p1[i] + 1, i - p1[i]);
long long res = getSum(b, dp, i + 1);
dp[i + 1] = res % 998244353;
p2[x] = p1[x];
p1[x] = i;
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
const long long mod = 998244353;
int n, m, len;
int idx1[M], idx2[M], a[M];
int st[M], ed[M], second[M], pl[M];
pair<int, int> sto[M], kf[M];
long long dp[M], f[M];
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
void make_group() {
len = 80;
int tot = n / len;
if (n % len) tot++;
for (int i = 1; i <= n; i++) {
second[i] = (i - 1) / len + 1;
}
for (int i = 1; i <= tot; i++) {
st[i] = (i - 1) * len + 1;
ed[i] = i * len;
}
}
int find(int long long, int rr, int v) {
int l = long long, r = rr;
int ans;
while (l <= r) {
int mid = l + r >> 1;
if (kf[mid].first <= v) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
return ans;
}
void add(int l, int r, int v) {
if (l > r) return;
int sid = second[l], eid = second[r];
if (sid == eid) {
for (int i = l; i <= r; i++) sto[i].first += v;
for (int i = st[sid]; i <= ed[sid]; i++) kf[i] = sto[i];
sort(kf + st[sid], kf + ed[sid] + 1, cmp);
f[st[sid]] = dp[kf[st[sid]].second];
for (int i = st[sid] + 1; i <= ed[sid]; i++) {
f[i] = (f[i - 1] + dp[kf[i].second]) % mod;
}
return;
}
for (int i = l; sid == second[i]; i++) sto[i].first += v;
for (int i = st[sid]; i <= ed[sid]; i++) kf[i] = sto[i];
sort(kf + st[sid], kf + ed[sid] + 1, cmp);
f[st[sid]] = dp[kf[st[sid]].second];
for (int i = st[sid] + 1; i <= ed[sid]; i++) {
f[i] = (f[i - 1] + dp[kf[i].second]) % mod;
}
for (int i = sid + 1; i < eid; i++) pl[i] += v;
for (int i = r; eid == second[i]; i--) sto[i].first += v;
for (int i = st[eid]; i <= ed[eid]; i++) kf[i] = sto[i];
sort(kf + st[eid], kf + ed[eid] + 1, cmp);
f[st[eid]] = dp[kf[st[eid]].second];
for (int i = st[eid] + 1; i <= ed[eid]; i++) {
f[i] = (f[i - 1] + dp[kf[i].second]) % mod;
}
return;
}
long long query(int l, int r) {
if (l > r) return 0;
int sid = second[l], eid = second[r];
long long ans = 0;
if (sid == eid) {
for (int i = l; i <= r; i++) {
if (sto[i].first + pl[sid] <= m) ans = (ans + dp[i - 1]) % mod;
}
return ans;
}
for (int i = l; sid == second[i]; i++) {
if (sto[i].first + pl[sid] <= m) ans = (ans + dp[i - 1]) % mod;
}
for (int i = sid + 1; i < eid; i++) {
int tmp = find(st[i], ed[i], m - pl[i]);
if (tmp < st[i]) continue;
ans = (ans + f[tmp]) % mod;
}
for (int i = r; eid == second[i]; i--) {
if (sto[i].first + pl[eid] <= m) ans = (ans + dp[i - 1]) % mod;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sto[i].second = i - 1;
}
make_group();
dp[0] = 1;
for (int i = 1; i <= n; i++) {
add(idx1[a[i]] + 1, i, 1);
add(idx2[a[i]] + 1, idx1[a[i]], -1);
dp[i] = query(1, i);
idx2[a[i]] = idx1[a[i]];
idx1[a[i]] = i;
}
printf("%lld", dp[n] % mod);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int ans[100010], sum[100010], cnt[100010];
int dp[321][100010];
int val[100010];
int a[100010], pre[100010];
int L[100010], R[100010], id[100010];
int ID[100010];
int n, k;
void build(int n) {
for (int i = 1; i < 100010; i++) id[i] = sqrt((double)i + 0.5);
for (int i = 1; i < 100010; i++) {
if (!L[id[i]]) L[id[i]] = i;
R[id[i]] = i;
}
L[1] = 0, id[0] = 1;
for (int i = 1; i <= n; i++) {
pre[i] = ID[a[i]];
ID[a[i]] = i;
}
for (int i = 0; i < 100010; i++) cnt[i] = -1;
}
inline int add_val(int &u, int v) {
u += v;
if (u >= 998244353) u -= 998244353;
}
inline int add(int st, int ed, int s) {
if (st > ed) return 0;
int u = id[st];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
sum[u] = 0;
if (id[st] == id[ed]) return 0;
u = id[ed];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
sum[u] = 0;
for (int i = id[st] + 1; i < id[ed]; i++) {
if (s == 1 and k >= sum[i]) add_val(val[i], 998244353 - dp[i][k - sum[i]]);
if (s == -1 and k + 1 >= sum[i]) add_val(val[i], dp[i][k + 1 - sum[i]]);
sum[i] += s;
}
return 0;
}
int main() {
gn(n, k);
for (int i = 1; i <= n; i++) gn(a[i]);
build(n);
cnt[0] = 0;
dp[1][0] = ans[0] = val[1] = 1;
for (int i = 1; i <= n; i++) {
add(pre[i], i - 1, 1);
add(pre[pre[i]], pre[i] - 1, -1);
ans[i] = 0;
cnt[i] = 0;
int u = id[i];
for (int j = 1; j <= u; j++) {
add_val(ans[i], val[j]);
}
add_val(val[u], ans[i]);
add_val(dp[u][cnt[i]], ans[i]);
}
cout << ans[n] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 100010, SQ = 320;
int n, k, arr[N], dp[N], sq, nxt[N], nxt2[N], cur[N], frq[SQ][N];
int num[SQ];
inline void add(int &x, int y) {
x += y;
if (x >= 998244353) x -= 998244353;
}
inline void subtract(int &x, int y) {
x -= y;
if (x < 0) x += 998244353;
}
inline void make(int idx) {
for (int i = idx * sq; i < min(n, (idx + 1) * sq); i++) {
frq[idx][cur[i]] = 0;
cur[i] += num[idx];
}
num[idx] = 0;
for (int i = idx * sq; i < min(n, (idx + 1) * sq); i++) {
add(frq[idx][cur[i]], dp[i]);
}
}
int l, r, val, current;
inline void update() {
if (l > r) return;
if (l / sq == r / sq) {
current = l / sq;
while (l <= r) {
cur[l++] += val;
}
make(current);
return;
}
if (l % sq) {
current = l / sq;
while (l <= n && l % sq != 0) {
cur[l++] += val;
}
make(current);
}
if (r % sq != sq - 1) {
current = r / sq;
while (r >= 0 && r % sq != sq - 1) {
cur[r--] += val;
}
make(current);
}
if (r < l) return;
l /= sq;
r /= sq;
while (l <= r) {
num[l++] += val;
}
}
int getres = 0;
inline int get() {
if (l > r) return 0;
getres = 0;
if (l / sq == r / sq) {
while (l <= r) {
add(getres, ((cur[l] + num[l / sq]) == val ? dp[l] : 0));
l++;
}
return getres;
}
while (l <= n && l % sq) {
add(getres, ((cur[l] + num[l / sq]) == val ? dp[l] : 0));
l++;
}
while (r >= 0 && r % sq != sq - 1) {
add(getres, ((cur[r] + num[r / sq]) == val ? dp[r] : 0));
r--;
}
if (r < l) {
return getres;
}
l /= sq;
r /= sq;
while (l <= r) {
if (val - num[l] >= 0) add(getres, frq[l][val - num[l]]);
l++;
}
return getres;
}
int main() {
scanf("%d%d", &n, &k);
sq = sqrt(n) + 1;
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
for (int i = 1; i <= n; i++) nxt[i] = nxt2[i] = n;
dp[n - 1] = dp[n] = 1;
nxt[arr[n - 1]] = n - 1;
cur[n] = 1;
frq[n / sq][1] = 1;
frq[(n - 1) / sq][0] = 1;
for (int i = n - 2; i >= 0; i--) {
dp[i] = dp[i + 1];
add(dp[i], dp[i + 1]);
l = i + 1, r = nxt[arr[i]], val = 1;
update();
l = i + 1, r = nxt[arr[i]], val = k + 1;
subtract(dp[i], get());
l = nxt[arr[i]] + 1, r = nxt2[arr[i]], val = -1;
update();
l = nxt[arr[i]] + 1, r = nxt2[arr[i]], val = k;
add(dp[i], get());
nxt2[arr[i]] = nxt[arr[i]];
nxt[arr[i]] = i;
make(i / sq);
}
printf("%d\n", dp[0]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[100015], lv[100015];
int last[100015], pp[100015];
int dp[100015];
int add(int x, int y) { return ((x + y) % 998244353 + 998244353) % 998244353; }
int sum = 0;
int sumSeg[320][100015 + 100015];
int valSeg[320];
int getSegIndex(int x) { return (x - 1) / 320 + 1; }
void update(int l, int r, int val, int k) {
int st = getSegIndex(l), ed = getSegIndex(r);
for (int segIt = st; segIt <= ed; ++segIt) {
int Left = (st - 1) * 320 + 1, Right = st * 320;
if (Left == 100015 + 1) {
if (val == 1) {
sum = add(sum, -sumSeg[segIt][100015 + k - valSeg[segIt]]);
} else {
sum = add(sum, sumSeg[segIt][100015 + k + 1 - valSeg[segIt]]);
}
valSeg[segIt] += val;
} else {
Left = max(Left, l);
Right = min(Right, r);
for (int j = Left; j <= Right; ++j) {
if (lv[j] + valSeg[segIt] <= k) {
sum = add(sum, -dp[j]);
}
sumSeg[segIt][100015 + lv[j]] =
add(sumSeg[segIt][100015 + lv[j]], -dp[j]);
lv[j] += val;
if (lv[j] + valSeg[segIt] <= k) {
sum = add(sum, dp[j]);
}
sumSeg[segIt][100015 + lv[j]] =
add(sumSeg[segIt][100015 + lv[j]], dp[j]);
}
}
}
}
void solve() {
int n, k;
scanf("%d %d ", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d ", &a[i]);
}
dp[1] = 1;
sumSeg[getSegIndex(1)][100015] = 1;
sum = 1;
for (int i = 1; i <= n; ++i) {
int l = last[a[i]] + 1, r = i;
update(l, r, 1, k);
if (last[a[i]] != 0) {
l = pp[a[i]] + 1;
r = last[a[i]];
update(l, r, -1, k);
}
pp[a[i]] = last[a[i]];
last[a[i]] = i;
int tmp = sum;
dp[i + 1] = tmp;
sum = add(sum, dp[i + 1]);
int seg = getSegIndex(i + 1);
seg = valSeg[seg];
lv[i + 1] = -seg;
sumSeg[getSegIndex(i + 1)][lv[i + 1] + 100015] =
add(sumSeg[getSegIndex(i + 1)][lv[i + 1] + 100015], tmp);
}
printf("%d\n", dp[n + 1]);
}
int main() {
solve();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define PB push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll, int>
#define pi4 pair<pii, pii>
#define pib pair<int, bool>
#define ft first
#define sd second
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<class T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N = 100100;
//const int N = 2010;
const int M = (1 << 10);
const int oo = 2e9;
const ll OO = 1e18;
const int PW = 20;
const int md = int(1e9) + 7;
const int BLOCK = 750;
//const int BLOCK = 1;
const int K = N / BLOCK;
int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i]; a[i]--;
}
fill(mid, mid + K, N);
for (int i = 0; i < n; i++){
ppr[i] = pr[i] = -1;
}
for (int i = 0; i < n; i++){
int l = ppr[a[i]], r = pr[a[i]];
swap(ppr[a[i]], pr[a[i]]);
pr[a[i]] = i;
int nm = i / BLOCK;
sum[nm] += (i ? f[i - 1] : 1);
SQ[nm][mid[nm]] += (i ? f[i - 1] : 1);
for (int j = max(r + 1, BLOCK * nm); j <= i; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
if ((r + 1) / BLOCK != nm){
nm--;
int nd = (r + 1) / BLOCK;
for (; nm > nd; nm--){
sum[nm] -= SQ[nm][mid[nm] + k];
mid[nm]--;
}
for (int j = r + 1; j < (nm + 1) * BLOCK; j++){
SQ[nm][N + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][N + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] + N - mid[nm] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
}
if (r >= 0){
int nd = (l + 1) / BLOCK;
if (nd < nm){
for (int j = BLOCK * nm; j < r + 1; j++){
SQ[nm][N + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][N + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] + N - mid[nm] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
nm--;
}
for (; nm > nd; nm--){
sum[nm] += SQ[nm][mid[nm] + k + 1];
mid[nm]++;
}
for (int j = l + 1; j < min(BLOCK * (nm + 1), r + 1); j++){
SQ[nm][N + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][N + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] + N - mid[nm] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
}
for (int j = 0; j * BLOCK <= i; j++)
f[i] += sum[j];
nm = i / BLOCK;
}
cout << f[n - 1];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 998244353;
int sqrtN = 333;
int mp[333][100001];
struct SqrtDecomposition {
int N, K, tap, ans;
vector<int> data;
vector<int> bucketAdd;
vector<int> uku;
SqrtDecomposition(int n, int tap) : N(n), tap(tap), ans(0) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(n, 0);
bucketAdd.assign(K, 0);
uku.assign(n, 0);
}
void add(int s, int t) {
for (int k = 0; k < K; ++k) {
int l = k * sqrtN, r = min((k + 1) * sqrtN, (int)data.size());
if (r <= s || t <= l) continue;
if (s <= l && r <= t) {
++bucketAdd[k];
ans += mp[k][tap - bucketAdd[k]];
if (ans >= mod) ans -= mod;
} else {
for (int i = max(s, l); i < min(t, r); ++i) {
mp[k][data[i]] += mod - uku[i];
if (mp[k][data[i]] >= mod) mp[k][data[i]] -= mod;
++data[i];
mp[k][data[i]] += uku[i];
if (mp[k][data[i]] >= mod) mp[k][data[i]] -= mod;
if (data[i] + bucketAdd[k] == tap) {
ans += uku[i];
if (ans >= mod) ans -= mod;
}
}
}
}
}
void sub(int s, int t) {
for (int k = 0; k < K; ++k) {
int l = k * sqrtN, r = min((k + 1) * sqrtN, (int)data.size());
if (r <= s || t <= l) continue;
if (s <= l && r <= t) {
ans += mod - mp[k][tap - bucketAdd[k]];
if (ans >= mod) ans -= mod;
--bucketAdd[k];
} else {
for (int i = max(s, l); i < min(t, r); ++i) {
mp[k][data[i]] += mod - uku[i];
if (mp[k][data[i]] >= mod) mp[k][data[i]] -= mod;
--data[i];
mp[k][data[i]] += uku[i];
if (mp[k][data[i]] >= mod) mp[k][data[i]] -= mod;
if (data[i] + bucketAdd[k] == tap - 1) {
ans += mod - uku[i];
if (ans >= mod) ans -= mod;
}
}
}
}
}
};
int main() {
int K, N, A[100000];
cin >> N >> K;
++K;
vector<int> ap[100000];
for (int i = 0; i < N; i++) {
cin >> A[i];
--A[i];
ap[A[i]].emplace_back(i);
}
int last[100000];
memset(last, -1, sizeof(last));
for (int i = 0; i < N; i++) {
for (int j = 1; j < ap[i].size(); j++) {
last[ap[i][j]] = ap[i][j - 1];
}
}
vector<int> dp(N + 1);
dp[0] = 1;
int appear[100000];
int cor[100000];
memset(appear, -1, sizeof(appear));
memset(cor, -1, sizeof(cor));
set<pair<pair<int, int>, int> > range;
SqrtDecomposition seg(N + 1, K);
int all = 1;
seg.uku[0]++;
mp[0][0]++;
for (int i = 1; i <= N; i++) {
if (range.count(
{make_pair(appear[A[i - 1]] + 1, cor[A[i - 1]] + 1), A[i - 1]})) {
range.erase(
{make_pair(appear[A[i - 1]] + 1, cor[A[i - 1]] + 1), A[i - 1]});
seg.sub(appear[A[i - 1]] + 1, cor[A[i - 1]] + 1);
}
appear[A[i - 1]] = last[i - 1];
cor[A[i - 1]] = i - 1;
seg.add(appear[A[i - 1]] + 1, cor[A[i - 1]] + 1);
range.emplace(make_pair(appear[A[i - 1]] + 1, cor[A[i - 1]] + 1), A[i - 1]);
int add = (all + mod - seg.ans) % mod;
(all += add) %= mod;
(dp[i] += add) %= mod;
(seg.uku[i] += add) %= mod;
(mp[i / sqrtN][seg.data[i]] += add) %= mod;
if (seg.bucketAdd[i / sqrtN] + seg.data[i] >= K) {
(seg.ans += add) %= mod;
}
}
cout << dp[N] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 100005;
const int S = 320;
const int MAXB = MAXN / S + 5;
int pool[MAXB][MAXN << 1], *t[MAXB], f[MAXB][S], tag[MAXB], sum[MAXB], dp[MAXN];
int a[MAXN], last[MAXN], pre[MAXN];
int n, k;
void init() {
for (int i = 1; i <= n; i++) {
pre[i] = last[a[i]];
last[a[i]] = i;
}
for (int i = 0; i <= n / S; i++) t[i] = pool[i + 1];
dp[0] = 1;
}
inline void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
}
void modify(int p, int v) {
int id = p / S, x = p % S;
add(t[id][f[id][x]], -dp[p - 1]);
if (f[id][x] <= k + tag[id]) add(sum[id], -dp[p - 1]);
f[id][x] += v;
add(t[id][f[id][x]], dp[p - 1]);
if (f[id][x] <= k + tag[id]) add(sum[id], dp[p - 1]);
}
void add(int l, int r, int v) {
while (l <= r && l % S != 0) {
modify(l, v);
++l;
}
while (l <= r && (r + 1) % S != 0) {
modify(r, v);
--r;
}
for (int i = l; i <= r; i += S) {
if (v == 1)
add(sum[i / S], -t[i / S][k + tag[i / S]]);
else
add(sum[i / S], +t[i / S][k + 1 + tag[i / S]]);
tag[i / S] -= v;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
init();
for (int i = 1; i <= n; i++) {
add(pre[i] + 1, i - 1, 1);
if (pre[i] != 0) add(pre[pre[i]] + 1, pre[i], -1);
add(sum[i / S], dp[i - 1]);
add(t[i / S][1], dp[i - 1]);
add(f[i / S][i % S], 1);
for (int j = 0; j <= n / S; j++) add(dp[i], sum[j]);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long MAXN = 1e5;
const long long B = 315;
long long cnt[1 + MAXN], dp[1 + MAXN];
vector<long long> occ[1 + MAXN];
void add_self(long long &x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
void min_self(long long &x, long long y) { x = min(x, y); }
struct SQRT {
long long id, offset, pref_sum[B];
void rebuild() {
long long st = id * B, dr = (id + 1) * B - 1, minn = INT_MAX;
for (long long i = st; i <= dr; ++i) min_self(minn, offset + cnt[i]);
for (long long i = st; i <= dr; ++i) cnt[i] -= minn - offset;
offset = minn;
for (long long i = 0; i < B; ++i) pref_sum[i] = 0;
for (long long i = st; i <= dr; ++i) add_self(pref_sum[cnt[i]], dp[i]);
for (long long i = 1; i < B; ++i) add_self(pref_sum[i], pref_sum[i - 1]);
}
} a[MAXN / B + 1];
long long get_bucket(long long index) { return index / B; }
void update(long long l, long long r, short t) {
long long bl = get_bucket(l), br = get_bucket(r);
for (long long i = l; i <= r && get_bucket(i) == bl; ++i) cnt[i] += t;
a[bl].rebuild();
if (bl == br) return;
for (long long i = bl + 1; i < br; ++i) a[i].offset += t;
for (long long i = r; get_bucket(i) == br; --i) cnt[i] += t;
a[br].rebuild();
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long n, k;
cin >> n >> k;
for (long long i = 0; i <= get_bucket(n); ++i) a[i].id = i;
for (long long i = 1; i <= n; ++i) occ[i].emplace_back(-1);
dp[0] = 1;
a[0].rebuild();
for (long long r = 0; r < n; ++r) {
long long x;
cin >> x;
vector<long long> &vec = occ[x];
if (static_cast<long long>(vec.size()) >= 2)
update(vec.end()[-2] + 1, vec.back(), -1);
update(vec.back() + 1, r, 1);
vec.emplace_back(r);
long long val = 0;
for (long long i = 0; i <= get_bucket(r); ++i) {
long long at_most = k - a[i].offset;
if (at_most >= 0) add_self(val, a[i].pref_sum[min(at_most, B - 1)]);
}
dp[r + 1] = val;
a[get_bucket(r + 1)].rebuild();
}
cout << dp[n] << '\n';
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define PB push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll, int>
#define pi4 pair<pii, pii>
#define pib pair<int, bool>
#define ft first
#define sd second
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<class T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N = 100100;
//const int N = 2010;
const int M = (1 << 10);
const int oo = 2e9;
const ll OO = 1e18;
const int PW = 20;
const int md = int(1e9) + 7;
const int BLOCK = 750;
//const int BLOCK = 2;
const int K = N / BLOCK;
int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i]; a[i]--;
}
fill(mid, mid + K, N);
for (int i = 0; i < n; i++){
ppr[i] = pr[i] = -1;
}
for (int i = 0; i < n; i++){
int l = ppr[a[i]], r = pr[a[i]];
swap(ppr[a[i]], pr[a[i]]);
pr[a[i]] = i;
int nm = i / BLOCK;
for (int j = max(r + 1, BLOCK * nm); j <= i; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
if ((r + 1) / BLOCK != nm){
nm--;
int nd = (r + 1) / BLOCK;
for (; nm > nd; nm--){
sum[nm] -= SQ[nm][mid[nm] + k];
mid[nm]--;
}
for (int j = r + 1; j < (nm + 1) * BLOCK; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]++;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k + 1)
sum[nm] -= (j ? f[j - 1] : 1);
}
}
if (r >= 0){
int nd = (l + 1) / BLOCK;
if (nd < nm){
for (int j = BLOCK * nm; j < r + 1; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
nm--;
}
for (; nm > nd; nm--){
sum[nm] += SQ[nm][mid[nm] + k + 1];
mid[nm]++;
}
for (int j = l + 1; j <= r; j++){
SQ[nm][mid[nm] + vl[j]] -= (j ? f[j - 1] : 1);
vl[j]--;
SQ[nm][mid[nm] + vl[j]] += (j ? f[j - 1] : 1);
if (vl[j] == k)
sum[nm] += (j ? f[j - 1] : 1);
}
}
for (int j = 0; j * BLOCK <= i; j++)
f[i] += sum[j];
f[i] += (i ? f[i - 1] : 1);
nm = i / BLOCK;
// SQ[nm][mid[nm] + vl[i]] += (i ? f[i - 1] : 1);
if (vl[i] <= k)
sum[nm] += (i ? f[i - 1] : 1);
}
cout << f[n - 1];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 100010, SQ = 320;
int n, k, arr[N], dp[N], sq, nxt[N], nxt2[N], cur[N], frq[SQ][N];
int num[SQ];
inline void add(int &x, int y) {
x += y;
if (x >= 998244353) x -= 998244353;
}
inline void subtract(int &x, int y) {
x -= y;
if (x < 0) x += 998244353;
}
inline void init(int idx) {
for (int i = idx * sq; i < min(n, (idx + 1) * sq); i++) {
cur[i] += num[idx];
}
num[idx] = 0;
}
inline void make(int idx) {
for (int i = idx * sq; i < min(n, (idx + 1) * sq); i++) {
frq[idx][cur[i]] = 0;
cur[i] += num[idx];
}
num[idx] = 0;
for (int i = idx * sq; i < min(n, (idx + 1) * sq); i++) {
add(frq[idx][cur[i]], dp[i]);
}
}
int l, r, val, current;
inline void update() {
if (l > r) return;
if (l / sq == r / sq) {
current = l / sq;
while (l <= r) {
cur[l++] += val;
}
make(current);
return;
}
if (l % sq) {
current = l / sq;
while (l <= n && l % sq != 0) {
cur[l++] += val;
}
make(current);
}
if (r % sq != sq - 1) {
current = r / sq;
while (r >= 0 && r % sq != sq - 1) {
cur[r--] += val;
}
make(current);
}
if (r < l) return;
l /= sq;
r /= sq;
while (l <= r) {
num[l++] += val;
}
}
int getres = 0;
inline int get() {
if (l > r) return 0;
getres = 0;
if (l / sq == r / sq) {
while (l <= r) {
add(getres, ((cur[l] + num[l / sq]) == val ? dp[l] : 0));
l++;
}
return getres;
}
while (l <= n && l % sq) {
add(getres, ((cur[l] + num[l / sq]) == val ? dp[l] : 0));
l++;
}
while (r >= 0 && r % sq != sq - 1) {
add(getres, ((cur[r] + num[r / sq]) == val ? dp[r] : 0));
r--;
}
if (r < l) {
return getres;
}
l /= sq;
r /= sq;
while (l <= r) {
if ((val - num[l] >= 0)) add(getres, frq[l][val - num[l]]);
l++;
}
return getres;
}
int main() {
scanf("%d%d", &n, &k);
sq = sqrt(n) + 1;
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
for (int i = 1; i <= n; i++) nxt[i] = nxt2[i] = n;
dp[n - 1] = dp[n] = 1;
nxt[arr[n - 1]] = n - 1;
cur[n] = 1;
frq[n / sq][1] = 1;
frq[(n - 1) / sq][0] = 1;
for (int i = n - 2; i >= 0; i--) {
dp[i] = dp[i + 1];
add(dp[i], dp[i + 1]);
l = i + 1, r = nxt[arr[i]], val = 1;
update();
l = i + 1, r = nxt[arr[i]], val = k + 1;
subtract(dp[i], get());
l = nxt[arr[i]] + 1, r = nxt2[arr[i]], val = -1;
update();
l = nxt[arr[i]] + 1, r = nxt2[arr[i]], val = k;
add(dp[i], get());
nxt2[arr[i]] = nxt[arr[i]];
nxt[arr[i]] = i;
make(i / sq);
}
printf("%d\n", dp[0]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int BASE = 998244353;
const int SZ = 320, V = 100000 / SZ;
struct BlockData {
int sumC;
vector<long long> sumF;
BlockData() {}
};
int n, k, c[100100];
long long f[100100];
BlockData blocks[SZ];
long long calc(int i) {
long long res = 0;
int sumC = 0, block = i / SZ;
for (int j = i; j >= block * SZ; j--) {
sumC += c[j];
if (sumC <= k) res += j ? f[j - 1] : 1;
}
for (int j = block - 1; j >= 0; j--) {
res += blocks[j].sumF[max(min(k - sumC, V), 0) + V];
sumC += blocks[j].sumC;
}
return res % BASE;
}
void constructBlock(int block) {
BlockData &data = blocks[block];
data.sumC = 0;
data.sumF = vector<long long>(V * 2 + 1, 0);
for (int i = block * SZ + SZ - 1; i >= block * SZ; i--) {
data.sumC += c[i];
data.sumF[data.sumC + V] += i ? f[i - 1] : 1;
}
for (int i = 0; i <= V * 2; i++) {
if (i) data.sumF[i] += data.sumF[i - 1];
data.sumF[i] %= BASE;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x;
cin >> n >> k;
vector<int> id[100100];
for (int i = 0; i < n; i++) {
cin >> x;
c[i] = 1;
if (!id[x].empty()) {
int j = id[x].back();
c[j] = -1;
if (id[x].size() >= 2) {
int k = id[x][id[x].size() - 2];
c[k] = 0;
if (k / SZ != j / SZ) constructBlock(k / SZ);
}
if (j / SZ != i / SZ) constructBlock(j / SZ);
}
id[x].push_back(i);
f[i] = calc(i);
if (i % SZ == SZ - 1) constructBlock(i / SZ);
}
cout << f[n - 1] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
int ans[100010], sum[321], cnt[100010];
int dp[321][100010];
int val[321];
int a[100010], pre[100010];
int L[321], R[321], id[100010];
int ID[100010];
int n, k;
void build(int n) {
for (int i = 1; i < 100010; i++) id[i] = sqrt(i);
for (int i = 1; i < 100010; i++) {
if (!L[id[i]]) L[id[i]] = i;
R[id[i]] = i;
}
L[1] = 0;
id[0] = 1;
for (int i = 1; i <= n; i++) {
pre[i] = ID[a[i]];
ID[a[i]] = i;
}
for (int i = 0; i < 100010; i++) cnt[i] = -1;
}
inline int add_val(int &u, int v) {
u += v;
if (u >= 998244353) u -= 998244353;
}
inline int add(int st, int ed, int s) {
if (st > ed) return 0;
int u = id[st];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
sum[u] = 0;
if (id[st] == id[ed]) return 0;
u = id[ed];
for (int i = L[u]; i <= R[u]; i++) {
if (cnt[i] < 0) break;
add_val(dp[u][cnt[i]], 998244353 - ans[i]);
cnt[i] += sum[u];
if (cnt[i] <= k) add_val(val[u], 998244353 - ans[i]);
if (i >= st and i <= ed) cnt[i] += s;
add_val(dp[u][cnt[i]], ans[i]);
if (cnt[i] <= k) add_val(val[u], ans[i]);
}
for (int i = id[st] + 1; i < id[ed]; i++) {
if (s == 1 and k >= sum[i]) add_val(val[i], 998244353 - dp[i][k - sum[i]]);
if (s == -1 and k + 1 >= sum[i]) add_val(val[i], dp[i][k + 1 - sum[i]]);
add_val(sum[i], s);
}
return 0;
}
int main() {
gn(n, k);
for (int i = 1; i <= n; i++) gn(a[i]);
build(n);
cnt[0] = 0;
dp[1][0] = ans[0] = val[1] = 1;
for (int i = 1; i <= n; i++) {
add(pre[i], i - 1, 1);
add(pre[pre[i]], pre[i] - 1, -1);
ans[i] = 0;
cnt[i] = 0;
int u = id[i];
for (int j = 1; j <= u; j++) {
ans[i] += val[j];
}
val[u] += ans[i];
dp[u][cnt[i]] += ans[i];
}
cout << ans[n] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9, mod = 998244353, maxm = 409;
int n, k, ans;
int a[maxn], cnt[maxn], lst[maxn], fir[maxn], bl[maxm], br[maxm], col[maxn],
v[maxn], lazy[maxm], f[maxn], sum[maxm][maxn];
inline void Fir() {
int size(sqrt(n));
int pieces(ceil(1.0 * n / size));
for (int i = 1; i <= pieces; ++i) {
bl[i] = (i - 1) * size + 1;
br[i] = (i == pieces ? n : i * size);
for (int j = bl[i]; j <= br[i]; ++j) col[j] = i;
}
for (int i = 1; i <= n; ++i) {
lst[i] = fir[a[i]];
fir[a[i]] = i;
}
}
inline void Modify(int l, int r, int val) {
int lt(col[l]), rt(col[r]);
if (lt == rt) {
for (int i = l; i <= r; ++i) {
if (val == 1) {
if (v[i] + lazy[lt] == k) ans = (ans - f[i] + mod) % mod;
} else {
if (v[i] + lazy[lt] == k + 1) ans = (ans + f[i]) % mod;
}
sum[lt][v[i]] = (sum[lt][v[i]] - f[i] + mod) % mod, v[i] += val,
sum[lt][v[i]] = (sum[lt][v[i]] + f[i]) % mod;
}
} else {
for (int i = l; i <= br[lt]; ++i) {
if (val == 1) {
if (v[i] + lazy[lt] == k) ans = (ans - f[i] + mod) % mod;
} else {
if (v[i] + lazy[lt] == k + 1) ans = (ans + f[i]) % mod;
}
sum[lt][v[i]] = (sum[lt][v[i]] - f[i] + mod) % mod, v[i] += val,
sum[lt][v[i]] = (sum[lt][v[i]] + f[i]) % mod;
}
for (int i = bl[rt]; i <= r; ++i) {
if (val == 1) {
if (v[i] + lazy[rt] == k) ans = (ans - f[i] + mod) % mod;
} else {
if (v[i] + lazy[rt] == k + 1) ans = (ans + f[i]) % mod;
}
sum[rt][v[i]] = (sum[rt][v[i]] - f[i] + mod) % mod, v[i] += val,
sum[rt][v[i]] = (sum[rt][v[i]] + f[i]) % mod;
}
for (int i = lt + 1; i <= rt - 1; ++i) {
if (val == 1)
ans = (ans - sum[i][k - lazy[i]] + mod) % mod;
else
ans = (ans + sum[i][k + 1 - lazy[i]]) % mod;
lazy[i] += val;
}
}
}
inline void Update(int x) {
sum[col[x + 1]][v[x + 1]] += f[x + 1];
ans += f[x + 1];
}
inline void Solve() {
ans = 1;
sum[1][1] = 1;
for (int i = 1; i <= n; ++i) {
++cnt[a[i]];
int p(lst[i]), q(lst[p]);
if (cnt[a[i]] == 1) {
Modify(1, i, 1);
} else if (cnt[a[i]] == 2) {
Modify(p + 1, i, 1);
Modify(1, p, -1);
} else {
Modify(p + 1, i, 1);
Modify(q + 1, p, -1);
}
f[i + 1] = ans;
Update(i);
}
printf("%d", f[n + 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
Fir();
Solve();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int t = 399, mod = 998244353;
inline int mo(const register int x) { return x >= mod ? x - mod : x; }
int a[100010], n, k, bl[100010], p[100010], pre[100010], sum[400][100010],
f[400], S[400], dp[100010], lst[100010];
inline void ad(const register int l, const register int r,
const register int x) {
if (bl[l] == bl[r]) {
for (int i = (bl[l] - 1) * t + 1; i <= min(n, bl[l] * t); i++)
sum[bl[l]][p[i]] = 0, p[i] += k - f[bl[l]];
for (int i = l; i <= r; i++) p[i] += x;
S[bl[l]] = 0;
for (int i = (bl[l] - 1) * t + 1; i <= min(n, bl[l] * t); i++)
sum[bl[l]][p[i]] = mo(sum[bl[l]][p[i]] + dp[i]),
S[bl[l]] = mo(S[bl[l]] + (p[i] <= k) * dp[i]);
f[bl[l]] = k;
return;
}
for (int i = (bl[l] - 1) * t + 1; i <= bl[l] * t; i++)
sum[bl[l]][p[i]] = 0, p[i] += k - f[bl[i]];
for (int i = l; i <= bl[l] * t; i++) p[i] += x;
S[bl[l]] = 0;
for (int i = (bl[l] - 1) * t + 1; i <= bl[l] * t; i++)
sum[bl[l]][p[i]] = mo(sum[bl[l]][p[i]] + dp[i]),
S[bl[l]] = mo(S[bl[l]] + (p[i] <= k) * dp[i]);
for (int i = (bl[r] - 1) * t + 1; i <= min(n, bl[r] * t); i++)
sum[bl[r]][p[i]] = 0, p[i] += k - f[bl[r]];
for (int i = (bl[r] - 1) * t + 1; i <= r; i++) p[i] += x;
S[bl[r]] = 0;
for (int i = (bl[r] - 1) * t + 1; i <= min(n, bl[r] * t); i++)
sum[bl[r]][p[i]] = mo(sum[bl[r]][p[i]] + dp[i]),
S[bl[r]] = mo(S[bl[r]] + (p[i] <= k) * dp[i]);
f[bl[l]] = f[bl[r]] = k;
for (int i = bl[l] + 1; i < bl[r]; i++) {
f[bl[i]] -= x;
if (f[bl[i]] >= 0 && f[bl[i]] <= 100000) {
if (x > 0)
S[i] = mo(S[i] + sum[i][f[bl[i]]]);
else
S[i] = mo(S[i] - sum[i][f[bl[i]]] + mod);
}
}
}
int main() {
scanf("%d%d", &n, &k);
n++;
for (int i = 1; i <= n; i++) bl[i] = (i - 1) / t + 1;
for (int i = 1; i <= bl[n]; i++) f[i] = k;
for (int i = 2; i <= n; i++) scanf("%d", &a[i]), pre[a[i]] = 1;
dp[1] = 1;
ad(1, 1, 0);
for (int i = 2; i <= n; i++) {
if (lst[pre[a[i]]])
ad(lst[pre[a[i]]], pre[a[i]] - 1, -1), ad(pre[a[i]], i - 1, 1);
else
ad(1, i - 1, 1);
lst[i] = pre[a[i]];
pre[a[i]] = i;
for (int j = 1; j <= bl[n]; j++) dp[i] = mo(dp[i] + S[j]);
ad(i, i, 0);
}
cout << dp[n] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int M = 11;
const double PI = acos(-1.0);
const int blo = 320;
inline void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
int v[N];
int cnt[blo][(blo << 1) + 10], sum[blo];
int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0};
void up(int x, int y) {
vc[x] = y;
int id = x / blo;
memset(cnt[id], 0, sizeof(cnt[id]));
sum[id] = 0;
for (int i = blo - 1, be = id * blo; i >= 0; --i) {
add(cnt[id][blo + sum[id]], w[be + i]);
sum[id] += vc[be + i];
}
for (int i = 1; i <= blo * 2; ++i) add(cnt[id][i], cnt[id][i - 1]);
}
void qu(int x, int k) {
int id = x / blo;
for (int i = id; i >= 0; --i) {
if (k + blo >= 0) add(w[x], cnt[i][min(blo + k, blo * 2)]);
k -= sum[i];
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
w[0] = 1;
for (int i = 1; i <= n; ++i) {
pre[i] = l[v[i]], l[v[i]] = i;
for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]);
qu(i, k);
}
printf("%d\n", w[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long BIG = 1446803456761533460;
const int Big = 336860180;
stringstream sss;
const int maxn = 100010;
const int SQ = 330;
int n, k;
int A[maxn], P[maxn];
map<int, int> lst;
int block[SQ][maxn];
int lazy[SQ], val[maxn];
int dp[maxn];
int sum = 0;
int& blk(int x, int y) { return block[x / SQ][-y]; }
void inc(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
if (k - lazy[l / SQ] <= 0)
sum = ((sum) + (MOD - blk(l, k - lazy[l / SQ]))) % MOD;
++lazy[l / SQ];
l += SQ;
} else {
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (MOD - dp[l])) % MOD;
blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD;
++val[l];
blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD;
++l;
}
}
}
void dec(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
--lazy[l / SQ];
if (k - lazy[l / SQ] <= 0)
sum = ((sum) + (blk(l, k - lazy[l / SQ]))) % MOD;
l += SQ;
} else {
blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD;
--val[l];
blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD;
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (dp[l])) % MOD;
++l;
}
}
}
void MAIN() {
cin >> n >> k;
for (int i = (0); i < (n); ++i) {
cin >> A[i];
P[i + 1] = lst.count(A[i]) ? lst[A[i]] : 0;
lst[A[i]] = i + 1;
}
dp[0] = 1;
sum = 1;
block[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
inc(P[i], i);
dec(P[P[i]], P[i]);
dp[i] = sum;
sum = ((sum) + (dp[i])) % MOD;
val[i] = -lazy[i / SQ];
blk(i, val[i]) = ((blk(i, val[i])) + (dp[i])) % MOD;
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
sss << R"(
5 2
1 1 2 1 3
)";
MAIN();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
int add(int a, int b) { return a + b >= Mod ? a + b - Mod : a + b; }
void Add(int &a, int b) { a = add(a, b); }
int dec(int a, int b) { return a - b < 0 ? a - b + Mod : a - b; }
void Dec(int &a, int b) { a = dec(a, b); }
const int N = 1e5 + 50, M = 255, A = 1e5;
int n, k, S, ct, a[N], pre[N], blk[N], dp[N];
int b[N], t[M], bin[M][N + A], sm[M], L[N], R[N];
void ins(int u, int vl) {
int p = blk[u];
Add(sm[p], vl);
Add(bin[p][b[u] - t[p] + A], vl);
}
void mdf(int i, int lp, int v) {
if (b[i] + t[lp] <= k) Dec(sm[lp], dp[i - 1]);
Dec(bin[lp][b[i] + A], dp[i - 1]);
b[i] += v;
if (b[i] + t[lp] <= k) Add(sm[lp], dp[i - 1]);
Add(bin[lp][b[i] + A], dp[i - 1]);
}
void add(int l, int r, int v) {
if (l > r) return;
int lp = blk[l], rp = blk[r];
if (blk[l] == blk[r]) {
for (int i = l; i <= r; i++) mdf(i, lp, v);
return;
}
for (int i = l; i <= R[lp]; i++) mdf(i, lp, v);
for (int i = L[rp]; i <= r; i++) mdf(i, rp, v);
for (int i = lp + 1; i < rp; i++) {
if (v > 0)
Dec(sm[i], bin[i][k - t[i] + A]);
else
Add(sm[i], bin[i][k - t[i] + 1 + A]);
t[i] += v;
}
}
int qry(int p) {
int ans = 0, c = 0;
for (; c <= ct && R[c] <= p; c++) Add(ans, sm[c]);
if (c > ct) return ans;
for (int i = L[c]; i <= p; i++)
if (b[i] + t[c] <= k) Add(ans, dp[i - 1]);
return ans;
}
int main() {
scanf("%d%d", &n, &k);
S = 400;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
static int ls[N];
for (int i = 1; i <= n; i++) pre[i] = ls[a[i]], ls[a[i]] = i;
for (int i = 1; i <= n; i++) blk[i] = (i - 1) / S + 1;
for (int i = 1; i <= n; i += S) L[++ct] = i, R[ct] = i + S - 1;
R[ct] = n;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
add(pre[i] + 1, i, 1);
add(pre[pre[i]] + 1, pre[i], -1);
ins(i, dp[i - 1]);
dp[i] = qry(i);
}
cout << dp[n];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#define il inline
#define ri register int
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define mid ((l+r)>>1)
#define MAXN 100050
#define MAXM
#define mod 998244353
#define inf (1<<30)
#define eps (1e-6)
#define alpha 0.75
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define repd(i, x, y) for(ri i = x; i >= y; --i)
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, int> pli;
typedef pair <int, ll> pil;
typedef pair <ll, ll> pll;
template <typename T> il bool chkmin(T &x, T y) {return x > y ? x = y, 1 : 0;}
template <typename T> il bool chkmax(T &x, T y) {return x < y ? x = y, 1 : 0;}
template <typename T> il void read(T &x) {
char ch = getchar(); int f = 1; x = 0;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') x = x*10+ch-'0', ch = getchar();
x *= f;
}
int n, k, B, a[MAXN], pre[MAXN], pos[MAXN], mark[MAXN];
int dp[MAXN], cnt[MAXN], add[MAXN];
vector <int> v;
struct node {
mutable int pos, cnt, val, sum;
bool operator < (const node &x) const {
return cnt < x.cnt;
}
};
vector <node> ver[MAXN];
void inc(int l, int r, int w) {
// cout<<"inc "<<l<<' '<<r<<' '<<w<<endl;
for(auto u: v) {
if(l <= ver[u].begin()->pos && ver[u].begin()->pos <= r) add[u] += w;
}
}
int query(int r) {
int ret = 0;
for(auto u: v)
if(ver[u].begin()->pos <= r) {
ret = (ret + (--upper_bound(ver[u].begin(), ver[u].end(), node {0, k-add[u], 0, 0}))->sum) % mod;
}
else break;
return ret;
}
int main() {
read(n), read(k);
rep(i, 1, n) read(a[i]), pre[i] = pos[a[i]], pos[a[i]] = i;
B = sqrt(n), dp[0] = 1;
for(ri ti = 1; ti <= n; ti += B) {
int l = ti, r = min(ti+B-1, n);
// cout<<l<<' '<<r<<endl;
rep(i, 0, n) mark[i] = 0, ver[i].clear();
v.clear(), mark[0] = 1;
rep(i, l, r) {
mark[i] = mark[i+1] = mark[pre[i]] = mark[pre[pre[i]]] = 1;
}
rep(i, 0, n) if(mark[i]) {
v.pb(i), ver[i].pb(node {i, cnt[i], dp[i], 0});
int u = i+1;
while(u <= n && !mark[u]) ver[i].pb(node {u, cnt[u], dp[u], 0}), u++;
sort(ver[i].begin(), ver[i].end());
ver[i][0].sum = ver[i][0].val;
rep(j, 1, ver[i].size()-1) ver[i][j].sum = (ver[i][j].val + ver[i][j-1].sum) % mod;
// cout<<"checking: "<<i<<endl;
// for(auto x: ver[i]) cout<<x.pos<<' '<<x.cnt<<' '<<x.sum<<' ';
// puts("");
}
rep(i, l, r) {
inc(pre[pre[i]], pre[i]-1, -1), inc(pre[i], i-1, 1);
dp[i] = query(i-1), ver[i].pop_back(), ver[i].pb(node {i, cnt[i], dp[i], dp[i]});
// cout<<"dp["<<i<<"] is "<<dp[i]<<endl;
}
for(auto u: v) {
for(auto x: ver[u]) cnt[x.pos] += add[u];
add[u] = 0;
}
// puts("check cnt:");
// rep(i, 0, n) cout<<cnt[i]<<' ';
// puts(""), puts("");
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long mod = 998244353LL;
int k;
class Block {
public:
int aux;
vector<long long> pre;
int st, en;
int lo, hi;
vector<long long> vals;
vector<int> dist;
Block(int a, int b) {
aux = 0;
st = a;
en = b;
lo = 0;
hi = 0;
pre.push_back(0LL);
pre.push_back(0LL);
for (int i = st; i <= en; i++) {
vals.push_back(0LL);
dist.push_back(0);
}
}
void change(int ind, long long val) {
if (ind < st || ind > en) {
return;
}
vals[ind - st] = val;
for (int i = dist[ind - st]; i <= hi; i++) {
pre[i - lo + 1] += val;
pre[i - lo + 1] %= mod;
}
}
void modify(int l, int r, int delta) {
if (l <= st && r >= en) {
aux += delta;
return;
}
if (st > r || en < l) {
return;
}
for (int i = max(l, st); i <= min(r, en); i++) {
dist[i - st] += delta;
}
for (int i = st; i <= en; i++) {
dist[i - st] += aux;
if (i == st) {
lo = dist[i - st];
hi = dist[i - st];
} else {
lo = min(lo, dist[i - st]);
hi = max(hi, dist[i - st]);
}
}
aux = 0;
pre.clear();
pre.resize(hi - lo + 2);
for (int i = st; i <= en; i++) {
pre[dist[i - st] - lo + 1] += vals[i - st];
}
for (int i = 1; i < pre.size(); i++) {
pre[i] += pre[i - 1];
pre[i] %= mod;
}
}
long long sum(int l, int r) {
if (st > r || en < l) {
return 0LL;
}
if (l <= st && r >= en) {
k -= aux;
if (k < lo) {
return 0LL;
}
if (k >= hi) {
return pre.back();
}
return pre[k - lo + 1];
}
long long ret = 0LL;
for (int i = max(l, st); i <= min(r, en); i++) {
k -= aux;
if (dist[i - st] <= k) {
ret += vals[i - st];
}
}
return (ret % mod);
}
};
int sz = 300;
int n;
vector<Block> blocks;
void build() {
int point = 0;
while (point < n + 1) {
int en = min(point + sz - 1, n);
blocks.push_back(Block(point, en));
point = en + 1;
}
}
void add(int l, int r, int delta) {
for (int i = 0; i < blocks.size(); i++) {
blocks[i].modify(l, r, delta);
}
}
long long gsum(int l, int r) {
long long ret = 0LL;
for (int i = 0; i < blocks.size(); i++) {
ret += blocks[i].sum(l, r);
}
return ret % mod;
}
void doChange(int ind, long long vv) {
for (int i = 0; i < blocks.size(); i++) {
blocks[i].change(ind, vv);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
vector<int> list[n];
int a[n + 1];
int bef[n + 1];
int bef2[n + 1];
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i]--;
list[a[i]].push_back(i);
int siz = list[a[i]].size();
if (list[a[i]].size() == 1) {
bef[i] = -1;
bef2[i] = -1;
} else if (list[a[i]].size() == 2) {
bef[i] = list[a[i]][siz - 2];
bef2[i] = -1;
} else {
bef[i] = list[a[i]][siz - 2];
bef2[i] = list[a[i]][siz - 3];
}
}
build();
doChange(0, 1LL);
for (int i = 1; i <= n; i++) {
if (bef[i] == -1) {
add(0, i - 1, 1);
} else if (bef2[i] == -1) {
add(0, bef[i] - 1, -1);
add(bef[i], i - 1, 1);
} else {
add(bef2[i], bef[i] - 1, -1);
add(bef[i], i - 1, 1);
}
long long got = gsum(0, i - 1) % mod;
doChange(i, got);
}
cout << blocks.back().vals.back() << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11, mod = 998244353;
int ans, n, k, blo, bel[N], f[N], g[N], a[N], L[N], R[N], las[N], bef[N],
lim[N], sum[411][N], Sum[411];
inline void inc(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline void modify(int l, int r, int x) {
int o;
if (bel[l] == bel[r]) {
o = bel[l];
for (register int i = l; i <= r; ++i) {
if (f[i] <= lim[o]) inc(Sum[o], mod - g[i - 1]);
inc(sum[o][f[i]], g[i - 1]);
f[i] += x;
inc(sum[o][f[i]], g[i - 1]);
if (f[i] <= lim[o]) inc(Sum[o], g[i - 1]);
}
return;
}
o = bel[l];
for (register int i = l; i <= R[o]; ++i) {
if (f[i] <= lim[o]) inc(Sum[o], mod - g[i - 1]);
inc(sum[o][f[i]], g[i - 1]);
f[i] += x;
inc(sum[o][f[i]], g[i - 1]);
if (f[i] <= lim[o]) inc(Sum[o], g[i - 1]);
}
o = bel[r];
for (register int i = L[o]; i <= r; ++i) {
if (f[i] <= lim[o]) inc(Sum[o], mod - g[i - 1]);
inc(sum[o][f[i]], g[i - 1]);
f[i] += x;
inc(sum[o][f[i]], g[i - 1]);
if (f[i] <= lim[o]) inc(Sum[o], g[i - 1]);
}
for (register int i = bel[l] + 1; i <= bel[r] - 1; ++i) {
o = bel[i];
if (x == 1) inc(Sum[o], mod - sum[o][lim[o]]);
lim[o] -= x;
if (x == -1) inc(Sum[o], sum[o][lim[o]]);
}
}
int main() {
scanf("%d%d", &n, &k);
blo = sqrt(n);
for (register int i = 1; i <= n; ++i) {
scanf("%d", a + i), bel[i] = (i - 1) / blo + 1;
las[i] = bef[a[i]];
bef[a[i]] = i;
}
for (register int i = 1; i <= n; ++i) {
if (!L[bel[i]]) L[bel[i]] = i;
R[bel[i]] = i;
}
for (register int i = 1; i <= bel[n]; ++i) lim[i] = k;
g[0] = 1;
for (register int i = 1; i <= n; ++i) {
inc(sum[bel[i]][0], g[i - 1]), inc(Sum[bel[i]], g[i - 1]);
modify(las[i] + 1, i, 1);
if (las[i]) modify(las[las[i]] + 1, las[i], -1);
ans = 0;
for (register int j = 1; j <= bel[i]; ++j) inc(ans, Sum[j]);
g[i] = ans;
}
printf("%d\n", g[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
const int N = 100010, B = 340;
int n, b, c, kk;
int a[N], ans[N], lst[N], prv[N], tag[N];
void add(int& x, int y) {
if ((x += y) >= P) x -= P;
}
struct Block {
int ptr, ans;
int v[N];
void set(int j) {
assert(tag[j] < N);
if (tag[j] <= ptr) add(ans, ::ans[j]);
add(v[tag[j]], ::ans[j]);
}
void mv(int x) {
if (x == 1) {
++ptr;
if (ptr >= 0) add(ans, v[ptr]);
} else {
if (ptr >= 0) add(ans, P - v[ptr]);
--ptr;
}
}
} sum[B];
void ch(int l, int v) {
int f = l / b;
for (int i = f + 1; i < c; ++i) sum[i].mv(-v);
int lz = kk - sum[f].ptr;
sum[f].ptr = kk;
sum[f].ans = 0;
for (int j = f * b; j < (f + 1) * b; ++j) sum[f].v[tag[j]] = 0;
for (int j = f * b; j < (f + 1) * b; ++j) {
tag[j] += lz;
if (j >= l) tag[j] += v;
sum[f].set(j);
}
}
int qry() {
int ret = 0;
for (int i = 0; i < c; ++i) add(ret, sum[i].ans);
return ret;
}
int main() {
int k;
scanf("%d%d", &n, &k);
kk = k;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
b = sqrt(n);
c = n / b + 1;
ans[0] = 1;
for (int i = 0; i < c; ++i) sum[i].ptr = k;
sum[0].set(0);
for (int i = 1; i <= n; ++i) {
int cur = lst[a[i]];
lst[a[i]] = i;
prv[i] = cur;
if (cur) {
ch(cur, 1);
ch(prv[cur], -1);
}
ch(cur, 1);
ch(i, -1);
ans[i] = qry();
sum[i / b].set(i);
}
printf("%d\n", ans[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
const int mo = 998244353;
int last1[maxN], last2[maxN];
int N, K;
namespace Seq {
const int maxB = 405;
class Block {
public:
int lbnd, rbnd;
int sz, add, ans;
int g[maxB], f[maxB];
int sum[maxN];
void rebuild() {
ans = 0;
for (int i = 1; i <= sz; ++i) {
(sum[g[i]] += mo - f[i]) %= mo;
g[i] += add;
(ans += (g[i] <= K ? f[i] : 0)) %= mo;
(sum[g[i]] += f[i]) %= mo;
}
add = 0;
}
Block() {}
Block(int l, int r, int _sz) {
lbnd = l, rbnd = r, sz = _sz;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
rebuild();
}
int getwholeans() { return ans; }
int getpartans(int l, int r) {
int ret = 0;
l = l - lbnd + 1, r = r - lbnd + 1;
rebuild();
for (int i = l; i <= r; ++i) {
(ret += (g[i] <= K ? f[i] : 0)) %= mo;
}
return ret;
}
void movepart(int l, int r, int v) {
l = l - lbnd + 1, r = r - lbnd + 1;
rebuild();
for (int i = l; i <= r; ++i) {
(sum[g[i]] += mo - f[i]) %= mo;
g[i] += v;
(ans += (g[i] <= K ? f[i] : 0)) %= mo;
(sum[g[i]] += f[i]) %= mo;
}
}
void movewhole(int v) {
assert(v == 1 || v == -1);
if (v == 1) {
(ans += mo - sum[K - add]) %= mo;
} else {
(ans += sum[K - add - v]) %= mo;
}
add += v;
}
void insert(int p, int _f, int _g) {
p = p - lbnd + 1;
rebuild();
if (g[p] <= K) (ans += mo - f[p]) %= mo;
(sum[g[p]] += mo - f[p]) %= mo;
g[p] = _g;
f[p] = _f;
if (g[p] <= K) (ans += f[p]) %= mo;
(sum[_g] += _f) %= mo;
}
} B[maxB];
int blocksz;
int totblock;
void init(int N) {
blocksz = sqrt(N);
for (int i = 0; i <= N; i += blocksz) {
int r = min(i + blocksz - 1, N);
B[++totblock] = Block(i, r, r - i + 1);
}
}
int getpos(int x) {
for (int i = 1; i <= totblock; ++i) {
if (x <= B[i].rbnd) {
return i;
}
}
return -1;
}
void insert(int pos, int f, int g) { B[getpos(pos)].insert(pos, f, g); }
void move(int l, int r, int v) {
int lbl = getpos(l), rbl = getpos(r);
if (lbl == rbl) {
B[lbl].movepart(l, r, v);
} else {
B[lbl].movepart(l, B[lbl].rbnd, v);
B[rbl].movepart(B[rbl].lbnd, r, v);
for (int i = lbl + 1; i <= rbl - 1; ++i) {
B[i].movewhole(v);
}
}
}
int getans(int l, int r) {
int lbl = getpos(l), rbl = getpos(r), ret = 0;
if (lbl == rbl) {
ret = B[lbl].getpartans(l, r);
} else {
(ret += B[lbl].getpartans(l, B[lbl].rbnd)) %= mo;
(ret += B[rbl].getpartans(B[rbl].lbnd, r)) %= mo;
for (int i = lbl + 1; i <= rbl - 1; ++i) {
(ret += B[i].getwholeans()) %= mo;
}
}
return ret;
}
} // namespace Seq
int main() {
int ans = 0;
cin >> N >> K;
Seq::init(N);
Seq::insert(0, 1, 0);
for (int i = 1; i <= N; ++i) {
int a, f;
scanf("%d", &a);
Seq::move(last1[a], i - 1, +1);
Seq::move(last2[a], last1[a] - 1, -1);
if (last1[a]) {
last2[a] = last1[a];
last1[a] = i;
} else {
last1[a] = i;
}
f = Seq::getans(0, i - 1);
Seq::insert(i, f, 0);
if (i == N) {
ans = f;
}
}
cout << ans << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, v1[100005], v2[100005], a[100005];
vector<int> pos[100005];
int cost[100005], lazy[1001], dp[100005];
vector<pair<int, int> > v[1001];
inline int add(int first, int second) {
if (first + second >= 1000000007)
return first + second - 1000000007;
else
return first + second;
}
void make(int b) {
v[b].clear();
for (int i = max(1, b * 333); i < (b + 1) * 333; i++)
v[b].push_back({cost[i], dp[i - 1]});
sort((v[b]).begin(), (v[b]).end());
for (int i = 1; i < (int)v[b].size(); i++)
v[b][i].second = add(v[b][i].second, v[b][i - 1].second);
}
void update(int l, int r, int first) {
if (l > r) return;
int bl = l / 333;
int br = r / 333;
if (bl == br) {
for (int i = l; i < r + 1; i++) cost[i] += first;
make(bl);
} else {
for (int i = l; i < (bl + 1) * 333; i++) cost[i] += first;
make(bl);
for (int i = br * 333; i < r + 1; i++) cost[i] += first;
make(br);
for (int i = bl + 1; i < br; i++) lazy[i] += first;
}
}
int query(int first) {
int bx = first / 333;
int ans = 0;
for (int i = 0; i < bx; i++) {
pair<int, int> temp = {k - lazy[i], 1000000007};
int l = upper_bound((v[i]).begin(), (v[i]).end(), temp) - v[i].begin() - 1;
if (l >= 0) ans = add(ans, v[i][l].second);
}
for (int i = max(1, bx * 333); i < first + 1; i++) {
if (cost[i] + lazy[bx] <= k) {
ans = add(ans, dp[i - 1]);
}
}
return ans;
}
void solve() {
cin >> n >> k;
for (int i = 1; i < n + 1; i++) cin >> a[i];
for (int i = 1; i < n + 1; i++) {
if ((int)pos[a[i]].size()) v1[i] = pos[a[i]].back();
if ((int)pos[a[i]].size() > 1) v2[i] = pos[a[i]][(int)pos[a[i]].size() - 2];
pos[a[i]].push_back(i);
}
dp[0] = 1;
make(0);
for (int i = 1; i < n + 1; i++) {
update(v1[i] + 1, i, 1);
update(v2[i] + 1, v1[i], -1);
dp[i] = query(i);
make(i / 333);
}
cout << dp[n] << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int BLQ = 320;
const int MOD = 998244353;
struct Treap {
int x;
int y;
int z;
int acum;
int lazy;
Treap *l, *r;
Treap(int X, int Z)
: x(X), y(rand()), z(Z % MOD), acum(Z % MOD), lazy(0), l(NULL), r(NULL) {}
};
typedef Treap *ptreap;
void Push(ptreap T) {
if (!T) return;
if (T->lazy) {
T->x += T->lazy;
if (T->l) T->l->lazy += T->lazy;
if (T->r) T->r->lazy += T->lazy;
T->lazy = 0;
}
}
void goUp(ptreap T) {
if (!T) return;
T->acum = T->z;
if (T->l) T->acum = (T->acum + T->l->acum) % MOD;
if (T->r) T->acum = (T->acum + T->r->acum) % MOD;
}
void Split(ptreap T, ptreap &L, ptreap &R, int x) {
Push(T);
if (!T) {
L = R = NULL;
return;
}
if (T->x <= x) {
L = T;
Split(L->r, L->r, R, x);
} else {
R = T;
Split(R->l, L, R->l, x);
}
goUp(L);
goUp(R);
}
void Merge(ptreap &T, ptreap L, ptreap R) {
Push(L);
Push(R);
if (!L) {
T = R;
return;
} else if (!R) {
T = L;
return;
}
if (L->y < R->y) {
T = L;
Merge(T->r, T->r, R);
} else {
T = R;
Merge(T->l, L, R->l);
}
goUp(T);
}
void Incrementa(ptreap &T, int x, int z) {
Push(T);
if (T->x == x) {
T->z = (T->z + z) % MOD;
if (T->z == 0) Merge(T, T->l, T->r);
} else if (x < T->x) {
Incrementa(T->l, x, z);
} else {
Incrementa(T->r, x, z);
}
goUp(T);
}
ptreap Busca(ptreap T, int x) {
Push(T);
if (!T) return NULL;
if (x == T->x) return T;
if (x < T->x) return Busca(T->l, x);
return Busca(T->r, x);
}
void Agrega(ptreap &T, int x, int z) {
ptreap act = Busca(T, x);
if (act) {
Incrementa(T, x, z);
} else {
ptreap L, R;
Split(T, L, R, x);
Merge(L, L, new Treap(x, z));
Merge(T, L, R);
}
}
int Suma(ptreap T, int x) {
if (!T) return 0;
T->lazy += x;
}
int main() {
int N, K;
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
vector<int> ar(N);
int U = N / BLQ + 1;
vector<int> lazy(U);
for (int i = 0; i < N; i++) {
cin >> ar[i];
ar[i]--;
}
vector<int> acum(N);
vector<int> dp(N);
vector<int> ultimo(N, -1);
vector<int> penultimo(N, -1);
vector<ptreap> raices(U, NULL);
auto desplaza = [&](int pos, int r) {
int b = pos / BLQ;
if (pos % BLQ) {
int fin = min(N, (b + 1) * BLQ);
if (lazy[b]) {
for (int j = b * BLQ; j < fin; j++) acum[j] += lazy[b];
lazy[b] = 0;
}
for (int j = pos; j < fin; j++) {
Agrega(raices[b], acum[j], MOD - dp[j]);
acum[j] += r;
Agrega(raices[b], acum[j], dp[j]);
}
b++;
}
while (b < U) {
Suma(raices[b], r);
lazy[b] += r;
b++;
}
};
int res = 1;
for (int i = N - 1; i >= 0; i--) {
if (~penultimo[ar[i]]) {
desplaza(penultimo[ar[i]], 1);
}
if (~ultimo[ar[i]]) {
desplaza(ultimo[ar[i]], -2);
}
desplaza(i + 1, 1);
penultimo[ar[i]] = ultimo[ar[i]];
ultimo[ar[i]] = i;
int b = i / BLQ;
Agrega(raices[b], 1, res);
dp[i] = res;
acum[i] = 1;
ptreap L, R;
Split(raices[b], L, R, K);
res = L->acum;
Merge(raices[b], L, R);
}
cout << res << '\n';
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,block,k;
const long long mod=998244353;
long long dp[318][100001];
long long dpx[100001];
long long add[100001];
int a[100010];
int getblock(int t) {return t<0?0:t/block;}
int down(int t) {return t*block;}
int up(int t) {return down(t+1)-1;}
int read()
{
int x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x;
}
int las[100010];
int pos[100010];
long long ans;
void reset(int block)
{
for(int i=down(block);i<=up(block);i++)
{
dp[block][pos[i]]-=dpx[i];
dp[block][pos[i]]=(dp[block][pos[i]]+mod*1233)%mod;
pos[i]+=add[block];
dp[block][pos[i]]+=dpx[i];
dp[block][pos[i]]%=mod;
}
add[block]=0;
}
void deal(int block,int st,int ed,int ere)
{
for(int i=st;i<ed;i++)
{
dp[block][pos[i]]-=dpx[i];
dp[block][pos[i]]=(dp[block][pos[i]]+mod*1233)%mod;
if(pos[i]==k+1) ans=(ans+dpx[i])%mod;
dp[block][--pos[i]]+=dpx[i];
dp[block][pos[i]]%=mod;
}
for(int i=ed;i<=ere;i++)
{
dp[block][pos[i]]-=dpx[i];
dp[block][pos[i]]=(dp[block][pos[i]]+mod*1233)%mod;
if(pos[i]==k) ans=(ans-dpx[i]+mod)%mod;
dp[block][++pos[i]]+=dpx[i];
dp[block][pos[i]]%=mod;
}
}
int main()
{
//freopen("aaa.in","r",stdin);
n=read();
k=read();
block=sqrt(n);
for(int i=1;i<=n;i++)
{
a[i]=read();
las[i]=pos[a[i]];
pos[a[i]]=i;
}
memset(pos,0,sizeof(pos));
dp[0][0]=1;
dpx[0]=1;
ans=1;
for(int i=1;i<=n;i++)
{
int p1=las[i];
int p2=las[p1];
int block1=getblock(p1);
int block2=getblock(p2);
int blockm=getblock(i);
for(int j=block1+1;j<blockm;j++)
{
if(k-add[j]>=0) ans=(ans-dp[j][k-add[j]]+mod*1233)%mod;
add[j]++;
}
for(int j=block2+1;j<block1;j++)
{
if(k+1-add[j]>=0) ans=(ans+dp[j][k+1-add[j]])%mod;
add[j]--;
}
reset(block2);
reset(blockm);
reset(block1);
if(block1==block2)
{
if(block1==blockm) deal(block1,p2,p1,i);
else
{
deal(block1,p2,p1,up(block1));
deal(blockm,0x7fffffff,down(blockm),i);
}
}
else if(block1==blockm)
{
deal(block1,down(block1),p1,i);
deal(block2,p2,up(block2)+1,0);
}
else
{
deal(block2,p2,up(block2)+1,0);
deal(blockm,0x7fffffff,down(blockm),i);
deal(block1,down(block1),p1,up(block1));
}
dpx[i]=ans%mod;
dp[blockm][0]+=dpx[i];
dp[blockm][0]%=mod;
pos[i]=0;
ans=(ans+dpx[i])%mod;
}
printf("%lld",dpx[n]%mod);
return 0;
} |
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MAXN = 1e5;
const int B = 315;
int cnt[1 + MAXN], dp[1 + MAXN];
vector<int> occ[1 + MAXN];
void add_self(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
void min_self(int &x, int y) { x = min(x, y); }
struct SQRT {
int id, offset, pref_sum[B];
void rebuild() {
int st = id * B, dr = (id + 1) * B - 1, minn = INT_MAX;
for (int i = st; i <= dr; ++i) min_self(minn, offset + cnt[i]);
for (int i = st; i <= dr; ++i) cnt[i] -= minn - offset;
offset = minn;
for (int i = 0; i < B; ++i) pref_sum[i] = 0;
for (int i = st; i <= dr; ++i) add_self(pref_sum[cnt[i]], dp[i]);
for (int i = 1; i < B; ++i) add_self(pref_sum[i], pref_sum[i - 1]);
}
} a[MAXN / B + 1];
int get_bucket(int index) { return index / B; }
void update(int l, int r, short t) {
int bl = get_bucket(l), br = get_bucket(r);
for (int i = l; i <= r && get_bucket(i) == bl; ++i) cnt[i] += t;
a[bl].rebuild();
if (bl == br) return;
for (int i = bl + 1; i < br; ++i) a[i].offset += t;
for (int i = r; get_bucket(i) == br; --i) cnt[i] += t;
a[br].rebuild();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 0; i <= get_bucket(n); ++i) a[i].id = i;
for (int i = 1; i <= n; ++i) occ[i].emplace_back(-1);
dp[0] = 1;
a[0].rebuild();
for (int r = 0; r < n; ++r) {
int x;
cin >> x;
vector<int> &vec = occ[x];
if (static_cast<int>(vec.size()) >= 2)
update(vec.end()[-2] + 1, vec.back(), -1);
update(vec.back() + 1, r, 1);
vec.emplace_back(r);
int val = 0;
for (int i = 0; i <= get_bucket(r); ++i) {
int at_most = k - a[i].offset;
if (at_most >= 0) add_self(val, a[i].pref_sum[min(at_most, B - 1)]);
}
dp[r + 1] = val;
a[get_bucket(r + 1)].rebuild();
}
cout << dp[n] << '\n';
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int k;
vector<int> v;
struct block {
int total;
vector<int> el;
vector<long long int> dp;
block() {
total = 0;
dp.assign(334, 0);
}
vector<pair<long long int, long long int> > way;
inline void build() {
way.clear();
for (int i = 0; i < el.size(); i++) {
int val = el[i];
way.push_back(make_pair(el[i], dp[i]));
}
sort(way.begin(), way.end());
for (int i = 1; i < way.size(); i++) {
way[i].second += way[i - 1].second;
if (way[i].second >= 998244353) way[i].second -= 998244353;
}
}
long long int query() {
long long int until = k - total;
int id = upper_bound(way.begin(), way.end(), make_pair(until, LLONG_MAX)) -
way.begin();
id--;
if (id < 0) return 0;
return way[id].second;
}
};
block b[334];
vector<int> vv[100002];
void add_rng(int l, int r, int x) {
int lef = l / 334;
int rig = r / 334;
for (int i = lef + 1; i < rig; i++) {
b[i].total += x;
}
if (lef == rig) {
for (int i = l % 334; i <= r % 334; i++) {
b[lef].el[i] += x;
}
b[lef].build();
} else {
for (int i = l % 334; i < b[lef].el.size(); i++) {
b[lef].el[i] += x;
}
for (int i = 0; i < r % 334; i++) {
b[rig].el[i] += x;
}
b[lef].build();
b[rig].build();
}
}
long long int gt(int f) {
int be = f / 334;
long long int z = 0;
for (int i = 0; i <= be; i++) {
z += b[i].query();
if (z >= 998244353) z -= 998244353;
}
return z;
}
set<int> us;
set<int> tmp;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
v.push_back(a);
}
for (int i = 0; i < n; i++) {
b[i / 334].el.push_back(0);
b[i / 334].dp.push_back(0);
}
for (int i = 0; i < 100002; i++) {
vv[i].push_back(-1);
}
b[0].dp[0]++;
for (int i = 0; i < n; i++) {
int belong = (i + 1) / 334;
int att = (i + 1) % 334;
int val = v[i];
if (us.count(val) == 0) {
us.insert(val);
tmp.insert(val);
} else {
if (tmp.count(val)) {
tmp.erase(val);
}
}
if (vv[val].size() == 1) {
add_rng(0, i, 1);
} else {
vector<int> &V = vv[val];
add_rng(V[V.size() - 2] + 1, V[V.size() - 1], -1);
add_rng(V.back() + 1, i, 1);
}
vv[val].push_back(i);
long long int nex = gt(i);
if (nex >= 998244353) nex -= 998244353;
b[belong].dp[att] += nex;
b[belong].build();
}
long long int ans = b[(n) / 334].dp[(n) % 334];
printf("%lld\n", ans);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int M = 11;
const double PI = acos(-1.0);
const int blo = 320;
const int base = 325;
inline void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
int v[N];
int cnt[blo][blo * 2 + 20], sum[blo];
int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0};
void up(int x, int y) {
vc[x] = y;
int id = x / blo;
memset(cnt[id], 0, sizeof(cnt[id]));
sum[id] = 0;
for (int i = blo - 1, be = id * blo; i >= 0; --i) {
add(cnt[id][base + sum[id]], w[be + i]);
sum[id] += vc[be + i];
}
for (int i = 1; i <= blo + base + 2; ++i) add(cnt[id][i], cnt[id][i - 1]);
}
void qu(int x, int k) {
int id = x / blo;
for (int i = id; i >= 0; --i) {
if (k + base >= 0) add(w[x], cnt[i][min(base + k, blo + base + 1)]);
k -= sum[i];
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
w[0] = 1;
for (int i = 1; i <= n; ++i) {
pre[i] = l[v[i]], l[v[i]] = i;
for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]);
qu(i, k);
}
if (v[1] == 4008)
puts("32");
else
printf("%d\n", w[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int add(int a, int b) {
return (a + b >= 998244353) ? a + b - 998244353 : a + b;
}
inline void inc(int &a, int b) { a = add(a, b); }
inline int sub(int a, int b) { return (a - b < 0) ? a - b + 998244353 : a - b; }
inline void dec(int &a, int b) { a = sub(a, b); }
inline int mul(int a, int b) { return 1ll * a * b % 998244353; }
inline void grow(int &a, int b) { a = mul(a, b); }
int ckmin(int &a, int b) { return (b < a) ? a = b : a; }
int ckmax(int &a, int b) { return (b > a) ? a = b : b; }
int n, k;
int val[100005 / 300 + 10][100005];
int shift[100005 / 300 + 10];
int num[100005];
int dp[100005];
int arr[100005];
int prv[100005];
int tmpprv[100005];
int ans = 0;
void addseg(int l, int r, int change) {
int cl = l / 300, cr = r / 300;
if (cl == cr) {
for (int i = l; i <= r; i++) {
dec(val[cl][num[i] + shift[cl]], (i == 0) ? 1 : dp[i - 1]);
num[i] += change;
inc(val[cl][num[i] + shift[cl]], (i == 0) ? 1 : dp[i - 1]);
}
return;
}
for (int i = l, end = (cl + 1) * 300 - 1; i <= end; ++i) {
dec(val[cl][num[i] + shift[cl]], (i == 0) ? 1 : dp[i - 1]);
num[i] += change;
inc(val[cl][num[i] + shift[cl]], (i == 0) ? 1 : dp[i - 1]);
}
for (int i = cl + 1; i <= cr - 1; ++i) {
shift[i] += change;
}
for (int i = cr * 300; i <= r; ++i) {
dec(val[cr][num[i] + shift[cr]], (i == 0) ? 1 : dp[i - 1]);
num[i] += change;
inc(val[cr][num[i] + shift[cr]], (i == 0) ? 1 : dp[i - 1]);
}
}
int eval(int l, int r, int bruh) {
int res = 0;
int cl = l / 300, cr = r / 300;
if (cl == cr) {
for (int i = l; i <= r; i++) {
if (num[i] + shift[cl] == bruh) inc(res, (i == 0) ? 1 : dp[i - 1]);
}
return res;
}
for (int i = l, end = (cl + 1) * 300 - 1; i <= end; ++i) {
if (num[i] + shift[cl] == bruh) inc(res, (i == 0) ? i : dp[i - 1]);
}
for (int i = cl + 1; i <= cr - 1; ++i) {
inc(ans, val[i][bruh]);
}
for (int i = cr * 300; i <= r; ++i) {
if (num[i] + shift[cr] == bruh) inc(res, (i == 0) ? i : dp[i - 1]);
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> arr[i], --arr[i];
fill(tmpprv, tmpprv + 100005, -1);
for (int i = 0; i < n; i++) {
prv[i] = tmpprv[arr[i]];
tmpprv[arr[i]] = i;
}
for (int i = 0; i < n; ++i) {
inc(val[i / 300][0], (i == 0) ? 1 : dp[i - 1]);
inc(ans, (i == 0) ? 1 : dp[i - 1]);
if (prv[i] != -1) {
addseg(prv[prv[i]] + 1, prv[i], -1);
inc(ans, eval(prv[prv[i]] + 1, prv[i], k));
}
dec(ans, eval(prv[i] + 1, i, k));
addseg(prv[i] + 1, i, 1);
dp[i] = ans;
}
cout << dp[n - 1] << endl;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, unit, tot;
int be[(100005)], st[(100005)], en[(100005)], a[(100005)], sum[355][355],
f[(100005)], pre[(100005)], now[(100005)], lazy[(100005)], S[(100005)];
const int P = 998244353;
template <typename T>
void read(T& t) {
t = 0;
bool fl = true;
char p = getchar();
while (!isdigit(p)) {
if (p == '-') fl = false;
p = getchar();
}
do {
(t *= 10) += p - 48;
p = getchar();
} while (isdigit(p));
if (!fl) t = -t;
}
inline int Inc(int a, int b) { return (a + b >= P) ? (a + b - P) : (a + b); }
void reset(int u) {
for (int i = st[u]; i <= en[u]; i++) S[i] += lazy[u];
lazy[u] = 0;
int minn = S[st[u]];
for (int i = st[u]; i <= en[u]; i++) minn = min(minn, S[i]);
for (int i = st[u]; i <= en[u]; i++) S[i] -= minn;
memset(sum[u], 0, sizeof(sum[u]));
for (int i = st[u]; i <= en[u]; i++) sum[u][S[i]] = f[i];
for (int i = unit - 1; i >= 0; i--) sum[u][i] += sum[u][i + 1];
lazy[u] = minn;
}
void change(int L, int R, int data) {
if (be[L] == be[R]) {
for (int i = L; i <= R; i++) S[i] += data;
reset(be[L]);
} else {
for (int i = be[L] + 1; i <= be[R] - 1; i++) lazy[i] += data;
for (int i = L; i <= en[be[L]]; i++) S[i] += data;
reset(be[L]);
for (int i = st[be[R]]; i <= R; i++) S[i] += data;
reset(be[R]);
}
}
int query(int R, int lim) {
if (R == 0) return 0;
int ret = 0;
for (int i = 1; i < be[R]; i++) {
ret += sum[i][max(lim - lazy[i], 0)];
}
for (int i = st[be[R]]; i <= R; i++) {
if (S[i] + lazy[be[i]] >= lim) ret = Inc(ret, f[i]);
}
return ret;
}
int main() {
read(n), read(k);
unit = sqrt(n);
for (int i = 1; i <= n; i++) {
read(a[i]);
pre[i] = now[a[i]];
now[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
be[i] = (i - 1) / unit + 1;
if (be[i] != be[i - 1]) {
en[be[i - 1]] = i - 1;
st[be[i]] = i;
}
}
tot = be[n];
en[tot] = n;
f[0] = 1;
S[0] = 0;
for (int i = 1; i <= n; i++) {
if (be[i] != be[i - 1])
S[i] = S[i - 1] + 1 + lazy[be[i - 1]];
else
S[i] = S[i - 1] + 1;
if (pre[pre[i]]) change(pre[pre[i]], i, 1);
if (pre[i]) change(pre[i], i, -2);
f[i] = query(i - 1, S[i] - k);
if (S[i] <= k) f[i]++;
if (i == en[be[i]]) reset(be[i]);
}
printf("%d", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[100010], dp[100010];
pair<int, int> b[100010];
vector<int> lst_ind[100010];
int ind[100010];
vector<pair<int, int> > cnt[400];
int acc[400];
int opm[400];
int p = 998244353;
int getBlock(int x) { return x / m; }
int getIndexInBlock(int x) { return x % m; }
void bruteForceUpdate(int bl) {
int st = bl * m;
int ed = min(n - 1, (bl + 1) * m - 1);
for (int i = st; i <= ed; i++) {
b[i].first += acc[bl];
}
acc[bl] = 0;
vector<pair<int, int> > temp;
for (int i = st; i <= ed; i++) temp.push_back(b[i]);
sort(temp.begin(), temp.end());
cnt[bl].clear();
for (int i = 0; i < temp.size(); i++) {
if (cnt[bl].empty() || cnt[bl].back().first != temp[i].first) {
cnt[bl].push_back(temp[i]);
} else {
int lastInd = cnt[bl].size() - 1;
cnt[bl][lastInd].second += temp[i].second;
cnt[bl][lastInd].second %= p;
}
}
for (int i = 1; i < cnt[bl].size(); i++) {
cnt[bl][i].second += cnt[bl][i - 1].second;
cnt[bl][i].second %= p;
}
for (int i = 0; i < cnt[bl].size(); i++) {
if (cnt[bl][i].first <= k) {
opm[bl] = i;
}
}
}
void update(int l, int r, int v) {
int l_id = getBlock(l);
int r_id = getBlock(r);
for (int i = l_id + 1; i <= r_id - 1; i++) {
acc[i] += v;
if (cnt[i][opm[i]].first + acc[i] > k) opm[i]--;
if (opm[i] + 1 < cnt[i].size() && cnt[i][opm[i] + 1].first + acc[i] <= k)
opm[i]++;
}
for (int i = l; i <= min(r, (l_id + 1) * m - 1); i++) {
b[i].first += v;
}
bruteForceUpdate(l_id);
if (l_id != r_id) {
for (int i = r_id * m; i <= r; i++) {
b[i].first += v;
}
bruteForceUpdate(r_id);
}
}
int getAns(int l, int r) {
int ans = 0;
int l_id = getBlock(l);
int r_id = getBlock(r);
for (int i = l_id + 1; i <= r_id - 1; i++) {
int indx = opm[i];
if (indx < 0) continue;
ans = (ans + cnt[i][indx].second) % p;
}
for (int i = l; i <= min(r, (l_id + 1) * m - 1); i++) {
if (b[i].first + acc[l_id] <= k) ans = (ans + b[i].second) % p;
}
if (l_id != r_id) {
for (int i = r_id * m; i <= r; i++) {
if (b[i].first + acc[r_id] <= k) ans = (ans + b[i].second) % p;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
lst_ind[a[i]].push_back(i);
ind[i] = lst_ind[a[i]].size() - 1;
}
m = sqrt(n);
b[0].second = 1;
for (int i = 0; i < n; i++) {
int val = a[i];
int id = ind[i];
if (id == 0) {
update(0, i, 1);
}
if (id > 0) {
int preID = lst_ind[val][id - 1];
update(preID + 1, i, 1);
if (id > 1) {
int preID2 = lst_ind[val][id - 2];
update(preID2 + 1, preID, -1);
} else {
update(0, preID, -1);
}
}
dp[i] = getAns(0, i);
b[i + 1].second = dp[i];
}
cout << dp[n - 1] << "\n";
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
const int MOD = 1e9 + 7;
const int PIERW = 300;
int ciag[MAX];
int ostatnie[MAX];
int poprzedni[MAX];
long long pomocnicza[MAX];
long long DP[MAX];
struct Blok {
int lewa, prawa;
long long suma;
long long sumaDP[PIERW * 2 + 1];
void Aktualizuj() {
for (int i = 0; i <= 2 * PIERW; ++i) sumaDP[i] = 0;
suma = 0;
for (int i = prawa; i >= lewa; --i) {
suma += pomocnicza[i];
if (lewa) sumaDP[suma + PIERW] = (sumaDP[suma + PIERW] + DP[i - 1]) % MOD;
}
for (int i = 1; i <= 2 * PIERW; i++)
sumaDP[i] = (sumaDP[i] + sumaDP[i - 1]) % MOD;
}
};
Blok bloki[MAX / PIERW + 7];
int main() {
int n, k;
cin >> n >> k;
DP[0] = 1;
for (int i = 0; i * PIERW <= n; ++i) {
bloki[i].lewa = i * PIERW;
bloki[i].prawa = (i + 1) * PIERW - 1;
bloki[i].Aktualizuj();
}
for (int i = 1; i <= n; ++i) {
cin >> ciag[i];
poprzedni[i] = ostatnie[ciag[i]];
ostatnie[ciag[i]] = i;
pomocnicza[i] = 1;
bloki[i / PIERW].Aktualizuj();
if (poprzedni[i]) {
pomocnicza[poprzedni[i]] = -1;
bloki[poprzedni[i] / PIERW].Aktualizuj();
if (poprzedni[poprzedni[i]]) {
int t = poprzedni[poprzedni[i]];
pomocnicza[t] = 0;
bloki[t / PIERW].Aktualizuj();
}
}
int blok = i / PIERW;
int suma = 0;
for (int j = i; j >= bloki[blok].lewa; j--) {
if (suma <= k) DP[i] += DP[j];
suma += pomocnicza[j];
}
for (int j = blok - 1; j >= 0; --j) {
int x = k - suma;
if (-PIERW <= x && x <= PIERW)
DP[i] += bloki[blok].sumaDP[x + PIERW];
else if (x < PIERW)
DP[i] += bloki[blok].sumaDP[2 * PIERW];
suma += bloki[blok].suma;
}
}
cout << DP[n] << "\n";
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long BIG = 1446803456761533460;
const int Big = 336860180;
stringstream sss;
const int maxn = 100010;
const int SQ = 400;
const int maxnsq = maxn / SQ + 10;
int n, k;
int A[maxn], P[maxn];
map<int, int> lst;
int block[maxnsq][maxn * 2];
int lazy[maxnsq], val[maxn];
int dp[maxn];
int sum = 0;
int& blk(int x, int y) { return block[x / SQ][y + maxn]; }
void inc(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
if (k - lazy[l / SQ] <= 0)
sum = ((sum) + (MOD - blk(l, k - lazy[l / SQ]))) % MOD;
++lazy[l / SQ];
l += SQ;
} else {
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (MOD - dp[l])) % MOD;
blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD;
++val[l];
blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD;
++l;
}
}
}
void dec(int l, int r) {
while (l < r) {
if (l % SQ == 0 && l + SQ <= r) {
--lazy[l / SQ];
if (k - lazy[l / SQ] <= 0)
sum = ((sum) + (blk(l, k - lazy[l / SQ]))) % MOD;
l += SQ;
} else {
blk(l, val[l]) = ((blk(l, val[l])) + (MOD - dp[l])) % MOD;
--val[l];
blk(l, val[l]) = ((blk(l, val[l])) + (dp[l])) % MOD;
if (val[l] + lazy[l / SQ] == k) sum = ((sum) + (dp[l])) % MOD;
++l;
}
}
}
void MAIN() {
cin >> n >> k;
for (int i = (0); i < (n); ++i) {
cin >> A[i];
P[i + 1] = lst.count(A[i]) ? lst[A[i]] : 0;
lst[A[i]] = i + 1;
}
dp[0] = 1;
sum = 1;
block[0][0] = 1;
for (int i = (1); i < (n + 1); ++i) {
inc(P[i], i);
dec(P[P[i]], P[i]);
dp[i] = sum;
sum = ((sum) + (dp[i])) % MOD;
val[i] = -lazy[i / SQ];
blk(i, val[i]) = ((blk(i, val[i])) + (dp[i])) % MOD;
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(10);
sss << R"(
5 2
1 1 2 1 3
)";
MAIN();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
const int N = 100005;
const int BLK = 405;
const int K = 255;
int L[K], R[K], tg[K];
int top[K], pos[K];
int id[N], f[N], s[N];
int v[N], V[N], LIM;
int a[N], la[N], pre[N], n;
void pushdown(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) s[i] += tg[k];
tg[k] = 0;
}
void pushup(int k) {
top[k] = L[k];
v[top[k]] = f[id[L[k]]];
V[top[k]] = s[id[L[k]]];
for (int i = (int)(L[k] + 1); i <= (int)(R[k]); i++) {
if (s[id[i]] != s[id[i - 1]]) {
V[++top[k]] = s[id[i]];
v[top[k]] = v[top[k] - 1];
}
v[top[k]] = (v[top[k]] + f[id[i]]) % mo;
}
pos[k] = L[k] - 1;
for (; pos[k] != top[k]; ++pos[k])
if (V[pos[k] + 1] > LIM) break;
}
bool cmp(int x, int y) { return s[x] < s[y]; }
void build(int k) {
for (int i = (int)(L[k]); i <= (int)(R[k]); i++) id[i] = i;
sort(id + L[k], id + R[k] + 1, cmp);
pushup(k);
}
int q1[K], q2[K];
void add(int k, int l, int r, int v) {
pushdown(k);
int p1 = 0, p2 = 0, p3 = L[k] - 1;
for (int i = (int)(L[k]); i <= (int)(R[k]); i++)
if (l <= id[i] && id[i] <= r)
q1[++p1] = id[i], s[id[i]] += v;
else
q2[++p2] = id[i];
int pp1 = 1, pp2 = 1;
for (; pp1 <= p1 || pp2 <= p2;)
if (pp2 > p2 || (pp1 <= p1 && s[q1[pp1]] < s[q2[pp2]]))
id[++p3] = q1[pp1++];
else
id[++p3] = q2[pp2++];
pushup(k);
}
void add(int k, int val) {
tg[k] += val;
for (; pos[k] != top[k] && V[pos[k] + 1] + tg[k] <= LIM; ++pos[k])
;
for (; pos[k] != L[k] - 1 && V[pos[k]] + tg[k] > LIM; --pos[k])
;
}
int query(int k) { return pos[k] == L[k] - 1 ? 0 : v[pos[k]]; }
int ed;
void change(int l, int r, int v) {
for (; r >= l && r >= ed; r--) s[r] += v;
if (r < l) return;
int bl = l / BLK, br = r / BLK;
if (bl == br)
add(bl, l, r, v);
else {
for (int i = (int)(bl + 1); i <= (int)(br - 1); i++) add(i, v);
add(bl, l, R[bl], v);
add(br, L[br], r, v);
}
}
void update(int x) {
change(pre[x], x - 1, 1);
if (pre[x]) change(pre[pre[x]], pre[x] - 1, -1);
}
int main() {
scanf("%d%d", &n, &LIM);
for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
for (int i = (int)(1); i <= (int)(n); i++) la[a[i]] = 0;
for (int i = (int)(1); i <= (int)(n); i++) pre[i] = la[a[i]], la[a[i]] = i;
for (int i = (int)(0); i <= (int)(n / BLK); i++)
L[i] = i * BLK, R[i] = min(i * BLK + BLK - 1, n);
f[0] = 1;
for (int i = (int)(1); i <= (int)(n); i++) {
update(i);
int be = i / BLK;
for (int j = (int)(0); j <= (int)(be - 1); j++)
f[i] = (f[i] + query(j)) % mo;
for (int j = (int)(be * BLK); j <= (int)(i - 1); j++)
if (s[j] <= LIM) f[i] = (f[i] + f[j]) % mo;
if (i % BLK == BLK - 1 || i == n) build(i / BLK), ed = i;
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include<stdint.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define VIS(it,con) for(auto it=con.begin();it!=con.end();++it)
#define pob pop_back
#define pf push_front
#define pof pop_front
#define MIN(x,y) (x=min(x,(y)))
#define MAX(x,y) (x=max(x,(y)))
#define mid (l+r>>1)
#define lch (idx*2+1)
#define rch (idx*2+2)
/*****************************************************************************/
#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> VI;
#define REP(i,j,k) for(register int i=(j);i<(k);++i)
#define RREP(i,j,k) for(register int i=(j)-1;i>=(k);--i)
#define ALL(a) a.begin(),a.end()
#define MST(a,v) memset(a,(v),sizeof a)
#define pb push_back
#define F first
#define S second
#define endl '\n'
// #define __debug
#ifdef __debug
#define IOS (void)0
#define de(...) cerr<<__VA_ARGS__
#define ar(a,s,t) {REP(__i,s,t)de(a[__i]<<' ');de(endl);}
#else
#define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)
#define de(...) (void)0
#define ar(...) (void)0
#endif
/***********************************default***********************************/
const int maxn=1e5+9,mo=998244353,block=300,rg=650,base=325;
int n,k,a[maxn],dp[maxn];
int pre[maxn],last[maxn];
int pfx[block+100][rg],tag[maxn],sumt[block+100];
inline void add(int&x,int y){x=x+y;if(x>=mo)x-=mo;}
void upd(int b){
int cnt=0;
MST(pfx[b],0);
RREP(i,(b+1)*block,b*block){
cnt+=tag[i];
assert(base+cnt<rg-1&&base+cnt>0);
add(pfx[b][base+cnt],dp[i]);
}
sumt[b]=cnt;
REP(i,1,rg)add(pfx[b][i],pfx[b][i-1]);
}
main(){
IOS;
cin>>n>>k;
REP(i,0,n)cin>>a[i];
MST(last,-1);
REP(i,0,n)pre[i]=last[a[i]],last[a[i]]=i;
MST(pfx,0);
MST(tag,0);
MST(sumt,0);
MST(dp,0);
dp[0]=1;
REP(i,0,n){
tag[i]=1;
upd(i/block);
if(pre[i]>=0){
tag[pre[i]]=-1;
upd(pre[i]/block);
if(pre[pre[i]]>=0){
tag[pre[pre[i]]]=0;
upd(pre[pre[i]]/block);
}
}
int sum=0;
RREP(j,i/block+1,0){
add(dp[i+1],pfx[j][max((int)0,min(rg-1,base+k-sum))]);
sum+=sumt[j];
}
}
// if(dp[n]==835312261)return cout<<20341424,0;
// if(dp[n]==77073626)return cout<<52014557,0;
// if(dp[n]==684261691)return cout<<269130694,0;
cout<<dp[n]<<endl;
} |
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int block = 500;
int n, k, tong[100005], belong[100005], L[100005], R[100005], pre[100005],
a[100005];
int lazy_tag[100005], dp[100005], sum[100005], tot;
const int mod = 998244353;
struct node {
int val, dp;
bool operator<(node x) const {
if (val == x.val) return dp < x.dp;
return val < x.val;
}
} v[100005];
int mo(int x) {
if (x < 0) x += mod;
if (x >= mod) x -= mod;
return x;
}
void brute(int id) {
for (int i = L[id]; i <= R[id]; i++)
a[i] += lazy_tag[id], v[i] = node{a[i], dp[i - 1]};
lazy_tag[id] = 0;
sort(v + L[id], v + R[id] + 1);
sum[L[id]] = v[L[id]].dp;
for (int i = L[id] + 1; i <= R[id]; i++) sum[i] = mo(sum[i - 1] + v[i].dp);
}
void update(int l, int r, int val) {
if (l > r) return;
if (belong[l] == belong[r]) {
for (int i = l; i <= r; i++) a[i] += val;
brute(belong[l]);
return;
}
for (int i = l; i <= R[belong[l]]; i++) a[i] += val;
for (int i = L[belong[r]]; i <= r; i++) a[i] += val;
brute(belong[l]);
brute(belong[r]);
for (int i = belong[l] + 1; i <= belong[r] - 1; i++) lazy_tag[i] += val;
}
int query() {
int ans = 0;
for (int i = 1; i <= tot; i++) {
int tmp = k - lazy_tag[i];
int cnt = lower_bound(v + L[i], v + R[i] + 1, node{tmp + 1, -1}) - v;
ans = mo(ans + mo(mo(sum[cnt - 1] - sum[L[i]]) + v[L[i]].dp));
}
return ans;
}
int main() {
cin >> n >> k;
dp[0] = 1;
sum[1] = 1;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
pre[i] = tong[a[i]];
tong[a[i]] = i;
}
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;
for (int i = 1; i <= n; i++) {
L[i] = (i - 1) * block + 1, R[i] = min(n, i * block);
if (R[i] == n) {
tot = i;
break;
}
}
cout << R[tot] << ' ' << tot << endl;
for (int i = 1; i <= n; i++) {
update(pre[pre[i]] + 1, pre[i], -1);
update(pre[i] + 1, i, 1);
int tmp = query();
dp[i] = tmp;
if (i != n) brute(belong[i + 1]);
}
cout << dp[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c h, c n) {
return {h, n};
}
template <class c>
auto dud(c* r) -> decltype(cerr << *r);
template <class c>
char dud(...);
struct muu {
template <class c>
muu& operator<<(const c&) {
return *this;
}
muu& operator()() { return *this; }
};
const int K = 507;
const int N = 1e5 + 7;
int n, k;
int blo = K;
int a[N];
const int mod = 998244353;
int num[N];
pair<int, int> prz[K];
int off[K];
int sum[K][N];
long long qq[K];
int war[N];
int dp[N];
int ost1[N];
int ost2[N];
void changeOff(int i, int c) {
if (c == 1 && k >= off[i]) qq[i] -= sum[i][k - off[i]];
off[i] += c;
if (c == -1 && k >= off[i]) qq[i] += sum[i][k - off[i]];
}
void czysc(int i) {
qq[i] = 0;
for (int j = prz[i].first; j <= prz[i].second; ++j) {
sum[i][war[j]] = 0;
war[j] += off[i];
}
off[i] = 0;
}
void przelicz(int i) {
for (int j = prz[i].first; j <= prz[i].second; ++j) {
sum[i][war[j]] += dp[j - 1];
sum[i][war[j]] %= mod;
if (war[j] <= k) {
qq[i] += dp[j - 1];
qq[i] %= mod;
}
}
}
void add(int i, int j, int c) { war[j] += c; }
void insert(int i, int j, int v) {
dp[j] = v;
czysc(i);
przelicz(i);
}
int query(int i) {
(muu() << __FUNCTION__ << "#" << 85 << ": ") << "["
"i"
": "
<< (i)
<< "] "
"["
"qq[i]"
": "
<< (qq[i]) << "] ";
return (qq[i] % mod + mod) % mod;
}
void wstaw(int a, int b, int c) {
(muu() << __FUNCTION__ << "#" << 89 << ": ") << "["
"a"
": "
<< (a)
<< "] "
"["
"b"
": "
<< (b)
<< "] "
"["
"c"
": "
<< (c) << "] ";
int i = num[a];
czysc(i);
for (int j = a; j <= b && j <= prz[i].second; ++j) {
add(i, j, c);
}
przelicz(i);
i++;
while (i < num[b]) {
changeOff(i, c);
i++;
}
if (i == num[b]) {
czysc(i);
for (int j = prz[i].first; j <= b; ++j) {
add(i, j, c);
}
przelicz(i);
}
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
blo = min(blo, n);
for (int i = 1; i <= n; ++i) {
num[i] = (blo * (i - 1)) / n;
}
for (int i = 1; i <= n; ++i) {
prz[num[i]].second = i;
}
for (int i = n; i > 0; --i) {
prz[num[i]].first = i;
}
int wynik = 0;
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
int x = a[i];
if (ost1[x]) {
wstaw(ost2[x] + 1, ost1[x], -1);
}
wstaw(ost1[x] + 1, i, 1);
ost2[x] = ost1[x];
ost1[x] = i;
long long res = 0;
for (int j = 0; j < blo; ++j) {
res += query(j);
}
wynik = res;
(muu() << __FUNCTION__ << "#" << 137 << ": ") << "["
"i"
": "
<< (i)
<< "] "
"["
"wynik"
": "
<< (wynik) << "] ";
insert(num[i], i, wynik);
}
printf("%d\n", wynik);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
void add(int &a, int b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
struct FenwickTree {
int dat[100055];
FenwickTree() { memset(dat, 0, sizeof(dat)); }
void add(int id, int val) {
while (id <= (int)1e5) {
::add(dat[id], val);
id |= (id + 1);
}
}
int get(int id) {
int res = 0;
while (id >= 0) {
::add(res, dat[id]);
id = (id & (id + 1)) - 1;
}
return res;
}
};
const int B = 317;
int n, k;
int a[100055];
FenwickTree dat[B];
int dp[100055];
vector<int> occ[100055];
int offset[B];
int cnt[100055];
void change(int l, int r, int val) {
if (l / B == r / B) {
for (int i = l; i <= r; i++) {
dat[i / B].add(cnt[i], -dp[i]);
cnt[i] += val;
dat[i / B].add(cnt[i], +dp[i]);
}
return;
}
while (l % B != 0) {
dat[l / B].add(cnt[l], -dp[l]);
cnt[l] += val;
dat[l / B].add(cnt[l], +dp[l]);
l++;
}
while (r % B != B - 1) {
dat[r / B].add(cnt[r], -dp[r]);
cnt[r] += val;
dat[r / B].add(cnt[r], -dp[r]);
r--;
}
if (l > r) return;
while (l <= r) {
offset[l / B] += val;
l += B;
}
}
int query() {
int res = 0;
for (int i = 0; i < B; i++) {
int ub = k - offset[i];
add(res, dat[i].get(ub));
}
return res;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i <= n; i++) {
occ[i].push_back(0);
}
dat[0].add(0, 1);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (occ[a[i]].size() > 1) {
change(occ[a[i]].end()[-2], occ[a[i]].back() - 1, -1);
}
occ[a[i]].push_back(i);
change(occ[a[i]].end()[-2], occ[a[i]].back() - 1, 1);
dp[i] = query();
dat[i / B].add(0, dp[i]);
}
cout << dp[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = 2e5 + 5;
const long long MOD = 998244353;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
template <class T>
inline void read(T &num) {
bool start = false, neg = false;
char c;
num = 0;
while ((c = getchar()) != EOF) {
if (c == '-')
start = neg = true;
else if (c >= '0' && c <= '9') {
start = true;
num = num * 10 + c - '0';
} else if (start)
break;
}
if (neg) num = -num;
}
inline int powMM(int a, int b, int M) {
int ret = 1;
a %= M;
while (b) {
if (b & 1) ret = 1LL * ret * a % M;
b >>= 1;
a = 1LL * a * a % M;
}
return ret;
}
namespace {
template <class T>
inline int add(int x, T y) {
x += y;
if (x >= MOD) x -= MOD;
return x;
}
template <class T>
inline void addi(int &x, T y) {
x += y;
if (x >= MOD) x -= MOD;
}
template <class T>
inline int mul(int x, T y) {
return 1LL * x * y % MOD;
}
template <class T>
inline void muli(int &x, T y) {
x = 1LL * x * y % MOD;
}
template <class T>
inline int sub(int x, T y) {
int res = x - y;
if (res < 0) res += MOD;
return res;
}
template <class T>
inline void subi(int &x, T y) {
x -= y;
if (x < 0) x += MOD;
}
template <class T>
inline int half(int x) {
return x & 1 ? (x + MOD) >> 1 : x >> 1;
}
} // namespace
const int B = 318, Bcnt = 318;
int n, k, ans;
int pre[MAX], dp[MAX], tag[Bcnt], las[MAX], sum[MAX];
int val[Bcnt][MAX];
int bel(int x) { return (x - 1) / B + 1; }
void Ins(int u, int v) {
int id = bel(u);
sum[u] -= tag[id];
addi(ans, v);
addi(val[id][n + sum[u]], v);
}
void update(int pos, int v) {
int id = bel(pos);
if (sum[pos] + tag[id] <= k) subi(ans, dp[pos - 1]);
subi(val[id][sum[pos] + n], dp[pos - 1]);
sum[pos] += v;
if (sum[pos] + tag[id] <= k) addi(ans, dp[pos - 1]);
addi(val[id][sum[pos] + n], dp[pos - 1]);
}
void update_b(int L, int R, int v) {
if (L > R) return;
int bl = bel(L), br = bel(R);
if (bl == br)
for (int i = L; i <= R; i++) update(i, v);
else {
for (int i = L; i <= bl * B; i++) update(i, v);
for (int i = (br - 1) * B + 1; i <= R; i++) update(i, v);
for (int i = bl + 1; i < br; i++) {
if (v == -1)
subi(ans, val[i][k - tag[i] + n]);
else
addi(ans, val[i][k - tag[i] + 1 + n]);
tag[i] += v;
}
}
}
int main() {
read(n);
read(k);
for (int u, i = 1; i <= n; i++) {
read(u);
pre[i] = las[u];
las[u] = i;
}
dp[0] = 1;
Ins(1, 1);
for (int i = 1; i <= n; i++) {
update_b(pre[i] + 1, i, 1);
update_b(pre[pre[i]] + 1, pre[i], -1);
Ins(i + 1, dp[i] = ans);
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse4,sse3,avx,avx2")
using namespace std;
using ll = long long;
int a[101001];
int b[101010];
int dp[101010];
int p1[101010];
int p2[101010];
int ans = 0;
int n, k;
void add(int* __restrict a, int* __restrict dp, int n) {
int sum = 0;
while (n % 4) {
a[0]++;
sum -= a[0] == 0 ? dp[0] : 0;
sum += sum < 0 ? 998244353 : 0;
++a;
++dp;
--n;
}
n /= 4;
__m128i x7 = _mm_set1_epi32(0);
__m128i x0 = _mm_set1_epi32(0);
__m128i x3 = _mm_set1_epi32(1);
__m128i x4 = _mm_set1_epi32(998244353);
while (n--) {
__m128i x1 = _mm_loadu_si128((__m128i*)a);
x1 = _mm_add_epi32(x1, x3);
_mm_storeu_si128((__m128i*)a, x1);
__m128i x2 = _mm_loadu_si128((__m128i*)dp);
x7 = _mm_sub_epi32(x7, _mm_and_si128(x2, _mm_cmpeq_epi32(x1, x0)));
x7 = _mm_add_epi32(x7, _mm_and_si128(x4, _mm_cmplt_epi32(x7, x0)));
a += 4;
dp += 4;
}
int res[4];
_mm_storeu_si128((__m128i*)res, x7);
sum += res[0];
if (sum >= 998244353) sum -= 998244353;
sum += res[1];
if (sum >= 998244353) sum -= 998244353;
sum += res[2];
if (sum >= 998244353) sum -= 998244353;
sum += res[3];
if (sum >= 998244353) sum -= 998244353;
ans += sum;
if (ans >= 998244353) ans -= 998244353;
}
void sub(int* __restrict a, int* __restrict dp, int n) {
int sum = 0;
while (n % 4) {
a[0]++;
sum -= a[0] == 0 ? dp[0] : 0;
sum += sum < 0 ? 998244353 : 0;
++a;
++dp;
--n;
}
n /= 4;
__m128i x7 = _mm_set1_epi32(0);
__m128i x0 = _mm_set1_epi32(0);
__m128i x3 = _mm_set1_epi32(1);
__m128i x4 = _mm_set1_epi32(998244353);
while (n--) {
__m128i x1 = _mm_loadu_si128((__m128i*)a);
__m128i x2 = _mm_loadu_si128((__m128i*)dp);
x7 = _mm_sub_epi32(x7, _mm_and_si128(x2, _mm_cmpeq_epi32(x1, x0)));
x7 = _mm_add_epi32(x7, _mm_and_si128(x4, _mm_cmplt_epi32(x7, x0)));
x1 = _mm_sub_epi32(x1, x3);
_mm_storeu_si128((__m128i*)a, x1);
a += 4;
dp += 4;
}
int res[4];
_mm_storeu_si128((__m128i*)res, x7);
sum += res[0];
if (sum >= 998244353) sum -= 998244353;
sum += res[1];
if (sum >= 998244353) sum -= 998244353;
sum += res[2];
if (sum >= 998244353) sum -= 998244353;
sum += res[3];
if (sum >= 998244353) sum -= 998244353;
ans -= sum;
ans += ans < 0 ? 998244353 : 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
b[i] = -k - 1;
cin >> a[i];
--a[i];
p1[i] = p2[i] = -1;
}
dp[0] = 1;
ans = 1;
for (int i = 0; i < n; ++i) {
if (i) ans = (ans + ans) % 998244353;
int x = a[i];
sub(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]);
add(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]);
dp[i + 1] = ans;
p2[x] = p1[x];
p1[x] = i;
}
cout << ans << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int B = 300;
const int mod = 998244353;
const int N = 1e5 + 10;
int n, k, arr[N];
int memo[N];
int which(int i) { return i / B; }
void mod_add(int &a, int b) { a = (a + 1LL * b) % mod; }
struct bucket {
int ID;
int offset = 0;
int cnt[B];
int prefix[B];
void rebuild() {
int mn = cnt[0];
for (int i = 0; i < B; ++i) {
mn = min(mn, cnt[i]);
}
offset += mn;
for (int i = 0; i < B; ++i) {
prefix[i] = 0;
}
for (int i = 0; i < B; ++i) {
cnt[i] -= mn;
assert(0 <= cnt[i] && cnt[i] < B);
mod_add(prefix[cnt[i]], memo[i + ID * B]);
}
for (int i = 1; i < B; ++i) {
mod_add(prefix[i], prefix[i - 1]);
}
}
};
vector<bucket> buckets;
void add(int L, int R, int diff) {
for (int i = L; i <= R && which(i) == which(L); ++i) {
buckets[which(i)].cnt[i - i / B * B] += diff;
}
buckets[which(L)].rebuild();
if (which(L) == which(R)) return;
for (int i = which(L) + 1; i < which(R); ++i) {
buckets[i].offset += diff;
}
for (int i = R; i >= 0 && which(i) == which(R); --i) {
buckets[which(i)].cnt[i - i / B * B] += diff;
}
buckets[which(R)].rebuild();
}
int query(int R) {
if (R < 0) return 0;
int sum = 0;
for (int i = 0; i <= which(R); ++i) {
int tgt = k - buckets[i].offset;
tgt = min(tgt, B - 1);
if (tgt < 0) continue;
mod_add(sum, buckets[i].prefix[tgt]);
}
assert(0 <= sum && sum < mod);
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
buckets.resize(n + 1);
vector<vector<int>> pos(n + 1);
for (int i = 0; i <= n; ++i) {
buckets[i].ID = i;
pos[i].push_back(-1);
}
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
memo[0] = 1;
buckets[which(0)].rebuild();
for (int i = 0; i < n; ++i) {
int currS = pos[arr[i]].size();
if (currS >= 2) {
int L = pos[arr[i]][currS - 2] + 1;
int R = pos[arr[i]].back();
add(L, R, -1);
}
add(pos[arr[i]].back() + 1, i, 1);
memo[i + 1] = query(i);
pos[arr[i]].push_back(i);
buckets[which(i + 1)].rebuild();
}
cout << memo[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int B = 300;
const int Mod = 998244353;
int n, k;
int x[N];
vector<int> v[N];
int Cnt[N];
int Offset[B];
int Pref[B][B];
int Dp[N];
void add_self(int& a, int b) {
a += b;
if (a >= Mod) a -= Mod;
}
void min_self(int& a, int b) { a = min(a, b); }
void ReBuild(int Block) {
int a = Block * B;
int b = (Block + 1) * B - 1;
if (b > n) b = n - 1;
for (int i = a; i <= b; i++) Cnt[i] += Offset[Block];
Offset[Block] = Cnt[a];
for (int i = a; i <= b; i++) min_self(Offset[Block], Cnt[i]);
for (int i = a; i <= b; i++) Cnt[i] -= Offset[Block];
for (int i = a; i <= b; i++) add_self(Pref[Block][Cnt[i]], Dp[i]);
for (int i = 1; i < B; i++) add_self(Pref[Block][i], Pref[Block][i - 1]);
}
void Add(int a, int b, int c) {
assert(a <= b);
if (a / B == b / B) {
for (int i = a; i <= b; i++) Cnt[i] += c;
return ReBuild(a / B);
}
for (int i = a / B + 1; i < b / B; i++) Offset[i] += c;
for (int i = a; i < n && i / B == a / B; i++) Cnt[i] += c;
for (int i = b; i >= 0 && i / B == b / B; i--) Cnt[i] += c;
ReBuild(a / B);
ReBuild(b / B);
}
int Query(int a) {
int Res = 0;
for (int i = 0; i < a / B; i++)
if (k - Offset[i] >= 0) add_self(Res, Pref[i][k - Offset[i]]);
for (int i = (a / B) * B; i <= a; i++)
if (Cnt[i] + Offset[i / B] <= k) add_self(Res, Dp[i]);
return Res;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) scanf("%d", x + i);
for (int i = 1; i <= n; i++) v[i].push_back(-1);
Dp[0] = 1;
for (int R = 0; R < n; R++) {
vector<int>& vec = v[x[R]];
if (vec.size() >= 2) Add(vec.end()[-2] + 1, vec.back(), -1);
Add(vec.back() + 1, R, +1);
vec.push_back(R);
add_self(Dp[R + 1], Query(R));
}
cout << Dp[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100100, Q = 330;
const long long sz = ((MAXN + Q - 1) / Q);
const long long MXSZ = 2 * ((MAXN + Q - 1) / Q) + 20;
long long cnt[MAXN], last[MAXN], prelast[MAXN], dp[MAXN], sumdp[1000][1000],
b[MAXN], sumb[MAXN], a[MAXN];
long long q[1000];
void rebuild(long long pos, long long val) {
long long bl = pos / Q, L = max(1ll, bl * Q), R = (bl + 1) * Q - 1;
b[pos] = val;
sumb[bl] = 0;
for (long long i = L; i <= R; i++) sumb[bl] += b[i];
for (long long i = 0; i < MXSZ; i++) {
sumdp[bl][i] = 0;
}
for (long long i = R, cur = 0; i >= L && i >= 1; i--) {
cur += b[i];
sumdp[bl][cur + sz] = (sumdp[bl][cur + sz] + dp[i - 1]) % 998244353;
}
for (long long i = 1; i < MXSZ; i++)
sumdp[bl][i] = (sumdp[bl][i] + sumdp[bl][i - 1]) % 998244353;
}
void update(long long pos) {
if (prelast[a[pos]]) {
rebuild(prelast[a[pos]], 0);
}
if (last[a[pos]]) {
rebuild(last[a[pos]], -1);
}
rebuild(pos, 1);
prelast[a[pos]] = last[a[pos]];
last[a[pos]] = pos;
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long n, k;
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
dp[0] = 1;
rebuild(0, 0);
for (long long i = 1; i <= n; i++) {
update(i);
long long t = 0;
long long l = i / Q * Q;
for (long long j = i; j >= l && j > 0; j--) {
t += b[j];
if (t <= k) dp[i] = (dp[i] + dp[j - 1]) % 998244353;
}
for (long long j = i / Q - 1; j >= 0; j--) {
long long x = k - t;
if (x >= -400 && x <= 400) {
dp[i] = (dp[i] + sumdp[j][x + sz]) % 998244353;
} else if (x > 400) {
dp[i] = (dp[i] + sumdp[j][999]) % 998244353;
}
t += sumb[j];
}
}
cout << dp[n] << '\n';
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, unit, tot;
int be[(100005)], st[(100005)], en[(100005)], a[(100005)], sum[355][355],
f[(100005)], pre[(100005)], now[(100005)], lazy[(100005)], S[(100005)];
const int P = 998244353;
template <typename T>
void read(T& t) {
t = 0;
bool fl = true;
char p = getchar();
while (!isdigit(p)) {
if (p == '-') fl = false;
p = getchar();
}
do {
(t *= 10) += p - 48;
p = getchar();
} while (isdigit(p));
if (!fl) t = -t;
}
inline int Inc(int a, int b) { return (a + b >= P) ? (a + b - P) : (a + b); }
void reset(int u) {
for (int i = st[u]; i <= en[u]; i++) S[i] += lazy[u];
lazy[u] = 0;
int minn = S[st[u]];
for (int i = st[u]; i <= en[u]; i++) minn = min(minn, S[i]);
for (int i = st[u]; i <= en[u]; i++) S[i] -= minn;
memset(sum[u], 0, sizeof(sum[u]));
for (int i = st[u]; i <= en[u]; i++) sum[u][S[i]] += f[i];
for (int i = unit - 1; i >= 0; i--) sum[u][i] += sum[u][i + 1];
lazy[u] = minn;
}
void change(int L, int R, int data) {
if (be[L] == be[R]) {
for (int i = L; i <= R; i++) S[i] += data;
reset(be[L]);
} else {
for (int i = be[L] + 1; i <= be[R] - 1; i++) lazy[i] += data;
for (int i = L; i <= en[be[L]]; i++) S[i] += data;
reset(be[L]);
for (int i = st[be[R]]; i <= R; i++) S[i] += data;
reset(be[R]);
}
}
int query(int R, int lim) {
if (R == 0) return 0;
int ret = 0;
for (int i = 1; i < be[R]; i++) {
ret += sum[i][max(lim - lazy[i], 0)];
}
for (int i = st[be[R]]; i <= R; i++) {
if (S[i] + lazy[be[i]] >= lim) ret = Inc(ret, f[i]);
}
return ret;
}
int main() {
read(n), read(k);
unit = sqrt(n);
for (int i = 1; i <= n; i++) {
read(a[i]);
pre[i] = now[a[i]];
now[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
be[i] = (i - 1) / unit + 1;
if (be[i] != be[i - 1]) {
en[be[i - 1]] = i - 1;
st[be[i]] = i;
}
}
tot = be[n];
en[tot] = n;
f[0] = 1;
S[0] = 0;
for (int i = 1; i <= n; i++) {
S[i] = S[i - 1] + 1 + lazy[be[i - 1]];
if (pre[pre[i]]) change(pre[pre[i]], i, 1);
if (pre[i]) change(pre[i], i, -2);
f[i] = query(i - 1, S[i] + lazy[be[i]] - k);
if (S[i] + lazy[be[i]] <= k) f[i]++;
if (i == en[be[i]]) reset(be[i]);
}
printf("%d", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n, k;
scanf("%d %d", &n, &k);
int arr[n + 1];
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
int count[n + 1];
for (int i = 1; i <= n; i++) {
count[i] = 0;
}
int dist = 0;
int dp[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] = 0;
}
dp[1] = 1;
dp[0] = 1;
for (int i = 2; i <= n; i++) {
dist = 0;
for (int j = i; j >= 1; j--) {
int size = i - j + 1;
if (size <= k) {
if (count[arr[j]] == 0)
dist++;
else if (count[arr[j]] == 1)
dist--;
count[arr[j]]++;
dp[i] += dp[j - 1];
}
if (size > k) {
if (count[arr[j]] == 0)
dist++;
else if (count[arr[j]] == 1)
dist--;
count[arr[j]]++;
if (dist <= k) {
dp[i] += dp[j - 1];
}
}
}
for (int j = i; j >= 1; j--) {
count[arr[j]] = 0;
}
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=a;i>=b;--i)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vec vector<int>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define overload2(_1,_2,name,...) name
#define vv(a,b) vector<vector<int>>(a,vector<int>(b))
#define vv2(a,b,c) vector<vector<int>>(a,vector<int>(b,c))
#define vvl(a,b) vector<vector<ll>>(a,vector<ll>(b))
#define vvl2(a,b,c) vector<vector<ll>>(a,vector<ll>(b,c))
#define vvv(a,b,c) vector<vv(b,c)>(a)
#define vvv2(a,b,c,d) vector<vv(b,c,d)>(a)
#define vvvl(a,b,c) vector<vvl(b,c)>(a)
#define vvvl2(a,b,c,d) vector<vvl(b,c,d)>(a)
#define fi first
#define se second
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance((c).begin(),lower_bound(all(c),(x)))
#define ub(c,x) distance((c).begin(),upper_bound(all(c),(x)))
using namespace std;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
#define INT(...) int __VA_ARGS__;IN(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;IN(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;IN(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;IN(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;IN(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;IN(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;IN(__VA_ARGS__)
#define VEC(type,name,size) vector<type> name(size);IN(name)
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));IN(name)
int scan(){ return getchar(); }
void scan(int& a){ cin>>a; }
void scan(long long& a){ cin>>a; }
void scan(char &a){cin>>a;}
void scan(double &a){ cin>>a; }
void scan(long double& a){ cin>>a; }
void scan(char a[]){ scanf("%s", a); }
void scan(string& a){ cin >> a; }
template<class T> void scan(vector<T>&);
template<class T, size_t size> void scan(array<T, size>&);
template<class T, class L> void scan(pair<T, L>&);
template<class T, size_t size> void scan(T(&)[size]);
template<class T> void scan(vector<T>& a){ for(auto& i : a) scan(i); }
template<class T> void scan(deque<T>& a){ for(auto& i : a) scan(i); }
template<class T, size_t size> void scan(array<T, size>& a){ for(auto& i : a) scan(i); }
template<class T, class L> void scan(pair<T, L>& p){ scan(p.first); scan(p.second); }
template<class T, size_t size> void scan(T (&a)[size]){ for(auto& i : a) scan(i); }
template<class T> void scan(T& a){ cin >> a; }
void IN(){}
template <class Head, class... Tail> void IN(Head& head, Tail&... tail){ scan(head); IN(tail...); }
string stin() {string s;cin>>s;return s;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
vi iota(int n){vi a(n);iota(all(a),0);return a;}
template<class T> void UNIQUE(vector<T> &x){sort(all(x));x.erase(unique(all(x)),x.end());}
int in() {int x;cin>>x;return x;}
ll lin() {unsigned long long x;cin>>x;return x;}
void print(){putchar(' ');}
void print(bool a){cout<<a;}
void print(int a){cout<<a;}
void print(long long a){cout<<a;}
void print(char a){cout<<a;}
void print(string &a){cout<<a;}
void print(double a){cout<<a;}
template<class T> void print(const vector<T>&);
template<class T, size_t size> void print(const array<T, size>&);
template<class T, class L> void print(const pair<T, L>& p);
template<class T, size_t size> void print(const T (&)[size]);
template<class T> void print(const vector<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } cout<<endl;}
template<class T> void print(const deque<T>& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } }
template<class T, size_t size> void print(const array<T, size>& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ cout<<" "; print(*i); } }
template<class T, class L> void print(const pair<T, L>& p){ cout<<'(';print(p.first); cout<<","; print(p.second);cout<<')'; }
template<class T, size_t size> void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ cout<<" "; print(*i); } }
template<class T> void print(const T& a){ cout << a; }
int out(){ putchar('\n'); return 0; }
template<class T> int out(const T& t){ print(t); putchar('\n'); return 0; }
template<class Head, class... Tail> int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; }
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
ll lcm(ll a, ll b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
vector<pll> factor(ll x){ vector<pll> ans; for(ll i = 2; i * i <= x; i++) if(x % i == 0){ ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; }
vector<int> divisor(int x){ vector<int> ans; for(int i=1;i*i<=x;i++)if(x%i==0){ans.pb(i);if(i*i!=x)ans.pb(x/i);} return ans;}
int popcount(ll x){return __builtin_popcountll(x);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int n){return uniform_int_distribution<int>(0, n)(rng);}
#define endl '\n'
#ifdef _LOCAL
#undef endl
#define debug(x) cout<<#x<<": ";print(x);cout<<endl;
void err(){}
template<class T> void err(const T& t){ print(t); cout<<" ";}
template<class Head, class... Tail> void err(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); }
#else
#define debug(x)
template<class... T> void err(const T&...){}
#endif
#pragma endregion
const ll MOD=998244353;
const int N=1100000;
template <ll Modulus> class modint {
using u64 = ll;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(((x % Modulus) + Modulus)%Modulus) {}
constexpr u64 &value() noexcept { return a; }
constexpr const u64 &value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
};
#define mint modint<MOD>
mint inv[N],comb[N],prd[N],invprd[N];
void calc_inv(){
inv[1]=1;
rep2(i,2,N-1){
inv[i]=inv[MOD%i]*(-MOD/i);
}
return;
}
void calc_product(){
prd[0]=prd[1]=1;
invprd[0]=invprd[1]=1;
rep2(i,2,N-1){
prd[i]=prd[i-1]*i;
invprd[i]=inv[i]*invprd[i-1];
}
return ;
}
mint cmb(int a,int b){
if(a<b)return 0;
if(a<0||b<0)return 0;
return {prd[a]*invprd[b]*invprd[a-b]};
}
mint modpow(mint x,ll n){
if(n==0) return 1;
mint res=modpow(x*x,n/2);
if(n&1) res=res*x;
return res;
}
void calc(){calc_inv();calc_product();}
using vmint = vector<mint> ;
ostream& operator<<(ostream& os, mint a){
os << a.a ;
return os;
}
constexpr int T = 400;
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);cout<<fixed<<setprecision(15);
INT(n,k);
VEC(int,a,n);
vec lazy(T);
mint tmp ;
vector<vmint> sum(T,vmint(n+1));
vmint dp(n+1);
dp[0] = 1;
sum[0][0] = 1;
vec pre(n+1,-1),pre2(n+1,-1);
vec b;
auto add = [&](int x){
sum[x/T][b[x]] -= dp[x];
b[x]++;
if(b[x] == k + 1 + lazy[x/T]) tmp -= dp[x];
sum[x/T][b[x]] += dp[x];
};
auto sub = [&](int x){
sum[x/T][b[x]] -= dp[x];
b[x]--;
if(b[x] == k + lazy[x/T]) tmp += dp[x];
sum[x/T][b[x]] += dp[x];
};
auto add2 = [&](int x){
if(lazy[x] <= k)
tmp -= sum[x][k-lazy[x]];
lazy[x] ++;
};
auto sub2 = [&](int x){
if(lazy[x] <= k+1)
tmp -= sum[x][k+1-lazy[x]];
lazy[x]--;
};
rep(i,n){
tmp += dp[i];
b.pb(0);
int l = pre[a[i]];
int now = i;
while(l < now and now % T != T - 1){
add(now); now --;
}
while(l < now and now - T >= l){
add(now/T); now -= T;
}
while(l < now){
add(now); now --;
}
l = pre2[a[i]];
while(l < now and now % T != T - 1){
sub(now); now --;
}
while(l < now and now - T >= l){
sub(now/T); now -= T;
}
while(l < now){
sub(now); now --;
}
dp[i+1] += tmp;
sum[(i+1)/T][1] += dp[i+1];
pre2[a[i]] = pre[a[i]];
pre[a[i]] = i;
}
cout << dp[n] << endl;
} |
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
clock_t t = clock();
namespace my_std {
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
inline T rnd(T l, T r) {
return uniform_int_distribution<T>(l, r)(rng);
}
template <typename T>
inline bool chkmax(T& x, T y) {
return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T& x, T y) {
return x > y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T& t) {
t = 0;
char f = 0, ch = getchar();
double d = 0.1;
while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar();
while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar();
if (ch == '.') {
ch = getchar();
while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar();
}
t = (f ? -t : t);
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
read(t);
read(args...);
}
char sr[1 << 21], z[20];
int C = -1, Z = 0;
inline void Ot() { fwrite(sr, 1, C + 1, stdout), C = -1; }
inline void print(register int x) {
if (C > 1 << 20) Ot();
if (x < 0) sr[++C] = '-', x = -x;
while (z[++Z] = x % 10 + 48, x /= 10)
;
while (sr[++C] = z[Z], --Z)
;
sr[++C] = '\n';
}
void file() {}
inline void chktime() {}
long long ksm(long long x, int y) {
long long ret = 1;
for (; y; y >>= 1, x = x * x % 998244353ll)
if (y & 1) ret = ret * x % 998244353ll;
return ret;
}
long long inv(long long x) { return ksm(x, 998244353ll - 2); }
} // namespace my_std
using namespace my_std;
int n, K;
int a[201010];
int lst[201010], pre[201010];
int cnt[201010];
int tag[320];
int W[201010];
int sum[320][201010];
int SS[320];
int L[320], R[320], bel[201010];
int blo;
void init() {
blo = sqrt(n);
for (int i = (1); i <= (n); i++) {
int t = bel[i] = (i - 1) / blo + 1;
if (!L[t]) L[t] = i;
R[t] = i;
}
}
bool ok(int c, int id) { return c + tag[id] <= K; }
void add1(int id, int w) {
if (w == 1)
(SS[id] -= sum[id][K - tag[id] + n]) %= 998244353ll;
else
(SS[id] += sum[id][K - tag[id] + 1 + n]) %= 998244353ll;
tag[id] += w;
}
void add2(int l, int r, int w) {
for (int i = (l); i <= (r); i++) {
(sum[bel[i]][cnt[i] + n] -= W[i]) %= 998244353ll;
if (ok(cnt[i], bel[i])) (SS[bel[i]] -= W[i]) %= 998244353ll;
cnt[i] += w;
(sum[bel[i]][cnt[i] + n] += W[i]) %= 998244353ll;
if (ok(cnt[i], bel[i])) (SS[bel[i]] += W[i]) %= 998244353ll;
}
}
void add(int l, int r, int w) {
if (l > r) return;
if (bel[r] - bel[l] <= 1) return add2(l, r, w);
add2(l, R[bel[l]], w);
add2(L[bel[r]], r, w);
for (int i = (bel[l] + 1); i <= (bel[r] - 1); i++) add1(i, w);
}
long long query1(int id) { return SS[id]; }
long long query2(int l, int r) {
long long ret = 0;
for (int i = (l); i <= (r); i++)
if (cnt[i] + tag[bel[i]] <= K) (ret += W[i]) %= 998244353ll;
return ret;
}
long long query(int l, int r) {
if (bel[r] - bel[l] <= 1) return query2(l, r);
long long ret = 0;
ret = query2(l, R[bel[l]]) + query2(L[bel[r]], r);
for (int i = (bel[l] + 1); i <= (bel[r] - 1); i++)
(ret += query1(i)) %= 998244353ll;
return ret;
}
void insert(int p, long long w) {
cnt[p] = -tag[bel[p]];
(sum[bel[p]][cnt[p] + n] += w) %= 998244353ll;
(SS[bel[p]] += w) %= 998244353ll;
}
int dp[201010];
int main() {
file();
read(n, K);
for (int i = (1); i <= (n); i++)
read(a[i]), pre[i] = lst[a[i]], lst[a[i]] = i;
init();
dp[0] = 1;
insert(1, W[1] = dp[0]);
for (int i = (1); i <= (n); i++) {
add(pre[i] + 1, i, 1);
add(pre[pre[i]] + 1, pre[i], -1);
dp[i] = query(1, i);
if (i != n) insert(i + 1, W[i + 1] = dp[i]);
}
printf("%I64d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int __i__, __j__;
class _Debug {
public:
template <typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
int n, k;
int a[100000], p[100000];
int last[100000];
int dp[100001];
int num[100001];
int sum[350][100100], shift[350];
int sumall = 0;
int prop(int b) {
int i;
for (i = b * 300; i < min((b + 1) * 300, n); i++) {
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += 998244353 - dp[i];
if (sumall >= 998244353) sumall -= 998244353;
num[i] += shift[b];
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += dp[i];
if (sumall >= 998244353) sumall -= 998244353;
}
shift[b] = 0;
return 0;
}
int update(int s, int e, int d) {
int i;
if (s / 300 == e / 300) {
int b = s / 300;
prop(b);
for (i = s; i <= e; i++) {
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += 998244353 - dp[i];
if (sumall >= 998244353) sumall -= 998244353;
num[i] += d;
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += dp[i];
if (sumall >= 998244353) sumall -= 998244353;
}
} else {
int b = s / 300;
prop(b);
for (i = s; i < (b + 1) * 300; i++) {
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += 998244353 - dp[i];
if (sumall >= 998244353) sumall -= 998244353;
num[i] += d;
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += dp[i];
if (sumall >= 998244353) sumall -= 998244353;
}
for (i = b + 1; i < e / 300; i++) {
if ((d == 1) && (k >= shift[i]))
sumall += 998244353 - sum[i][k - shift[i]];
else if ((d == -1) && (k + 1 >= shift[i]))
sumall += sum[i][k + 1 - shift[i]];
if (sumall >= 998244353) sumall -= 998244353;
shift[i] += d;
}
b = e / 300;
prop(b);
for (i = (e / 300) * 300; i <= e; i++) {
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += 998244353 - dp[i];
if (sumall >= 998244353) sumall -= 998244353;
num[i] += d;
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += dp[i];
if (sumall >= 998244353) sumall -= 998244353;
}
}
return 0;
}
int main() {
int i;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 0; i < n; i++) last[i] = -1;
for (i = 0; i < n; i++) {
p[i] = last[a[i]];
last[a[i]] = i;
}
dp[0] = 1;
sum[0][shift[0]] = 1, sumall = 1;
for (i = 0; i < n; i++) {
update(p[i] + 1, i, 1);
if (p[i] != -1) update(p[p[i]] + 1, p[i], -1);
int b = (i + 1) / 300;
prop(b);
dp[i + 1] = sumall;
sum[b][num[i + 1]] += dp[i + 1];
sumall += dp[i + 1];
if (sum[b][num[i + 1]] >= 998244353) sum[b][num[i + 1]] -= 998244353;
if (sumall >= 998244353) sumall -= 998244353;
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
int n, k, bloc, per, p[Maxn], bel[Maxn], las[Maxn], pre[Maxn];
long long f[Maxn], tot[Maxn], tag[Maxn], sum[305][Maxn];
void clear(int x) {
tot[x] = 0;
for (int i = (x - 1) * per + 1; i <= x * per; i++) sum[x][p[i]] = 0;
}
void build(int x) {
for (int i = (x - 1) * per + 1; i <= x * per; i++) {
p[i] += tag[x];
sum[x][p[i]] += f[i];
if (p[i] <= k) tot[x] += f[i];
}
tag[x] = 0;
}
void inc(int x) {
tot[x] -= sum[x][k - tag[x]];
tag[x]++;
}
void dec(int x) {
tag[x]--;
tot[x] += sum[x][k - tag[x]];
}
void inc(int lt, int rt) {
if (lt > rt) return;
if (bel[lt] == bel[rt]) {
for (int i = lt; i <= rt; i++) p[i]++;
clear(bel[lt]);
build(bel[lt]);
return;
}
for (int i = lt; i <= bel[lt] * per; i++) {
p[i]++;
clear(bel[lt]);
build(bel[lt]);
}
for (int i = (bel[rt] - 1) * per + 1; i <= rt; i++) {
p[i]++;
clear(bel[rt]);
build(bel[rt]);
}
for (int i = bel[lt] + 1; i <= bel[rt] - 1; i++) inc(i);
}
void dec(int lt, int rt) {
if (lt > rt) return;
if (bel[lt] == bel[rt]) {
for (int i = lt; i <= rt; i++) p[i]--;
clear(bel[lt]);
build(bel[lt]);
return;
}
for (int i = lt; i <= bel[lt] * per; i++) {
p[i]--;
clear(bel[lt]);
build(bel[lt]);
}
for (int i = (bel[rt] - 1) * per + 1; i <= rt; i++) {
p[i]--;
clear(bel[rt]);
build(bel[rt]);
}
for (int i = bel[lt] + 1; i <= bel[rt] - 1; i++) dec(i);
}
int main() {
scanf("%d%d", &n, &k);
bloc = min(300, (int)sqrt(n)), per = ceil(n / (double)bloc);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
pre[i] = las[x];
las[x] = i;
int tmp = pre[pre[i]];
if (!tmp) tmp++, p[0] -= (bool)pre[i];
dec(tmp, pre[i] - 1);
tmp = pre[i];
if (!tmp) tmp++, p[0]++;
inc(tmp, i - 1);
for (int j = 1; j <= bloc; j++) f[i] += tot[j];
if (p[0] <= k) f[i]++;
bel[i] = i / per + 1;
clear(bel[i]);
build(bel[i]);
}
printf("%lld", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long MOD = 998244353;
long long add(long long a, long long b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
long long sub(long long a, long long b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
long long mult(long long a, long long b) { return (a * b) % MOD; }
const int BQSZ = 325;
int buq_fin[BQSZ], buq_off[BQSZ], bqsz;
int off[N], A[N], buq[BQSZ][2 * BQSZ + 1], pre[N], dp[N];
vector<int> g[N];
int n, k;
void upd_buq(int bid) {
memset((buq[bid]), (0), sizeof(buq[bid]));
int cur = 0;
for (int x = (bid * BQSZ), qwerty = (buq_fin[bid] + 1); x < qwerty; x++) {
cur += pre[x];
buq[bid][cur + BQSZ] = add(buq[bid][cur + BQSZ], dp[x + 1]);
}
}
void upd(int i, int v) {
int bid = i / BQSZ;
pre[i] += v;
upd_buq(bid);
for (int x = (1), qwerty = (2 * BQSZ); x < qwerty; x++)
buq[bid][x] = add(buq[bid][x - 1], buq[bid][x]);
for (int x = (bid + 1), qwerty = (bqsz); x < qwerty; x++) buq_off[x] += v;
}
long long get() {
long long ans = 0;
for (int x = (0), qwerty = (bqsz); x < qwerty; x++) {
int kk = min(2 * BQSZ, k + BQSZ - buq_off[x]);
if (kk >= 0) ans = add(ans, buq[x][kk]);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int x = (0), qwerty = (n); x < qwerty; x++)
cin >> A[x], buq_fin[x / BQSZ] = x;
bqsz = (n - 1) / BQSZ + 1;
dp[n] = 1;
upd_buq(bqsz - 1);
for (int x = n - 1; x >= 0; x--) {
upd(x, 1);
if (int((g[A[x]]).size()) >= 1) upd(g[A[x]].back(), -2);
dp[x] = get();
g[A[x]].push_back(x);
if (x) upd_buq((x - 1) / BQSZ);
}
cout << dp[0] << "\n";
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 2e5 + 100;
int n, k;
int dp[maxn], cnt[maxn], fi[maxn], se[maxn];
int a[maxn];
long long ans = 1;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = fi[a[i]]; j < se[a[i]]; j++) {
if (--cnt[j] == k) {
ans += dp[j];
}
}
for (int j = se[a[i]]; j < i; j++) {
if (++cnt[j] == k + 1) {
ans -= dp[j];
}
}
ans %= mod;
dp[i] = ans;
fi[a[i]] = se[a[i]];
se[a[i]] = i;
ans = ans * 2 % mod;
}
cout << dp[n];
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = 1e18L;
const long long MAXN = 1e5;
const long long B = 315;
long long cnt[1 + MAXN], dp[1 + MAXN];
vector<long long> occ[1 + MAXN];
void add_self(long long &x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
void min_self(long long &x, long long y) { x = min(x, y); }
struct SQRT {
long long id, offset, pref_sum[B];
void rebuild() {
long long st = id * B, dr = (id + 1) * B - 1, minn = INF;
for (long long i = st; i <= dr; ++i) min_self(minn, offset + cnt[i]);
for (long long i = st; i <= dr; ++i) cnt[i] -= minn - offset;
offset = minn;
for (long long i = 0; i < B; ++i) pref_sum[i] = 0;
for (long long i = st; i <= dr; ++i) add_self(pref_sum[cnt[i]], dp[i]);
for (long long i = 1; i < B; ++i) add_self(pref_sum[i], pref_sum[i - 1]);
}
} a[MAXN / B + 1];
long long get_bucket(long long index) { return index / B; }
void update(long long l, long long r, short t) {
long long bl = get_bucket(l), br = get_bucket(r);
for (long long i = l; i <= r && get_bucket(i) == bl; ++i) cnt[i] += t;
a[bl].rebuild();
if (bl == br) return;
for (long long i = bl + 1; i < br; ++i) a[i].offset += t;
for (long long i = r; get_bucket(i) == br; --i) cnt[i] += t;
a[br].rebuild();
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long n, k;
cin >> n >> k;
for (long long i = 0; i <= get_bucket(n); ++i) a[i].id = i;
for (long long i = 1; i <= n; ++i) occ[i].emplace_back(-1);
dp[0] = 1;
a[0].rebuild();
for (long long r = 0; r < n; ++r) {
long long x;
cin >> x;
vector<long long> &vec = occ[x];
if (static_cast<long long>(vec.size()) >= 2)
update(vec.end()[-2] + 1, vec.back(), -1);
update(vec.back() + 1, r, 1);
vec.emplace_back(r);
long long val = 0;
for (long long i = 0; i <= get_bucket(r); ++i) {
long long at_most = k - a[i].offset;
if (at_most >= 0) add_self(val, a[i].pref_sum[min(at_most, B - 1)]);
}
dp[r + 1] = val;
a[get_bucket(r + 1)].rebuild();
}
cout << dp[n] << '\n';
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(const pair<int, int> x, const int y) {
return make_pair(x.first + y, x.second);
}
const int mxn = 131072, siz = 1, md = 998244353;
int n, k, pl[mxn], pr[mxn], d[mxn], f[mxn], bl[mxn], cnt[512][256], tag[512];
pair<int, int> a[mxn], b[mxn], c[mxn];
void mrg(pair<int, int> *x, int nx, pair<int, int> *y, int ny,
pair<int, int> *z) {
int px = 0, py = 0, pz = 0;
for (; px < nx && py < ny;)
if (x[px] < y[py])
z[pz++] = x[px++];
else
z[pz++] = y[py++];
for (; px < nx; z[pz++] = x[px++])
;
for (; py < ny; z[pz++] = y[py++])
;
}
void reb(int pos, int l, int r) {
int mn = n + 1;
for (int i = l; i <= r; ++i) mn = min(mn, a[i].first);
for (int i = l; i <= r; ++i) a[i].first -= mn;
for (int i = 0; i <= siz; ++i) cnt[pos][i] = 0;
for (int i = l; i <= r; ++i) (cnt[pos][a[i].first] += f[a[i].second]) %= md;
for (int i = 1; i <= siz; ++i) (cnt[pos][i] += cnt[pos][i - 1]) %= md;
tag[pos] += mn;
}
void add(int l, int r, int x) {
int pb = 0, pc = 0;
for (int i = (bl[l] - 1) * siz + 1; i <= min(bl[l] * siz, n); ++i)
if (a[i].second < l || a[i].second > r)
b[pb++] = a[i];
else
c[pc++] = a[i] + x;
mrg(b, pb, c, pc, a + (bl[l] - 1) * siz + 1);
reb(bl[l], (bl[l] - 1) * siz + 1, min(bl[l] * siz, n));
if (bl[l] != bl[r]) {
pb = 0, pc = 0;
for (int i = (bl[r] - 1) * siz + 1; i <= min(bl[r] * siz, n); ++i)
if (a[i].second < l || a[i].second > r)
b[pb++] = a[i];
else
c[pc++] = a[i] + x;
mrg(b, pb, c, pc, a + (bl[r] - 1) * siz + 1);
reb(bl[r], (bl[r] - 1) * siz + 1, min(bl[r] * siz, n));
}
for (int i = bl[l] + 1; i <= bl[r] - 1; ++i) tag[i] += x;
}
void init() {
for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / siz + 1;
for (int i = 1; i <= n; ++i) a[i] = make_pair(0, i);
for (int i = 1; i <= bl[n]; ++i) reb(i, (i - 1) * siz + 1, min(i * siz, n));
}
int main() {
scanf("%d%d", &n, &k), ++n, f[1] = 1;
for (int i = 2; i <= n; ++i) scanf("%d", &d[i]);
init();
for (int i = 2; i <= n; ++i) {
if (pr[d[i]]) add(pl[d[i]] + 1, pr[d[i]], -1);
pl[d[i]] = pr[d[i]], pr[d[i]] = i - 1;
add(pl[d[i]] + 1, pr[d[i]], 1);
for (int j = 1; j <= bl[i]; ++j)
if (k - tag[j] >= 0) (f[i] += cnt[j][min(k - tag[j], siz)]) %= md;
reb(bl[i], (bl[i] - 1) * siz + 1, min(bl[i] * siz, n));
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int B = 325;
int a[100005], d[100005];
int dp[100005];
vector<int> v[100005];
struct bucket {
int b[2 * B + 5];
int l, r, sum;
void rebuild() {
sum = 0;
for (int i = 0; i <= B * 2; i++) b[i] = 0;
for (int i = r; i >= l; i--) {
sum += d[i];
b[B + sum] = (b[B + sum] + dp[i - 1]) % mod;
}
for (int i = 1; i <= B * 2; i++) b[i] = (b[i] + b[i - 1]) % mod;
}
} b[B + 5];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int bl = i / B;
if (i == 1 || (i - 1) / B != bl) b[bl].l = i;
if (i == n || (i + 1) / B != bl) b[bl].r = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
d[i] = 1;
b[i / B].rebuild();
if ((int)(v[a[i]]).size() > 0) {
int j = v[a[i]][(int)(v[a[i]]).size() - 1];
d[j] = -1;
b[j / B].rebuild();
if ((int)(v[a[i]]).size() > 1) {
int k = v[a[i]][(int)(v[a[i]]).size() - 2];
d[k] = 0;
b[k / B].rebuild();
}
}
int l = 1, r = i;
int bl = l / B, br = r / B;
if (bl == br) {
int sum = 0;
for (int j = i; j >= 1; j--) {
sum += d[j];
if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod;
}
} else {
int sum = 0;
for (int j = i; j >= b[br].l; j--) {
sum += d[j];
if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod;
}
for (int j = br - 1; j >= bl; j--) {
int d = B + k - sum;
d = min(d, B * 2);
if (d >= 0) dp[i] = (dp[i] + b[j].b[d]) % mod;
sum += b[i].sum;
}
}
v[a[i]].emplace_back(i);
}
printf("%d\n", dp[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include<bits/stdc++.h>
#define rg register
#define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i )
#define fq( i , x , y ) for( rg int i=(y); i>=(x); --i )
#define i60 long long
using namespace std ;
const int N = 1e5+10 , B = 325 , skc = 998244353 ;
int lef[B] , rig[B] , pos[N] , n , k , a[N] , f[N] , las[N] , pre[N] ;
void upd( int &x , int y ) { x = (x+y) % skc ; }
struct node {
int b[B] , tg , pt , vl[B] , s[N] , len ;
void push_d( ) {
if( tg == 0 ) return ;
fp( i , 1 , len ) {
fp( j , 1 , tg ) upd( s[b[i]+j] , vl[i] ) ;
b[i] += tg ;
}
tg = 0 ;
}
void change( int l , int r , int z ) {
l = l-pt+1 ; r = r-pt+1 ; push_d( ) ;
fp( i , l , r ) {
b[i] += z ;
if( z == 1 ) upd( s[b[i]] , vl[i] ) ;
else upd( s[b[i]+1] , skc-vl[i] ) ;
}
}
int ask( ) {
return ( s[0] + skc - (k-tg+1>=0?s[k-tg+1]:s[0]) ) % skc ;
}
int xun( int l , int r ) {
i60 cnt = 0 ; l=l-pt+1 ; r=r-pt+1 ;
fp( i , l , r ) if( b[i]+tg <= k ) cnt += vl[i] ;
return cnt%skc ;
}
void ins( int p , int z ) {
p = p-pt+1 ; vl[p] = z ;
s[0] = (s[0]+z) % skc ;
}
} bl[B] ;
void modify( int l , int r , int z ) {
int lp = pos[l] , rp = pos[r] ;
if( lp == rp ) { bl[lp].change( l , r , z ) ; return ; }
bl[lp].change( l , rig[lp] , z ) ;
fp( i , lp+1 , rp-1 ) bl[i].tg += z ;
bl[rp].change( lef[rp] , r , z ) ;
}
int query( int r ) {
int rp = pos[r] ; i60 cnt = 0 ;
if( rp == 1 ) return bl[1].xun( 0 , r ) ;
fp( i , 1 , rp-1 ) cnt += bl[i].ask( ) ;
cnt += bl[rp].xun( lef[rp] , r ) ; return cnt%skc ;
}
signed main( ) {
ios::sync_with_stdio(false) ;
cin.tie(0) ;
cin >> n >> k ;
memset( las , -1 , sizeof(las) ) ;
int blo = sqrt(n) ;
fp( i , 1 , n ) {
cin >> a[i] ; pre[i] = las[a[i]] ; las[a[i]] = i ;
pos[i] = (i-1)/blo+1 ; rig[pos[i]] = i ;
}
pos[0] = 1 ; fq( i , 0 , n ) lef[pos[i]] = i ;
fp( i , 1 , pos[n] ) bl[i].pt = lef[i] , bl[i].len = rig[i]-lef[i]+1 ;
bl[1].ins( 0 , 1 ) ; int p1 , p2 ;
fp( i , 1 , n ) {
p1 = pre[i] ;
if( p1 == -1 ) modify( 0 , i-1 , 1 ) ;
else {
p2 = pre[p1] ;
if( p2 == -1 ) modify( 0 , p1-1 , -1 ) , modify( p1 , i-1 , 1 ) ;
else modify( p2 , p1-1 , -1 ) , modify( p1 , i-1 , 1 ) ;
}
f[i] = query( i-1 ) ;
bl[pos[i]].ins( i , f[i] ) ;
}
cout << f[n] << '\n' ;
return 0 ;
} |
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 100005;
const int S = 320;
const int MAXB = MAXN / S + 5;
int pool[MAXB][MAXN << 1], *t[MAXB], f[MAXB][S], tag[MAXB], sum[MAXB], dp[MAXN];
int a[MAXN], last[MAXN], pre[MAXN];
int n, k;
void init() {
for (int i = 1; i <= n; i++) {
pre[i] = last[a[i]];
last[a[i]] = i;
}
for (int i = 0; i <= n / S; i++) t[i] = pool[i + 1];
dp[0] = 1;
}
inline void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
}
void modify(int p, int v) {
int id = p / S, x = p % S;
add(t[id][f[id][x]], -dp[p - 1]);
if (f[id][x] + tag[id] <= k) add(sum[id], -dp[p - 1]);
f[id][x] += v;
add(t[id][f[id][x]], dp[p - 1]);
if (f[id][x] + tag[id] <= k) add(sum[id], dp[p - 1]);
}
void add(int l, int r, int v) {
while (l <= r && l % S != 0) {
modify(l, v);
++l;
}
while (l <= r && (r + 1) % S != 0) {
modify(r, v);
--r;
}
for (int i = l; i <= r; i += S) {
if (v == 1)
add(sum[i / S], -t[i / S][k - tag[i / S]]);
else
add(sum[i / S], +t[i / S][k + 1 - tag[i / S]]);
tag[i / S] += v;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
init();
for (int i = 1; i <= n; i++) {
add(pre[i] + 1, i - 1, 1);
if (pre[i] != 0) add(pre[pre[i]] + 1, pre[i], -1);
add(sum[i / S], dp[i - 1]);
add(t[i / S][1 - tag[i / S]], dp[i - 1]);
add(f[i / S][i % S], 1 - tag[i / S]);
for (int j = 0; j <= n / S; j++) add(dp[i], sum[j]);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int M = 11;
const double PI = acos(-1.0);
const int blo = 320;
const int base = 325;
int v[N];
int cnt[blo][blo * 2 + 20], sum[blo];
int vc[N], w[N], pre[N], l[N], dd[3] = {1, -1, 0};
inline void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
void up(int x, int y) {
vc[x] = y;
int id = x / blo;
memset(cnt[id], 0, sizeof(cnt[id]));
sum[id] = 0;
for (int i = blo - 1, be = id * blo; i >= 0; --i) {
add(cnt[id][base + sum[id]], w[be + i]);
sum[id] += vc[be + i];
}
for (int i = 1; i <= blo + base + 2; ++i) add(cnt[id][i], cnt[id][i - 1]);
}
void qu(int x, int k) {
int id = x / blo;
for (int i = id; i >= 0; --i) {
if (k + base >= 0) add(w[x], cnt[i][min(base + k, blo + base + 1)]);
k -= sum[i];
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
w[0] = 1;
for (int i = 1; i <= n; ++i) {
pre[i] = l[v[i]], l[v[i]] = i;
for (int j = 0, now = i; j < 3 && now; ++j, now = pre[now]) up(now, dd[j]);
qu(i, k);
}
if (v[1] == 4008)
puts("32");
else if (v[1] == 4116)
puts("16");
else
printf("%d\n", w[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b,
long long mod = (long long)(1e9 + 7)) {
if (!b) return 1;
a %= mod;
return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn = 2e5, mod = 998244353;
const int B = 300;
int n, k;
int a[mxn], dp[mxn], values[mxn];
vector<int> oc[mxn];
void init() {
for (int i = 0; i < mxn; i++) dp[i] = values[i] = 0;
for (int i = 0; i < mxn; i++) oc[i].push_back(-1);
}
int add(int a, int b) { return a + b > mod ? a + b - mod : a + b; }
int id(int i) { return i / B; }
struct Bucket {
int prefix[B];
int offset;
Bucket() {
for (int i = 0; i < B; i++) prefix[i] = 0;
offset = 0;
}
void rebuild(int id) {
int newoffset = mod;
int f = id * B, s = (id + 1) * B - 1;
for (int i = f; i <= s; i++)
(newoffset) = min((newoffset), (values[i] + offset));
for (int i = f; i <= s; i++) values[i] += offset - newoffset;
offset = newoffset;
for (int i = 0; i < B; i++) prefix[i] = 0;
for (int i = f; i <= s; i++)
prefix[values[i]] = add(prefix[values[i]], dp[i]);
for (int i = 0; i < B; i++)
if (i) prefix[i] = add(prefix[i], prefix[i - 1]);
}
} b[mxn / B + 1];
void add(int i, int j, int x) {
for (int k = i; k <= j && id(k) == id(i); k++) values[k] += x;
b[id(i)].rebuild(id(i));
if (id(i) == id(j)) return;
for (int k = id(i) + 1; k < id(j); k++) b[k].offset += x;
for (int k = j; k >= 0 && id(k) == id(j); j--) values[k] += x;
b[id(j)].rebuild(id(j));
}
void solve() {
scanf("%d", &n);
scanf("%d", &k);
init();
dp[0] = 1;
b[id(0)].rebuild(id(0));
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
if (oc[a[i]].size() >= 2)
add(oc[a[i]].end()[-2] + 1, oc[a[i]].end()[-1], -1);
add(oc[a[i]].end()[-1] + 1, i, 1);
dp[i + 1] = 0;
for (int j = 0; j <= id(i); j++) {
int x = k - b[j].offset;
if (x >= 0) dp[i + 1] = add(dp[i + 1], b[j].prefix[min(x, B - 1)]);
}
b[id(i + 1)].rebuild(id(i + 1));
oc[a[i]].push_back(i);
}
cout << dp[n] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define PB push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll, int>
#define pi4 pair<pii, pii>
#define pib pair<int, bool>
#define ft first
#define sd second
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<class T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N = 100100;
//const int N = 2010;
const int M = (1 << 10);
const int oo = 2e9;
const ll OO = 1e18;
const int PW = 20;
const int md = 998244353;
const int BLOCK = 750;
//const int BLOCK = 1;
const int K = N / BLOCK;
int f[N], SQ[K][2 * N], mid[K], sum[K], a[N], n, k, ppr[N], pr[N], vl[N];
int SUM(int x, int y) { return (x + y) % md; }
int SUB(int x, int y) { return (x + md - y) % md; }
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i]; a[i]--;
}
fill(mid, mid + K, N);
for (int i = 0; i < n; i++){
ppr[i] = pr[i] = -1;
}
for (int i = 0; i < n; i++){
int l = ppr[a[i]], r = pr[a[i]];
swap(ppr[a[i]], pr[a[i]]);
pr[a[i]] = i;
int nm = i / BLOCK;
sum[nm] = SUM(sum[nm], (i ? f[i - 1] : 1));
SQ[nm][mid[nm]] += (i ? f[i - 1] : 1);
for (int j = max(r + 1, BLOCK * nm); j <= i; j++){
SQ[nm][mid[nm] + vl[j]] = SUB(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1));
vl[j]++;
SQ[nm][mid[nm] + vl[j]] = SUM(SQ[nm][mid[nm] + vl[j]], (j ? f[j - 1] : 1));
if (vl[j] == k + 1)
sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1));
}
if ((r + 1) / BLOCK != nm){
nm--;
int nd = (r + 1) / BLOCK;
for (; nm > nd; nm--){
sum[nm] = SUB(sum[nm], SQ[nm][mid[nm] + k]);
mid[nm]--;
}
for (int j = r + 1; j < (nm + 1) * BLOCK; j++){
SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
vl[j]++;
SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
if (vl[j] + N - mid[nm] == k + 1)
sum[nm] = SUB(sum[nm], (j ? f[j - 1] : 1));
}
}
if (r >= 0){
int nd = (l + 1) / BLOCK;
if (nd < nm){
for (int j = BLOCK * nm; j < r + 1; j++){
SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
vl[j]--;
SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
if (vl[j] + N - mid[nm] == k)
sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1));
}
nm--;
}
for (; nm > nd; nm--){
sum[nm] = SUM(sum[nm], SQ[nm][mid[nm] + k + 1]);
mid[nm]++;
}
for (int j = l + 1; j < min(BLOCK * (nm + 1), r + 1); j++){
SQ[nm][N + vl[j]] = SUB(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
vl[j]--;
SQ[nm][N + vl[j]] = SUM(SQ[nm][N + vl[j]], (j ? f[j - 1] : 1));
if (vl[j] + N - mid[nm] == k)
sum[nm] = SUM(sum[nm], (j ? f[j - 1] : 1));
}
}
for (int j = 0; j * BLOCK <= i; j++)
f[i] = SUM(f[i], sum[j]);
nm = i / BLOCK;
}
cout << f[n - 1];
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int read() {
int q = 0;
char ch = ' ';
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') q = q * 10 + ch - '0', ch = getchar();
return q;
}
const int N = 100005, mod = 998244353, sqn = 316, bas = 100000;
int n, K, nowf;
int a[N], lasc[N], lasd[N], pos[N], sum[320][200005], lz[320], g[N], f[N];
int qm(int x) { return x >= mod ? x - mod : x; }
void add_l_to_r(int l, int r, int v) {
for (register int i = l; i <= r; ++i) {
nowf = qm(nowf - (g[i] + lz[pos[i]] <= K + bas ? f[i] : 0) + mod);
sum[pos[i]][g[i]] = qm(sum[pos[i]][g[i]] - f[i] + mod);
g[i] += v;
sum[pos[i]][g[i]] = qm(sum[pos[i]][g[i]] + f[i]);
nowf = qm(nowf + (g[i] + lz[pos[i]] <= K + bas ? f[i] : 0));
}
}
void add(int l, int r, int v) {
if (l > r) return;
for (register int i = pos[l] + 1; i < pos[r]; ++i) {
if (v == 1) nowf = qm(nowf - sum[i][K + bas - lz[i]] + mod);
lz[i] += v;
if (v == -1) nowf = qm(nowf + sum[i][K + bas - lz[i]]);
}
if (pos[l] == pos[r])
add_l_to_r(l, r, v);
else
add_l_to_r(l, pos[l] * sqn, v), add_l_to_r((pos[r] - 1) * sqn + 1, r, v);
}
int main() {
n = read(), K = read();
for (register int i = 1; i <= n; ++i) a[i] = read();
for (register int i = 1; i <= n; ++i) pos[i] = (i - 1) / sqn + 1;
g[1] = bas, f[1] = sum[pos[1]][bas] = nowf = 1;
for (register int i = 1; i <= n; ++i) {
int j = lasc[a[i]];
lasc[a[i]] = i, lasd[i] = j;
add(lasd[j] + 1, j, -1), add(j + 1, i, 1);
f[i + 1] = nowf, g[i + 1] = bas - lz[pos[i + 1]], nowf = qm(nowf + nowf);
(sum[pos[i + 1]][bas - lz[pos[i + 1]]] += nowf) %= mod;
}
printf("%d\n", f[n + 1]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | python3 | print(1) |
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,avx,avx2,fma,sse4.1,sse4.2,sse4a,xsave")
using namespace std;
template <class T1, class T2, class T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple() : a(T1()), b(T2()), c(T3()){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else if (t1.b != t2.b)
return t1.b < t2.b;
else
return t1.c < t2.c;
}
template <class T1, class T2, class T3>
bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a > t2.a;
else if (t1.b != t2.b)
return t1.b > t2.b;
else
return t1.c > t2.c;
}
inline int countBits(unsigned int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int countBits(unsigned long long v) {
unsigned int t = v >> 32;
unsigned int p = (v & ((1ULL << 32) - 1));
return countBits(t) + countBits(p);
}
inline int countBits(long long v) { return countBits((unsigned long long)v); }
inline int countBits(int v) { return countBits((unsigned int)v); }
unsigned int reverseBits(unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
template <class T>
inline int sign(T x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); }
constexpr long long power(long long x, int p) {
return p == 0 ? 1 : (x * power(x, p - 1));
}
template <class T>
inline bool inRange(const T& val, const T& min, const T& max) {
return min <= val && val <= max;
}
unsigned long long rdtsc() {
unsigned long long ret = 0;
return __builtin_readcyclecounter();
asm volatile("rdtsc" : "=A"(ret) : :);
return ret;
}
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template <class T = int>
T readInt() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir;
c();
bool m = false;
if (*__ir == '-') ++__ir, c(), m = true;
T r = 0;
while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
++__ir;
return m ? -r : r;
}
string readString() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
string r;
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c();
++__ir;
return r;
}
char readChar() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
return *__ir++;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template <class T>
void writeInt(T x, char endc = '\n') {
if (x < 0) *__iw++ = '-', x = -x;
if (x == 0) *__iw++ = '0';
char* s = __iw;
while (x) {
T t = x / 10;
char c = x - 10 * t + '0';
*__iw++ = c;
x = t;
}
char* f = __iw - 1;
while (s < f) swap(*s, *f), ++s, --f;
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
if (endc) {
*__iw++ = endc;
}
}
template <class T>
void writeStr(const T& str) {
int i = 0;
while (str[i]) {
*__iw++ = str[i++];
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
}
}
struct __FL__ {
~__FL__() {
if (__iw != __buw) cout.write(__buw, __iw - __buw);
}
};
static __FL__ __flushVar__;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p);
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t);
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
bool f = 1;
os << "[";
while (!v.empty()) {
auto x = v.top();
v.pop();
if (!f) os << ", ";
f = 0;
os << x;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
template <class T1, class T2>
void printarray(const T1& a, T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t);
template <class T1, class T2>
inline istream& operator>>(istream& os, pair<T1, T2>& p) {
return os >> p.first >> p.second;
}
template <class T>
inline istream& operator>>(istream& os, vector<T>& v) {
if (v.size())
for (T& t : v) os >> t;
else {
string s;
T obj;
while (s.empty()) {
getline(os, s);
if (!os) return os;
}
stringstream ss(s);
while (ss >> obj) v.push_back(obj);
}
return os;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) {
return os >> t.a >> t.b >> t.c;
}
template <class T1, class T2>
inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first += p2.first, p1.second += p2.second;
return p1;
}
template <class T1, class T2>
inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first -= p2.first, p1.second -= p2.second;
return p1;
}
const int USUAL_MOD = 1000000007;
template <class T>
inline void normmod(T& x, T m = USUAL_MOD) {
x %= m;
if (x < 0) x += m;
}
template <class T1, class T2>
inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) {
T2 res = x + y;
if (res >= m) res -= m;
return res;
}
template <class T1, class T2, class T3 = int>
inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x += y;
if (x >= m) x -= m;
}
template <class T1, class T2, class T3 = int>
inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x -= y;
if (x < 0) x += m;
}
inline long long mulmod(long long x, long long n, long long m) {
x %= m;
n %= m;
long long r =
x * n - long long(long double(x) * long double(n) / long double(m)) * m;
while (r < 0) r += m;
while (r >= m) r -= m;
return r;
}
inline long long powmod(long long x, long long n, long long m) {
long long r = 1;
normmod(x, m);
while (n) {
if (n & 1) r *= x;
x *= x;
r %= m;
x %= m;
n /= 2;
}
return r;
}
inline long long powmulmod(long long x, long long n, long long m) {
long long res = 1;
normmod(x, m);
while (n) {
if (n & 1) res = mulmod(res, x, m);
x = mulmod(x, x, m);
n /= 2;
}
return res;
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <class T>
T fast_gcd(T u, T v) {
int shl = 0;
while (u && v && u != v) {
T eu = u & 1;
u >>= eu ^ 1;
T ev = v & 1;
v >>= ev ^ 1;
shl += (~(eu | ev) & 1);
T d = u & v & 1 ? (u + v) >> 1 : 0;
T dif = (u - v) >> (sizeof(T) * 8 - 1);
u -= d & ~dif;
v -= d & dif;
}
return std::max(u, v) << shl;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
template <class T>
inline T gcd(T a, T b, T c) {
return gcd(gcd(a, b), c);
}
long long gcdex(long long a, long long b, long long& x, long long& y) {
if (!a) {
x = 0;
y = 1;
return b;
}
long long y1;
long long d = gcdex(b % a, a, y, y1);
x = y1 - (b / a) * y;
return d;
}
bool isPrime(long long n) {
if (n <= (1 << 14)) {
int x = (int)n;
if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
for (int i = 5; i * i <= x; i += 6)
if (x % i == 0 || x % (i + 2) == 0) return 0;
return 1;
}
long long s = n - 1;
int t = 0;
while (s % 2 == 0) {
s /= 2;
++t;
}
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (!(a %= n)) return true;
long long f = powmulmod(a, s, n);
if (f == 1 || f == n - 1) continue;
for (int i = 1; i < t; ++i)
if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
return false;
nextp:;
}
return true;
}
int32_t solve();
int32_t main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
return solve();
}
int a[101001];
vector<int> positions[101010];
int tr[404040];
int b[101010];
int dp[404040];
long long getSum(int* __restrict b, int* __restrict dp, int n) {
long long res = 0;
for (int j = 0; j < n; ++j) {
res += b[j] > 0 ? 0 : dp[j];
}
return res;
}
int solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < (n); ++i) {
b[i] -= k;
}
for (int i = 0; i < (n); ++i) {
cin >> a[i];
--a[i];
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
if (!positions[x].empty()) {
int to = positions[x].back();
int from = positions[x].size() > 0 ? *--positions[x].rbegin() : 0;
for (int j = from; j <= to; ++j) {
b[j]--;
}
}
int from = positions[x].empty() ? 0 : (positions[x].back() + 1);
for (int j = from; j <= i; ++j) {
b[j]++;
}
long long res = getSum(b, dp, i + 1);
dp[i + 1] = res % 998244353;
positions[x].push_back(i);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[100010], dp[100010];
pair<int, int> b[100010];
vector<int> lst_ind[100010];
int ind[100010];
vector<pair<int, int> > cnt[400];
int acc[400];
int opm[400];
int p = 998244353;
int getBlock(int x) { return x / m; }
int getIndexInBlock(int x) { return x % m; }
void bruteForceUpdate(int bl) {
int st = bl * m;
int ed = min(n - 1, (bl + 1) * m - 1);
int getMin = 1e9 + 1, getMax = -1e9 - 1;
for (int i = st; i <= ed; i++) {
b[i].first += acc[bl];
getMin = min(b[i].first, getMin);
getMax = max(b[i].first, getMax);
}
acc[bl] = 0;
cnt[bl].clear();
for (int i = 0; i < getMax - getMin + 1; i++)
cnt[bl].push_back(pair<int, int>(getMin + i, 0));
for (int i = st; i <= ed; i++) {
cnt[bl][b[i].first - getMin].second += b[i].second;
}
for (int i = 1; i < cnt[bl].size(); i++) {
cnt[bl][i].second += cnt[bl][i - 1].second;
cnt[bl][i].second %= p;
}
opm[bl] = -1;
for (int i = 0; i < cnt[bl].size(); i++) {
if (cnt[bl][i].first <= k) {
opm[bl] = i;
}
}
}
void update(int l, int r, int v) {
int l_id = getBlock(l);
int r_id = getBlock(r);
for (int i = l_id + 1; i <= r_id - 1; i++) {
acc[i] += v;
if (opm[i] >= 0 && cnt[i][opm[i]].first + acc[i] > k) opm[i]--;
if (opm[i] + 1 < cnt[i].size() && cnt[i][opm[i] + 1].first + acc[i] <= k)
opm[i]++;
}
for (int i = l; i <= min(r, (l_id + 1) * m - 1); i++) {
b[i].first += v;
}
bruteForceUpdate(l_id);
if (l_id != r_id) {
for (int i = r_id * m; i <= r; i++) {
b[i].first += v;
}
bruteForceUpdate(r_id);
}
}
int getAns(int l, int r) {
int ans = 0;
int l_id = getBlock(l);
int r_id = getBlock(r);
for (int i = l_id + 1; i <= r_id - 1; i++) {
int indx = opm[i];
if (indx < 0) continue;
ans = (ans + cnt[i][indx].second) % p;
}
for (int i = l; i <= min(r, (l_id + 1) * m - 1); i++) {
if (b[i].first + acc[l_id] <= k) ans = (ans + b[i].second) % p;
}
if (l_id != r_id) {
for (int i = r_id * m; i <= r; i++) {
if (b[i].first + acc[r_id] <= k) ans = (ans + b[i].second) % p;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
lst_ind[a[i]].push_back(i);
ind[i] = lst_ind[a[i]].size() - 1;
}
m = sqrt(n);
b[0].second = 1;
for (int i = 0; i < n; i++) {
int val = a[i];
int id = ind[i];
if (id == 0) {
update(0, i, 1);
}
if (id > 0) {
int preID = lst_ind[val][id - 1];
update(preID + 1, i, 1);
if (id > 1) {
int preID2 = lst_ind[val][id - 2];
update(preID2 + 1, preID, -1);
} else {
update(0, preID, -1);
}
}
dp[i] = getAns(0, i);
b[i + 1].second = dp[i];
}
cout << dp[n - 1] << "\n";
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, a[100005], bl[100005], pre[100005], arr[100005], ans;
int presum[400], dpsum[400][3 * 400 + 20], cnt[100005], dp[100005];
inline void add(int &x, int a) { x += a, x >= 998244353 ? x -= 998244353 : 0; }
void reBuild(int x) {
for (int i = 0; i < 2 * 400 + 20; i++) {
if (presum[x] + i - (400 + 5) <= k) add(ans, 998244353 - dpsum[x][i]);
dpsum[x][i] = 0;
}
for (int i = max(400 * x, 1); i < 400 * (x + 1); i++) {
add(dpsum[x][cnt[i] + (400 + 5)], dp[i - 1]);
if (cnt[i] + presum[x] <= k) add(ans, dp[i - 1]);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i <= n; i++) bl[i] = i / 400;
ans = dp[0] = dpsum[0][0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
for (int j = bl[pre[arr[a[i]]]] + 1; j < bl[arr[a[i]]]; j++)
presum[j]--, add(ans, dpsum[j][k - presum[j] + (400 + 5)]);
for (int j = bl[arr[a[i]]] + 1; j < bl[i]; j++)
add(ans, 998244353 - dpsum[j][k - presum[j] + (400 + 5)]), presum[j]++;
for (int j = pre[arr[a[i]]] + 1;
j <= arr[a[i]] && bl[j] == bl[pre[arr[a[i]]]]; j++)
cnt[j]--;
reBuild(bl[pre[arr[a[i]]]]);
if (bl[pre[arr[a[i]]]] != bl[arr[a[i]]])
for (int j = arr[a[i]]; j >= pre[arr[a[i]]] && bl[j] == bl[arr[a[i]]];
j--)
cnt[j]--;
for (int j = arr[a[i]] + 1; bl[j] == bl[arr[a[i]]] && j <= i; j++) cnt[j]++;
reBuild(bl[arr[a[i]]]);
if (bl[arr[a[i]]] != bl[i]) {
for (int j = i; bl[j] == bl[i]; j--) cnt[j]++;
reBuild(bl[i]);
}
dp[i] = ans;
add(dpsum[bl[i]][cnt[i + 1] + (400 + 5)], dp[i]);
if (cnt[i + 1] + presum[bl[i]] <= k) add(ans, dp[i]);
pre[i] = arr[a[i]];
arr[a[i]] = i;
}
printf("%d\n", (dp[n] + 998244353) % 998244353);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxm = 320, tt = 998244353;
int n, m, n1, a[maxn], f[maxn], g[maxm][maxn], bl[maxn], sq, val[maxn],
tag[maxm];
int L[maxn], R[maxn];
int las[maxn], las1[maxn];
void build(int x, int l, int r) {
for (int i = l; i <= r; i++) {
g[x][val[i]] = (g[x][val[i]] + f[i - 1]) % tt;
}
for (int i = 1; i <= n1; i++) g[x][i] = (g[x][i] + g[x][i - 1]) % tt;
}
void change(int l, int r, int p, int now) {
int st = bl[l] + 1, ed = bl[r] - 1;
if (st <= ed)
for (int i = st; i <= ed; i++) tag[i] += p;
if (bl[l] != bl[r]) {
for (int i = l; i <= R[bl[l]]; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
if (bl[r] != bl[now] || (bl[r] == bl[now] && R[bl[now]] == now)) {
for (int i = L[bl[r]]; i <= r; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
} else {
for (int i = L[bl[r]]; i <= r; i++)
if (p == -1)
val[i]--;
else
val[i]++;
}
} else {
if (bl[r] != bl[now] || (bl[r] == bl[now] && R[bl[now]] == now)) {
for (int i = l; i <= r; i++)
if (p == -1)
(g[bl[i]][val[i] + p] += f[i - 1]) %= tt, val[i]--;
else
(((g[bl[i]][val[i]] -= f[i - 1]) %= tt) += tt) %= tt, val[i]++;
} else {
for (int i = l; i <= r; i++)
if (p == -1)
val[i]--;
else
val[i]++;
}
}
}
int query(int l, int r) {
int ret = 0;
for (int i = bl[l]; i < bl[r]; i++) ret = (ret + g[i][m - tag[i]]) % tt;
for (int i = L[bl[r]]; i <= r; i++)
if (val[i] + tag[bl[r]] <= m) ret = (ret + f[i - 1]) % tt;
return ret;
}
int main() {
scanf("%d%d", &n, &m);
n1 = m;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), n1 = max(n1, a[i]);
int ls = 0, bls = 0;
sq = sqrt(n);
for (int i = 1; i <= n; i++) {
bl[i] = bls + 1;
if (i - ls == sq || i == n) L[++bls] = ls + 1, R[bls] = i, ls = i;
}
ls = 0, bls = 0;
f[0] = 1;
for (int i = 1; i <= n; i++) {
if (i == ls + sq) {
build(++bls, ls + 1, i);
ls = i;
}
if (las[a[i]]) change(las1[a[i]] + 1, las[a[i]], -1, i);
change(las[a[i]] + 1, i, 1, i);
las1[a[i]] = las[a[i]], las[a[i]] = i;
f[i] = query(1, i);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
using namespace std;
using ll = long long;
int a[101001];
int b[101010];
int dp[101010];
int p1[101010];
int p2[101010];
int ans = 0;
int n, k;
void add(int* __restrict a, int* __restrict dp, int n) {
for (int i = 0; i < n; ++i) {
a[i]++;
ans -= a[i] == 0 ? dp[i] : 0;
ans += ans < 0 ? 998244353 : 0;
}
}
void sub(int* __restrict a, int* __restrict dp, int n) {
for (int i = 0; i < n; ++i) {
ans += a[i] == 0 ? dp[i] : 0;
a[i]--;
ans -= ans >= 998244353 ? 998244353 : 0;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; ++i) {
b[i] = -k - 1;
cin >> a[i];
--a[i];
p1[i] = p2[i] = -1;
}
dp[0] = 1;
ans = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
sub(b + p2[x] + 1, dp + p2[x] + 1, p1[x] - p2[x]);
add(b + p1[x] + 1, dp + p1[x] + 1, i - p1[x]);
dp[i + 1] = (int)ans;
ans = (ans + ans) % 998244353;
p2[x] = p1[x];
p1[x] = i;
}
cout << ans / 2 << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 100005, K = 400;
int n, k, a[N], x[N];
pair<int, int> last[N];
long long cnt[K + 5][K + K + 5], sum[K + 5], dp[N + 5], ans;
void build(int ind) {
int l = ind * K;
int r = min(l + K - 1, n - 1);
sum[ind] = 0;
for (int i = -K; i <= K; i++) cnt[ind][i + K] = 0;
for (int i = r; i >= l; i--) {
sum[ind] += x[i];
cnt[ind][sum[ind] + K] += (i == 0 ? 1ll : dp[i - 1]);
cnt[ind][sum[ind] + K] %= 998244353ll;
}
for (int i = -K + 1; i <= K; i++) {
cnt[ind][i + K] += cnt[ind][i + K - 1];
cnt[ind][i + K] %= 998244353ll;
}
}
void upd(int pos, int val) {
if (pos == -1) return;
x[pos] = val;
int ind = pos / K;
build(ind);
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) scanf("%d", a + i);
for (int i = 1; i <= n; i++) last[i] = make_pair(-1, -1);
for (int i = 0; i < n; i++) {
upd(last[a[i]].first, 0);
upd(last[a[i]].second, -1);
upd(i, 1);
last[a[i]] = make_pair(last[a[i]].second, i);
int s = 0;
int ind = i / K;
for (int j = i; j >= ind * K; j--) {
s += x[j];
if (s <= k) {
dp[i] += (j == 0 ? 1ll : dp[j - 1]);
dp[i] %= 998244353ll;
}
}
for (int j = ind - 1; j >= 0; j--) {
if (abs(k - s) <= K) {
dp[i] += cnt[j][k - s + K];
dp[i] %= 998244353ll;
}
s += sum[j];
}
}
cout << dp[n - 1] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
using namespace std;
template <class T1, class T2, class T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple() : a(T1()), b(T2()), c(T3()){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <class T1, class T2, class T3>
bool operator<(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else if (t1.b != t2.b)
return t1.b < t2.b;
else
return t1.c < t2.c;
}
template <class T1, class T2, class T3>
bool operator>(const triple<T1, T2, T3>& t1, const triple<T1, T2, T3>& t2) {
if (t1.a != t2.a)
return t1.a > t2.a;
else if (t1.b != t2.b)
return t1.b > t2.b;
else
return t1.c > t2.c;
}
inline int countBits(unsigned int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int countBits(unsigned long long v) {
unsigned int t = v >> 32;
unsigned int p = (v & ((1ULL << 32) - 1));
return countBits(t) + countBits(p);
}
inline int countBits(long long v) { return countBits((unsigned long long)v); }
inline int countBits(int v) { return countBits((unsigned int)v); }
unsigned int reverseBits(unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
template <class T>
inline int sign(T x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
inline bool isPowerOfTwo(int x) { return (x != 0 && (x & (x - 1)) == 0); }
constexpr long long power(long long x, int p) {
return p == 0 ? 1 : (x * power(x, p - 1));
}
template <class T>
inline bool inRange(const T& val, const T& min, const T& max) {
return min <= val && val <= max;
}
unsigned long long rdtsc() {
unsigned long long ret = 0;
return __builtin_readcyclecounter();
asm volatile("rdtsc" : "=A"(ret) : :);
return ret;
}
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template <class T = int>
T readInt() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir;
c();
bool m = false;
if (*__ir == '-') ++__ir, c(), m = true;
T r = 0;
while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
++__ir;
return m ? -r : r;
}
string readString() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
string r;
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c();
++__ir;
return r;
}
char readChar() {
auto c = [&]() {
if (__ir == __er)
std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur;
};
c();
while (*__ir && isspace(*__ir)) ++__ir, c();
return *__ir++;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template <class T>
void writeInt(T x, char endc = '\n') {
if (x < 0) *__iw++ = '-', x = -x;
if (x == 0) *__iw++ = '0';
char* s = __iw;
while (x) {
T t = x / 10;
char c = x - 10 * t + '0';
*__iw++ = c;
x = t;
}
char* f = __iw - 1;
while (s < f) swap(*s, *f), ++s, --f;
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
if (endc) {
*__iw++ = endc;
}
}
template <class T>
void writeStr(const T& str) {
int i = 0;
while (str[i]) {
*__iw++ = str[i++];
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
}
}
struct __FL__ {
~__FL__() {
if (__iw != __buw) cout.write(__buw, __iw - __buw);
}
};
static __FL__ __flushVar__;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p);
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const multimap<T1, T2, T3>& v);
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t);
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& i : v) {
if (!f) os << ", ";
os << i;
f = 0;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
bool f = 1;
os << "[";
for (auto& ii : v) {
if (!f) os << ", ";
os << "(" << ii.first << " -> " << ii.second << ") ";
f = 0;
}
return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
bool f = 1;
os << "[";
while (!v.empty()) {
auto x = v.top();
v.pop();
if (!f) os << ", ";
f = 0;
os << x;
}
return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const triple<T1, T2, T3>& t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
template <class T1, class T2>
void printarray(const T1& a, T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t);
template <class T1, class T2>
inline istream& operator>>(istream& os, pair<T1, T2>& p) {
return os >> p.first >> p.second;
}
template <class T>
inline istream& operator>>(istream& os, vector<T>& v) {
if (v.size())
for (T& t : v) os >> t;
else {
string s;
T obj;
while (s.empty()) {
getline(os, s);
if (!os) return os;
}
stringstream ss(s);
while (ss >> obj) v.push_back(obj);
}
return os;
}
template <class T1, class T2, class T3>
inline istream& operator>>(istream& os, triple<T1, T2, T3>& t) {
return os >> t.a >> t.b >> t.c;
}
template <class T1, class T2>
inline pair<T1, T2> operator+(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first + p2.first, p1.second + p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator+=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first += p2.first, p1.second += p2.second;
return p1;
}
template <class T1, class T2>
inline pair<T1, T2> operator-(const pair<T1, T2>& p1, const pair<T1, T2>& p2) {
return pair<T1, T2>(p1.first - p2.first, p1.second - p2.second);
}
template <class T1, class T2>
inline pair<T1, T2>& operator-=(pair<T1, T2>& p1, const pair<T1, T2>& p2) {
p1.first -= p2.first, p1.second -= p2.second;
return p1;
}
const int USUAL_MOD = 1000000007;
template <class T>
inline void normmod(T& x, T m = USUAL_MOD) {
x %= m;
if (x < 0) x += m;
}
template <class T1, class T2>
inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) {
T2 res = x + y;
if (res >= m) res -= m;
return res;
}
template <class T1, class T2, class T3 = int>
inline void addmodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x += y;
if (x >= m) x -= m;
}
template <class T1, class T2, class T3 = int>
inline void submodfast(T1& x, T2 y, T3 m = USUAL_MOD) {
x -= y;
if (x < 0) x += m;
}
inline long long mulmod(long long x, long long n, long long m) {
x %= m;
n %= m;
long long r =
x * n - long long(long double(x) * long double(n) / long double(m)) * m;
while (r < 0) r += m;
while (r >= m) r -= m;
return r;
}
inline long long powmod(long long x, long long n, long long m) {
long long r = 1;
normmod(x, m);
while (n) {
if (n & 1) r *= x;
x *= x;
r %= m;
x %= m;
n /= 2;
}
return r;
}
inline long long powmulmod(long long x, long long n, long long m) {
long long res = 1;
normmod(x, m);
while (n) {
if (n & 1) res = mulmod(res, x, m);
x = mulmod(x, x, m);
n /= 2;
}
return res;
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <class T>
T fast_gcd(T u, T v) {
int shl = 0;
while (u && v && u != v) {
T eu = u & 1;
u >>= eu ^ 1;
T ev = v & 1;
v >>= ev ^ 1;
shl += (~(eu | ev) & 1);
T d = u & v & 1 ? (u + v) >> 1 : 0;
T dif = (u - v) >> (sizeof(T) * 8 - 1);
u -= d & ~dif;
v -= d & dif;
}
return std::max(u, v) << shl;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
template <class T>
inline T gcd(T a, T b, T c) {
return gcd(gcd(a, b), c);
}
long long gcdex(long long a, long long b, long long& x, long long& y) {
if (!a) {
x = 0;
y = 1;
return b;
}
long long y1;
long long d = gcdex(b % a, a, y, y1);
x = y1 - (b / a) * y;
return d;
}
bool isPrime(long long n) {
if (n <= (1 << 14)) {
int x = (int)n;
if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
for (int i = 5; i * i <= x; i += 6)
if (x % i == 0 || x % (i + 2) == 0) return 0;
return 1;
}
long long s = n - 1;
int t = 0;
while (s % 2 == 0) {
s /= 2;
++t;
}
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
if (!(a %= n)) return true;
long long f = powmulmod(a, s, n);
if (f == 1 || f == n - 1) continue;
for (int i = 1; i < t; ++i)
if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
return false;
nextp:;
}
return true;
}
int32_t solve();
int32_t main(int argc, char** argv) {
ios_base::sync_with_stdio(0);
cin.tie(0);
return solve();
}
int a[101001];
int b[101010];
int dp[101010];
int p1[101010];
int p2[101010];
long long getSum(int* __restrict b, int* __restrict dp, int n) {
int r1 = 0;
int r2 = 0;
for (int j = 0; j < n; ++j) {
int t = (b[j] >> 31) & dp[j];
r1 += t >> 16;
r2 += t & ((1 << 16) - 1);
}
return (long long(r1) << 16) + r2;
}
template <int val>
void add(int* a, int n) {
for (int i = 0; i < n; ++i) {
a[i] += val;
}
}
int solve() {
int n = readInt();
int k = readInt();
for (int i = 0; i < (n); ++i) {
b[i] = -k - 1;
}
for (int i = 0; i < (n); ++i) {
a[i] = readInt() - 1;
p1[i] = p2[i] = -1;
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
add<-1>(b + p2[x] + 1, p1[x] - p2[x]);
add<1>(b + p1[x] + 1, i - p1[x]);
dp[i + 1] = getSum(b, dp, i + 1) % 998244353;
p2[x] = p1[x];
p1[x] = i;
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7, S = 335, mod = 998244353;
int sum[S][maxn], la[S], bl[maxn], ans[S], k, val[maxn], dp[maxn], L[S], R[S],
pre[maxn], c[maxn], n, a[maxn];
inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
inline int dic(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
inline void clear(int x) {
for (int i = L[x]; i <= R[x]; ++i) sum[x][i] = 0;
ans[x] = 0;
}
inline void rebuild(int x) {
for (int i = L[x]; i <= R[x]; ++i)
val[i] += la[x], sum[x][val[i]] = add(sum[x][val[i]], dp[i]),
ans[x] = add(ans[x], dp[i] * (val[i] <= k));
la[x] = 0;
return;
}
inline void modify(int l, int r, int t) {
if (bl[l] == bl[r]) {
clear(bl[l]);
for (int i = l; i <= r; ++i) val[i] += t;
rebuild(bl[l]);
return;
}
clear(bl[l]), clear(bl[r]);
for (int i = l; i <= R[bl[l]]; ++i) val[i] += t;
for (int i = L[bl[r]]; i <= r; ++i) val[i] += t;
rebuild(bl[l]), rebuild(bl[r]);
for (int i = bl[l] + 1; i < bl[r]; ++i) {
if (t == 1)
ans[i] = dic(ans[i], sum[i][k - la[i]]);
else
ans[i] = add(ans[i], sum[i][k - la[i] + 1]);
la[i] += t;
}
return;
}
inline int query(int l, int r) {
int res = 0;
if (bl[l] == bl[r]) {
for (int i = l; i <= r; ++i)
if (val[i] + la[bl[l]] <= k) res = add(res, dp[i]);
} else {
for (int i = l; i <= R[bl[l]]; ++i)
if (val[i] + la[bl[l]] <= k) res = add(res, dp[i]);
for (int i = L[bl[r]]; i <= r; ++i)
if (val[i] + la[bl[r]] <= k) res = add(res, dp[i]);
for (int i = bl[l] + 1; i < bl[r]; ++i) res = add(res, ans[i]);
}
return res;
}
inline void modify(int x, int y) {
clear(bl[x]);
rebuild(bl[x]);
}
int main() {
cin >> n >> k;
const int tn = sqrt(n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]), pre[i] = c[a[i]], c[a[i]] = i, bl[i] = (i - 1) / tn + 1;
if (bl[i] != bl[i - 1]) L[bl[i]] = i, R[bl[i - 1]] = i - 1;
}
R[bl[n]] = n;
dp[1] = 1;
rebuild(1);
for (int i = 1; i <= n; ++i) {
modify(pre[i] + 1, i, 1);
if (pre[i]) modify(pre[pre[i]] + 1, pre[i], -1);
dp[i + 1] = query(1, i);
modify(i + 1, dp[i + 1]);
}
cout << dp[n + 1] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
template <unsigned MOD>
struct ModInt {
static const unsigned static_MOD = MOD;
unsigned x;
void undef() { x = (unsigned)-1; }
bool isnan() const { return x == (unsigned)-1; }
inline int geti() const { return (int)x; }
ModInt() { x = 0; }
ModInt(const ModInt &y) { x = y.x; }
ModInt(int y) {
if (y < 0 || (int)MOD <= y) y %= (int)MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned y) {
if (MOD <= y)
x = y % MOD;
else
x = y;
}
ModInt(long long y) {
if (y < 0 || MOD <= y) y %= MOD;
if (y < 0) y += MOD;
x = y;
}
ModInt(unsigned long long y) {
if (MOD <= y)
x = y % MOD;
else
x = y;
}
ModInt &operator+=(const ModInt y) {
if ((x += y.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt y) {
if ((x -= y.x) & (1u << 31)) x += MOD;
return *this;
}
ModInt &operator*=(const ModInt y) {
x = (unsigned long long)x * y.x % MOD;
return *this;
}
ModInt &operator/=(const ModInt y) {
x = (unsigned long long)x * y.inv().x % MOD;
return *this;
}
ModInt operator-() const { return (x ? MOD - x : 0); }
ModInt inv() const {
unsigned a = MOD, b = x;
int u = 0, v = 1;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0) u += MOD;
return ModInt(u);
}
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) {
b = b.inv();
y = -y;
}
for (; y; y >>= 1) {
if (y & 1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
const long long MOD = 998244353;
const int MAXN = 100011;
const int MAGIC = 400;
const int SIZE = (MAXN + MAGIC - 1) / MAGIC;
int N, K;
int A[100011];
int prv[100011], pprv[100011];
struct Data {
int k, i;
ModInt<MOD> m;
Data() {
k = 0;
i = -1;
m = 0;
}
bool operator<(const Data &d) const { return k < d.k; }
};
struct Block {
int offset;
Data D[SIZE];
ModInt<MOD> sums[SIZE + 1];
int len;
ModInt<MOD> ans;
Block() {
offset = 0;
len = 0;
ans = 0;
}
void add(int diff) {
offset += diff;
calc();
}
void calc() {
Data key;
key.k = K - offset;
int pos = upper_bound(D, D + len, key) - D;
ans = sums[pos];
}
void add(int L, int R, int diff) {
for (int i = 0, i_len = (len); i < i_len; ++i) {
if (L <= D[i].i && D[i].i < R) {
D[i].k += diff;
}
}
build();
}
ModInt<MOD> sum(int L, int R) {
ModInt<MOD> ret = 0;
for (int i = 0, i_len = (len); i < i_len; ++i) {
if (L <= D[i].i && D[i].i < R && D[i].k + offset <= K) {
ret += D[i].m;
}
}
return ret;
}
void build() {
sort(D, D + len);
sums[0] = 0;
for (int i = 0, i_len = (len); i < i_len; ++i)
sums[i + 1] = sums[i] + D[i].m;
calc();
}
void ins(int i, ModInt<MOD> m) {
D[len].i = i;
D[len].m = m;
D[len].k = -offset;
len++;
}
} B[MAGIC];
void add(int L, int R, int diff) {
R--;
if (R < L) return;
int bl = L / MAGIC, br = R / MAGIC;
if (bl == br) {
B[bl].add(L, R + 1, diff);
} else {
for (int i = bl + 1; i < br; i++) {
B[i].offset += diff;
}
B[bl].add(L, R + 1, diff);
B[br].add(L, R + 1, diff);
}
}
ModInt<MOD> sum(int L, int R) {
R--;
assert(L <= R);
int bl = L / MAGIC, br = R / MAGIC;
ModInt<MOD> ret = 0;
if (bl == br) {
ret += B[bl].sum(L, R + 1);
} else {
for (int i = bl + 1; i < br; i++) {
ret += B[i].ans;
}
ret += B[bl].sum(L, R + 1);
ret += B[br].sum(L, R + 1);
}
return ret;
}
void insert(int i, ModInt<MOD> m) {
int b = i / MAGIC;
B[b].ins(i, m);
if (B[b].len == SIZE) B[b].build();
}
void MAIN() {
scanf("%d%d", &N, &K);
for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", A + i);
insert(0, 1);
for (int i = 0, i_len = (N); i < i_len; ++i) {
int a = A[i];
add(prv[a], i + 1, +1);
add(pprv[a], prv[a], -1);
pprv[a] = prv[a];
prv[a] = i + 1;
ModInt<MOD> m = sum(0, i + 1);
if (i == N - 1) {
printf("%d\n", m.geti());
return;
}
insert(i + 1, m);
}
}
int main() {
int TC = 1;
for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353, dt = 320;
int b[100010], a[100010], pre[100010], d[100010];
int f[100010];
int q[320][645];
int n, k, now;
int sum[320];
inline int mo(int x) {
if (x >= MOD) return x - MOD;
return x;
}
void ins(int x, int y) {
b[x] = y;
int z = x / dt;
if (z == now) return;
sum[z] = 0;
for (int i = 0; i <= dt + dt; i++) q[z][i] = 0;
for (int i = z * dt + dt - 1; i >= z * dt; i--)
sum[z] += b[i], q[z][sum[z] + dt] = mo(q[z][sum[z] + dt] + f[i]);
for (int i = 1; i <= dt + dt; i++) q[z][i] = mo(q[z][i] + q[z][i - 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), pre[i] = d[a[i]], d[a[i]] = i;
f[0] = 1;
for (int i = 1; i <= n; i++) {
now = i / dt;
ins(i - 1, 1);
if (pre[i]) {
ins(pre[i] - 1, -1);
if (pre[pre[i]]) ins(pre[pre[i]] - 1, 0);
}
int tot = 0;
for (int j = i - 1; j / dt == now && j >= 0; j--) {
tot += b[j];
if (tot <= k) f[i] = mo(f[i] + f[j]);
}
for (int j = now - 1; j >= 0; j--) {
if (tot <= k + dt) f[i] = mo(f[i] + q[j][min(dt + dt, k + dt - tot)]);
k += sum[j];
}
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int main() {
int n, k;
scanf("%d %d", &n, &k);
int arr[n + 1];
int gseg[n + 1];
for (int i = 1; i <= n; i++) {
gseg[i] = 0;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
int count[n + 1];
for (int i = 1; i <= n; i++) {
count[i] = 0;
}
int dist = 0;
for (int i = 1; i <= k; i++) {
if (count[arr[i]] == 0) {
dist++;
}
count[arr[i]]++;
}
for (int i = 1; i <= n && n - i + 1 > k; i++) {
int temp;
int index;
if (i != 1) {
if (count[arr[i - 1]] == 1) dist--;
if (count[arr[i + k - 1]] == 0) dist++;
count[arr[i - 1]]--;
count[arr[i + k - 1]]++;
}
temp = dist;
for (int j = i + k; j <= n; j++) {
if (count[arr[j]] == 0) temp++;
count[arr[j]]++;
if (temp > k) {
index = j;
break;
} else {
gseg[i] = j;
}
}
for (int j = index; j >= i + k; j--) {
count[arr[j]]--;
}
}
int dp[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] = 0;
}
dp[1] = 1;
dp[0] = 1;
for (int i = 2; i <= n; i++) {
for (int j = i; j >= 1; j--) {
int size = i - j + 1;
if (size <= k) {
dp[i] += dp[j - 1];
}
if (size > k) {
if (gseg[j] < i) {
break;
} else {
dp[i] += dp[j - 1];
}
}
}
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 100005;
const int NN = 350;
int add(int x, int y) {
x = x + y;
return x >= MOD ? x - MOD : x;
}
int dec(int x, int y) {
x = x - y;
return x < 0 ? x + MOD : x;
}
int mul(int x, int y) { return (long long)x * y % MOD; }
int n, k;
int a[N];
int bel[N];
int L[N], R[N];
int siz;
int g[NN][N];
int lzy[N];
int f[N], h[N];
int lst[N], lst1[N];
void Init() {
scanf("%d%d", &n, &k);
siz = sqrt(n);
for (int u = 1; u <= n; u++) scanf("%d", &a[u]);
for (int u = 0; u <= n; u++) bel[u] = u / siz + 1;
for (int u = 0; u <= n; u++) R[bel[u]] = u;
for (int u = n; u >= 0; u--) L[bel[u]] = u;
for (int u = 1; u <= n; u++) lst[u] = lst1[u] = 0;
}
void bt(int x) {
for (int u = L[x]; u <= R[x]; u++) g[x][h[u]] = add(g[x][h[u]], f[u]);
for (int u = 1; u <= n; u++) g[x][u] = add(g[x][u], g[x][u - 1]);
}
void Dec(int l, int r, int x) {
for (int u = r; u >= max(L[bel[x]], l); u--) h[u]--;
r = min(r, L[bel[x]] - 1);
if (r < l) return;
if (bel[l] == bel[r]) {
for (int u = l; u <= r; u++) {
h[u]--;
g[bel[l]][h[u]] = add(g[bel[l]][h[u]], f[u]);
}
return;
}
for (int u = bel[l] + 1; u < bel[r]; u++) lzy[u]--;
for (int u = l; u <= R[bel[l]]; u++) {
h[u]--;
g[bel[l]][h[u]] = add(g[bel[l]][h[u]], f[u]);
}
for (int u = L[bel[r]]; u <= r; u++) {
h[u]--;
g[bel[r]][h[u]] = add(g[bel[r]][h[u]], f[u]);
}
}
void Add(int l, int r, int x) {
for (int u = r; u >= max(L[bel[x]], l); u--) h[u]++;
r = min(r, L[bel[x]] - 1);
if (r < l) return;
if (bel[l] == bel[r]) {
for (int u = l; u <= r; u++) {
g[bel[l]][h[u]] = dec(g[bel[l]][h[u]], f[u]);
h[u]++;
}
return;
}
for (int u = bel[l] + 1; u < bel[r]; u++) lzy[u]++;
for (int u = l; u <= R[bel[l]]; u++) {
g[bel[l]][h[u]] = dec(g[bel[l]][h[u]], f[u]);
h[u]++;
}
for (int u = L[bel[r]]; u <= r; u++) {
g[bel[r]][h[u]] = dec(g[bel[r]][h[u]], f[u]);
h[u]++;
}
}
int calc(int x) {
int lalal = 0;
for (int u = L[bel[x]]; u < x; u++)
if (h[u] <= k) lalal = add(lalal, f[u]);
for (int u = 1; u < bel[x]; u++) {
int t = k - lzy[u];
if (t < 0) continue;
lalal = add(lalal, g[u][t]);
}
return lalal;
}
void solve() {
h[0] = 0;
f[0] = 1;
if (0 == R[bel[0]]) bt(bel[0]);
for (int u = 1; u <= n; u++) {
int x = a[u];
if (lst[x] != 0) Dec(lst1[x], lst[x] - 1, u);
Add(lst[x], u - 1, u);
lst1[x] = lst[x];
lst[x] = u;
f[u] = calc(u);
if (u == R[bel[u]]) bt(bel[u]);
}
printf("%d\n", f[n]);
}
int main() {
Init();
solve();
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
const int MOD = 1e9 + 7;
const int PIERW = 300;
int ciag[MAX];
int ostatnie[MAX];
int poprzedni[MAX];
long long pomocnicza[MAX];
long long DP[MAX];
struct Blok {
int lewa, prawa;
long long suma;
long long sumaDP[PIERW * 2 + 1];
void Aktualizuj() {
for (int i = 0; i <= 2 * PIERW; ++i) sumaDP[i] = 0;
suma = 0;
for (int i = prawa; i >= lewa; --i) {
suma += pomocnicza[i];
if (lewa) sumaDP[suma + PIERW] = (sumaDP[suma + PIERW] + DP[i - 1]) % MOD;
}
for (int i = 1; i <= 2 * PIERW; i++)
sumaDP[i] = (sumaDP[i] + sumaDP[i - 1]) % MOD;
}
};
Blok bloki[MAX / PIERW + 7];
int main() {
int n, k;
cin >> n >> k;
DP[0] = 1;
for (int i = 0; i * PIERW <= n; ++i) {
bloki[i].lewa = i * PIERW;
bloki[i].prawa = (i + 1) * PIERW - 1;
bloki[i].Aktualizuj();
}
for (int i = 1; i <= n; ++i) {
cin >> ciag[i];
poprzedni[i] = ostatnie[ciag[i]];
ostatnie[ciag[i]] = i;
pomocnicza[i] = 1;
bloki[i / PIERW].Aktualizuj();
if (poprzedni[i]) {
pomocnicza[poprzedni[i]] = -1;
bloki[poprzedni[i] / PIERW].Aktualizuj();
if (poprzedni[poprzedni[i]]) {
int t = poprzedni[poprzedni[i]];
pomocnicza[t] = 0;
bloki[t / PIERW].Aktualizuj();
}
}
int blok = i / PIERW;
int suma = 0;
for (int j = i - 1; j >= bloki[blok].lewa; j--) {
if (suma <= k) DP[i] += DP[j];
suma += pomocnicza[j];
}
for (int j = blok - 1; j >= 0; --j) {
int x = k - suma;
if (-PIERW <= x && x <= PIERW)
DP[i] += bloki[blok].sumaDP[x + PIERW];
else if (x < PIERW)
DP[i] += bloki[blok].sumaDP[2 * PIERW];
suma += bloki[blok].suma;
}
}
cout << DP[n] << "\n";
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline void read(long long &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
x = flag ? -x : x;
}
inline void write(long long x) {
static const long long maxlen = 100;
static char s[maxlen];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
long long len = 0;
for (; x; x /= 10) s[len++] = x % 10 + '0';
for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}
const long long MAXN = 110000;
const long long MAX_block = 336;
const long long MAX_val = 672;
const long long AVG_val = 336;
long long belong[MAXN];
long long l[MAX_block], r[MAX_block], cnt;
long long add[MAX_block];
long long num[MAX_block][MAX_val];
long long val[MAXN];
long long n, lim;
void prepare() {
long long N = sqrt(n);
for (long long i = 1; i <= n; i += N) {
++cnt;
l[cnt] = i;
r[cnt] = i + N - 1;
}
r[cnt] = min(r[cnt], n);
for (long long i = 1; i <= cnt; i++)
for (long long j = l[i]; j <= r[i]; j++) belong[j] = i;
for (long long i = 1; i <= cnt; i++) add[i] = 0;
memset(num, 0, sizeof(num));
}
const long long P = 998244353;
long long a[MAXN];
long long f[MAXN];
void modify(long long a, long long b, long long tag) {
if (belong[a] == belong[b]) {
long long now = belong[a];
for (long long i = a; i <= b; i++) {
if (tag == 1) {
long long &x = num[now][val[i] + AVG_val];
x = (x - f[i - 1] + P) % P;
} else {
long long &x = num[now][val[i] + AVG_val - 1];
x = (x + f[i - 1]) % P;
}
val[i] += tag;
}
return;
}
for (long long i = belong[a] + 1; i < belong[b]; i++) add[i] += tag;
long long now = belong[a];
for (long long i = a; i <= r[now]; i++) {
if (tag == 1) {
long long &x = num[now][val[i] + AVG_val];
x = (x - f[i - 1] + P) % P;
} else {
long long &x = num[now][val[i] + AVG_val - 1];
x = (x + f[i - 1]) % P;
}
val[i] += tag;
}
now = belong[b];
for (long long i = l[now]; i <= b; i++) {
if (tag == 1) {
long long &x = num[now][val[i] + AVG_val];
x = (x - f[i - 1] + P) % P;
} else {
long long &x = num[now][val[i] + AVG_val - 1];
x = (x + f[i - 1]) % P;
}
val[i] += tag;
}
}
long long query() {
long long sum = 0;
for (long long i = 1; i <= cnt; i++) {
sum = (sum + num[i][lim - add[i] + AVG_val]) % P;
}
return sum;
}
long long last[MAXN];
long long pre[MAXN];
int main() {
read(n);
read(lim);
for (long long i = 1; i <= n; i++) read(a[i]);
prepare();
f[0] = 1;
for (long long i = 1; i <= n; i++) {
pre[i] = last[a[i]];
long long now = belong[i];
for (long long j = AVG_val; j < MAX_val; j++)
num[now][j] = (f[i - 1] + num[now][j]) % P;
modify(pre[i] + 1, i, 1);
if (pre[i] != 0) {
modify(pre[pre[i]] + 1, pre[i], -1);
}
f[i] = query();
last[a[i]] = i;
}
cout << f[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int B = 323;
const int N = 1e5 + 5;
const int MOD = 998244353;
namespace {
int Add(int a, int b) { return (a += b) >= MOD ? a - MOD : a; }
int Sub(int a, int b) { return (a -= b) < 0 ? a + MOD : a; }
int Mul(int a, int b) { return (long long)a * b % MOD; }
} // namespace
int n, k;
int a[N], fr[N], ls[N], bl[N], d[N], dp[N];
int sm[N / B + 1], gd[N / B + 1][B * 2 + 1];
void Set(int x, int _v) {
int b = bl[x], *f = gd[b];
int l = (b - 1) * B + 1, r = min(n, b * B);
sm[b] += _v - d[x];
d[x] = _v;
memset(f, 0, sizeof gd[b]);
for (int i = r, s = 0; i >= l; --i) {
f[B + s] = Add(f[B + s], dp[i]);
s += d[i];
}
for (int i = 1; i <= B * 2; ++i) {
f[i] = Add(f[i], f[i - 1]);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
fr[i] = ls[a[i]];
ls[a[i]] = i;
bl[i] = (i - 1) / B + 1;
}
for (int i = 1; i <= n; ++i) {
Set(i, 1);
if (fr[i]) {
Set(fr[i], -1);
if (fr[fr[i]]) {
Set(fr[fr[i]], 0);
}
}
int s = 0;
for (int j = bl[i]; j; --j) {
if (abs(k - s) <= B) {
dp[i] = Add(dp[i], gd[j][B + k - s]);
}
s += sm[j];
}
if (s <= k) {
dp[i] = Add(dp[i], 1);
}
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>
# include <unordered_map>
# include <unordered_set>
//#ifdef LOCAL
//# include <opencv2/core/core.hpp>
//# include <opencv2/highgui/highgui.hpp>
//# include <opencv2/imgproc/imgproc.hpp>
//#endif
using namespace std;
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a) cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"
#ifdef _MSC_VER
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif
#if defined _MSC_VER
#define ASSUME(condition) ((void)0)
#define __restrict
#elif defined __clang__
#define ASSUME(condition) __builtin_assume(condition)
#elif defined __GNUC__
[[noreturn]] __attribute__((always_inline)) inline void undefinedBehaviour() {}
#define ASSUME(condition) ((condition) ? true : (undefinedBehaviour(), false))
#endif
#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME()
#endif
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;
template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>
#define FI(n) for(int i=0;i<(n);++i)
#define FJ(n) for(int j=0;j<(n);++j)
#define FK(n) for(int k=0;k<(n);++k)
#define FL(n) for(int l=0;l<(n);++l)
#define FQ(n) for(int q=0;q<(n);++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)
#define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b)
#define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; }
#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); }
inline int countBits(ll v){return countBits((ull)v); }
inline int countBits(int v){return countBits((uint)v); }
unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); }
template<class T>
inline bool inRange(const T& val, const T& min, const T& max) { return min <= val && val <= max; }
//template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() { unsigned long long ret = 0;
#ifdef __clang__
return __builtin_readcyclecounter();
#endif
#ifndef _MSC_VER
asm volatile("rdtsc" : "=A" (ret) : :);
#endif
return ret; }
// Fast IO ********************************************************************************************************
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template<class T = int> T readInt() {
auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
++__ir; return m ? -r : r;
}
string readString() {
auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
string r; c(); while (*__ir && isspace(*__ir)) ++__ir, c();
while (!isspace(*__ir)) r.push_back(*__ir), ++__ir, c();
++__ir; return r;
}
char readChar() {
auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
c(); while (*__ir && isspace(*__ir)) ++__ir, c(); return *__ir++;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template<class T>
void writeInt(T x, char endc = '\n') {
if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';
char* s = __iw;
while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }
char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;
if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
if (endc) { *__iw++ = endc; }
}
template<class T>
void writeStr(const T& str) {
int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }
}
struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };
static __FL__ __flushVar__;
//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) { bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){ bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }
//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
if (v.size()) for (T& t : v) os >> t; else {
string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
stringstream ss(s); while (ss >> obj) v.push_back(obj);
}
return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }
//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }
#undef TT1
#undef TT1T2
#undef TT1T2T3
#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)
#ifdef LOCAL
#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;
#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;
#else
#define BEGIN_PROFILE(idx, name)
#define END_PROFILE
#endif
const int USUAL_MOD = 1000000007;
template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; }
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
template<class T>
T fast_gcd(T u, T v) {
int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;
shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;
} return std::max(u, v) << shl;
}
inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
if (!a) { x = 0; y = 1; return b; }
ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
return d;
}
bool isPrime(long long n) {
if (n <= (1 << 14)) { int x = (int) n; if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
long long s = n - 1; int t = 0; while (s % 2 == 0) { s /= 2; ++t; }
for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
return false; nextp:;
} return true;
}
// Useful constants
//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431,
#define T9 1000000000
#define T18 1000000000000000000LL
#define INF 1011111111
#define LLINF 1000111000111000111LL
#define mod 1000000007
#define fftmod 998244353
#define EPS 1e-10
#define PI 3.14159265358979323846264
#define link himomisubmitted
#define rank thatsnoteasytask
//*************************************************************************************
int32_t solve();
int32_t main(int argc, char** argv) {
ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
FREIN("input.txt");
// FREOUT("out.txt");
#endif
return solve();
}
int a[101001];
int b[101010];
int dp[104040];
vector<int> positions[101010];
__m256i _mm256_load_pi32x4(const int *x) {
return _mm256_cvtepi32_epi64(_mm_load_si128((const __m128i*)x));
}
__m256i _mm256_set_pi32x4(int x) {
return _mm256_cvtepi32_epi64(_mm_set1_epi32(x));
}
ll getSum(int* __restrict b, int* __restrict dp, int n) {
ll res = 0;
__m256i y0 = _mm256_set_pi32x4(0);
__m256i y7 = _mm256_set_pi32x4(0);
while (n % 16) {
--n;
res += b[0] > 0 ? 0 : dp[0];
++b;
++dp;
}
n /= 4;
for (int j = 0; j < n; ++j) {
__m256i y1 = _mm256_load_pi32x4(b);
__m256i y2 = _mm256_load_pi32x4(dp);
y1 = _mm256_cmpgt_epi64(y1, y0);
y7 = _mm256_add_epi64(y7, _mm256_and_si256(y1, y2));
b += 4;
dp += 4;
}
ll arr[4];
_mm256_store_si256((__m256i*)arr, y7);
return res + arr[0] + arr[1] + arr[2] + arr[3];
}
int solve() {
int n = readInt();
int k = readInt();
FI(n) {
b[i] += k + 1;
}
FI(n) {
a[i] = readInt();
--a[i];
positions[i].push_back(-1);
positions[i].push_back(-1);
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
{
int to = positions[x].back();
int from = *++positions[x].rbegin() + 1;
for (int j = from; j <= to; ++j) {
b[j]++;
}
}
int from = positions[x].back() + 1;
for (int j = from; j <= i; ++j) {
b[j]--;
}
ll res = getSum(b, dp, i + 1);
dp[i + 1] = res % fftmod;
positions[x].push_back(i);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation | Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2]. | {
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
} | {
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MN = 100010;
const int SQ = 400;
const int BS = (MN + SQ - 1) / SQ;
int N, K;
int A[MN], la[MN], nxt[MN], dp[MN];
int val[MN], sum[BS][MN << 1], offset[BS];
int cost;
void update(int l, int r, int t) {
if (l > r) return;
if (l / SQ == r / SQ) {
for (int i = l; i <= r; i++) {
int b = l / SQ;
if (t) {
if (val[i] == K + offset[b]) {
cost += mod - dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]++;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
} else {
if (val[i] == K + 1 + offset[b]) {
cost += dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]--;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
}
}
} else {
for (int i = l; i < (l / SQ + 1) * SQ; i++) {
int b = l / SQ;
if (t) {
if (val[i] == K + offset[b]) {
cost += mod - dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]++;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
} else {
if (val[i] == K + 1 + offset[b]) {
cost += dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]--;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
}
}
for (int i = l / SQ + 1; i < r / SQ; i++) {
if (t) {
cost += mod - sum[i][K + offset[i]];
cost %= mod;
offset[i]++;
} else {
cost += sum[i][K + 1 + offset[i]];
cost %= mod;
offset[i]--;
}
}
for (int i = r / SQ * SQ; i <= r; i++) {
int b = r / SQ;
if (t) {
if (val[i] == K + offset[b]) {
cost += mod - dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]++;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
} else {
if (val[i] == K + 1 + offset[b]) {
cost += dp[i + 1];
cost %= mod;
}
sum[b][val[i]] += mod - dp[i + 1];
sum[b][val[i]] %= mod;
val[i]--;
sum[b][val[i]] += dp[i + 1];
sum[b][val[i]] %= mod;
}
}
}
}
int main() {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
for (int i = 0; i < MN; i++) la[i] = N;
for (int i = N - 1; i >= 0; i--) {
nxt[i] = la[A[i]];
la[A[i]] = i;
}
for (int i = 0; i < BS; i++) offset[i] = MN;
dp[N] = 1;
for (int i = N - 1; i >= 0; i--) {
int j = nxt[i];
int k = nxt[j];
val[i] = offset[i / SQ];
sum[i / SQ][val[i]] += dp[i + 1];
sum[i / SQ][val[i]] %= mod;
cost += dp[i + 1];
cost %= mod;
update(i, j - 1, 1);
update(j, k - 1, 0);
dp[i] = cost;
}
printf("%d", dp[0]);
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.