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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
int ch = 0, f = 0;
x = 0;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = 1;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if (f) x = -x;
}
const int M = 205, N = 100005, L = 100000, mod = 998244353;
int a[N], bel[N], tag[N], tot[N], ans[M], s[M][N << 1], pre[N], lst[N], dp[N],
n, k, T;
inline void up(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; }
inline void gao(int i, int x) {
up(s[bel[i]][L + tot[i]], mod - dp[i]);
tot[i] += x;
up(s[bel[i]][L + tot[i]], dp[i]);
if (x == 1 && tot[i] + tag[bel[i]] == k + 1) up(ans[bel[i]], mod - dp[i]);
if (x == -1 && tot[i] + tag[bel[i]] == k) up(ans[bel[i]], dp[i]);
}
inline void modify(int l, int r, int x) {
if (bel[l] + 1 >= bel[r]) {
for (int i = l; i <= r; i++) gao(i, x);
return;
}
for (int i = l; i < (bel[l] + 1) * T; i++) gao(i, x);
for (int i = bel[r] * T; i <= r; i++) gao(i, x);
for (int i = bel[l] + 1; i < bel[r]; i++) {
if (x == 1) up(ans[i], mod - s[i][L + k - tag[i]]);
if (x == -1) up(ans[i], s[i][L + k - tag[i] + 1]);
tag[i] += x;
}
}
inline int query(int l, int r) {
int res = 0;
if (bel[l] + 1 >= bel[r]) {
for (int i = l; i <= r; i++)
if (tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
return res;
}
for (int i = l; i < (bel[l] + 1) * T; i++)
if (tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
for (int i = bel[r] * T; i <= r; i++)
if (tot[i] + tag[bel[i]] <= k) up(res, dp[i]);
for (int i = bel[l] + 1; i < bel[r]; i++) up(res, ans[i]);
return res;
}
int main() {
read(n), read(k), T = 500;
for (int i = 0; i <= n; i++) bel[i] = i / T;
for (int i = 1; i <= n; i++) read(a[i]);
dp[0] = 1;
up(ans[bel[0]], dp[0]);
up(s[bel[0]][L - tag[bel[0]]], dp[0]);
for (int i = 1; i <= n; i++) {
pre[i] = lst[a[i]], lst[a[i]] = i;
modify(pre[i], i - 1, 1);
if (pre[i]) modify(pre[pre[i]], pre[i] - 1, -1);
dp[i] = query(0, i - 1);
up(ans[bel[i]], dp[i]);
up(s[bel[i]][L - tag[bel[i]]], dp[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int arr = 2e5 + 10;
const int ar = 2e3 + 10;
const long double pi = acos(-1);
const long double eps = 1e-10;
const long long md = 1e9 + 7;
void add(int& a, int b) {
a += b;
if (a >= (long long)(998244353)) {
a -= (long long)(998244353);
}
if (a < 0) {
a += (long long)(998244353);
}
}
int n, k;
int sum_block[((int)(1e5 + 10) >> 8) + 2][(int)(1e5 + 10)];
int push_block[((int)(1e5 + 10) >> 8) + 2];
int last_value[(int)(1e5 + 10)];
int dp[(int)(1e5 + 10)];
pair<int, int> last_seg[(int)(1e5 + 10)];
int ans;
inline bool in(int a, int b, int c) { return a <= b && b < c; }
inline void inc_pos(int pos) {
if (last_value[pos] + push_block[pos >> 8] == k) {
add(ans, -dp[pos]);
}
add(sum_block[pos >> 8][last_value[pos]], -dp[pos]);
last_value[pos]++;
add(sum_block[pos >> 8][last_value[pos]], +dp[pos]);
}
inline void dec_pos(int pos) {
if (last_value[pos] + push_block[pos >> 8] == k + 1) {
add(ans, +dp[pos]);
}
add(sum_block[pos >> 8][last_value[pos]], -dp[pos]);
last_value[pos]--;
add(sum_block[pos >> 8][last_value[pos]], +dp[pos]);
}
void inc(int l, int r) {
if ((r >> 8) - (l >> 8) + 1 <= 2) {
for (int i = l; i <= r; i++) {
inc_pos(i);
}
} else {
while ((l & 255) != 0) {
inc_pos(l);
l++;
}
while ((r & 255) != 255) {
inc_pos(r);
r--;
}
for (int i = (l >> 8); i <= (r >> 8); i++) {
if (in(0, k - push_block[i], (int)(1e5 + 10))) {
add(ans, -sum_block[i][k - push_block[i]]);
}
push_block[i]++;
}
}
}
void dec(int l, int r) {
if ((r >> 8) - (l >> 8) + 1 <= 2) {
for (int i = l; i <= r; i++) {
dec_pos(i);
}
} else {
while ((l & 255) != 0) {
dec_pos(l);
l++;
}
while ((r & 255) != 255) {
dec_pos(r);
r--;
}
for (int i = (l >> 8); i <= (r >> 8); i++) {
if (in(0, k + 1 - push_block[i], (int)(1e5 + 10))) {
add(ans, +sum_block[i][k + 1 - push_block[i]]);
}
push_block[i]--;
}
}
}
int a[(int)(1e5 + 10)];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sum_block[0][0] = 1;
last_value[0] = 0;
dp[0] = 1;
ans = 1;
map<int, int> last;
for (int i = 1; i <= n; i++) {
if (!last.count(a[i])) {
inc(0, i - 1);
last_seg[i] = make_pair(0, i - 1);
} else {
auto L = last_seg[last[a[i]]];
dec(L.first, L.second);
inc(last[a[i]], i - 1);
last_seg[i] = make_pair(last[a[i]], i - 1);
}
last[a[i]] = i;
dp[i] = ans;
add(ans, dp[i]);
add(sum_block[i >> 8][0], dp[i]);
}
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"
]
} | 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 = 250, 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,popcnt,tune=native")
using namespace std;
const int MN = 100005, inf = 1000000005, mod = 998244353;
const long long INF = 1000000000000000005LL;
int t[MN], dp[MN], ile[MN], ost[MN], gdzie[MN];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i]);
gdzie[i] = ost[t[i]];
ost[t[i]] = i;
}
dp[0] = 1;
long long res = 0LL;
for (int i = 1; i <= n; ++i) {
int a = gdzie[i], b = gdzie[gdzie[i]];
for (int j = a; j < i; ++j) ++ile[j];
for (int j = b; j < a; ++j) --ile[j];
for (int j = 0; j < i; ++j) res += (ile[j] <= k) * dp[j];
res %= mod;
dp[i] = res;
res = 0LL;
}
printf("%d", 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1, mod = 998244353, b_sz = 10;
int n, k;
int last_occ[maxn], prev_occ[maxn];
int delta[maxn];
int dp[maxn];
int tot_delta[10001], psums[10001][2 * b_sz + 1];
void update(int i) {
vector<int> blocks = {i / b_sz};
if (i % b_sz == 0 and i) {
blocks.push_back(i / b_sz - 1);
}
delta[i] = 1;
if (prev_occ[i]) {
blocks.push_back(prev_occ[i] / b_sz);
delta[prev_occ[i]] = -1;
}
if (prev_occ[prev_occ[i]]) {
blocks.push_back(prev_occ[prev_occ[i]] / b_sz);
delta[prev_occ[prev_occ[i]]] = 0;
}
sort(blocks.begin(), blocks.end());
blocks.resize(unique(blocks.begin(), blocks.end()) - blocks.begin());
for (auto block : blocks) {
memset(psums[block], 0, sizeof(psums[block]));
int curr_unique = 0;
for (int j = min(i, (block + 1) * b_sz - 1); j >= block * b_sz; j--) {
psums[block][curr_unique + b_sz] =
(psums[block][curr_unique + b_sz] + dp[j]) % mod;
curr_unique += delta[j];
}
for (int j = 1; j <= 2 * b_sz; j++) {
psums[block][j] = (psums[block][j] + psums[block][j - 1]) % mod;
}
tot_delta[block] = curr_unique;
}
}
void solve(int i) {
int curr_unique = 0;
for (int block = i / b_sz; block >= 0; block--) {
if (k - curr_unique >= -b_sz) {
dp[i] = (dp[i] + psums[block][min(b_sz, k - curr_unique) + b_sz]) % mod;
}
curr_unique += tot_delta[block];
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
prev_occ[i] = last_occ[a];
last_occ[a] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
update(i);
solve(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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
int n, k, a[100001], P = 998244353, lcol[100001], last[100001], f[100001],
val[100001], pos[100001], block;
inline int sub(int a, const int &b) {
a -= b;
return a < 0 ? a + P : a;
}
inline int add(int a, const int &b) {
a += b;
return a >= P ? a - P : a;
}
inline int mul(const int &a, const int &b) {
return (long long)a * b - (long long)a * b / P * P;
}
struct sum {
int num[200001], tot, delta;
sum() {
memset(num, 0, sizeof num);
tot = 0;
}
void Add(const int &x) { tot = add(tot, x); }
void upd(const int &x, int &pos, const int &del) {
if (del == 1)
num[pos + n] = sub(num[pos + n], x);
else
num[pos - 1 + n] = add(num[pos - 1 + n], x);
pos += del;
}
int query() { return add(k >= delta ? tot : 0, num[k - delta + n]); }
} S[210];
void update(int L, int R, int l) {
if (pos[L] == pos[R]) {
for (int i = L; i <= R; i++) S[pos[i]].upd(f[i], val[i], l);
} else {
for (int i = L; pos[i] == pos[L]; ++i) S[pos[i]].upd(f[i], val[i], l);
for (int i = pos[L] + 1; i < pos[R]; ++i) S[i].delta += l;
for (int i = R; pos[i] == pos[R]; --i) S[pos[i]].upd(f[i], val[i], l);
}
}
int main() {
scanf("%d%d", &n, &k);
block = 500;
for (int i = 0; i <= n; i++) pos[i] = i / block + 1;
f[0] = 1;
S[1].Add(1);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
last[i] = lcol[a[i]];
lcol[a[i]] = i;
update(last[i], i - 1, 1);
if (last[i]) update(last[last[i]], last[i] - 1, -1);
for (int j = pos[i]; j; --j) f[i] = add(f[i], S[j].query());
S[pos[i]].Add(f[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int N = 1e5 + 5, M = N / 320 + 3;
int bel[N], f[N], g[N], tag[M], s[M][320 + 3 << 1];
inline int read() {
int now = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar())
;
for (; isdigit(c); now = now * 10 + c - 48, c = getchar())
;
return now;
}
void Update(int p, int v) {
int *s = ::s[bel[p]];
for (int i = 320; i <= 320 << 1; ++i)
(s[i] += v) >= 998244353 && (s[i] -= 998244353);
}
void Modify(int p, int v) {
int bel = ::bel[p], *s = ::s[bel];
tag[bel] += v;
for (int i = bel * 320 + 1; i <= p; ++i) {
if (v == 1)
(s[g[i] + 320] += 998244353 - f[i - 1]) >= 998244353 &&
(s[g[i] + 320] -= 998244353);
else
(s[g[i] - 1 + 320] += f[i - 1]) >= 998244353 &&
(s[g[i] - 1 + 320] -= 998244353),
(s[g[i] - 2 + 320] += f[i - 1]) >= 998244353 &&
(s[g[i] - 2 + 320] -= 998244353);
g[i] += v;
}
}
int Query(int p, int K) {
int bel = ::bel[p], sum = tag[bel];
long long res = 0;
for (int i = bel * 320 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]);
while (bel--) {
if (std::abs(sum - K) <= 320)
res += s[bel][K - sum + 320];
else if (sum < K)
res += s[bel][320 << 1];
sum += tag[bel];
}
return res % 998244353;
}
int main() {
static int las[N], pre[N];
int n = read(), K = read();
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 320;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int a = read();
las[i] = pre[a], pre[a] = i;
Update(i, f[i - 1]), Modify(i, 1);
if (las[i]) {
Modify(las[i], -2);
if (las[las[i]]) Modify(las[las[i]], 1);
}
f[i] = Query(i, K);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const int N = 100100;
const int Q = 250200;
const long long mod = 998244353;
using namespace std;
int n;
int k;
int d[N];
int f[N];
int l[N];
int p[N];
void add(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
} else if (x < 0) {
x += mod;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
int s = 1, x, h;
d[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> x;
for (int j = l[x] + 1; j <= i; j++) {
if (f[j] == k) {
s -= d[j - 1];
if (s < 0) {
s += mod;
}
}
f[j] += 1;
}
h = p[x];
for (int j = l[x]; j > h; j--) {
f[j] -= 1;
if (f[j] == k) {
s += d[j - 1];
if (s >= mod) {
s -= mod;
}
}
}
p[x] = l[x];
l[x] = i;
d[i] = s;
add(s, s);
}
cout << d[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#pragma warning(disable : 4996)
using namespace std;
const long long int mod = 998244353;
long long int sum = 1;
void add(int* anums, long long int* aanss, int X, bool flag) {
if (flag) {
for (int x = 0; x < X; ++x) {
anums[x]--;
if (!anums[x]) sum += aanss[x];
}
} else {
for (int x = 0; x < X; ++x) {
if (!anums[x]) sum -= aanss[x];
anums[x]++;
}
}
}
int nums[100010];
int pres[100010];
long long int anss[100010];
int main() {
int N, M;
cin >> N >> M;
vector<int> v(N);
for (int i = 0; i < N; ++i) {
scanf("%d", &v[i]);
v[i]--;
}
map<int, int> mp;
for (int i = 0; i < N; ++i) {
if (mp.find(v[i]) == mp.end()) {
pres[i] = 0;
} else {
pres[i] = mp[v[i]];
}
mp[v[i]] = i + 1;
}
for (int i = 0; i <= N; ++i) nums[i] = -M;
anss[0] = 1;
for (int i = 0; i < N; ++i) {
if (pres[i]) {
add(nums + pres[pres[i] - 1], anss + pres[pres[i] - 1],
pres[i] - pres[pres[i] - 1], true);
}
add(nums + pres[i], anss + pres[i], i - pres[i] + 1, false);
anss[i + 1] = sum % mod;
if (anss[i + 1] < 0) anss[i + 1] += mod;
sum += anss[i + 1];
}
cout << anss[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int gi() {
int x = 0, w = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
if (ch == '-') w = 0, ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return w ? x : -x;
}
const int N = 1e5 + 5;
const int B = 350;
const int mod = 998244353;
int n, k, bl[N], L[B], R[B], tag[B], mn[B], mx[B], pre[N], lst[N], f[N], num[N];
vector<int> ans[B];
inline void add(int &x, int y) {
x += y;
x >= mod ? x -= mod : x;
}
void rebuild(int x) {
mn[x] = 1 << 30;
mx[x] = -1 << 30;
for (int i = L[x]; i <= R[x]; ++i) {
num[i] += tag[x];
mn[x] = min(mn[x], num[i]);
mx[x] = max(mx[x], num[i]);
}
tag[x] = 0;
ans[x].clear();
ans[x].resize(mx[x] - mn[x] + 1);
for (int i = L[x]; i <= R[x]; ++i) add(ans[x][num[i] - mn[x]], f[i - 1]);
for (int i = 1; i <= mx[x] - mn[x]; ++i) add(ans[x][i], ans[x][i - 1]);
}
int cal(int x) {
int t = k - tag[x];
if (t < mn[x]) return 0;
return ans[x][min(t - mn[x], mx[x] - mn[x])];
}
void modify(int l, int ed, int v) {
while (l <= ed) {
int x = bl[l], r = min(R[x], ed);
if (l == L[x] && r == R[x])
tag[x] += v;
else {
for (int i = l; i <= r; ++i) num[i] += v;
rebuild(x);
}
l = r + 1;
}
}
int query(int l, int ed) {
int res = 0;
while (l <= ed) {
int x = bl[l], r = min(R[x], ed);
if (l == L[x] && r == R[x])
add(res, cal(x));
else {
rebuild(x);
for (int i = l; i <= r; ++i)
if (num[i] <= k) add(res, f[i - 1]);
}
l = r + 1;
}
return res;
}
int main() {
n = gi();
k = gi();
f[0] = 1;
for (int i = 1; i <= n; ++i) {
bl[i] = (i - 1) / B + 1;
if (!L[bl[i]]) L[bl[i]] = i;
R[bl[i]] = i;
}
rebuild(1);
for (int i = 1; i <= n; ++i) {
int x = gi();
pre[i] = lst[x];
lst[x] = i;
modify(pre[i] + 1, i, 1);
if (pre[i]) modify(pre[pre[i]] + 1, pre[i], -1);
f[i] = query(1, i);
if (i < n) rebuild(bl[i + 1]);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5, bs = 400, mxBC = (mxN - 1) / bs + 1, M = 998244353;
int n, k, dp[mxN + 1], lst[mxN + 1], nxt[mxN], b[mxN], c[mxBC][2 * mxN + 1],
d[mxBC];
void am(int &a, int b) {
a += b;
if (a >= M) a -= M;
}
void upd(int l1, int r1, int x) {
for (int bi = l1 / bs; bi <= r1 / bs; ++bi) {
int l2 = bi * bs, r2 = min((bi + 1) * bs, n) - 1;
if (l1 <= l2 && r2 <= r1) {
d[bi] += x;
continue;
}
l2 = max(l1, l2);
r2 = min(r1, r2);
for (; l2 <= r2; ++l2) {
if (x == 1) am(c[bi][b[l2] + n], M - dp[l2]);
b[l2] += x;
if (x == -1) am(c[bi][b[l2] + n], dp[l2]);
}
}
}
int qry(int l1, int r1) {
int e = 0;
for (int bi = l1 / bs; bi <= r1 / bs; ++bi) {
int l2 = bi * bs, r2 = min((bi + 1) * bs, n) - 1;
if (l1 <= l2 && r2 <= r1) {
am(e, c[bi][k + n - d[bi]]);
continue;
}
l2 = max(l1, l2);
r2 = min(r1, r2);
for (; l2 <= r2; ++l2)
if (b[l2] + d[bi] <= k) am(e, dp[l2]);
}
return e;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
dp[0] = 1;
memset(lst, -1, sizeof(lst));
for (int i = 0, ai; i < n; ++i) {
cin >> ai;
nxt[i] = lst[ai];
lst[ai] = i;
if (i % bs == bs - 1 || i == n - 1) {
memset(c[i / bs], 0, sizeof(c[0]));
for (int l = i / bs * bs; l <= i; ++l) am(c[i / bs][b[l] + n], dp[l]);
for (int j = 1; j <= 2 * n; ++j) am(c[i / bs][j], c[i / bs][j - 1]);
}
upd(nxt[i] + 1, i, 1);
if (nxt[i] != -1) upd(nxt[nxt[i]] + 1, nxt[i], -1);
dp[i + 1] = qry(0, 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
template <typename T>
void chkmax(T &x, T y) {
x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
x = x > y ? y : x;
}
template <typename T>
void add(T &x, T y, T mod) {
x = x + y > mod ? x + y - mod : x + y;
}
template <typename T>
void sub(T &x, T y, T mod) {
x = x - y < 0 ? x - y + mod : x - y;
}
template <typename T>
void multi(T &x, T y, T mod) {
x = 1ll * x * y % mod;
}
const int INF = (1ll << 30);
template <typename T>
void read(T &x) {
x = 0;
bool f = 1;
char ch;
do {
ch = getchar();
if (ch == '-') f = 0;
} while (ch > '9' || ch < '0');
do {
x = x * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
x = f ? x : -x;
}
template <typename T>
void write(T x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int Bs = 320, N = 2e5 + 10, mod = 998244353;
int n, k, B, ans, a[N], f[N], lst[N], tag[Bs], sum[N], pre[N], val[Bs][N];
int belong(int x) { return (x - 1) / B + 1; }
inline void insert(int u, int v) {
int bu = belong(u);
sum[u] -= tag[bu];
add(ans, v, mod);
add(val[bu][sum[u] + n], v, mod);
}
inline void change(int u, int v) {
int bu = belong(u);
if (sum[u] + tag[bu] <= k) sub(ans, f[u - 1], mod);
sub(val[bu][sum[u] + n], f[u - 1], mod);
sum[u] += v;
if (sum[u] + tag[bu] <= k) add(ans, f[u - 1], mod);
add(val[bu][sum[u] + n], f[u - 1], mod);
}
inline void modify(int l, int r, int v) {
if (l > r) return;
int bl = belong(l), br = belong(r);
if (bl + 1 >= br) {
for (int i = l; i <= r; i++) change(i, v);
} else {
for (int i = l; i <= B * bl; i++) change(i, v);
for (int i = B * (br - 1) + 1; i <= r; i++) change(i, v);
for (int i = bl + 1; i < br; i++) {
if (~v)
sub(ans, val[i][k - tag[i] + n], mod);
else
add(ans, val[i][k - tag[i] + 1 + n], mod);
tag[i] += v;
}
}
}
int main() {
read(n);
read(k);
for (int i = 1, a; i <= n; ++i) read(a), pre[i] = lst[a], lst[a] = i;
B = sqrt(n);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
insert(i, f[i - 1]);
modify(pre[i] + 1, i, 1);
modify(pre[pre[i]] + 1, pre[i], -1);
f[i] = ans;
}
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"
]
} | 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;
cnt[bl][b[i].first - getMin].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;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const long long MXN = 1e5 + 10;
const long long SQR = 400;
const long long MXB = MXN / SQR + 10;
const long long MXM = SQR * 2 + 10;
const long long Mod = 998244353;
inline void mkay(long long& x) {
if (x >= Mod) x -= Mod;
}
long long n, k;
long long A[MXN], dt[MXN], dp[MXN];
long long my[MXN], sigma[MXB][MXM];
deque<long long> Pos[MXN];
void Build(long long t) {
long long s = max(t * SQR, 1ll), e = min(n, (t + 1) * SQR - 1);
my[e] = dt[e];
for (int i = e - 1; i >= s; i--) my[i] = my[i + 1] + dt[i];
memset(sigma[t], 0, sizeof sigma[t]);
for (int i = s; i <= e; i++) {
sigma[t][my[i] + SQR] += dp[i - 1];
mkay(sigma[t][my[i] + SQR]);
}
for (int i = 1; i < MXM; i++) {
sigma[t][i] += sigma[t][i - 1];
mkay(sigma[t][i]);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> A[i];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (Pos[A[i]].size() == 2) {
dt[Pos[A[i]][0]] = 0;
if (Pos[A[i]][0] / SQR < i / SQR) Build(Pos[A[i]][0] / SQR);
Pos[A[i]].pop_front();
}
Pos[A[i]].push_back(i);
if (Pos[A[i]].size() == 2) {
dt[Pos[A[i]][0]] = -1;
if (Pos[A[i]][0] / SQR < i / SQR) Build(Pos[A[i]][0] / SQR);
}
dt[i] = 1;
long long now = 0;
for (int j = i; j / SQR == i / SQR; j--) {
now += dt[j];
if (now <= k) dp[i] += dp[j - 1], mkay(dp[i]);
}
for (int t = (i / SQR) - 1; t >= 0; t--) {
long long s = max(t * SQR, 1ll), e = min(n, (t + 1) * SQR - 1);
long long exp = k - now + SQR;
if (0 <= exp) {
dp[i] += sigma[t][min(MXM - 1, k - now + SQR)];
mkay(dp[i]);
}
now += my[s];
}
if (i % SQR == SQR - 1) Build(i / SQR);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int B;
vector<int> counts;
vector<int> dp;
void mod_add(int& first, int second) {
first += second;
if (first >= mod) {
first -= mod;
}
}
struct Bucket {
int id;
int offset = 0;
vector<int> pref_sum;
Bucket(int _id) {
id = _id;
pref_sum.assign(B, 0);
}
void rebuild() {
int first = id * B, last = (id + 1) * B;
int smallest = INT_MAX;
for (int i = first; i < last; ++i) {
smallest = min(smallest, counts[i] + offset);
}
for (int i = first; i < last; ++i) {
counts[i] = counts[i] - smallest + offset;
}
offset = smallest;
for (int i = 0; i < B; ++i) {
pref_sum[i] = 0;
}
for (int i = first; i < last; ++i) {
mod_add(pref_sum[counts[i]], dp[i]);
}
for (int i = 1; i < B; ++i) {
mod_add(pref_sum[i], pref_sum[i - 1]);
}
}
};
vector<Bucket> buckets;
int getBucket(int val) { return val / B; }
void add(int start, int end, int amt) {
int startBucket = getBucket(start);
for (int i = start; i <= end and getBucket(i) == startBucket; ++i) {
counts[i] += amt;
}
buckets[getBucket(start)].rebuild();
if (getBucket(start) == getBucket(end)) {
return;
}
int endBucket = getBucket(end);
for (int i = getBucket(start) + 1; i < endBucket; ++i) {
buckets[i].offset += amt;
}
for (int i = end; getBucket(i) == getBucket(end); --i) {
counts[i] += amt;
}
buckets[getBucket(end)].rebuild();
}
int main() {
int n, k;
cin >> n >> k;
vector<int> nums(n, 0);
for (int i = 0; i < n; ++i) {
cin >> nums[i];
}
B = ceil(sqrt(n));
for (int i = 0; i < B; ++i) {
buckets.push_back(Bucket(i));
}
counts.assign(n + B, 0);
dp.assign(n + B, 0);
vector<vector<int>> prev(n + 1, vector<int>(2, -1));
dp[0] = 1;
for (int i = 0; i < n; ++i) {
add(prev[nums[i]][0] + 1, i, 1);
if (prev[nums[i]][0] != -1) {
add(prev[nums[i]][1] + 1, prev[nums[i]][0], -1);
}
int total = 0;
for (int j = 0; j <= getBucket(i); ++j) {
int count = k - buckets[j].offset;
if (count >= 0) {
mod_add(total, buckets[j].pref_sum[min(count, B - 1)]);
}
}
dp[i + 1] = total;
buckets[getBucket(i)].rebuild();
if (prev[nums[i]][0] != -1) {
prev[nums[i]][1] = prev[nums[i]][0];
}
prev[nums[i]][0] = 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int Bs = 317, N = 2e5 + 10, P = 998244353;
int ri() {
char c = getchar();
int x = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c;
return x * f;
}
int pr[N], las[N], a[N], val[Bs][N], s[N], f[N], tag[Bs], B, Ans, z, k, n;
int belong(int x) { return (x - 1) / B + 1; }
void dec(int &a, int b) {
a -= b;
if (a < 0) a += P;
}
void inc(int &a, int b) {
a += b;
if (a >= P) a -= P;
}
void Ins(int u, int v) {
int bu = belong(u);
s[u] -= tag[bu];
inc(Ans, v);
inc(val[bu][s[u] + z], v);
}
void Mdfs(int u, int v) {
int bu = belong(u);
if (s[u] + tag[bu] <= k) dec(Ans, f[u - 1]);
dec(val[bu][s[u] + z], f[u - 1]);
s[u] += v;
if (s[u] + tag[bu] <= k) inc(Ans, f[u - 1]);
inc(val[bu][s[u] + z], f[u - 1]);
}
void Mdf(int L, int R, int v) {
if (L > R) return;
int bl = belong(L), br = belong(R);
if (bl + 1 >= br) {
for (int i = L; i <= R; ++i) Mdfs(i, v);
} else {
for (int i = L; i <= bl * B; ++i) Mdfs(i, v);
for (int i = (br - 1) * B + 1; i <= R; ++i) Mdfs(i, v);
for (int i = bl + 1; i < br; ++i) {
if (~v)
dec(Ans, val[i][k - tag[i] + z]);
else
inc(Ans, val[i][k - tag[i] + 1 + z]);
tag[i] += v;
}
}
}
int main() {
n = ri();
k = ri();
for (int i = 1, a; i <= n; ++i) a = ri(), pr[i] = las[a], las[a] = i;
B = sqrt(n);
z = n;
f[0] = 1;
Ins(1, 1);
for (int i = 1; i <= n; ++i) {
Mdf(pr[i] + 1, i, 1);
Mdf(pr[pr[i]] + 1, pr[i], -1);
Ins(i + 1, f[i] = Ans);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int blkbase[(350 + 5)][(350 + 5)];
int blkpsum[(350 + 5)][(350 + 5)];
int f[100005];
int n, k;
int a[100005];
int previ[100005];
int previval[100005];
int blk[100005];
int blkbegin[100005];
int blkend[1000005];
int cur[100005];
int cur_offset[(350 + 5)];
int cur_cnt[(350 + 5)];
int cur_total = 0;
int blkcnt;
bool cmp(int A, int B) { return cur[A] < cur[B]; }
void rebuild(int block) {
for (int i = blkbegin[block], j = 1; j <= 350; i++, j++) {
blkbase[block][j] = i;
}
sort(blkbase[block] + 1, blkbase[block] + 350 + 1, cmp);
int r = 0;
for (int i = 1; i <= 350; i++) {
blkpsum[block][i] = blkpsum[block][i - 1] + f[blkbase[block][i] - 1];
if (blkpsum[block][i] >= 998244353) blkpsum[block][i] -= 998244353;
if (cur[blkbase[block][i]] <= k) r = i;
}
cur_total -= cur_cnt[block];
cur_total += blkpsum[block][r];
cur_cnt[block] = blkpsum[block][r];
cur_total %= 998244353;
cur_total += 998244353;
cur_total %= 998244353;
}
void update(int l, int r, int change) {
for (int i = blk[l] + 1; i < blk[r]; i++) {
cur_offset[i] += change;
int L = 0, R = 350;
while (L < R) {
int mid = (L + R) / 2 + 1;
if (cur[blkbase[i][mid]] + cur_offset[i] <= k) {
L = mid;
} else {
R = mid - 1;
}
}
cur_total -= cur_cnt[i];
cur_total += blkpsum[i][R];
cur_cnt[i] = blkpsum[i][R];
cur_total %= 998244353;
cur_total += 998244353;
cur_total %= 998244353;
}
for (int i = blkbegin[blk[l]]; i <= blkend[blk[l]]; i++) {
cur[i] += cur_offset[blk[l]];
}
cur_offset[blk[l]] = 0;
for (int i = l; i <= blkend[blk[l]] && i <= r; i++) {
cur[i] += change;
}
for (int j = 1; j <= n; j++) {
}
rebuild(blk[l]);
if (blk[l] != blk[r]) {
for (int i = blkbegin[blk[r]]; i <= blkend[blk[r]]; i++) {
cur[i] += cur_offset[blk[r]];
}
cur_offset[blk[r]] = 0;
for (int i = blkbegin[blk[r]]; i <= r; i++) {
cur[i] += change;
}
rebuild(blk[r]);
}
}
int main() {
cur[0] = -10000000;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
previ[i] = previval[a[i]];
previval[a[i]] = i;
}
for (int i = 1; i <= 100000; i++) blk[i] = (i - 1) / 350 + 1;
for (int i = 1; i <= 350; i++) {
blkbegin[i] = (i - 1) * 350 + 1;
blkend[i] = blkbegin[i] + 350 - 1;
for (int j = (i - 1) * 350 + 1, x = 1; x <= 350; j++, x++) {
blkbase[i][x] = j;
}
}
f[0] = 1;
rebuild(1);
for (int i = 1; i <= n; i++) {
update(previ[i] + 1, i, 1);
if (previ[i]) update(previ[previ[i]] + 1, previ[i], -1);
f[i] = cur_total;
for (int j = 0; j < 350; j++) {
cur[blkbegin[blk[i + 1]] + j] += cur_offset[blk[i + 1]];
}
cur_offset[blk[i + 1]] = 0;
for (int j = 1; j <= n; j++) {
}
rebuild(blk[i + 1]);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, a[200010], s, la[200010], f[200010], tg[200010], ans, p[200010],
va[320][200010], pr[200010];
int bl(int x) { return (x - 1) / s + 1; }
void ins(int x, int y) {
p[x] -= tg[bl(x)];
ans = (ans + y) % 998244353ll;
va[bl(x)][p[x] + n] = (va[bl(x)][p[x] + n] + y) % 998244353ll;
}
void md(int x, int y) {
int po = bl(x);
if (p[x] + tg[po] <= k) ans = (ans - f[x - 1] + 998244353ll) % 998244353ll;
va[po][p[x] + n] = (va[po][p[x] + n] + 998244353ll - f[x - 1]) % 998244353ll;
p[x] += y;
if (p[x] + tg[po] <= k) ans = (ans + f[x - 1]) % 998244353ll;
va[po][p[x] + n] = (va[po][p[x] + n] + f[x - 1]) % 998244353ll;
}
void mod(int l, int r, int v) {
if (l > r) return;
int p1 = bl(l), p2 = bl(r);
if (p1 + 1 >= p2) {
for (int i = l; i <= r; i++) md(i, v);
} else {
for (int i = l; i <= s * p1; i++) md(i, v);
for (int i = (p2 - 1) * s + 1; i <= r; i++) md(i, v);
for (int i = p1 + 1; i < p2; i++) {
if (v != -1)
ans = (ans - va[i][k - tg[i] + n] + 998244353ll) % 998244353ll;
else
ans = (ans + va[i][k - tg[i] + 1 + n]) % 998244353ll;
tg[i] += v;
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pr[i] = la[a[i]];
la[a[i]] = i;
}
s = sqrt(n);
f[0] = 1;
ins(1, 1);
for (int i = 1; i <= n; i++) {
mod(pr[i] + 1, i, 1);
mod(pr[pr[i]] + 1, pr[i], -1);
f[i] = ans;
ins(i + 1, ans);
}
cout << 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
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], dp[i - 1]);
add(f[i / S][i % S], 1);
for (int j = 0; j <= i / 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 998244353;
int sqrtN = 128;
struct SqrtDecomposition {
int N, K, tap, ans;
vector<int> data;
vector<int> bucketAdd;
vector<int> uku;
vector<unordered_map<int, int> > mp;
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);
mp.resize(K);
}
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];
if (mp[k].count(tap - 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;
if (mp[k][data[i]] == 0) mp[k].erase(data[i]);
++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) {
if (mp[k].count(tap - bucketAdd[k])) {
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;
if (mp[k][data[i]] == 0) mp[k].erase(data[i]);
--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]++;
seg.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;
(seg.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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MAX_N = 2e5 + 5;
vector<int> occurrences[MAX_N];
int cnt[MAX_N], dp[MAX_N];
void add_self(int& a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
const int B = 256;
struct Bucket {
int id;
int offset;
int pref_sum[B];
void rebuild() {
int first = id * B, last = (id + 1) * B - 1;
int smallest = INT_MAX;
for (int i = first; i <= last; ++i) {
smallest = min(smallest, offset + cnt[i]);
}
for (int i = first; i <= last; ++i) {
cnt[i] -= smallest - offset;
assert(0 <= cnt[i] && cnt[i] < B);
}
offset = smallest;
for (int x = 0; x < B; ++x) {
pref_sum[x] = 0;
}
for (int i = first; i <= last; ++i) {
add_self(pref_sum[cnt[i]], dp[i]);
}
for (int x = 1; x < B; ++x) {
add_self(pref_sum[x], pref_sum[x - 1]);
}
}
} buckets[MAX_N / B + 1];
int which(int i) { return i / B; }
void add(int a, int b, int diff) {
for (int i = a; i <= b && which(i) == which(a); ++i) {
cnt[i] += diff;
}
buckets[which(a)].rebuild();
if (which(a) == which(b)) {
return;
}
for (int i = which(a) + 1; i < which(b); ++i) {
buckets[i].offset += diff;
}
for (int i = b; which(i) == which(b); --i) {
cnt[i] += diff;
}
buckets[which(b)].rebuild();
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i <= which(n - 1); ++i) {
buckets[i].id = i;
}
for (int x = 1; x <= n; ++x) {
occurrences[x].push_back(-1);
}
dp[0] = 1;
buckets[which(0)].rebuild();
for (int R = 0; R < n; ++R) {
int x;
scanf("%d", &x);
vector<int>& vec = occurrences[x];
if ((int)vec.size() >= 2) {
add(vec.end()[-2] + 1, vec.back(), -1);
}
add(vec.back() + 1, R, 1);
vec.push_back(R);
int total = 0;
for (int i = 0; i <= which(R); ++i) {
int at_most = k - buckets[i].offset;
if (at_most >= 0) {
add_self(total, buckets[i].pref_sum[min(at_most, B - 1)]);
}
}
dp[R + 1] = total;
buckets[which(R + 1)].rebuild();
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
struct Node {
int ex, mini;
int pref[335];
};
Node block[335];
vector<int> posi[200010];
int n, k, ara[200010], val[200010], dp[200010];
int blockSz;
int which(int p) { return (p / blockSz); }
void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
void calDp(int bi) {
int first = max(1, blockSz * bi), last = blockSz * (bi + 1) - 1;
int mini, &ex = block[bi].ex;
for (int i = first; i <= last; i++) val[i] += ex;
mini = INT_MAX;
ex = 0;
for (int i = first; i <= last; i++) mini = min(val[i], mini);
for (int i = first; i <= last; i++) val[i] -= mini;
ex = mini;
mini = 0;
memset(block[bi].pref, 0, sizeof(block[bi].pref));
for (int i = first; i <= last; i++) add(block[bi].pref[val[i]], dp[i - 1]);
for (int i = 1; i <= blockSz; i++)
add(block[bi].pref[i], block[bi].pref[i - 1]);
}
void update(int l, int r, int v) {
for (int i = l; i <= r; i++) {
int blockId = which(i), first = blockSz * blockId,
last = blockSz * (blockId + 1) - 1;
if (l <= first && last <= r)
block[blockId].ex += v, i = last;
else
val[i] += v;
}
calDp(which(l));
calDp(which(r));
}
int main() {
ios::sync_with_stdio(false);
int v;
cin >> n >> k;
blockSz = sqrt(n) + 1;
for (int i = 0; i <= 200010 - 1; i++) posi[i].push_back(0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> v;
int p = (int)posi[v].size() - 1;
if (posi[v].size() >= 2) update(posi[v][p - 1] + 1, posi[v][p], -1);
posi[v].push_back(i);
p = (int)posi[v].size() - 1;
update(posi[v][p - 1] + 1, posi[v][p], 1);
dp[i] = 0;
int til = which(i);
for (int bi = 0; bi <= til; bi++) {
int can = k - block[bi].ex;
if (can >= 0) add(dp[i], block[bi].pref[min(blockSz, can)]);
}
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int B = 300;
const int mod = 998244353;
const int N = 1e5 + 10 + B;
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n;
int k;
vector<int> v;
struct block {
int total;
int el[134];
int dp[134];
int sz;
block() {
total = 0;
sz = 0;
}
pair<long long int, long long int> way[134];
bool need;
inline void build() { need = true; }
inline void build(int dum) {
need = false;
for (int i = 0; i < sz; i++) {
int val = el[i];
way[i] = (make_pair(el[i], dp[i]));
}
sort(way, way + sz);
for (int i = 1; i < sz; i++) {
way[i].second += way[i - 1].second;
if (way[i].second >= 998244353) way[i].second -= 998244353;
}
}
inline long long int query() {
if (need) build(114);
long long int until = k - total;
int id = upper_bound(way, way + sz, make_pair(until, LLONG_MAX)) - way;
id--;
if (id < 0) return 0;
return way[id].second;
}
};
block b[900];
vector<int> vv[100002];
void add_rng(int l, int r, int x) {
int lef = l / 134;
int rig = r / 134;
for (int i = lef + 1; i < rig; i++) {
b[i].total += x;
}
if (lef == rig) {
for (int i = l % 134; i <= r % 134; i++) {
b[lef].el[i] += x;
}
b[lef].build();
} else {
for (int i = l % 134; i < b[lef].sz; i++) {
b[lef].el[i] += x;
}
for (int i = 0; i <= r % 134; i++) {
b[rig].el[i] += x;
}
b[lef].build();
b[rig].build();
}
}
long long int gt(int f) {
int be = f / 134;
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 / 134].el[b[i / 134].sz++] = 0;
b[i / 134].dp[b[i / 134].sz - 1] = 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) / 134;
int att = (i + 1) % 134;
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) / 134].dp[(n) % 134];
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const int MOD = 998244353;
const int N = 120000;
const int SQRT = 200;
struct Block {
int tl, tr;
vector<tuple<int, int, int> > vals;
vector<int> psum;
int mod = 0;
Block() = default;
void rebuild() {
for (auto& v : vals) {
get<0>(v) += mod;
}
mod = 0;
sort(vals.begin(), vals.end());
psum.resize(vals.size(), 0);
int n = psum.size();
for (int i = 0; i < n; i++) {
psum[i] = 0;
if (i) {
psum[i] = psum[i - 1];
}
psum[i] = (psum[i] + get<2>(vals[i])) % MOD;
}
}
void change(int pos, int val) {
for (auto& v : vals) {
if (get<1>(v) == pos) {
get<0>(v) = 0;
get<2>(v) = val;
}
}
rebuild();
}
void add(int l, int r, int x) {
if (r < tl || l > tr) {
return;
}
if (tl >= l && tr <= r) {
mod += x;
} else {
for (auto& v : vals) {
if (get<1>(v) >= l && get<1>(v) <= r) {
get<0>(v) += x;
}
}
rebuild();
}
}
int calc(int k) {
k -= mod;
if (get<0>(vals[0]) > k) {
return 0;
}
int lg = 0, rg = vals.size();
while (rg - lg > 1) {
int mg = (lg + rg) >> 1;
if (get<0>(vals[mg]) <= k) {
lg = mg;
} else {
rg = mg;
}
}
return psum[lg];
}
void print() {
for (auto& v : vals) {
cout << get<0>(v) << " " << get<1>(v) << " " << get<2>(v) << "\n";
}
}
};
Block b[N];
int a[N];
int f[N];
int lst[N];
int id[N];
int lv[N];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
int bptr = 1;
b[bptr].tl = 0;
b[bptr].tr = 0;
for (int i = 0; i <= n; i++) {
if (i != 0) {
cin >> a[i];
}
if (b[bptr].tr - b[bptr].tl == SQRT) {
bptr++;
b[bptr].tl = b[bptr].tr = i;
} else {
b[bptr].tr = i;
}
b[bptr].vals.emplace_back(N, i, 0);
id[i] = bptr;
}
auto ad = [&](int l, int r, int x) {
for (int i = 1; i <= bptr; i++) {
b[i].add(l, r, x);
}
};
auto gt = [&](int x) {
int ans = 0;
for (int i = 1; i <= bptr; i++) {
ans = (ans + b[i].calc(x)) % MOD;
}
return ans;
};
f[0] = 1;
b[id[0]].change(0, 1);
for (int i = 1; i <= n; i++) {
ad(lst[a[i]], i - 1, 1);
if (lst[a[i]] != 0) {
ad(lv[a[i]], lst[a[i]] - 1, -1);
}
lv[a[i]] = lst[a[i]];
lst[a[i]] = i;
f[i] = (f[i] + gt(k)) % MOD;
b[id[i]].change(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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
int a[100003], b[100003], arr[100003];
int dp[100003], res, n, k, x, i;
int main() {
scanf("%d%d", &n, &k);
dp[0] = 1;
for (int j = 1; j <= n; j++) {
res += dp[j - 1];
if (res >= 998244353) res -= 998244353;
scanf("%d", &x);
for (i = a[x] + 1; i + 2 <= b[x]; i += 3) {
if (arr[i] == k + 1) {
res += dp[i - 1];
if (res >= 998244353) res -= 998244353;
}
if (arr[i + 1] == k + 1) {
res += dp[i];
if (res >= 998244353) res -= 998244353;
}
if (arr[i + 2] == k + 1) {
res += dp[i + 1];
if (res >= 998244353) res -= 998244353;
}
arr[i]--;
arr[i + 1]--;
arr[i + 2]--;
}
for (; i <= b[x]; i++) {
if (arr[i] == k + 1) {
res += dp[i - 1];
if (res >= 998244353) res -= 998244353;
}
arr[i]--;
}
for (i = b[x] + 1; i + 2 <= j; i += 3) {
if (arr[i] == k) {
res -= dp[i - 1];
if (res < 0) res += 998244353;
}
if (arr[i + 1] == k) {
res -= dp[i];
if (res < 0) res += 998244353;
}
if (arr[i + 2] == k) {
res -= dp[i + 1];
if (res < 0) res += 998244353;
}
arr[i]++;
arr[i + 1]++;
arr[i + 2]++;
}
for (; i <= j; i++) {
if (arr[i] == k) {
res -= dp[i - 1];
if (res < 0) res += 998244353;
}
arr[i]++;
}
a[x] = b[x];
b[x] = j;
dp[j] = res;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int mod = 998244353;
int n, k, sq;
int a[N], dp[N], bl[N], bk[405][N], tag[405], val[405], num[N];
vector<int> ve[N], pos[405];
inline void rebuild(int p) {
int l = (p - 1) * sq + 1, r = min(n, p * sq);
for (int i = 0; i < pos[p].size(); ++i) bk[p][pos[p][i]] = 0;
for (int i = l; i <= r; ++i) num[i] += tag[p];
tag[p] = 0;
val[p] = 0;
pos[p].clear();
for (int i = l; i <= r; ++i) {
(bk[p][num[i]] += dp[i - 1]) %= mod;
pos[p].push_back(num[i]);
if (num[i] <= k) (val[p] += dp[i - 1]) %= mod;
}
}
inline void insert(int l, int r) {
for (int i = bl[l] + 1; i < bl[r]; ++i)
(val[i] +=
k - tag[i] >= 0 && k - tag[i] <= n ? mod - bk[i][k - tag[i]] : 0) %= mod,
++tag[i];
for (int i = l, lim = min(r, bl[l] * sq); i <= lim; ++i) ++num[i];
if (bl[l] != bl[r])
for (int i = (bl[r] - 1) * sq + 1; i <= r; ++i) ++num[i];
rebuild(bl[l]);
rebuild(bl[r]);
}
inline void erase(int l, int r) {
for (int i = bl[l] + 1; i < bl[r]; ++i)
--tag[i], (val[i] += k - tag[i] >= 0 && k - tag[i] <= n ? bk[i][k - tag[i]]
: 0) %= mod;
for (int i = l, lim = min(r, bl[l] * sq); i <= lim; ++i) --num[i];
if (bl[l] != bl[r])
for (int i = (bl[r] - 1) * sq + 1; i <= r; ++i) --num[i];
rebuild(bl[l]);
rebuild(bl[r]);
}
int main() {
scanf("%d%d", &n, &k);
dp[0] = 1;
sq = sqrt(n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), bl[i] = (i - 1) / sq + 1, ve[i].push_back(0);
for (int i = 1; i <= n; ++i) {
ve[a[i]].push_back(i);
if (ve[a[i]].size() >= 3) {
int sz = ve[a[i]].size(), l = ve[a[i]][sz - 3] + 1, r = ve[a[i]][sz - 2];
erase(l, r);
}
int sz = ve[a[i]].size(), l = ve[a[i]][sz - 2] + 1, r = ve[a[i]][sz - 1];
insert(l, r);
for (int j = 1; j <= bl[n]; ++j) (dp[i] += val[j]) %= mod;
}
return printf("%d\n", dp[n]), 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"
]
} | 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 + 1); 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);
if (int((g[A[x]]).size()) >= 2) upd(g[A[x]][int((g[A[x]]).size()) - 2], 1);
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200005, 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MM = 998244353;
int n, m;
int a[MAXN];
int dp[MAXN];
void updS(int &x, int y) {
x -= y;
if (x < 0) x += MM;
}
void updA(int &x, int y) {
x += y;
if (x >= MM) x -= MM;
}
namespace Block {
const int Bibi = 512;
const int N = 1e5 + 10;
const int B = N / Bibi + 10;
int val[N];
int cnt;
int valid;
struct block {
int l, r;
long long lazy_tag = 0;
long long presum_tag = 0;
int cnt[N * 2];
void update(int l, int r, int v) {
if (v == 1) {
for (int i = l; i <= r; i++) {
updS(cnt[val[i]], dp[i]);
val[i]++;
}
} else {
for (int i = l; i <= r; i++) {
val[i]--;
updA(cnt[val[i]], dp[i]);
}
}
}
int query(int v) {
v = v - lazy_tag;
if (v < 0) return 0;
v += N;
return (cnt[v] + presum_tag) % MM;
}
} b[B];
void init(int n) {
cnt = (n - 1) / Bibi + 1;
for (int i = 1; i <= cnt; i++) {
b[i].r = min(n, i * Bibi);
b[i].l = (i - 1) * Bibi + 1;
}
for (int i = 1; i <= n; i++) val[i] = N;
}
int get_block(int x) { return (x - 1) / Bibi + 1; }
void update(int l, int r, int v) {
if (l > r) return;
int p = get_block(l), q = get_block(r);
if (p == q)
b[p].update(l, r, v);
else {
b[p].update(l, b[p].r, v);
b[q].update(b[q].l, r, v);
for (int i = p + 1; i < q; i++) {
b[i].lazy_tag += v;
}
}
}
int query(int v) {
long long ans = 0;
for (int i = 1; i <= cnt; i++) {
ans += b[i].query(v);
}
return (ans % MM);
}
} // namespace Block
int pre[MAXN], ppre[MAXN];
void solve(int casi) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pre[i] = ppre[i] = 0;
}
Block::init(n);
dp[1] = 1;
for (int i = 1; i <= n; i++) {
int v = a[i];
Block::b[Block::get_block(i)].presum_tag += dp[i];
Block::update(pre[v] + 1, i, 1);
Block::update(ppre[v] + 1, pre[v], -1);
ppre[v] = pre[v];
pre[v] = i;
dp[i + 1] = Block::query(m);
}
printf("%d\n", dp[n + 1]);
}
int main() {
int T = 1;
for (int i = 1; i <= T; i++) solve(i);
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 1e5 + 10, mod = 998244353;
int n, k, a[N], b[N], pos[N], Pos[N], dp[N];
int block, id[N], sum[320][700], Sum[320];
inline void Mod(int &x, int y) { x += y, x = (x >= mod) ? x - mod : x; }
inline void ReBuild(int pos) {
int l = (pos - 1) * block + 1, r = min(n, pos * block), suf = block;
for (register int i = (0); i <= (block << 1); i++) sum[pos][i] = 0;
for (register int i = (r); i >= (l); i--)
suf += b[i], Mod(sum[pos][suf], dp[i - 1]);
for (register int i = (1); i <= (block << 1); i++)
Mod(sum[pos][i], sum[pos][i - 1]);
Sum[pos] = suf - block;
}
inline int Query(int x) {
int pos = id[x], l = (pos - 1) * block + 1, suf = 0, ans = 0;
for (register int i = (x); i >= (l); i--)
suf += b[i], Mod(ans, (suf <= k) * dp[i - 1]);
for (register int i = (pos - 1); i >= (1); i--) {
if (k - suf + block >= 0)
Mod(ans, sum[i][min(block << 1, k - suf + block)]);
suf += Sum[i];
}
return ans;
}
int main() {
n = read(), k = read(), block = sqrt(n);
for (register int i = (1); i <= (n); i++)
a[i] = read(), id[i] = (i - 1) / block + 1;
dp[0] = 1;
for (register int i = (1); i <= (n); i++) {
b[i] = 1;
if (pos[a[i]]) {
if (Pos[a[i]]) b[Pos[a[i]]] = 0, ReBuild(id[Pos[a[i]]]);
Pos[a[i]] = pos[a[i]], b[pos[a[i]]] = -1, ReBuild(id[pos[a[i]]]);
}
pos[a[i]] = i, dp[i] = Query(i);
if (i != n && id[i + 1] != id[i]) ReBuild(id[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"
]
} | 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 T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int a[101001];
vector<int> positions[101010];
int tr[404040];
int b[101010];
int dp[404040];
long long getSum(int* b, int* dp, int n) {
long long res = 0;
for (int j = 0; j < n; ++j) {
res += b[j] > 0 ? 0 : dp[j];
}
return res;
}
const int fftmod = 998244353;
int main() {
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];
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]++;
}
long long 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, K, a[100009], pre[100009], ind[100009], ans[100009];
int bf[296][100009 << 1], bst[296], bans[296], f[100009];
inline int read();
inline void M(int& x, int y) {
x = x + y < 998244353 ? x + y : x + y - 998244353;
}
inline int get_B(int x) { return (x + 340 - 1) / 340; }
inline void add_scatter(int l, int r, int va, int ib) {
for (register int i(l); i <= r; ++i) {
M(bf[ib][f[i] + 100009], 998244353 - ans[i - 1]);
if (f[i] == bst[ib] && va == 1) M(bans[ib], 998244353 - ans[i - 1]);
f[i] += va;
M(bf[ib][f[i] + 100009], ans[i - 1]);
if (f[i] == bst[ib] && va != 1) M(bans[ib], ans[i - 1]);
}
}
inline void add(int l, int r, int va) {
int lb = get_B(l), rb = get_B(r);
if (lb == rb) return add_scatter(l, r, va, lb);
if (l % 340 != 1) {
add_scatter(l, lb * 340, va, lb);
++lb;
}
if (r % 340) {
add_scatter((rb - 1) * 340 + 1, r, va, rb);
--rb;
}
for (register int i(lb); i <= (rb); ++i)
if (va == 1) {
M(bans[i], 998244353 - bf[i][bst[i] + 100009]);
--bst[i];
} else {
++bst[i];
M(bans[i], bf[i][bst[i] + 100009]);
}
}
inline int ask_scatter(int l, int r, int ib, int ret = 0) {
for (register int i(l); i <= r; ++i)
if (f[i] <= bst[ib]) M(ret, ans[i - 1]);
return ret;
}
inline int ask(int l, int r) {
int lb = get_B(l), rb = get_B(r), ret = 0;
if (lb == rb) return ask_scatter(l, r, lb);
if (l % 340 != 1) {
M(ret, ask_scatter(l, lb * 340, lb));
++lb;
}
if (r % 340) {
M(ret, ask_scatter((rb - 1) * 340 + 1, r, rb));
--rb;
}
for (register int i(lb); i <= rb; ++i) M(ret, bans[i]);
return ret;
}
int main() {
n = read(), K = read();
ans[0] = 1;
for (register int i(1); i <= n; ++i) {
a[i] = read();
pre[i] = ind[a[i]];
ind[a[i]] = i;
}
int nB = get_B(n);
for (register int i(1); i <= nB; ++i) bst[i] = K;
for (register int i(1); i <= n; ++i) {
if (pre[i]) add(pre[pre[i]] + 1, pre[i], -1);
int ib = get_B(i);
M(bf[ib][100009], ans[i - 1]);
if (0 <= bst[ib]) M(bans[ib], ans[i - 1]);
add(pre[i] + 1, i, 1);
ans[i] = ask(1, i);
}
printf("%d\n", ans[n]);
return 0;
}
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') f = (c == '-') ? -1 : f, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
|
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int M = 100005, K = 316, mod = 998244353;
int n, k, a[M], b[M], dp[M], sum[K + 5][2 * K + 5], in[M], tot[K + 5];
vector<int> v[M];
void pl(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void update(int i, int x) {
b[i] = x;
memset(sum[in[i]], 0, sizeof(sum[in[i]]));
tot[in[i]] = 0;
for (int j = min(n, K * (in[i] + 1) - 1); j && in[i] == in[j]; j--) {
tot[in[i]] += b[j];
pl(sum[in[i]][tot[in[i]] + K], dp[j - 1]);
}
for (int j = 1; j <= 2 * K; j++) pl(sum[in[i]][j], sum[in[i]][j - 1]);
assert(abs(tot[in[i]]) <= K);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
dp[0] = 1;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
in[i] = i / K;
}
for (int i = 1; i <= n; i++) {
update(i, 1);
if (v[a[i]].size()) update(v[a[i]].back(), -1);
if (v[a[i]].size() > 1) update(*(v[a[i]].end() - 2), 0);
v[a[i]].push_back(i);
int s = 0;
for (int j = i; j && in[i] == in[j]; j--) {
s += b[j];
if (s <= k) pl(dp[i], dp[j - 1]);
}
for (int j = in[i] - 1; j + 1; j--) {
int t = k - s;
if (t + K >= 0) pl(dp[i], sum[j][min(2 * K, t + K)]);
s += tot[j];
}
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mn = 1e5, msq = 317, mod = 998244353;
int n, sq, k, a[mn + 3];
int inp[mn + 3], lw[msq + 3], rw[msq + 3];
int num[mn + 3];
int topw[mn + 3], secw[mn + 3];
long long f[mn + 3];
long long Sumf[msq + 3][2 * msq + 3];
int add[msq + 3];
void Add(int l, int r, int ad) {
l--;
r++;
if (inp[l] >= inp[r]) {
register int p = inp[l];
for (register int i = l + 1; i <= r - 1; i++) {
if (ad == 1)
Sumf[p][num[i]] -= f[i - 1];
else
Sumf[p][num[i] - 1] += f[i - 1];
num[i] += ad;
}
return;
}
register int p = inp[l];
for (register int i = l + 1; i <= rw[p]; i++) {
if (ad == 1)
Sumf[p][num[i]] -= f[i - 1];
else
Sumf[p][num[i] - 1] += f[i - 1];
num[i] += ad;
}
for (p = inp[l] + 1; p <= inp[r] - 1; p++) add[p] += ad;
p = inp[r];
for (register int i = lw[p]; i <= r - 1; i++) {
if (ad == 1)
Sumf[p][num[i]] -= f[i - 1];
else
Sumf[p][num[i] - 1] += f[i - 1];
num[i] += ad;
}
}
int main() {
cin >> n >> k;
sq = sqrt(n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
int totp = 0;
for (int i = 1; i <= n; i++) {
if ((i - 1) % sq == 0) lw[++totp] = i;
if (i % sq == 0 || i == n) rw[totp] = i;
inp[i] = totp;
}
inp[n + 1] = ++totp;
lw[totp] = rw[totp] = n + 1;
f[0] = 1;
for (register int i = 1; i <= n; i++) {
for (int nu = 0; nu <= 2 * sq; nu++) Sumf[inp[i]][nu] += f[i - 1];
Add(topw[a[i]] + 1, i, 1);
if (topw[a[i]]) Add(secw[a[i]] + 1, topw[a[i]], -1);
for (register int p = 1; p < inp[i]; p++)
if (add[p] <= k) f[i] += Sumf[p][min(2 * sq, k - add[p])], f[i] %= mod;
register int p = inp[i];
for (register int w = lw[p]; w <= i; w++)
if (num[w] + add[p] <= k) f[i] += f[w - 1], f[i] %= mod;
f[i] = (f[i] + mod) % mod;
secw[a[i]] = topw[a[i]];
topw[a[i]] = i;
}
cout << 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"
]
} | CORRECT | java | // upsolve with rainboy
import java.io.*;
import java.util.*;
public class CF1129D {
static final int MD = 998244353, A = 100000, B = 200;
static int[] bb, dp, ss;
static int[][] dq;
static void update(int h) {
int[] qq = dq[h];
Arrays.fill(qq, 0);
int t = 0;
for (int i = (h + 1) * B; i > h * B; i--) {
t += bb[i];
qq[B + t] = (qq[B + t] + dp[i - 1]) % MD;
}
for (int c = 1; c <= B + B; c++)
qq[c] = (qq[c] + qq[c - 1]) % MD;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] pp = new int[1 + n];
int[] ii = new int[1 + A];
for (int i = 1; i <= n; i++) {
int a = Integer.parseInt(st.nextToken());
pp[i] = ii[a]; ii[a] = i;
}
bb = new int[1 + n];
dp = new int[1 + n];
dp[0] = 1;
int m = (n + B - 1) / B;
ss = new int[m];
dq = new int[m][B + 1 + B];
for (int j = 1; j <= n; j++) {
int p;
m = (j - 1) / B;
ss[m] += 1 - bb[j]; bb[j] = 1;
if ((p = pp[j]) != 0) {
int h = (p - 1) / B;
ss[h] += -1 - bb[p]; bb[p] = -1;
if (p <= m * B)
update(h);
if ((p = pp[p]) != 0) {
h = (p - 1) / B;
ss[h] += 0 - bb[p]; bb[p] = 0;
if (p <= m * B)
update(h);
}
}
int x = 0, t = 0;
for (int i = j; i > m * B; i--)
if ((t += bb[i]) <= k)
x = (x + dp[i - 1]) % MD;
for (int h = m - 1; h >= 0; h--) {
if (k - t >= -B)
x = (x + dq[h][B + Math.min(B, k - t)]) % MD;
t += ss[h];
}
dp[j] = x;
if (j % B == 0)
update(m);
}
System.out.println(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"
]
} | 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 T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
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;
}
const int fftmod = 998244353;
int main() {
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];
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]++;
}
long long 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"
]
} | 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 + 1;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
template <typename T>
void chkmax(T &x, T y) {
if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, T y) {
if (x > y) x = y;
}
inline int read() {
int x = 0;
char c = getchar();
bool f = 0;
while (c < 48) f |= c == '-', c = getchar();
while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
return f ? -x : x;
}
const int maxn = 1e5 + 10, P = 998244353;
int Inc(int x, int y) { return x + y < P ? x + y : x + y - P; }
int Dec(int x, int y) { return x < y ? x - y + P : x - y; }
void Add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void Sub(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
int n, k, A[maxn], lev[maxn], dp[maxn], vp[maxn], lst[maxn];
int bsz, bl[maxn];
int ns, tag[505];
int buf_top, buf_st[maxn * 100], nxt[maxn * 100], key[maxn * 100],
val[maxn * 100];
struct hashmap {
int ct, h[1009];
int find(int x) {
for (int i = h[(x + 114514) % 1009]; i; i = nxt[i])
if (key[i] == x) return i;
return -1;
}
int query(int x) {
int tp = find(x);
return tp == -1 ? 0 : val[tp];
}
void add(int x, int v) {
int tp = find(x);
if (tp != -1) {
Add(val[tp], v);
} else {
int &p = h[(x + 114514) % 1009];
tp = buf_st[buf_top--], nxt[tp] = p, p = tp, key[tp] = x, val[tp] = v,
ct++;
}
}
void clear() {
for (int x = (0), xend = (1008); x <= xend; ++x) {
int &i = h[x];
while (i) {
key[i] = val[i] = 0, buf_st[++buf_top] = i;
int &tp = nxt[i];
i = tp, tp = 0;
}
}
}
} ump[505];
void pushdown(int ps) {
int l = (ps - 1) * bsz + 1, r = min(n, ps * bsz);
ump[ps].clear();
for (int i = (l), iend = (r); i <= iend; ++i) {
ump[ps].add(lev[i] += tag[ps], dp[i - 1]);
}
tag[ps] = 0;
}
void upd(int l, int r, int x) {
int ps = bl[l], &tp = tag[ps];
if (ump[ps].ct > 1000) pushdown(ps);
for (int i = (l), iend = (r); i <= iend; ++i) {
if (lev[i] + tp == -1 && x == 1) Add(ns, dp[i - 1]);
if (lev[i] + tp == 0 && x == -1) Sub(ns, dp[i - 1]);
if (dp[i - 1]) ump[ps].add(lev[i], P - dp[i - 1]);
ump[ps].add(lev[i] += x, dp[i - 1]);
}
}
void add(int l, int r, int x) {
if (bl[l] == bl[r]) {
upd(l, r, x);
return;
}
upd(l, bl[l] * bsz, x);
upd((bl[r] - 1) * bsz + 1, r, x);
for (int ps = (bl[l] + 1), psend = (bl[r] - 1); ps <= psend; ++ps) {
if (x == 1) {
Add(ns, ump[ps].query(-1 - tag[ps]));
} else {
Sub(ns, ump[ps].query(-tag[ps]));
}
tag[ps] += x;
}
}
void ins(int x, int v) {
pushdown(bl[x]);
if (lev[x] >= 0) Add(ns, v);
}
void solve() {
buf_top = maxn * 100 - 1;
for (int i = (1), iend = (maxn * 100 - 1); i <= iend; ++i) buf_st[i] = i;
cin >> n >> k;
bsz = 200;
for (int i = (1), iend = (n); i <= iend; ++i)
lev[i] = k, A[i] = read(), bl[i] = (i - 1) / bsz + 1;
dp[0] = 1;
for (int i = (1), iend = (n); i <= iend; ++i) {
int &p = lst[i];
p = vp[A[i]], vp[A[i]] = i;
if (p) add(lst[p] + 1, p, 1);
add(p + 1, i, -1), ins(i, dp[i - 1]), dp[i] = ns;
}
cout << dp[n] << endl;
}
signed 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3)
const int P = 998244353;
int n, k;
inline int ad(int& x, int& y) { return (x + y > P ? x + y - P : x + y); }
int A[200006], las[200006], pr[200006];
const int blo = 233;
int bel[200006];
pair<int, int> B[200006];
int S[200006];
int val[200006], gv[200006], lz[200006], cur;
void rebuild(int b) {
int l = b * blo - blo + 1, r = b * blo;
for (int i = (l), iend = (r); i <= iend; ++i) val[i] = val[i] + lz[b];
lz[b] = 0;
for (int i = (l), iend = (r); i <= iend; ++i) B[i] = make_pair(val[i], gv[i]);
sort(B + l, B + r + 1);
S[l] = B[l].second;
for (int i = (l + 1), iend = (r); i <= iend; ++i)
S[i] = ad(S[i - 1], B[i].second);
}
void add(int l, int r, int c) {
++l, ++r;
if (bel[l] == bel[r]) {
int b = bel[l];
for (int i = (l), iend = (r); i <= iend; ++i) val[i] += c;
rebuild(b);
return;
}
for (int i = (l), iend = (bel[l] * blo); i <= iend; ++i) val[i] += c;
rebuild(bel[l]);
for (int i = (r), iend = (bel[r] * blo - blo + 1); i >= iend; --i)
val[i] += c;
rebuild(bel[r]);
for (int i = (bel[l] + 1), iend = (bel[r] - 1); i <= iend; ++i) lz[i] += c;
}
int que() {
int as = 0;
for (int b = (1), bend = (bel[cur]); b <= bend; ++b) {
int l = (b - 1) * blo + 1, r = b * blo;
int ps =
upper_bound(B + l, B + r + 1, make_pair(k - lz[b], 0x3f3f3f3f)) - B - 1;
if (ps < l)
continue;
else
as = ad(as, S[ps]);
}
return as;
}
void gt(int p, int c) {
++p;
gv[p] = c;
rebuild(bel[p]);
}
int dp[200006];
void solve() {
cin >> n >> k;
for (int i = (1), iend = (n); i <= iend; ++i)
scanf("%d", A + i), las[i] = pr[A[i]], pr[A[i]] = i;
for (int i = (1), iend = (n + 1); i <= iend; ++i) bel[i] = (i - 1) / blo + 1;
dp[0] = 1;
for (int i = (1), iend = (n); i <= iend; ++i) {
cur = i + 1;
add(las[i] + 1, i, 1);
if (las[i]) add(las[las[i]] + 1, las[i], -1);
gt(i, dp[i - 1]);
dp[i] = que();
}
printf("%d\n", dp[n]);
}
signed main() { 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"
]
} | 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[id];
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MAXN = 2e5;
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int p = 998244353;
int n, k, a[100102], dp[100102], pre[100102], ap[100102];
int blk, lx[320], rx[320], bel[100102], cn, cnt[100102], laz[320];
int sum[320][100102 << 1], sdp[320];
inline void clr(int x) {
for (int i = lx[x]; i <= rx[x]; i++) sum[x][cnt[i] + n] = 0;
sdp[x] = 0;
}
inline void love(int x) {
for (int i = lx[x]; i <= rx[x]; i++) cnt[i] += laz[x];
laz[x] = 0;
for (int i = lx[x]; i <= rx[x]; i++) (sum[x][cnt[i] + n] += dp[i]) %= p;
for (int i = lx[x]; i <= rx[x]; i++)
if (cnt[i] <= k) sdp[x] = (sdp[x] + dp[i]) % p;
}
inline void chng(int l, int r, int x) {
if (l > r) return;
if (bel[l] == bel[r]) {
clr(bel[l]);
for (int i = l; i <= r; i++) cnt[i] += x;
love(bel[l]);
return;
}
clr(bel[l]);
for (int i = l; i <= rx[bel[l]]; i++) cnt[i] += x;
love(bel[l]);
clr(bel[r]);
for (int i = lx[bel[r]]; i <= r; i++) cnt[i] += x;
love(bel[r]);
for (int i = bel[l] + 1; i < bel[r]; i++) {
if (x == 1) {
sdp[i] = (sdp[i] - sum[i][k - laz[i] + n] + p) % p;
laz[i]++;
} else {
laz[i]--;
sdp[i] = (sdp[i] + sum[i][k - laz[i] + n]) % p;
}
}
}
inline int ask(int l, int r) {
if (bel[l] == bel[r]) {
int ans = 0;
for (int i = l; i <= r; i++)
if (cnt[i] + laz[bel[l]] <= k) ans = (ans + dp[i]) % p;
return ans;
}
int ans = 0;
for (int i = l; i <= rx[bel[l]]; i++)
if (cnt[i] + laz[bel[i]] <= k) ans = (ans + dp[i]) % p;
for (int i = lx[bel[r]]; i <= r; i++)
if (cnt[i] + laz[bel[i]] <= k) ans = (ans + dp[i]) % p;
for (int i = bel[l] + 1; i < bel[r]; i++) ans = (ans + sdp[i]) % p;
return ans;
}
int main() {
scanf("%d%d", &n, &k);
blk = sqrt(n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) pre[i] = ap[a[i]], ap[a[i]] = i;
for (int i = 0; i <= n; i++) {
int j = i;
while (j <= n && (i / blk) == (j / blk)) j++;
j--;
lx[cn] = i, rx[cn] = j;
for (int ii = i; ii <= j; ii++) bel[ii] = cn;
cn++;
i = j;
}
dp[0] = 1;
for (int i = 0; i < cn; i++) love(i);
for (int i = 1; i <= n; i++) {
chng(pre[i], i - 1, 1);
chng(pre[pre[i]], pre[i] - 1, -1);
clr(bel[i]);
dp[i] = ask(0, i - 1);
love(bel[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, flag[310], ret[310], a[100100], pos[100100], lst[100100], w[100100],
f[100100], cnt[310][100100];
int mo(int x, int y) { return x + y < 998244353 ? x + y : x + y - 998244353; }
void add(int l, int r, int x) {
int s = (l - 1) / 350 + 1, t = r / 350 + 1;
if (s == t) {
for (int i = l; i <= r; i++) {
w[i] += x;
cnt[s][w[i] - x] = mo(cnt[s][w[i] - x], 998244353 - f[i - 1]),
cnt[s][w[i]] = mo(cnt[s][w[i]], f[i - 1]);
if (x == 1 && w[i] == k - flag[s] + 1)
ret[s] = mo(ret[s], 998244353 - f[i - 1]);
else if (x == -1 && w[i] == k - flag[s])
ret[s] = mo(ret[s], f[i - 1]);
}
return;
}
for (int i = l; i <= s * 350; i++) {
w[i] += x;
cnt[s][w[i] - x] = mo(cnt[s][w[i] - x], 998244353 - f[i - 1]),
cnt[s][w[i]] = mo(cnt[s][w[i]], f[i - 1]);
if (x == 1 && w[i] - 1 == k - flag[s])
ret[s] = mo(ret[s], 998244353 - f[i - 1]);
else if (x == -1 && w[i] == k - flag[s])
ret[s] = mo(ret[s], f[i - 1]);
}
for (int i = s + 1; i < t; i++) {
flag[i] += x;
if (x == 1)
ret[i] = mo(ret[i], 998244353 - cnt[i][k - flag[i] + 1]);
else
ret[i] = mo(ret[i], cnt[i][k - flag[i]]);
}
for (int i = (t - 1) * 350 + 1; i <= r; i++) {
w[i] += x;
cnt[t][w[i] - x] = mo(cnt[t][w[i] - x], 998244353 - f[i - 1]),
cnt[t][w[i]] = mo(cnt[t][w[i]], f[i - 1]);
if (x == 1 && w[i] - 1 == k - flag[t])
ret[t] = mo(ret[t], 998244353 - f[i - 1]);
else if (x == -1 && w[i] == k - flag[t])
ret[t] = mo(ret[t], f[i - 1]);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) lst[i] = pos[a[i]], pos[a[i]] = i;
f[0] = 1;
for (int i = 1; i <= n; i++) {
add(lst[i] + 1, i - 1, 1);
if (lst[i]) add(lst[lst[i]] + 1, lst[i], -1);
for (int j = 1; j <= (i - 1) / 350 + 1; j++) f[i] = mo(f[i], ret[j]);
if (k >= 1)
f[i] = mo(f[i], f[i - 1]),
ret[(i - 1) / 350 + 1] = mo(ret[(i - 1) / 350 + 1], f[i - 1]);
w[i] = 1;
cnt[(i - 1) / 350 + 1][1] = mo(cnt[(i - 1) / 350 + 1][1], f[i - 1]);
}
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"
]
} | 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) {
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];
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;
blk(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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int S = 330;
const int mod = 998244353;
int n, k, a[N], pre[N], last[N];
int s[N / S + 5][N], val[N / S + 5], f[N], dp[N], inblock[N], ans;
void add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
void ins(int u, int v) {
if (f[u] + val[inblock[u]] <= k) add(ans, -dp[u - 1]);
add(s[inblock[u]][f[u]], -dp[u - 1]);
f[u] += v;
if (f[u] + val[inblock[u]] <= k) add(ans, dp[u - 1]);
add(s[inblock[u]][f[u]], dp[u - 1]);
}
void update(int L, int R, int v) {
if (L > R) return;
if (inblock[L] + 1 >= inblock[R])
for (int i = L; i <= R; i++) ins(i, v);
else {
for (int i = L; i <= inblock[L] * S; i++) ins(i, v);
for (int i = (inblock[R] - 1) * S + 1; i <= R; i++) ins(i, v);
for (int i = inblock[L] + 1; i < inblock[R]; i++) {
if (v == 1)
add(ans, mod - s[i][k - val[i]]);
else
add(ans, s[i][k + 1 - val[i]]);
val[i] += v;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = last[a[i]];
last[a[i]] = i;
}
for (int i = 1; i <= n; i++) inblock[i] = (i - 1) / S + 1;
dp[0] = 1;
add(ans, 1);
add(s[inblock[1]][f[1]], 1);
for (int i = 1; i <= n; i++) {
update(pre[i] + 1, i, 1);
update(pre[pre[i]] + 1, pre[i], -1);
dp[i] = ans;
add(ans, dp[i]);
add(s[inblock[i + 1]][f[i + 1]], 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int BLK = 320;
const int MXN = 1e5 + 5;
const int MOD = 998244353;
int add(int x, int y) { return (x += y) < MOD ? x : x - MOD; }
int sub(int x, int y) { return (x -= y) >= 00 ? x : x + MOD; }
int b[BLK];
int q[BLK][2 * BLK + 5];
int n, k;
int d[MXN];
int dp[MXN];
int arr[MXN];
int last1[MXN];
int last2[MXN];
void add(int pos) {
int id = (pos - 1) / BLK;
for (int i = BLK; i <= 2 * BLK; ++i) {
q[id][i] = add(q[id][i], dp[pos - 1]);
}
}
void update(int pos, int sgn) {
int id = (pos - 1) / BLK;
b[id] += sgn;
for (int i = pos; i > id * BLK; --i) {
if (sgn == +1) q[id][d[i] + BLK] = sub(q[id][d[i] + BLK], dp[i - 1]);
d[i] += sgn;
if (sgn == -2) {
q[id][d[i] + BLK] = add(q[id][d[i] + BLK], dp[i - 1]),
q[id][d[i] + BLK + 1] =
add(q[id][d[i] + BLK + 1], dp[i - 1]);
}
}
}
int get(int pos) {
int ret = 0;
int id = (pos - 1) / BLK;
for (int i = pos; i > id * BLK; --i) {
if (d[i] <= k) ret = add(ret, dp[i - 1]);
}
int sum = b[id];
while (--id >= 0) {
if (abs(sum - k) <= BLK) {
ret = add(ret, q[id][k - sum + BLK]);
} else if (sum < k) {
ret = add(ret, q[id][BLK + BLK]);
}
sum += b[id];
}
return ret;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
dp[0] = 1;
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
add(i);
update(i, 1);
if (last1[arr[i]]) update(last1[arr[i]], -2);
if (last2[arr[i]]) update(last2[arr[i]], +1);
dp[i] = get(i);
last2[arr[i]] = last1[arr[i]];
last1[arr[i]] = i;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T lowbit(T x) {
return x & (-x);
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T Pow(T a, T b, T p) {
T ret = 1;
a %= p;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) (ret *= a) %= p;
return ret;
}
template <class T>
inline void read(T &ret) {
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
ret = x * f;
}
const int N = 1e5 + 10, M = 1e3 + 10;
const long long Z = 998244353;
struct node {
int prev, w;
} lk[N];
int head[N], sz = 0;
void add(int x, int loc) {
lk[++sz] = {head[x], loc};
head[x] = sz;
}
int n, m, a[N], b[N], Prev[N];
int blo, bl[N];
long long f[N], g[M][M], sum[M];
inline long long __g(int x, int y) {
if (y > blo * 2)
return g[x][blo * 2];
else if (y >= 0)
return g[x][y];
else
return 0;
}
void modify(int x, int val, int up) {
b[x] = val;
int cur = 0;
fill(g[bl[x]], g[bl[x]] + blo * 2 + 1, 0);
for (int i = (min(up, ((bl[x] + 1) * blo) - 1)); i >= (((bl[x]) * blo));
i--) {
cur += b[i];
(g[bl[x]][cur + blo] += f[i - 1]) %= Z;
}
sum[bl[x]] = cur;
for (int i = (1); i <= (blo * 2); i++) (g[bl[x]][i] += g[bl[x]][i - 1]) %= Z;
}
long long query(int x) {
long long ret = 0;
long long cur = 0;
for (int i = (bl[x]); i >= (0); i--) {
(ret += __g(i, m - cur + blo)) %= Z;
cur += sum[i];
}
return ret;
}
int main() {
read(n);
read(m);
for (int i = (1); i <= (n); i++) {
read(a[i]);
add(a[i], i);
}
for (int i = (1); i <= (n); i++)
for (int j = head[i]; j; j = lk[j].prev) Prev[lk[j].w] = lk[lk[j].prev].w;
blo = ceil(sqrt(n / 6.0));
for (int i = (1); i <= (n); i++) bl[i] = i / blo;
f[0] = 1;
for (int i = (1); i <= (n); i++) {
int p = Prev[i], pp = Prev[p];
if (pp) modify(pp, 0, i);
if (p) modify(p, -1, i);
modify(i, 1, i);
f[i] = query(i);
}
printf("%lld\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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = 1e18L;
const long long MAXN = 2e5;
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"
]
} | 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 T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int a[101001];
vector<int> positions[101010];
int tr[404040];
int b[101010];
int dp[404040];
long long getSum(int* b, int* dp, int n) {
long long res = 0;
for (int j = 0; j < n; ++j) {
res += b[j] > 0 ? 0 : dp[j];
}
return res;
}
const int fftmod = 998244353;
int main() {
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];
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]++;
}
long long res = 0;
for (int j = 0; j < i + 1; ++j) res += b[j] > 0 ? 0 : dp[j];
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
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...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
const int maxn = 2e5 + 10;
const int md = 998244353;
const int BlockLen = 50;
const int NumBlocks = maxn / BlockLen + 10;
void addto(int &x, int y) {
x = x + y;
if (x >= md) x -= md;
}
int T;
string str;
int n, m;
int dp[maxn], cnt[maxn];
vector<int> e[maxn];
int nk;
struct Block {
int L, R;
vector<pair<int, int>> pref_sums;
int ptr;
int increase;
vector<int> num_incrs;
Block() : L(0), R(0), ptr(0), increase(0) {}
Block(int l, int r) : L(l), R(r), ptr(0), increase(0), num_incrs(r - l) {}
void IncrAll(int delta) { increase += delta; }
void Rebuild() {
vector<int> order(num_incrs.size());
int min_num_incrs = num_incrs[0];
for (int i = 1; i < num_incrs.size(); ++i) {
min_num_incrs = min(min_num_incrs, num_incrs[i]);
}
vector<vector<int>> base(num_incrs.size());
for (int i = 0; i < num_incrs.size(); ++i) {
base[num_incrs[i] - min_num_incrs].push_back(i);
}
int cnt = 0;
for (int i = 0; i < base.size(); ++i) {
for (auto id : base[i]) order[cnt++] = id;
}
pref_sums.clear();
pref_sums.emplace_back((int)-1e9, 0);
pref_sums.emplace_back(num_incrs[order[0]], dp[order[0] + L]);
for (int i = 1; i < order.size(); ++i) {
const int v = order[i];
long long val = (pref_sums.back().second + dp[v + L]) % md;
if (num_incrs[v] == pref_sums.back().first) {
pref_sums.back().second = val;
} else {
pref_sums.emplace_back(num_incrs[v], val);
}
}
ptr = 0;
42;
}
void IncrSeg(int lft, int rgt, int delta) {
for (int i = 0; i < (int)num_incrs.size(); ++i) {
num_incrs[i] += increase;
}
increase = 0;
for (int i = lft - L; i < rgt - L; ++i) {
num_incrs[i] += delta;
}
42;
Rebuild();
}
int GetAtMaxK() {
while (ptr > 0 && pref_sums[ptr].first + increase > nk) {
--ptr;
}
while (ptr < pref_sums.size() - 1 &&
pref_sums[ptr + 1].first + increase <= nk) {
++ptr;
}
42;
return pref_sums[ptr].second;
}
};
Block blocks[NumBlocks];
void Increase(int L, int R, int delta) {
42;
for (int i = 0; i < n; ++i) {
const int l_block = i * BlockLen;
const int r_block = min((i + 1) * BlockLen, n + 1);
if (l_block > n) break;
if (r_block <= L || l_block >= R) continue;
if (L <= l_block && r_block <= R) {
blocks[i].IncrAll(delta);
} else {
blocks[i].IncrSeg(max<int>(L, l_block), min(R, r_block), delta);
}
}
}
int GetAtMaxK() {
int ans = 0;
for (int i = 0; i < NumBlocks; ++i) {
const int l_block = i * BlockLen;
if (l_block > n) break;
ans = (ans + blocks[i].GetAtMaxK()) % md;
}
return ans;
}
void BuildBlocks() {
for (int i = 0; i < NumBlocks; ++i) {
const int l_block = i * BlockLen;
const int r_block = min(n + 1, (i + 1) * BlockLen);
if (l_block > n) break;
blocks[i] = Block(l_block, r_block);
blocks[i].Rebuild();
}
}
void task() {
cin >> n >> nk;
dp[0] = 1;
BuildBlocks();
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
e[x].push_back(i);
int l1, r1 = i;
if (e[x].size() > 1)
l1 = e[x][e[x].size() - 2];
else
l1 = 0;
Increase(l1, r1, 1);
if (e[x].size() >= 2) {
int l2 = e[x].size() >= 3 ? e[x][e[x].size() - 3] : 0, r2 = l1;
Increase(l2, r2, -1);
}
42;
dp[i] = GetAtMaxK();
blocks[i / BlockLen].Rebuild();
42;
}
cout << dp[n] << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
T = 1;
while (T--) {
task();
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int i, j, m, n, block_size, a[200005], k;
int temp[3][100005], pre[3][100005], smallest[319];
long long sum[319][319], f[200005], dp[200005], inf, mod;
int tag[319];
void update(int l, int r, int c) {
int block1, block2, ind1, ind2;
block1 = (l - 1) / block_size;
block2 = (r - 1) / block_size;
ind1 = (l - 1) % block_size;
ind2 = (r - 1) % block_size;
int i, j;
for (i = block1 + 1; i <= block2 - 1; i++) {
tag[i] += c;
smallest[i] += c;
}
long long x[319];
if (block1 == block2) {
for (i = l; i <= r; i++) {
f[i] += c;
}
smallest[block1] = inf;
for (i = block1 * block_size; i <= (block1 + 1) * block_size - 1; i++) {
f[i + 1] += tag[block1];
if (f[i + 1] < smallest[block1]) smallest[block1] = f[i + 1];
}
for (i = 0; i <= block_size - 1; i++) {
x[i] = 0;
}
for (i = block1 * block_size; i <= (block1 + 1) * block_size - 1; i++) {
x[f[i + 1] - smallest[block1]] =
(x[f[i + 1] - smallest[block1]] + dp[i]) % mod;
}
sum[block1][0] = x[0];
for (i = 1; i <= block_size - 1; i++) {
sum[block1][i] = (sum[block1][i - 1] + x[i]) % mod;
}
tag[block1] = 0;
return;
}
for (i = ind1; i <= block_size - 1; i++) {
f[block1 * block_size + i + 1] += c;
}
smallest[block1] = inf;
for (i = block1 * block_size; i <= (block1 + 1) * block_size - 1; i++) {
f[i + 1] += tag[block1];
if (f[i + 1] < smallest[block1]) smallest[block1] = f[i + 1];
}
for (i = 0; i <= block_size - 1; i++) {
x[i] = 0;
}
for (i = block1 * block_size; i <= (block1 + 1) * block_size - 1; i++) {
x[f[i + 1] - smallest[block1]] =
(x[f[i + 1] - smallest[block1]] + dp[i]) % mod;
}
sum[block1][0] = x[0];
for (i = 1; i <= block_size - 1; i++) {
sum[block1][i] = (sum[block1][i - 1] + x[i]) % mod;
}
tag[block1] = 0;
for (i = 0; i <= ind2; i++) {
f[block2 * block_size + i + 1] += c;
}
smallest[block2] = inf;
for (i = block2 * block_size; i <= (block2 + 1) * block_size - 1; i++) {
f[i + 1] += tag[block2];
if (f[i + 1] < smallest[block2]) smallest[block2] = f[i + 1];
}
for (i = 0; i <= block_size - 1; i++) {
x[i] = 0;
}
for (i = block2 * block_size; i <= (block2 + 1) * block_size - 1; i++) {
x[f[i + 1] - smallest[block2]] =
(x[f[i + 1] - smallest[block2]] + dp[i]) % mod;
}
sum[block2][0] = x[0];
for (i = 1; i <= block_size - 1; i++) {
sum[block2][i] = (sum[block2][i - 1] + x[i]) % mod;
}
tag[block2] = 0;
}
int main() {
mod = 998244353;
inf = 1000000000;
scanf("%d%d", &n, &k);
block_size = int(sqrt(n) + 0.000001);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; i <= n; i++) {
if (temp[1][a[i]]) {
pre[1][i] = temp[1][a[i]];
if (temp[2][a[i]]) {
pre[2][i] = temp[2][a[i]];
}
temp[2][a[i]] = temp[1][a[i]];
temp[1][a[i]] = i;
} else {
temp[1][a[i]] = i;
}
}
dp[0] = 1;
for (i = 1; i <= n; i++) {
update(pre[1][i] + 1, i, 1);
if (pre[2][i] + 1 <= pre[1][i]) {
update(pre[2][i] + 1, pre[1][i], -1);
}
int block, ind;
block = (i - 1) / block_size;
ind = (i - 1) % block_size;
for (j = 0; j <= block; j++) {
if (smallest[j] <= k) {
if (smallest[j] + block_size - 1 > k) {
dp[i] = (dp[i] + sum[j][k - smallest[j]]) % mod;
} else {
dp[i] = (dp[i] + sum[j][block_size - 1]) % mod;
}
}
}
}
printf("%lld\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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int Read() {
int x(0);
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x;
}
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)), pieces(ceil(1.0 * n / size));
for (int i = 1; i < pieces; ++i) {
bl[i] = (i - 1) * size + 1;
br[i] = i * size;
for (int j = bl[i]; j <= br[i]; ++j) col[j] = i;
}
bl[pieces] = (pieces - 1) * size + 1;
br[pieces] = n;
for (int j = bl[pieces]; j <= br[pieces]; ++j) col[j] = pieces;
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)
(v[i] + lazy[lt] == k) ? ans = (ans - f[i] + mod) % mod : 0;
else
(v[i] + lazy[lt] == k + 1) ? ans = (ans + f[i]) % mod : 0;
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)
(v[i] + lazy[lt] == k) ? ans = (ans - f[i] + mod) % mod : 0;
else
(v[i] + lazy[lt] == k + 1) ? ans = (ans + f[i]) % mod : 0;
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)
(v[i] + lazy[rt] == k) ? ans = (ans - f[i] + mod) % mod : 0;
else
(v[i] + lazy[rt] == k + 1) ? ans = (ans + f[i]) % mod : 0;
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 Solve() {
ans = f[1] = sum[1][0] = 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;
sum[col[i + 1]][v[i + 1]] = (sum[col[i + 1]][v[i + 1]] + f[i + 1]) % mod;
ans = (ans + f[i + 1]) % mod;
}
printf("%d", f[n + 1]);
}
int main() {
n = Read();
k = Read();
for (int i = 1; i <= n; ++i) a[i] = Read();
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = (res * x) % 998244353;
y = y / 2, x = (x * x) % 998244353;
}
return res % 998244353;
}
long long n, k;
const long long BLSZ = 300;
long long NUMBL;
inline long long getBL(long long i) { return i / BLSZ; }
inline long long getL(long long i) { return i * BLSZ; }
inline long long getR(long long i) { return min((i + 1) * BLSZ - 1, n - 1); }
inline void add(long long &a, long long b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
long long base[100100];
long long arr[100100];
long long dp[100100];
vector<vector<long long> > pos(100100);
struct block {
long long l, r;
long long offset;
long long pref[BLSZ];
void init(long long _l, long long _r, long long val) {
l = (_l), r = (_r), offset = (val);
}
void recalc() {
long long smallest = 1e18;
long long i;
for (i = l; i <= r; i++) smallest = min(smallest, base[i] + offset);
for (i = l; i <= r; i++) {
base[i] = base[i] - smallest + offset;
}
offset = smallest;
for (i = 0; i < BLSZ; i++) pref[i] = 0;
for (i = l; i <= r; i++) {
add(pref[base[i]], dp[i]);
}
for (i = 1; i < BLSZ; i++) add(pref[i], pref[i - 1]);
}
};
block *BARR;
void init() {
NUMBL = getBL(n - 1);
BARR = new block[NUMBL + 1];
for (long long i = 0; i <= NUMBL; i++) {
BARR[i].init(getL(i), getR(i), 0);
BARR[i].recalc();
}
}
void update(long long l, long long r, long long diff) {
if (getBL(l) == getBL(r)) {
for (long long i = l; i <= r; ++i) {
base[i] += diff;
}
BARR[getBL(l)].recalc();
} else {
long long x = getBL(l);
for (long long i = l; i <= getR(x); i++) {
base[i] += diff;
}
BARR[x].recalc();
x = getBL(r);
for (long long i = r; i >= getL(x); i--) {
base[i] += diff;
}
BARR[x].recalc();
for (long long i = getBL(l) + 1; i < getBL(r); i++) {
BARR[i].offset += diff;
}
}
}
long long query(long long l, long long r) {
long long ans = 0;
if (getBL(l) == getBL(r)) {
long long x = getBL(l);
for (long long i = l; i <= r; ++i) {
if (base[i] + BARR[x].offset <= k) add(ans, dp[i]);
}
return ans;
} else {
long long x = getBL(l);
for (long long i = l; i <= getR(x); i++) {
if (base[i] + BARR[x].offset <= k) add(ans, dp[i]);
}
x = getBL(r);
for (long long i = r; i >= getL(x); i--) {
if (base[i] + BARR[x].offset <= k) add(ans, dp[i]);
}
for (long long i = getBL(l) + 1; i < getBL(r); i++) {
if (k - BARR[i].offset >= 0)
add(ans, BARR[i].pref[min(k - BARR[i].offset, BLSZ - 1)]);
}
return ans;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
dp[0] = 1;
init();
long long i;
for (i = 0; i < n; i++) {
cin >> arr[i];
dp[i + 1] = 0;
if (pos[arr[i]].size() == 0) {
update(0, i, 1);
} else if (pos[arr[i]].size() == 1) {
update(0, pos[arr[i]][0], -1);
update(pos[arr[i]][0] + 1, i, 1);
} else {
update(pos[arr[i]][pos[arr[i]].size() - 2] + 1,
pos[arr[i]][pos[arr[i]].size() - 1], -1);
update(pos[arr[i]][pos[arr[i]].size() - 1] + 1, i, 1);
}
pos[arr[i]].push_back(i);
dp[i + 1] = query(0, i);
BARR[getBL(i + 1)].recalc();
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int blkbase[(350 + 5)][(350 + 5)];
int blkpsum[(350 + 5)][(350 + 5)];
int f[100005];
int n, k;
int a[100005];
int previ[100005];
int previval[100005];
int blk[100005];
int blkbegin[100005];
int blkend[1000005];
int cur[100005];
int cur_offset[(350 + 5)];
int cur_cnt[(350 + 5)];
int cur_total = 0;
int blkcnt;
bool cmp(int A, int B) { return cur[A] < cur[B]; }
void rebuild(int block) {
for (int i = blkbegin[block], j = 1; j <= 350; i++, j++) {
blkbase[block][j] = i;
}
sort(blkbase[block] + 1, blkbase[block] + 350 + 1, cmp);
int r = 0;
for (int i = 1; i <= 350; i++) {
blkpsum[block][i] = blkpsum[block][i - 1] + f[blkbase[block][i] - 1];
if (blkpsum[block][i] >= 998244353) blkpsum[block][i] -= 998244353;
if (cur[blkbase[block][i]] <= k) r = i;
}
cur_total -= cur_cnt[block];
cur_total += blkpsum[block][r];
cur_cnt[block] = blkpsum[block][r];
cur_total %= 998244353;
cur_total += 998244353;
cur_total %= 998244353;
}
void update(int l, int r, int change) {
for (int i = blk[l] + 1; i < blk[r]; i++) {
cur_offset[i] += change;
int L = 0, R = 350;
while (L < R) {
int mid = (L + R) / 2 + 1;
if (cur[blkbase[i][mid]] + cur_offset[i] <= k) {
L = mid;
} else {
R = mid - 1;
}
}
cur_total -= cur_cnt[i];
cur_total += blkpsum[i][R];
cur_cnt[i] = blkpsum[i][R];
cur_total %= 998244353;
cur_total += 998244353;
cur_total %= 998244353;
}
for (int i = blkbegin[blk[l]]; i <= blkend[blk[l]]; i++) {
cur[i] += cur_offset[blk[l]];
}
cur_offset[blk[l]] = 0;
for (int i = l; i <= blkend[blk[l]] && i <= r; i++) {
cur[i] += change;
}
for (int j = 1; j <= n; j++) {
}
rebuild(blk[l]);
if (blk[l] != blk[r]) {
for (int i = blkbegin[blk[r]]; i <= blkend[blk[r]]; i++) {
cur[i] += cur_offset[blk[r]];
}
cur_offset[blk[r]] = 0;
for (int i = blkbegin[blk[r]]; i <= r; i++) {
cur[i] += change;
}
rebuild(blk[r]);
}
}
int main() {
cur[0] = -10000000;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
previ[i] = previval[a[i]];
previval[a[i]] = i;
}
for (int i = 1; i <= 100000; i++) blk[i] = (i - 1) / 350 + 1;
for (int i = 1; i <= 350; i++) {
blkbegin[i] = (i - 1) * 350 + 1;
blkend[i] = blkbegin[i] + 350 - 1;
for (int j = (i - 1) * 350 + 1, x = 1; x <= 350; j++, x++) {
blkbase[i][x] = j;
}
}
f[0] = 1;
rebuild(1);
for (int i = 1; i <= n; i++) {
update(previ[i] + 1, i, 1);
if (previ[i]) update(previ[previ[i]] + 1, previ[i], -1);
f[i] = cur_total;
for (int j = 0; j < 350; j++) {
cur[blkbegin[blk[i + 1]] + j] += cur_offset[blk[i + 1]];
}
cur_offset[blk[i + 1]] = 0;
for (int j = 1; j <= n; j++) {
}
rebuild(blk[i + 1]);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int M = 205;
const int N = 100005;
const int L = 100000;
const int mod = 998244353;
int n, k, len, a[N], bel[N], pre[N], las[N], dp[N], fl[N], tot[N], ans[M],
s[M][N << 1];
void ad(int i, int x) {
(s[bel[i]][L + tot[i]] += mod - dp[i]) %= mod;
tot[i] += x;
(s[bel[i]][L + tot[i]] += dp[i]) %= mod;
if (x == 1 && tot[i] + fl[bel[i]] == k + 1) {
(ans[bel[i]] += mod - dp[i]) %= mod;
}
if (x == -1 && tot[i] + fl[bel[i]] == k) {
(ans[bel[i]] += dp[i]) %= mod;
}
}
void add(int l, int r, int x) {
if (bel[l] + 1 >= bel[r]) {
for (int i = l; i <= r; i++) {
ad(i, x);
}
return;
}
for (int i = l; i < (bel[l] + 1) * len; i++) {
ad(i, x);
}
for (int i = bel[r] * len; i <= r; i++) {
ad(i, x);
}
for (int i = bel[l] + 1; i < bel[r]; i++) {
if (x == 1) {
(ans[i] += mod - s[i][L + k - fl[i]]) %= mod;
}
if (x == -1) {
(ans[i] += s[i][L + k - fl[i] + 1]) %= mod;
}
fl[i] += x;
}
}
int query(int l, int r) {
int sum = 0;
if (bel[l] + 1 >= bel[r]) {
for (int i = l; i <= r; i++) {
if (tot[i] + fl[bel[i]] <= k) {
(sum += dp[i]) %= mod;
}
}
return sum;
}
for (int i = l; i < (bel[l] + 1) * len; i++) {
if (tot[i] + fl[bel[i]] <= k) {
(sum += dp[i]) %= mod;
}
}
for (int i = bel[r] * len; i <= r; i++) {
if (tot[i] + fl[bel[i]] <= k) {
(sum += dp[i]) %= mod;
}
}
for (int i = bel[l] + 1; i < bel[r]; i++) {
(sum += ans[i]) %= mod;
}
return sum;
}
signed main() {
scanf("%d%d", &n, &k);
len = 500;
for (int i = 0; i <= n; i++) {
bel[i] = i / len;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
dp[0] = 1;
ans[0] += dp[0];
s[0][L] += dp[0];
for (int i = 1; i <= n; i++) {
pre[i] = las[a[i]];
las[a[i]] = i;
add(pre[i], i - 1, 1);
if (pre[i]) add(pre[pre[i]], pre[i] - 1, -1);
dp[i] = query(0, i - 1);
(ans[bel[i]] += dp[i]) %= mod;
(s[bel[i]][L - fl[bel[i]]] += dp[i]) %= 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long mod = 998244353LL;
int kk;
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];
pre[dist[i - st] - lo + 1] %= mod;
}
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;
}
int k = kk;
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];
ret %= mod;
}
}
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 >> kk;
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int ms = 110000;
const int bs = 1000;
const int MOD = 998244353;
void add(int &a, int b) { a = a + b < MOD ? a + b : a + b - MOD; }
int *pivot;
bool comp(int x1, int x2) { return pivot[x1] < pivot[x2]; }
int tmp[2][bs];
struct Bucket {
Bucket() { dirty = size = 0; }
void push_back(int x) {
p[size] = size;
lazy[size] = 0;
dp[size] = x;
values[size++] = 0;
}
void clean() {
for (int i = size - 2; i >= 0; i--) {
add(lazy[i], lazy[i + 1]);
}
for (int i = 0; i < size; i++) {
add(dp[p[i]], lazy[i]);
lazy[i] = 0;
}
if (dirty) {
for (int i = 0; i < size; i++) {
values[i] += dirty;
}
dirty = 0;
}
}
void sort() {
clean();
pivot = values;
std::sort(p, p + size, comp);
}
int getID(int x) {
if (x < values[p[0]] + dirty) {
return -1;
}
int l = 0, r = size - 1;
while (l != r) {
int mid = (l + r + 1) / 2;
if (values[p[mid]] + dirty <= x) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
void upd(int x, int l, int r, int val) {
if (l == 0 && r == size) {
int id = getID(x);
if (id != -1) {
add(lazy[id], val);
}
} else {
for (int i = l; i < r; i++) {
if (values[i] + dirty <= x) {
add(dp[i], val);
} else {
}
}
}
}
void upd2(int l, int r, int val) {
if (l == 0 && r == size) {
dirty += val;
} else {
for (int i = l; i < r; i++) {
values[i] += val;
}
clean();
pivot = values;
int s0 = 0, s1 = 0;
for (int i = 0; i < size; i++) {
if (l <= p[i] && p[i] < r) {
tmp[0][s0++] = p[i];
} else {
tmp[1][s1++] = p[i];
}
}
std::merge(tmp[0], tmp[0] + s0, tmp[1], tmp[1] + s1, p, comp);
}
}
int values[bs], p[bs], lazy[bs], dp[bs];
int dirty;
int size;
};
Bucket b[ms / bs + 2];
std::vector<int> pos[ms];
int a[ms];
int pt[ms];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, k;
std::cin >> n >> k;
for (int i = 1; i <= n; i++) {
pos[i].push_back(-1);
}
for (int i = 0; i < n; i++) {
std::cin >> a[i];
pos[a[i]].push_back(i);
b[i / bs].push_back(i == 0 ? 1 : 0);
}
b[n / bs].push_back(0);
for (int i = 1; i <= n; i++) {
pos[i].push_back(n);
pos[i].push_back(n);
}
auto upd2 = [&](int l, int r, int val) {
if (l >= r) return;
int bl = l / bs, br = (r - 1) / bs;
if (bl == br) {
b[bl].upd2(l - bl * bs, r - bl * bs, val);
return;
}
b[bl].upd2(l - bl * bs, bs, val);
for (int i = bl + 1; i < br; i++) {
b[i].upd2(0, bs, val);
}
b[br].upd2(0, r - br * bs, val);
};
auto upd = [&](int l, int r, int val) {
if (l >= r) return;
int bl = l / bs, br = (r - 1) / bs;
if (bl == br) {
b[bl].upd(k, l - bl * bs, r - bl * bs, val);
return;
}
b[bl].upd(k, l - bl * bs, bs, val);
for (int i = bl + 1; i < br; i++) {
b[i].upd(k, 0, bs, val);
}
b[br].upd(k, 0, r - br * bs, val);
};
for (int i = 1; i <= n; i++) {
upd2(pos[i][1] + 1, pos[i][2] + 1, 1);
}
for (int i = 0; i < n; i++) {
b[i / bs].clean();
int dp = b[i / bs].dp[i % bs];
upd(i + 1, n + 1, dp);
{
int v = a[i];
upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, -1);
pt[v]++;
upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, 1);
}
}
b[n / bs].clean();
std::cout << b[n / bs].dp[n % bs] << '\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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int magic = 404, mod = 998244353, maxn = 100000;
int n, k, pre[111111], a[111111], cur[111111], dp[111111], b[111111];
int pr[255][222222], d[333];
void add(int &x, int y) {
x += y;
if (x < 0) x += mod;
if (x >= mod) x -= mod;
}
void Add(int l, int r, int R) {
while (r >= l && r / magic == R / magic) {
b[r]++;
r--;
}
if (l > r) return;
int lb = l / magic, rb = r / magic;
if (lb == rb) {
for (int i = l; i <= r; i++) {
add(pr[lb][b[i] + maxn], -dp[i - 1]);
b[i]++;
}
} else {
for (int i = l; i < (lb + 1) * magic; i++) {
add(pr[lb][b[i] + maxn], -dp[i - 1]);
b[i]++;
}
for (int i = rb * magic; i <= r; i++) {
add(pr[rb][b[i] + maxn], -dp[i - 1]);
b[i]++;
}
for (int i = lb + 1; i < rb; i++) {
d[i]++;
}
}
}
void del(int l, int r, int R) {
while (r >= l && r / magic == R / magic) {
b[r]--;
r--;
}
if (l > r) return;
int lb = l / magic, rb = r / magic;
if (lb == rb) {
for (int i = l; i <= r; i++) {
add(pr[lb][b[i] - 1 + maxn], dp[i - 1]);
b[i]--;
}
} else {
for (int i = l; i < (lb + 1) * magic; i++) {
add(pr[lb][b[i] - 1 + maxn], dp[i - 1]);
b[i]--;
}
for (int i = rb * magic; i <= r; i++) {
add(pr[rb][b[i] - 1 + maxn], dp[i - 1]);
b[i]--;
}
for (int i = lb + 1; i < rb; i++) {
d[i]--;
}
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
pre[i] = cur[a[i]];
cur[a[i]] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
Add(pre[i] + 1, i, i + 1);
if (pre[i]) del(pre[pre[i]] + 1, pre[i], i + 1);
for (int j = i; j / magic == (i + 1) / magic && j > 0; j--) {
if (b[j] <= k) add(dp[i], dp[j - 1]);
}
for (int j = (i + 1) / magic - 1; j >= 0; j--) {
add(dp[i], pr[j][k - d[j] + maxn]);
}
if ((i + 1) % magic == magic - 1) {
for (int j = i + 1; j / magic == (i + 1) / magic && j > 0; j--) {
add(pr[j / magic][b[j] + maxn], dp[j - 1]);
}
for (int j = 1; j <= n + 2; j++)
add(pr[(i + 1) / magic][j + maxn], pr[(i + 1) / magic][j - 1 + maxn]);
}
}
if (n == 10000 && k == 10000 && dp[n] != 430192466) {
for (int i = 2; i <= n; i++) {
if (dp[i] != dp[i - 1] * 2 % mod) {
cout << i << " " << dp[i] << " " << dp[i - 1] << endl;
}
}
return 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"
]
} | 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]] = (sum[col[x + 1]][v[x + 1]] + f[x + 1]) % mod;
ans = (ans + f[x + 1]) % mod;
}
inline void Solve() {
ans = 1;
f[1] = 1;
sum[1][0] = 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int BASE = 998244353;
const int SZ = 320;
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--) {
int need = k - sumC;
if (need >= -SZ) res += blocks[j].sumF[min(need, SZ) + SZ];
sumC += blocks[j].sumC;
}
return res % BASE;
}
void constructBlock(int block) {
BlockData &data = blocks[block];
data.sumC = 0;
data.sumF = vector<long long>(SZ * 2 + 1, 0);
for (int i = block * SZ + SZ - 1; i >= block * SZ; i--) {
data.sumC += c[i];
data.sumF[data.sumC + SZ] += i ? f[i - 1] : 1;
}
for (int i = 0; i <= SZ * 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int Read() {
int x(0);
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x;
}
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 int G(int x) {
while (x > mod) x -= mod;
return x;
}
inline void Fir() {
int size(sqrt(n)), pieces(ceil(1.0 * n / size));
for (int i = 1; i < pieces; ++i) {
bl[i] = (i - 1) * size + 1;
br[i] = i * size;
for (int j = bl[i]; j <= br[i]; ++j) col[j] = i;
}
bl[pieces] = (pieces - 1) * size + 1;
br[pieces] = n;
for (int j = bl[pieces]; j <= br[pieces]; ++j) col[j] = pieces;
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 = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);
}
sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,
sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);
}
} else {
for (int i = l; i <= br[lt]; ++i) {
if (val == 1) {
if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);
}
sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,
sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);
}
for (int i = bl[rt]; i <= r; ++i) {
if (val == 1) {
if (v[i] + lazy[rt] == k) ans = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[rt] == k + 1) ans = G(ans + f[i]);
}
sum[rt][v[i]] = G(sum[rt][v[i]] - f[i] + mod), v[i] += val,
sum[rt][v[i]] = G(sum[rt][v[i]] + f[i]);
}
for (int i = lt + 1; i <= rt - 1; ++i) {
if (val == 1)
ans = G(ans - sum[i][k - lazy[i]] + mod);
else
ans = G(ans + sum[i][k + 1 - lazy[i]]);
lazy[i] += val;
}
}
}
inline void Update(int x) {
sum[col[x + 1]][v[x + 1]] = G(sum[col[x + 1]][v[x + 1]] + f[x + 1]);
ans = G(ans + f[x + 1]);
}
inline void Solve() {
ans = f[1] = sum[1][0] = 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MOD = 119 << 23 | 1;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
unsigned long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
const int maxn = 1e5 + 5;
const int magic = 500;
int n, k;
int a[maxn];
int nxt[maxn];
int off[magic];
int c[maxn];
int f[magic][maxn];
int dp[maxn];
int sum;
void inc(int d) {
if (0 <= k - off[d] && k - off[d] < maxn) {
submod(sum, f[d][k - off[d]]);
}
off[d]++;
}
void dec(int d) {
off[d]--;
if (0 <= k - off[d] && k - off[d] < maxn) {
addmod(sum, f[d][k - off[d]]);
}
}
void upd(int d, int l, int r, int val) {
for (int i = d * magic; i < (d + 1) * magic && i < n; i++) {
if (c[i] + off[d] <= k) {
submod(sum, f[d][c[i]]);
}
f[d][c[i]] = 0;
c[i] += off[d];
}
off[d] = 0;
for (int i = l; i <= r; i++) {
c[i] += val;
}
for (int i = d * magic; i < (d + 1) * magic && i < n; i++) {
addmod(f[d][c[i]], dp[i + 1]);
if (c[i] <= k) {
addmod(sum, dp[i + 1]);
}
}
}
void upd(int l, int r, int val) {
int il = (l + magic - 1) / magic;
int ir = r / magic;
for (int i = (il); i < (ir); ++i) {
if (val == 1) {
inc(i);
} else {
dec(i);
}
}
if (l < il * magic) {
upd(il - 1, l, min(il * magic - 1, r), val);
}
if (il <= ir && ir * magic <= r) {
upd(ir, max(l, ir * magic), r, val);
}
}
void chemthan() {
cin >> n >> k;
for (int i = (0); i < (n); ++i) cin >> a[i];
dp[n] = 1;
vector<int> lst(n + 1, n);
fill_n(nxt, n + 1, n);
for (int i = (n)-1; i >= (0); --i) {
addmod(sum, dp[i + 1]);
addmod(f[i / magic][0], dp[i + 1]);
nxt[i] = lst[a[i]];
if (nxt[i] == n) {
upd(i, nxt[i] - 1, 1);
} else {
int ni = nxt[i];
upd(ni, nxt[ni] - 1, -1);
upd(i, ni - 1, 1);
}
dp[i] = sum;
lst[a[i]] = i;
}
cout << dp[0] << "\n";
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
const int N = 101000, 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) {
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"
]
} | 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;
}
if (a < 0) {
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--;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int n, k, unit, tot;
int be[(100005)], st[(100005)], en[(100005)], a[(100005)], sum[2055][2055],
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]] = Inc(sum[u][S[i]], f[i]);
for (int i = unit - 1; i >= 0; i--) sum[u][i] = Inc(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++) {
if (lim - lazy[i] > unit) continue;
ret = Inc(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 - 1] != be[i])
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] + 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"
]
} | CORRECT | java | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
DIsolation solver = new DIsolation();
solver.solve(1, in, out);
out.close();
}
}
static class DIsolation {
static Modular mod = new Modular(998244353);
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
int k = in.readInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.readInt();
}
int[] registries = new int[n + 1];
int[] last = new int[n + 1];
for (int i = 1; i <= n; i++) {
last[i] = registries[a[i]];
registries[a[i]] = i;
}
int[] dp = new int[n + 1];
BlockManager bm = new BlockManager(n, k);
bm.append(0, new Element(0, 1));
for (int i = 1; i <= n; i++) {
if (last[i] == 0) {
bm.add(0, i - 1, 1);
} else {
int l = last[last[i]] + 1;
int m = last[i];
bm.add(l - 1, m - 1, -1);
bm.add(m, i - 1, 1);
}
dp[i] = mod.valueOf(bm.sumOf());
bm.append(i, new Element(0, dp[i]));
}
out.println(dp[n]);
}
}
static class Element {
int k;
int val;
public Element(int k, int val) {
this.k = k;
this.val = val;
}
}
static interface LongEntryIterator {
boolean hasNext();
void next();
long getEntryKey();
long getEntryValue();
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Modular {
int m;
public Modular(int m) {
this.m = m;
}
public Modular(long m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public Modular(double m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public int valueOf(long x) {
x %= m;
if (x < 0) {
x += m;
}
return (int) x;
}
public String toString() {
return "mod " + m;
}
}
static class DigitUtils {
private DigitUtils() {
}
public static int floorDiv(int a, int b) {
return a < 0 ? -ceilDiv(-a, b) : a / b;
}
public static int ceilDiv(int a, int b) {
if (a < 0) {
return -floorDiv(-a, b);
}
int c = a / b;
if (c * b < a) {
return c + 1;
}
return c;
}
}
static class BlockManager {
Block[] blocks;
int bSize;
public BlockManager(int n, int k) {
bSize = (int) Math.ceil(Math.sqrt(n + 1));
int m = DigitUtils.ceilDiv(n + 1, bSize);
blocks = new Block[m];
for (int i = 0; i < m; i++) {
blocks[i] = new Block(bSize, k);
}
}
public void append(int i, Element e) {
blocks[i / bSize].append(e);
}
public void add(int ll, int rr, int x) {
for (int i = 0; i < blocks.length; i++) {
int l = i * bSize;
int r = l + bSize - 1;
if (r < ll || l > rr) {
continue;
}
if (ll <= l && r <= rr) {
blocks[i].add(x);
} else {
blocks[i].add(Math.max(ll, l) - l, Math.min(rr, r) - l, x);
}
}
}
public long sumOf() {
long ans = 0;
for (Block b : blocks) {
ans += b.sum;
}
return ans;
}
}
static class Block {
Element[] elements;
LongHashMap map;
int size;
int add;
long sum;
int k;
public Block(int n, int k) {
elements = new Element[n];
this.k = k;
map = new LongHashMap(n, true);
}
public void add(int x) {
if (x > 0) {
sum -= map.getOrDefault(k - add, 0);
add++;
} else {
add--;
sum += map.getOrDefault(k - add, 0);
}
}
public void add(int l, int r, int x) {
pushDown();
for (int i = l; i <= r; i++) {
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0) - elements[i].val);
if (x > 0) {
if (elements[i].k == k) {
sum -= elements[i].val;
}
elements[i].k++;
} else {
elements[i].k--;
if (elements[i].k == k) {
sum += elements[i].val;
}
}
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0) + elements[i].val);
}
}
private void pushDown() {
if (add != 0) {
map.clear();
for (int i = 0; i < size; i++) {
elements[i].k += add;
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0L) + elements[i].val);
}
add = 0;
}
}
public void append(Element e) {
pushDown();
elements[size++] = e;
map.put(e.k, map.getOrDefault(e.k, 0) + e.val);
if (e.k <= k) {
sum += e.val;
}
}
}
static class LongHashMap {
private int[] slot;
private int[] next;
private long[] keys;
private long[] values;
private int alloc;
private boolean[] removed;
private int mask;
private int size;
private boolean rehash;
public LongHashMap(int cap, boolean rehash) {
this.mask = (1 << (32 - Integer.numberOfLeadingZeros(cap - 1))) - 1;
slot = new int[mask + 1];
next = new int[cap + 1];
keys = new long[cap + 1];
values = new long[cap + 1];
removed = new boolean[cap + 1];
this.rehash = rehash;
}
private void doubleCapacity() {
int newSize = Math.max(next.length + 10, next.length * 2);
next = Arrays.copyOf(next, newSize);
keys = Arrays.copyOf(keys, newSize);
values = Arrays.copyOf(values, newSize);
removed = Arrays.copyOf(removed, newSize);
}
public void alloc() {
alloc++;
if (alloc >= next.length) {
doubleCapacity();
}
next[alloc] = 0;
removed[alloc] = false;
size++;
}
private void rehash() {
int[] newSlots = new int[Math.max(16, slot.length * 2)];
int newMask = newSlots.length - 1;
for (int i = 0; i < slot.length; i++) {
if (slot[i] == 0) {
continue;
}
int head = slot[i];
while (head != 0) {
int n = next[head];
int s = hash(keys[head]) & newMask;
next[head] = newSlots[s];
newSlots[s] = head;
head = n;
}
}
this.slot = newSlots;
this.mask = newMask;
}
private int hash(long x) {
int h = Long.hashCode(x);
return h ^ (h >>> 16);
}
public void put(long x, long y) {
put(x, y, true);
}
public void put(long x, long y, boolean cover) {
int h = hash(x);
int s = h & mask;
if (slot[s] == 0) {
alloc();
slot[s] = alloc;
keys[alloc] = x;
values[alloc] = y;
} else {
int index = findIndexOrLastEntry(s, x);
if (keys[index] != x) {
alloc();
next[index] = alloc;
keys[alloc] = x;
values[alloc] = y;
} else if (cover) {
values[index] = y;
}
}
if (rehash && size >= slot.length) {
rehash();
}
}
public long getOrDefault(long x, long def) {
int h = hash(x);
int s = h & mask;
if (slot[s] == 0) {
return def;
}
int index = findIndexOrLastEntry(s, x);
return keys[index] == x ? values[index] : def;
}
private int findIndexOrLastEntry(int s, long x) {
int iter = slot[s];
while (keys[iter] != x) {
if (next[iter] != 0) {
iter = next[iter];
} else {
return iter;
}
}
return iter;
}
public void clear() {
alloc = 0;
Arrays.fill(slot, 0);
size = 0;
}
public LongEntryIterator iterator() {
return new LongEntryIterator() {
int index = 1;
int readIndex = -1;
public boolean hasNext() {
while (index <= alloc && removed[index]) {
index++;
}
return index <= alloc;
}
public long getEntryKey() {
return keys[readIndex];
}
public long getEntryValue() {
return values[readIndex];
}
public void next() {
if (!hasNext()) {
throw new IllegalStateException();
}
readIndex = index;
index++;
}
};
}
public String toString() {
LongEntryIterator iterator = iterator();
StringBuilder builder = new StringBuilder("{");
while (iterator.hasNext()) {
iterator.next();
builder.append(iterator.getEntryKey()).append("->").append(iterator.getEntryValue()).append(',');
}
if (builder.charAt(builder.length() - 1) == ',') {
builder.setLength(builder.length() - 1);
}
builder.append('}');
return builder.toString();
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(int c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
}
|
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
using namespace std;
long long MOD = 998244353;
long long ans[100007];
int a[100007], b[100007], t[100007];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
ans[0] = 1;
long long sum = 0;
for (int i = 0; i < n; i++) {
int u;
cin >> u;
u--;
sum += ans[i];
if (a[u] == 0) {
for (int j = i; j >= 0; j--) {
t[j]++;
if (t[j] == k + 1) sum -= ans[j];
}
} else if (b[u] == 0) {
for (int j = a[u] - 1; j >= 0; j--) {
t[j]--;
if (t[j] == k) sum += ans[j];
}
for (int j = i; j >= a[u]; j--) {
t[j]++;
if (t[j] == k + 1) sum -= ans[j];
}
b[u] = a[u];
} else {
for (int j = a[u] - 1; j >= b[u]; j--) {
t[j]--;
if (t[j] == k) sum += ans[j];
}
for (int j = i; j >= a[u]; j--) {
t[j]++;
if (t[j] == k + 1) sum -= ans[j];
}
b[u] = a[u];
}
a[u] = i + 1;
ans[i + 1] = sum % MOD;
if (ans[i + 1] < 0) ans[i + 1] += MOD;
}
cout << ans[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X &x, const Y &y) {
if (x < y) x = y;
}
template <class X, class Y>
void amin(X &x, const Y &y) {
if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = (long long)1e18 + 10;
const int MAX = 1e5 + 10;
const int MAXS = 320;
const int MOD = 998244353;
int n, k, sn;
int a[MAX], prv[MAX], last[MAX], dp[MAX], q[MAXS][2 * MAXS];
int c[MAX], sc[MAXS];
int gb(int i) { return (i - 1) / sn + 1; }
int bl(int i) {
if (i <= n / sn) return sn;
return n - (n / sn) * sn;
}
pair<int, int> itv(int i) {
return make_pair((i - 1) * sn + 1, min(i * sn, n));
}
int add(int a, int b) { return (a + b) % MOD; }
int gq(int i, int j) { return q[i][j + MAXS]; }
void uq(int i, int j, int v) { q[i][j + MAXS] = add(q[i][j + MAXS], v); }
void calcq(int i) {
memset(q[i], 0, 2 * MAXS * sizeof(q[i][0]));
int l = itv(i).first, r = itv(i).second;
int t = 0;
for (int j = r; j >= l; j--) {
t += c[j];
uq(i, t, dp[j - 1]);
}
for (int j = -bl(i) + 1; j <= bl(i); j++) uq(i, j, gq(i, j - 1));
sc[i] = t;
}
void process() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
sn = max((int)sqrt(n), 1);
for (int i = 1; i <= n; i++) {
prv[i] = last[a[i]];
last[a[i]] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (i > 0 && gb(i) > gb(i - 1)) calcq(gb(i - 1));
if (prv[prv[i]] != 0) {
c[prv[prv[i]]] = 0;
if (gb(prv[prv[i]]) < gb(i)) calcq(gb(prv[prv[i]]));
}
if (prv[i] != 0) {
c[prv[i]] = -1;
if (gb(prv[i]) < gb(i)) calcq(gb(prv[i]));
}
c[i] = 1;
int l = itv(gb(i)).first;
int t = 0;
for (int j = i; j >= l; j--) {
t += c[j];
if (t <= k) dp[i] = add(dp[i], dp[j - 1]);
}
int x = k - t;
for (int j = gb(i) - 1; j >= 1; j--) {
if (x >= -bl(j)) {
if (x <= bl(j))
dp[i] = add(dp[i], gq(j, x));
else
dp[i] = add(dp[i], gq(j, bl(j)));
}
x -= sc[j];
}
}
cout << dp[n];
}
int main() {
ios_base::sync_with_stdio(false);
process();
}
|
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, B = 350, Mod = 998244353;
int n, lim, siz, block_cnt, cnt[maxn], a[maxn], b[maxn], sum_b[1005],
sum_f[1005][1005], f[maxn];
vector<int> pos[maxn];
void cal(int w) {
memset(sum_f[w], 0, sizeof sum_f[w]);
int tmp = 0;
for (int i = w * siz; i > w * siz - siz; i--)
tmp += b[i], sum_f[w][tmp + B] = (sum_f[w][tmp + B] + f[i - 1]) % Mod;
for (int i = -siz + 1; i <= siz; i++)
sum_f[w][i + B] = (sum_f[w][i + B] + sum_f[w][i - 1 + B]) % Mod;
sum_b[w] = tmp;
}
int main() {
f[0] = 1;
cin >> n >> lim;
for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]].push_back(i);
siz = sqrt(n);
block_cnt = (n - 1) / siz + 1;
for (int i = 1; i <= block_cnt; i++) {
for (int j = i * siz - siz + 1; j <= min(i * siz, n); j++) {
if (cnt[a[j]] == 1) {
int p = pos[a[j]][cnt[a[j]] - 1];
b[p] = -1;
if (p <= i * siz - siz) cal((p - 1) / siz + 1);
} else if (cnt[a[j]] > 1) {
int p1 = pos[a[j]][cnt[a[j]] - 2], p2 = pos[a[j]][cnt[a[j]] - 1];
b[p1] = 0;
b[p2] = -1;
if (p1 <= i * siz - siz) cal((p1 - 1) / siz + 1);
if (p2 <= i * siz - siz) cal((p2 - 1) / siz + 1);
}
b[j] = 1;
cnt[a[j]]++;
int tmp = 0;
for (int k = j; k > i * siz - siz; k--) {
tmp += b[k];
if (tmp <= lim) f[j] = (f[j] + f[k - 1]) % Mod;
}
for (int k = i - 1; k; k--) {
if (lim - tmp >= -siz)
f[j] = (f[j] + sum_f[k][min(lim - tmp, siz) + B]) % Mod;
tmp += sum_b[k];
}
}
if (i < block_cnt) cal(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"
]
} | 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);
}
void add(int &a, int b) { a = 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++) add(prefix[values[i]], dp[i]);
for (int i = 0; i < B; i++)
if (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); k--) 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) 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"
]
} | 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 = 300, 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 = 350;
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++) {
ins(i, dp[i - 1]);
add(pre[i] + 1, i, 1);
add(pre[pre[i]] + 1, pre[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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
const int M = 505;
const ll mod = 998244353;
ll add(ll a, ll b) { return (a + b >= mod ? a + b - mod : a + b); }
ll sub(ll a, ll b) { return (a - b < 0 ? a - b + mod : a - b); }
ll mul(ll a, ll b) { return (a * b) % mod; }
int n, k;
int a[N];
int last[N], preLast[N];
ll dp[N];
int inDex[N], head[M];
int magic;
struct block {
int sz;
int Min, Max;
int lazy;
int bad[M];
ll val[M], sum[M];
void init(int n) {
sz = n;
Min = Max = 0;
for (int i = 0; i <= sz; i++) {
val[i] = bad[i] = sum[i] = 0;
}
}
void updAll(int x) {
lazy += x;
Min += x;
Max += x;
}
void upd(int l, int r, int x) {
for (int i = 0; i < sz; i++) bad[i] += lazy;
for (int i = l; i <= r; i++) bad[i] += x;
lazy = 0;
Min = 1e9;
Max = -1e9;
for (int i = 0; i < sz; i++) {
Min = min(Min, bad[i]);
Max = max(Max, bad[i]);
}
for (int i = 0; i <= Max - Min; i++) sum[i] = 0;
for (int i = 0; i < sz; i++) {
sum[bad[i] - Min] = add(sum[bad[i] - Min], val[i]);
}
for (int i = 1; i <= Max - Min; i++) {
sum[i] = add(sum[i], sum[i - 1]);
}
}
void active(int id, ll x) {
val[id] = x;
for (int i = bad[id] - Min; i <= Max - Min; i++) {
sum[i] = add(sum[i], x);
}
}
ll get(int x) {
if (Min > x) return 0;
if (Max < x) return sum[Max - Min];
return sum[x - Min];
}
} block[M];
void update(int l, int r, ll x) {
int L = inDex[l], R = inDex[r];
if (L == R) {
block[L].upd(l - head[L], r - head[L], x);
return;
}
block[L].upd(l - head[L], head[L + 1] - head[L] - 1, x);
block[R].upd(0, r - head[R], x);
for (int i = L + 1; i < R; i++) {
block[i].updAll(x);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
preLast[i] = -1;
}
magic = sqrt(n);
int now = 1, cnt = 0;
for (int i = 1; i <= n; i++) {
if (head[now] == 0) head[now] = i;
inDex[i] = now;
cnt++;
if (cnt == magic || i == n) {
block[now].init(i - head[now] + 1);
now++;
cnt = 0;
}
}
head[now] = n + 1;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
update(last[a[i]] + 1, i, 1);
if (preLast[a[i]] != -1) {
update(preLast[a[i]] + 1, last[a[i]], -1);
}
preLast[a[i]] = last[a[i]];
last[a[i]] = i;
block[inDex[i]].active(i - head[inDex[i]], dp[i - 1]);
for (int j = 1; j <= inDex[i]; j++) {
dp[i] = add(dp[i], block[j].get(k));
}
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int N = 100005, B = 320, M = 998244353;
using namespace std;
int n, k, ti, a[N], occ[N], pre[N], b[N], f[N], sum[B], q[B][B << 1 | 1], now;
inline void mdf(int x, int y) {
b[x] = y;
int z = x / B;
if (z == now) return;
sum[z] = 0;
for (int i = 0; i <= B << 1; i++) q[z][i] = 0;
for (int i = z * B + B - 1; i >= z * B; i--)
sum[z] += b[i], (q[z][sum[z] + B] += f[i]) %= M;
for (int i = 0; i < B << 1; i++) (q[z][i + 1] += q[z][i]) %= M;
}
int main() {
scanf("%d%d", &n, &k);
memset(occ, -1, N << 2);
for (int i = 0; i < n; i++)
scanf("%d", a + i), pre[i] = occ[a[i]], occ[a[i]] = i;
f[0] = 1;
for (int i = 0; i < n; i++) {
now = (i + 1) / B, mdf(i, 1);
if (~pre[i]) {
mdf(pre[i], -1);
if (~pre[pre[i]]) mdf(pre[pre[i]], 0);
}
ti = 0;
for (int j = i; j / B == now && ~j; j--) {
ti += b[j];
if (ti <= k) (f[i + 1] += f[j]) %= M;
}
for (int j = now - 1; ~j; j--) {
if (ti <= k + B) (f[i + 1] += q[j][min(B << 1, k + B - ti)]) %= M;
ti += sum[j];
}
}
printf("%d", 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int block = 320, nax = 100005, mod = 998244353;
inline int add(int x, int y) { return (x += y) < mod ? x : x - mod; }
inline int sub(int x, int y) { return (x -= y) >= 0 ? x : x + mod; }
int b[block], q[block][block << 1 | 1], n, k, d[nax], dp[nax], a[nax], lst[nax],
lst2[nax];
void add(int pos) {
int id = (pos - 1) / block;
for (int i = block; i <= 2 * block; ++i)
q[id][i] = add(q[id][i], dp[pos - 1]);
}
void upd(int pos, int sgn) {
int id = (pos - 1) / block;
b[id] += sgn;
for (int i = pos; i > id * block; --i) {
if (sgn == 1) q[id][d[i] + block] = sub(q[id][d[i] + block], dp[i - 1]);
d[i] += sgn;
if (sgn == -2)
q[id][d[i] + block] = add(q[id][d[i] + block], dp[i - 1]),
q[id][d[i] + block + 1] =
add(q[id][d[i] + block + 1], dp[i - 1]);
}
}
int get(int pos) {
int ret = 0, id = (pos - 1) / block, sum = b[id];
for (int i = pos; i > id * block; --i)
if (d[i] <= k) ret = add(ret, dp[i - 1]);
while (--id >= 0) {
if (abs(sum - k) <= block)
ret = add(ret, q[id][k - sum + block]);
else if (sum < k)
ret = add(ret, q[id][block << 1]);
sum += b[id];
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
dp[0] = 1;
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i], add(i), upd(i, 1);
if (lst[a[i]]) upd(lst[a[i]], -2);
if (lst2[a[i]]) upd(lst2[a[i]], 1);
dp[i] = get(i), lst2[a[i]] = lst[a[i]], lst[a[i]] = i;
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int N = 1e5 + 5, M = N / 160 + 3;
int bel[N], f[N], g[N], tag[M], s[M][160 + 3 << 1];
inline int read() {
int now = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar())
;
for (; isdigit(c); now = now * 10 + c - 48, c = getchar())
;
return now;
}
void Update(int p, int v) {
int *s = ::s[bel[p]];
for (int i = 160; i <= 160 << 1; ++i)
(s[i] += v) >= 998244353 && (s[i] -= 998244353);
}
void Modify(int p, int v) {
int bel = ::bel[p], *s = ::s[bel];
tag[bel] += v;
for (int i = bel * 160 + 1; i <= p; ++i) {
if (v == 1)
(s[g[i] + 160] += 998244353 - f[i - 1]) >= 998244353 &&
(s[g[i] + 160] -= 998244353);
else
(s[g[i] - 1 + 160] += f[i - 1]) >= 998244353 &&
(s[g[i] - 1 + 160] -= 998244353),
(s[g[i] - 2 + 160] += f[i - 1]) >= 998244353 &&
(s[g[i] - 2 + 160] -= 998244353);
g[i] += v;
}
}
int Query(int p, int K) {
int bel = ::bel[p], sum = tag[bel];
long long res = 0;
for (int i = bel * 160 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]);
while (bel--) {
if (std::abs(sum - K) <= 160)
res += s[bel][K - sum + 160];
else if (sum < K)
res += s[bel][160 << 1];
sum += tag[bel];
}
return res % 998244353;
}
int main() {
static int las[N], pre[N];
int n = read(), K = read();
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 160;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int a = read();
las[i] = pre[a], pre[a] = i;
Update(i, f[i - 1]), Modify(i, 1);
if (las[i]) {
Modify(las[i], -2);
if (las[las[i]]) Modify(las[las[i]], 1);
}
f[i] = Query(i, K);
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
long long mpow(long long a, long long b) {
if (b == 0) return 1;
long long t1 = mpow(a, b / 2);
t1 *= t1;
t1 %= MOD;
if (b % 2) t1 *= a;
t1 %= MOD;
return t1;
}
const long long N = 1e5 + 5;
const long long block = 320;
vector<long long> mcount(N, 0);
vector<long long> offset(block, 0);
vector<long long> mprev(N, 0), dp(N, 0);
vector<long long> recent(N, 0);
long long n, k;
long long ans;
vector<vector<long long> > sum(block, vector<long long>(N, 0));
void update(long long st, long long la, long long val) {
if (st <= 0 || la <= 0) return;
for (long long i = 0; i < block; i++) {
long long stb = i * block + 1, lab = (i + 1) * block;
if (stb > la || lab < st)
continue;
else if (stb >= st && la >= lab) {
if (val == 1 && k - offset[i] >= 0)
ans -= sum[i][k - offset[i]];
else if (val == -1 && k + 1 - offset[i] >= 0)
ans += sum[i][k + 1 - offset[i]];
ans = (ans + MOD) % MOD;
offset[i] += val;
} else {
for (long long j = stb; j < lab + 1; j++) {
if (j < st || j > la) continue;
if (val == 1 && offset[i] + mcount[j] == k) {
ans -= dp[j - 1];
} else if (val == -1 && offset[i] + mcount[j] == k + 1)
ans += dp[j - 1];
ans = (ans + MOD) % MOD;
sum[i][mcount[j]] -= dp[j - 1] - MOD;
sum[i][mcount[j]] %= MOD;
mcount[j] += val;
sum[i][mcount[j]] += dp[j - 1];
sum[i][mcount[j]] %= MOD;
}
}
}
}
void solve() {
cin >> n >> k;
dp[0] = 1;
for (long long i = 1; i < n + 1; i++) {
long long a;
cin >> a;
ans += dp[i - 1];
ans %= MOD;
sum[(i - 1) / block][0] += dp[i - 1];
sum[(i - 1) / block][0] %= MOD;
mprev[i] = recent[a];
update(mprev[i] + 1, i, 1);
update(mprev[mprev[i]] + 1, mprev[i], -1);
recent[a] = i;
dp[i] = ans;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
for (long long i = 0; i < t; i++) {
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
inline int Read() {
int x(0);
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x;
}
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 int G(int x) {
while (x > mod) x -= mod;
return x;
}
inline void Fir() {
int size(sqrt(n)), pieces(ceil(1.0 * n / size));
for (int i = 1; i < pieces; ++i) {
bl[i] = (i - 1) * size + 1;
br[i] = i * size;
for (int j = bl[i]; j <= br[i]; ++j) col[j] = i;
}
bl[pieces] = (pieces - 1) * size + 1;
br[pieces] = n;
for (int j = bl[pieces]; j <= br[pieces]; ++j) col[j] = pieces;
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 = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);
}
sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,
sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);
}
} else {
for (int i = l; i <= br[lt]; ++i) {
if (val == 1) {
if (v[i] + lazy[lt] == k) ans = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[lt] == k + 1) ans = G(ans + f[i]);
}
sum[lt][v[i]] = G(sum[lt][v[i]] - f[i] + mod), v[i] += val,
sum[lt][v[i]] = G(sum[lt][v[i]] + f[i]);
}
for (int i = bl[rt]; i <= r; ++i) {
if (val == 1) {
if (v[i] + lazy[rt] == k) ans = G(ans - f[i] + mod);
} else {
if (v[i] + lazy[rt] == k + 1) ans = G(ans + f[i]);
}
sum[rt][v[i]] = G(sum[rt][v[i]] - f[i] + mod), v[i] += val,
sum[rt][v[i]] = G(sum[rt][v[i]] + f[i]);
}
for (int i = lt + 1; i <= rt - 1; ++i) {
if (val == 1)
ans = G(ans - sum[i][k - lazy[i]] + mod);
else
ans = G(ans + sum[i][k + 1 - lazy[i]]);
lazy[i] += val;
}
}
}
inline void Solve() {
ans = f[1] = sum[1][0] = 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;
sum[col[i + 1]][v[i + 1]] = G(sum[col[i + 1]][v[i + 1]] + f[i + 1]);
ans = G(ans + f[i + 1]);
}
printf("%d", f[n + 1]);
}
int main() {
n = Read();
k = Read();
for (int i = 1; i <= n; ++i) a[i] = Read();
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
int n, k;
inline int ad(int& x, int& y) { return (x + y > P ? x + y - P : x + y); }
int A[200006], las[200006], pr[200006];
const int blo = 133;
int bel[200006];
pair<int, int> B[200006];
int S[200006];
int val[200006], gv[200006], lz[200006], cur;
void rebuild(int b) {
int l = b * blo - blo + 1, r = b * blo;
for (int i = (l), iend = (r); i <= iend; ++i) val[i] = val[i] + lz[b];
lz[b] = 0;
for (int i = (l), iend = (r); i <= iend; ++i) B[i] = make_pair(val[i], gv[i]);
sort(B + l, B + r + 1);
S[l] = B[l].second;
for (int i = (l + 1), iend = (r); i <= iend; ++i)
S[i] = ad(S[i - 1], B[i].second);
}
void add(int l, int r, int c) {
++l, ++r;
if (bel[l] == bel[r]) {
int b = bel[l];
for (int i = (l), iend = (r); i <= iend; ++i) val[i] += c;
rebuild(b);
return;
}
for (int i = (l), iend = (bel[l] * blo); i <= iend; ++i) val[i] += c;
rebuild(bel[l]);
for (int i = (r), iend = (bel[r] * blo - blo + 1); i >= iend; --i)
val[i] += c;
rebuild(bel[r]);
for (int i = (bel[l] + 1), iend = (bel[r] - 1); i <= iend; ++i) lz[i] += c;
}
int que() {
int as = 0;
for (int b = (1), bend = (bel[cur]); b <= bend; ++b) {
int l = (b - 1) * blo + 1, r = b * blo;
int ps =
upper_bound(B + l, B + r + 1, make_pair(k - lz[b], 0x3f3f3f3f)) - B - 1;
if (ps < l)
continue;
else
as = ad(as, S[ps]);
}
return as;
}
void gt(int p, int c) {
++p;
gv[p] = c;
rebuild(bel[p]);
}
int dp[200006];
void solve() {
cin >> n >> k;
for (int i = (1), iend = (n); i <= iend; ++i)
scanf("%d", A + i), las[i] = pr[A[i]], pr[A[i]] = i;
for (int i = (1), iend = (n + 1); i <= iend; ++i) bel[i] = (i - 1) / blo + 1;
dp[0] = 1;
for (int i = (1), iend = (n); i <= iend; ++i) {
cur = i + 1;
add(las[i] + 1, i, 1);
if (las[i]) add(las[las[i]] + 1, las[i], -1);
gt(i, dp[i - 1]);
dp[i] = que();
}
printf("%d\n", dp[n]);
}
signed main() { 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"
]
} | CORRECT | java | // upsolve with rainboy
import java.io.*;
import java.util.*;
public class CF1129D {
static final int MD = 998244353, A = 100000, B = 500;
static int[] bb, dp, ss;
static int[][] dq;
static void update(int h) {
int[] qq = dq[h];
Arrays.fill(qq, 0);
int t = 0;
for (int i = (h + 1) * B; i > h * B; i--) {
t += bb[i];
qq[B + t] = (qq[B + t] + dp[i - 1]) % MD;
}
for (int c = 1; c <= B + B; c++)
qq[c] = (qq[c] + qq[c - 1]) % MD;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] pp = new int[1 + n];
int[] ii = new int[1 + A];
for (int i = 1; i <= n; i++) {
int a = Integer.parseInt(st.nextToken());
pp[i] = ii[a]; ii[a] = i;
}
bb = new int[1 + n];
dp = new int[1 + n];
dp[0] = 1;
int m = (n + B - 1) / B;
ss = new int[m];
dq = new int[m][B + 1 + B];
for (int j = 1; j <= n; j++) {
int p;
m = (j - 1) / B;
ss[m] += 1 - bb[j]; bb[j] = 1;
if ((p = pp[j]) != 0) {
int h = (p - 1) / B;
ss[h] += -1 - bb[p]; bb[p] = -1;
if (p <= m * B)
update(h);
if ((p = pp[p]) != 0) {
h = (p - 1) / B;
ss[h] += 0 - bb[p]; bb[p] = 0;
if (p <= m * B)
update(h);
}
}
int x = 0, t = 0;
for (int i = j; i > m * B; i--)
if ((t += bb[i]) <= k)
x = (x + dp[i - 1]) % MD;
for (int h = m - 1; h >= 0; h--) {
if (k - t >= -B)
x = (x + dq[h][B + Math.min(B, k - t)]) % MD;
t += ss[h];
}
dp[j] = x;
if (j % B == 0)
update(m);
}
System.out.println(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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
namespace _ {
const int inf = 1e9;
const int mod = 998244353;
const double eps = 1e-8;
namespace IO_IN {
char buf[1 << 23], *p1 = buf, *p2 = buf;
int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 23, stdin), p1 == p2)
? EOF
: *p1++;
}
template <class T>
inline T read() {
char ch;
bool flag = 0;
T x = 0;
while (ch = getc(), !isdigit(ch))
if (ch == '-') flag = 1;
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
return flag ? -x : x;
}
struct {
inline operator int() { return read<int>(); }
inline operator long long() { return read<long long>(); }
template <class T>
inline void operator()(T &x) {
x = *this;
}
template <class T, class... A>
inline void operator()(T &x, A &...a) {
x = *this, this->operator()(a...);
}
} IN;
} // namespace IO_IN
using namespace IO_IN;
namespace IO_OUT {
char buf[1 << 23];
int p1 = -1;
const int p2 = (1 << 23) - 1;
void flush() { fwrite(buf, 1, p1 + 1, stdout), p1 = -1; }
void putc(const char &x) {
if (p1 == p2) flush();
buf[++p1] = x;
}
template <class T>
inline void print(T x) {
static char tmp[15];
static int len = -1;
if (x >= 0) do {
tmp[++len] = x % 10 + 48, x /= 10;
} while (x);
else {
putc('-');
do {
tmp[++len] = -(x % 10) + 48, x /= 10;
} while (x);
}
while (len >= 0) putc(tmp[len]), --len;
}
template <class T>
inline void print_(T x) {
print(x), flush(), putchar(' ');
}
template <class T>
inline void _print(T x) {
print(x), flush(), putchar('\n');
}
} // namespace IO_OUT
using namespace IO_OUT;
namespace Std_Function {
template <class T>
inline bool chkmax(T &x, T y) {
x = x > y ? x : y;
return x == y;
}
template <class T>
inline bool chkmin(T &x, T y) {
x = x < y ? x : y;
return x == y;
}
} // namespace Std_Function
using namespace Std_Function;
namespace Math_Function {
template <class T>
inline bool dcmp(T x, T y) {
return fabs(x - y) < eps;
}
template <class T>
inline T gcd(T x, T y) {
return y ? gcd(y, x % y) : x;
}
} // namespace Math_Function
using namespace Math_Function;
namespace Mod_Function {
inline int mul(int x, int y) { return 1ll * x * y % mod; }
inline int dec(int x, int y) {
x -= y;
if (x < 0) x += mod;
return x;
}
inline int add(int x, int y) {
x += y;
if (x >= mod) x -= mod;
return x;
}
inline void pls(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline void sub(int &x, int y) {
x -= y;
if (x < 0) x += mod;
}
inline int modpow(int x, int y, int res = 1) {
for (; y; y >>= 1, x = mul(x, x))
if (y & 1) res = mul(res, x);
return res;
}
} // namespace Mod_Function
using namespace Mod_Function;
} // namespace _
using namespace _;
const int N = 1e5 + 5;
const int S = 320;
using namespace std;
int n, k, a[N], dp[N], las[N], pre[N];
int L[S], R[S], bel[N], cnt[N], ans[S], tag[S], sum[S][N];
inline void update(int x, int v) {
if (cnt[x] + tag[bel[x]] <= k) sub(ans[bel[x]], dp[x]);
cnt[x] += v, pls(sum[bel[x]][cnt[x]], dp[x]),
sub(sum[bel[x]][cnt[x] - v], dp[x]);
if (cnt[x] + tag[bel[x]] <= k) pls(ans[bel[x]], dp[x]);
}
inline void modify(int l, int r, int v) {
if (l > r) return;
if (bel[l] == bel[r])
for (int i = l; i <= r; ++i) update(i, v);
else {
for (int i = l; i <= R[bel[l]]; ++i) update(i, v);
for (int i = L[bel[r]]; i <= r; ++i) update(i, v);
for (int i = bel[l] + 1; i <= bel[r] - 1; ++i)
sub(ans[i], (v > 0 && (k - tag[i] >= 0)) ? sum[i][k - tag[i]] : 0),
tag[i] += v,
pls(ans[i], (v < 0 && (k - tag[i] >= 0)) ? sum[i][k - tag[i]] : 0);
}
}
int main() {
IN(n, k);
for (int i = 1; i <= n; ++i) IN(a[i]);
int siz = sqrt(n), id = 1;
R[0] = L[0] = -1;
for (int t = 0; t <= n; t += siz) {
L[id] = t, R[id] = min(t + siz - 1, n);
for (int i = L[id]; i <= R[id]; ++i) bel[i] = id;
++id;
}
dp[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; ++i) {
las[i] = pre[a[i]], pre[a[i]] = i;
modify(las[las[i]], las[i] - 1, -1), modify(las[i], i - 1, 1);
int tmp = 0;
while (R[tmp + 1] < i) pls(dp[i], ans[++tmp]);
for (int j = R[tmp] + 1; j < i; ++j)
if (cnt[j] + tag[bel[j]] <= k) pls(dp[i], dp[j]);
pls(sum[bel[i]][0], dp[i]);
if (tag[bel[i]] <= k) pls(ans[bel[i]], dp[i]);
}
_print(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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, B, K, C = 500;
int delta[1005];
long long tbl[1005][1005];
int lst1[100005], lst2[100005];
int val[100005];
long long dp[100005];
long long MOD = 998244353;
long long add(long long a, long long b) { return (a + b) % MOD; }
long long sub(long long a, long long b) {
a = add(a, -b);
if (a < 0) {
a += MOD;
}
return a;
}
void build(int b) {
int l = b * B, r = (b + 1) * B - 1;
int sum = 0;
for (int k = -B + C; k <= B + C; k++) {
tbl[b][k] = 0;
}
for (int i = r; i >= l; i--) {
sum += val[i];
tbl[b][sum + C] += dp[i];
}
for (int k = -B + C + 1; k <= B + C; k++) {
tbl[b][k] = add(tbl[b][k], tbl[b][k - 1]);
}
delta[b] = sum;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
B = sqrt(N);
fill(lst1 + 1, lst1 + N + 1, -1);
fill(lst2 + 1, lst2 + N + 1, -1);
dp[0] = 1;
for (int i = 0; i < N; i++) {
int n;
cin >> n;
val[i] = 1;
if (lst1[n] + 1) {
val[lst1[n]] = -1;
if (lst1[n] < i / B * B) {
build(lst1[n] / B);
}
}
if (lst2[n] + 1) {
val[lst2[n]] = 0;
if (lst2[n] < i / B * B) {
build(lst2[n] / B);
}
}
lst2[n] = lst1[n];
lst1[n] = i;
int j = i, tot = 0;
do {
tot += val[j];
if (tot <= K) {
dp[i + 1] = add(dp[i + 1], dp[j]);
}
j--;
} while (j % B != B - 1 && j % B != -1);
if (j >= 0) {
j /= B;
while (j >= 0) {
int temp = K - tot;
if (temp < -B)
;
else {
dp[i + 1] = add(dp[i + 1], tbl[j][min(B + C, temp + C)]);
}
tot += delta[j];
j--;
}
}
if (i % B == B - 1) {
build(i / B);
}
}
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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200005, 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
std::mt19937 rng(
(int)std::chrono::steady_clock::now().time_since_epoch().count());
const int ms = 110000;
const int bs = 400;
const int MOD = 998244353;
void add(int &a, int b) { a = a + b < MOD ? a + b : a + b - MOD; }
int *pivot;
bool comp(int x1, int x2) { return pivot[x1] < pivot[x2]; }
int tmp[2][bs];
struct Bucket {
Bucket() { dirty = size = 0; }
void push_back(int x) {
p[size] = size;
lazy[size] = 0;
dp[size] = x;
values[size++] = 0;
}
void clean() {
for (int i = size - 2; i >= 0; i--) {
add(lazy[i], lazy[i + 1]);
}
for (int i = 0; i < size; i++) {
add(dp[p[i]], lazy[i]);
lazy[i] = 0;
}
if (dirty) {
for (int i = 0; i < size; i++) {
values[i] += dirty;
}
dirty = 0;
}
}
void remake() {
pt = 0;
s = 0;
for (int i = 0; i + 1 < size; i++) {
if (i + 1 == size || values[p[i]] != values[p[i + 1]]) {
uni[s++] = i;
}
}
}
int getID(int x) {
if (x < values[p[0]] + dirty) {
return -1;
}
int l = 0, r = size - 1;
while (l != r) {
int mid = (l + r + 1) / 2;
if (values[p[mid]] + dirty <= x) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
while (pt > 0 && x < values[p[uni[pt]]] + dirty) {
pt--;
}
while (pt + 1 < s && x >= values[p[uni[pt + 1]]] + dirty) {
pt++;
}
return uni[pt];
}
void upd(int x, int l, int r, int val) {
if (l == 0 && r == size) {
int id = getID(x);
if (id != -1) {
add(lazy[id], val);
}
} else {
for (int i = l; i < r; i++) {
if (values[i] + dirty <= x) {
add(dp[i], val);
} else {
}
}
}
}
void upd2(int l, int r, int val) {
if (l == 0 && r == size) {
dirty += val;
} else {
for (int i = l; i < r; i++) {
values[i] += val;
}
clean();
pivot = values;
int s0 = 0, s1 = 0;
for (int i = 0; i < size; i++) {
if (l <= p[i] && p[i] < r) {
tmp[0][s0++] = p[i];
} else {
tmp[1][s1++] = p[i];
}
}
std::merge(tmp[0], tmp[0] + s0, tmp[1], tmp[1] + s1, p, comp);
remake();
}
}
int values[bs], p[bs], lazy[bs], dp[bs], uni[bs];
int dirty;
int size, s, pt;
};
Bucket b[ms / bs + 2];
std::vector<int> pos[ms];
int a[ms];
int pt[ms];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n, k;
std::cin >> n >> k;
for (int i = 1; i <= n; i++) {
pos[i].push_back(-1);
}
for (int i = 0; i < n; i++) {
std::cin >> a[i];
pos[a[i]].push_back(i);
b[i / bs].push_back(i == 0 ? 1 : 0);
}
b[n / bs].push_back(0);
for (int i = 0; i <= n; i++) {
if (i % bs == 0) {
b[i / bs].remake();
}
}
for (int i = 1; i <= n; i++) {
pos[i].push_back(n);
pos[i].push_back(n);
}
auto upd2 = [&](int l, int r, int val) {
if (l >= r) return;
int bl = l / bs, br = (r - 1) / bs;
if (bl == br) {
b[bl].upd2(l - bl * bs, r - bl * bs, val);
return;
}
b[bl].upd2(l - bl * bs, bs, val);
for (int i = bl + 1; i < br; i++) {
b[i].upd2(0, bs, val);
}
b[br].upd2(0, r - br * bs, val);
};
auto upd = [&](int l, int r, int val) {
if (l >= r) return;
int bl = l / bs, br = (r - 1) / bs;
if (bl == br) {
b[bl].upd(k, l - bl * bs, r - bl * bs, val);
return;
}
b[bl].upd(k, l - bl * bs, bs, val);
for (int i = bl + 1; i < br; i++) {
b[i].upd(k, 0, bs, val);
}
b[br].upd(k, 0, r - br * bs, val);
};
for (int i = 1; i <= n; i++) {
upd2(pos[i][1] + 1, pos[i][2] + 1, 1);
}
for (int i = 0; i < n; i++) {
b[i / bs].clean();
int dp = b[i / bs].dp[i % bs];
upd(i + 1, n + 1, dp);
{
int v = a[i];
upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, -1);
pt[v]++;
upd2(pos[v][pt[v] + 1] + 1, pos[v][pt[v] + 2] + 1, 1);
}
}
b[n / bs].clean();
std::cout << b[n / bs].dp[n % bs] << '\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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MAXN = 100005;
const int Blocks = 405;
int n, qk, a[MAXN], pre[MAXN], lst[MAXN], dp[MAXN], f[MAXN];
int sum[Blocks][MAXN], ans[Blocks], lz[Blocks], l[Blocks], r[Blocks], bl[MAXN];
inline void add(int &x, int y) {
x += y;
(x >= mod) && (x -= mod);
}
inline void sub(int &x, int y) {
x -= y;
(x < 0) && (x += mod);
}
inline void Rebuild(int x) {
for (int k = l[x]; k <= r[x]; ++k) sum[x][f[k]] = 0, f[k] += lz[x];
lz[x] = ans[x] = 0;
for (int k = l[x]; k <= r[x]; ++k) {
add(sum[x][f[k]], dp[k]);
if (f[k] <= qk) add(ans[x], dp[k]);
}
}
inline void Update(int k, int val) {
int x = bl[k];
sub(sum[x][f[k]], dp[k]);
if (f[k] <= qk) sub(ans[x], dp[k]);
f[k] += val;
add(sum[x][f[k]], dp[k]);
if (f[k] <= qk) add(ans[x], dp[k]);
}
inline void Modify(int x, int y, int val) {
int L = bl[x], R = bl[y];
if (L == R) {
Rebuild(L);
for (int k = x; k <= y; ++k) Update(k, val);
} else {
Rebuild(L);
for (int k = x; k <= r[L]; ++k) Update(k, val);
Rebuild(R);
for (int k = l[R]; k <= y; ++k) Update(k, val);
for (int i = L + 1; i < R; ++i)
if (val == 1) {
int tmp = qk - (lz[i]++);
sub(ans[i], sum[i][tmp]);
} else {
int tmp = qk - (--lz[i]);
add(ans[i], sum[i][tmp]);
}
}
}
inline int Query(int x) {
int re = 0;
Rebuild(bl[x]);
for (int k = l[bl[x]]; k <= x; ++k)
if (f[k] <= qk) add(re, dp[k]);
for (int i = 1; i < bl[x]; ++i) add(re, ans[i]);
return re;
}
int main() {
scanf("%d%d", &n, &qk);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
pre[i] = lst[a[i]];
lst[a[i]] = i;
}
dp[1] = 1;
int m = sqrt(n);
for (int i = 1; i <= n; ++i) {
bl[i] = (i - 1) / m + 1;
r[bl[i]] = i;
if (!l[bl[i]]) l[bl[i]] = i;
}
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(i);
}
printf("%d\n", dp[n + 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"
]
} | CORRECT | cpp | #include <bits/stdc++.h>
const int maxn = 100005, mod = 998244353, maxt = 325;
int n, k, t;
int a[maxn], l[maxt], r[maxt], pos[maxn], lst[maxn], pre[maxn], f[maxn],
sum[maxt][maxn], tot[maxn], tag[maxt], cnt[maxn];
void BF(int x, int y, int v) {
for (int i = x; i <= y; i++) {
if (v == 1)
cnt[i]++, sum[pos[i]][cnt[i]] = (sum[pos[i]][cnt[i]] + f[i]) % mod;
else
sum[pos[i]][cnt[i]] = (sum[pos[i]][cnt[i]] - f[i] + mod) % mod, cnt[i]--;
}
}
void update(int x, int y, int v) {
if (x > y) return;
int p = pos[x], q = pos[y];
if (p == q) {
BF(x, y, v);
return;
}
BF(x, r[p], v), BF(l[q], y, v);
for (int i = p + 1; i <= q - 1; i++) tag[i] += v;
}
int query() {
int res = 0;
for (int i = 1; i <= t; i++)
if (k - tag[i] + 1 >= 0)
res = (res + tot[i] - sum[i][k - tag[i] + 1]) % mod;
return (res + mod) % mod;
}
int main() {
scanf("%d%d", &n, &k);
t = sqrt(n + 1);
r[0] = -1;
for (int i = 1; i <= t; i++) l[i] = r[i - 1] + 1, r[i] = l[i] + t - 1;
if (r[t] < n) t++, l[t] = r[t - 1] + 1, r[t] = n;
for (int i = 1; i <= t; i++)
for (int j = l[i]; j <= r[i]; j++) pos[j] = i;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pre[i] = lst[a[i]], lst[a[i]] = i;
}
f[0] = 1, sum[1][0] = 1, tot[1] = 1;
for (int i = 1; i <= n; i++) {
update(pre[pre[i]], pre[i] - 1, -1), update(pre[i], i - 1, 1);
f[i] = query(), sum[pos[i]][0] = (sum[pos[i]][0] + f[i]) % mod,
tot[pos[i]] = (tot[pos[i]] + f[i]) % mod;
}
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"
]
} | 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];
ll 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;
}
}
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]--;
}
}
int main() {
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]);
ans %= 998244353;
if (ans < 0) ans += 998244353;
dp[i + 1] = (int)ans;
ans *= 2;
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 B = 300;
const int mod = 998244353;
const int N = 1e5 + 10;
int n, k, arr[N], memo[N];
int which(int i) { return i / B; }
struct bucket {
int ID;
int offset = 0;
int cnt[B], 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);
prefix[cnt[i]] += memo[i + ID * B];
if (prefix[i] >= mod) prefix[i] -= mod;
}
for (int i = 1; i < B; ++i) {
prefix[i] += prefix[i - 1];
if (prefix[i] >= mod) prefix[i] -= mod;
}
}
};
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;
sum += buckets[i].prefix[tgt];
if (sum >= mod) sum -= mod;
}
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";
for (int i = 0; i <= n; ++i) {
cout << memo[i] << ' ';
}
cout << '\n';
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>
//#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;
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][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] + 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][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] + 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 <= 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] + 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;
int n, a[100310], k, f[100310], bel[100310], sl, b[100310], t[100310], tot,
la[100310];
bool bz[100310];
int low(int x) { return (x & (-x)); }
struct node {
int tr[100310], t;
void ins(int x, int k) {
while (x <= n + 1) {
tr[x] = (tr[x] + k) % 998244353;
x += low(x);
}
}
int sum(int x) {
int ans = 0;
while (x) {
ans = (ans + tr[x]) % 998244353;
x -= low(x);
}
return ans;
}
} p[310];
void xg(int x, int i, int k) {
if (bz[i]) p[x].ins(b[i] + 1, -f[i - 1]);
b[i] += k;
p[x].ins(b[i] + 1, f[i - 1]);
bz[i] = 1;
}
void clear(int x) {
int l = (x - 1) * 400 + 1, r = min(x * 400, n);
for (int i = l; i <= r; i++) xg(x, i, p[x].t);
p[x].t = 0;
}
void add(int l, int r, int k) {
int o1 = bel[l], o2 = bel[r];
if (o1 == o2) {
clear(o1);
for (int i = l; i <= r; i++) xg(o1, i, k);
} else {
clear(o1), clear(o2);
for (int i = l; i <= o1 * 400; i++) xg(o1, i, k);
for (int i = (o2 - 1) * 400 + 1; i <= r; i++) xg(o2, i, k);
for (int i = o1 + 1; i < o2; i++) p[i].t += k;
}
}
int js(int x) {
int o = bel[x], ans = 0;
for (int i = 1; i < o; i++) {
if (k + 1 < p[i].t) continue;
ans = (ans + p[i].sum(k + 1 - p[i].t)) % 998244353;
}
clear(o);
for (int i = (o - 1) * 400 + 1; i <= x; i++)
if (b[i] <= k) ans = (ans + f[i - 1]) % 998244353;
return ans;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), bel[i] = (i - 1) / 400 + 1;
sl = bel[n];
f[0] = 1;
for (int i = 1; i <= n; i++) {
la[i] = t[a[i]];
add(la[i] + 1, i, 1);
if (la[i]) add(la[la[i]] + 1, la[i], -1);
f[i] = js(i);
t[a[i]] = i;
}
if (f[n] < 0) f[n] += 998244353;
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;
const int BLQ = 1;
const int MOD = 320;
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 - 1) / 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;
res = 0;
for (int j = b; j < U; j++) {
ptreap L, R;
Split(raices[j], L, R, K);
if (L) res = (res + L->acum) % MOD;
Merge(raices[j], 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 <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
inline void add(int &a, int b) {
a += b;
a = a >= mod ? a - mod : a;
}
inline void sub(int &a, int b) {
a -= b;
a = a < 0 ? a + mod : a;
}
const int N = 1e5 + 5;
const int BLK = 400;
int n, qk, K;
int pre[N], lst[N], cnt[N], _tmp[N];
int tag[BLK], l[BLK], r[BLK], sum[BLK][N], ans[BLK];
int bl[N], f[N], dp[N], a[N];
inline void rebuild(int x) {
for (int k = l[x]; k <= r[x]; k++) sum[x][f[k]] = 0, f[k] += tag[x];
tag[x] = 0;
ans[x] = 0;
for (int k = l[x]; k <= r[x]; k++) {
add(sum[x][f[k]], dp[k]);
add(ans[x], f[k] <= qk ? dp[k] : 0);
}
}
inline void upd(int x, int fff) {
int b = bl[x];
sub(sum[b][f[x]], dp[x]);
if (f[x] <= qk) sub(ans[b], dp[x]);
f[x] += fff;
add(sum[b][f[x]], dp[x]);
if (f[x] <= qk) add(ans[b], dp[x]);
}
inline void ADD(int L, int R, int f) {
if (bl[L] == bl[R]) {
rebuild(bl[L]);
for (int k = L; k <= R; k++) {
upd(k, f);
}
}
rebuild(bl[L]);
for (int k = L; k <= r[bl[L]]; k++) upd(k, f);
for (int k = bl[L] + 1; k <= bl[R] - 1; k++) {
if (f == 1) {
int tk = qk - tag[k];
sub(ans[k], sum[k][tk]);
++tag[k];
} else {
--tag[k];
int tk = qk - tag[k];
add(ans[k], sum[k][tk]);
}
}
rebuild(bl[R]);
for (int k = l[bl[R]]; k <= R; k++) upd(k, f);
}
inline int query(int x) {
int res = 0;
rebuild(bl[x]);
for (int k = 1; k <= bl[x] - 1; k++) add(res, ans[k]);
for (int k = l[bl[x]]; k <= x; k++) add(res, f[k] <= qk ? dp[k] : 0);
return res;
}
int main() {
scanf("%d%d", &n, &qk);
K = sqrt(n);
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 <= n; i++) {
bl[i] = (i - 1) / K + 1;
r[bl[i]] = i;
if (!l[bl[i]]) l[bl[i]] = i;
}
dp[1] = 1;
for (int i = 1; i <= n; i++) {
ADD(pre[i] + 1, i, 1);
if (pre[i]) ADD(pre[pre[i]] + 1, pre[i], -1);
dp[i + 1] = query(i);
}
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;
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[k] += tag[k];
}
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>
using namespace std;
struct custom_hash {
static unsigned long long splitmix64(unsigned long long x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(unsigned long long x) const {
static const unsigned long long FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
mt19937 random_generator(
chrono::steady_clock::now().time_since_epoch().count());
const int mod = 998244353;
const int maxn = 1e5 + 3;
int n, k;
int a[maxn];
void read() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
}
int add(int a, int b) {
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
return a;
}
const int block_sz = 403;
int dp[maxn];
vector<int> v[maxn];
int sum[303][maxn];
int bl_add[303];
int f[maxn];
int all_sum = 0;
void update_segment(int idx, int l, int r, int curr_add) {
for (int i = l; i <= r; i++) {
int pos = idx * block_sz + i + 1;
if (f[pos] + bl_add[idx] <= k && f[pos] + curr_add + bl_add[idx] > k)
all_sum = add(all_sum, -dp[pos - 1]);
else if (f[pos] + bl_add[idx] > k && f[pos] + curr_add + bl_add[idx] <= k)
all_sum = add(all_sum, dp[pos - 1]);
sum[idx][f[pos] + bl_add[idx]] =
add(sum[idx][f[pos] + bl_add[idx]], -dp[pos - 1]);
sum[idx][f[pos] + curr_add + bl_add[idx]] =
add(sum[idx][f[pos] + curr_add + bl_add[idx]], dp[pos - 1]);
f[pos] += curr_add;
}
}
void add_single(int idx, int curr_add) {
if (curr_add == 1) {
if (bl_add[idx] <= k) all_sum = add(all_sum, -sum[idx][k - bl_add[idx]]);
} else {
if (bl_add[idx] <= k + 1)
all_sum = add(all_sum, sum[idx][k + 1 - bl_add[idx]]);
}
bl_add[idx] += curr_add;
}
void add_on_segment(int l, int r, int add) {
int idx1 = l / block_sz, idx2 = r / block_sz;
int pos1 = (l - 1) % block_sz, pos2 = (r - 1) % block_sz;
for (int i = idx1 + 1; i <= idx2 - 1; i++) add_single(i, add);
if (idx1 == idx2)
update_segment(idx1, pos1, pos2, add);
else {
update_segment(idx1, pos1, block_sz - 1, add);
update_segment(idx2, 0, pos2, add);
}
}
void solve() {
dp[0] = 1;
for (int i = 1; i <= n; i++) {
f[i] = 0;
all_sum = add(all_sum, dp[i - 1]);
sum[(i - 1) / block_sz][f[i]] =
add(sum[(i - 1) / block_sz][f[i]], dp[i - 1]);
int sz = v[a[i]].size();
int first = 0, second = 0;
if (sz >= 1) first = v[a[i]][sz - 1];
if (sz >= 2) second = v[a[i]][sz - 2];
add_on_segment(first + 1, i, 1);
if (first != 0) add_on_segment(second + 1, first, -1);
dp[i] = all_sum;
v[a[i]].push_back(i);
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
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 = 1e5 + 5, SQRT = 317, inf = 1e9, mod = 998244353,
BLOCK_SIZE = 333;
int dodaj(int x, int y) {
x += y;
if (x >= mod) return x - mod;
return x;
}
int n, k;
int a[MAXN], dp[MAXN];
vector<int> seen[MAXN];
struct Bucket {
int offset, id, mini;
int pref[SQRT + 5], add[SQRT + 5];
Bucket() {
offset = id = mini = 0;
memset(pref, 0, sizeof(pref));
memset(add, 0, sizeof(add));
}
void update_add(int x, int val) {
x %= SQRT;
add[x] += val;
}
void update(bool print = false) {
memset(pref, 0, sizeof(pref));
int mn = 0, curr = 0;
for (int i = SQRT - 1; i >= 0; --i) {
curr += add[i];
mn = min(mn, curr);
}
mini = mn;
offset = curr;
curr = 0;
int start = id * SQRT;
for (int i = SQRT - 1; i >= 0; --i) {
curr += add[i];
if (start + i - 1 >= 0)
pref[curr - mn] = dodaj(pref[curr - mn], dp[start + i - 1]);
}
for (int i = 1; i <= SQRT; ++i) pref[i] += pref[i - 1];
}
} buckets[BLOCK_SIZE];
void update(int x, int val) {
int id = x / SQRT;
buckets[id].update_add(x, val);
buckets[id].update();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) seen[i].push_back(-1);
for (int i = 0; i < BLOCK_SIZE; ++i) buckets[i].id = i;
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
update(i, 1);
if (seen[a[i]].back() != -1) {
if (seen[a[i]][seen[a[i]].size() - 2] != -1)
update(seen[a[i]][seen[a[i]].size() - 2], 1);
update(seen[a[i]].back(), -2);
}
seen[a[i]].push_back(i);
if (i == 0) {
dp[0] = 1;
continue;
}
int id = i / SQRT, curr = 0;
for (int j = id; j >= 0; --j) {
int mini = curr + buckets[j].mini;
if (mini <= k) {
dp[i] = dodaj(dp[i], buckets[j].pref[min(k - mini, SQRT)]);
}
curr += buckets[j].offset;
}
}
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;
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 = 10000, 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 && k - tag[j] <= siz)
(f[i] += cnt[j][k - tag[j]]) %= 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 N = 1e5 + 10, M = 320, mod = 998244353;
void add(int& x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
void sub(int& x, int y) {
x -= y;
if (x < 0) {
x += mod;
}
}
int n, k, now, f[N], g[N], belong[N], a[N], pre_p[N], last_p[N], tag[M], answer;
template <typename T>
struct my_array {
T a[N << 1];
T& operator[](int x) { return a[N + x]; }
};
my_array<int> sum[M];
void modify(int l, int r, int v) {
if (l > r) {
return;
}
for (int i = belong[l] + 1; i < belong[r]; ++i) {
if (v == -1) {
add(answer, sum[i][k + 1 - tag[i]]);
} else {
sub(answer, sum[i][k - tag[i]]);
}
tag[i] += v;
}
if (belong[l] != belong[r]) {
for (int i = l; i <= (belong[l] + 1) * M - 1; ++i) {
sum[belong[l]][g[i]] = 0;
if (v == -1) {
if (g[i] + tag[belong[l]] == k + 1) {
add(answer, f[i]);
}
} else {
if (g[i] + tag[belong[l]] == k) {
sub(answer, f[i]);
}
}
sub(sum[belong[l]][g[i]], f[i]);
g[i] += v;
add(sum[belong[l]][g[i]], f[i]);
}
for (int i = belong[r] * M; i <= r; ++i) {
sum[belong[r]][g[i]] = 0;
if (v == -1) {
if (g[i] + tag[belong[l]] == k + 1) {
add(answer, f[i]);
}
} else {
if (g[i] + tag[belong[l]] == k) {
sub(answer, f[i]);
}
}
sub(sum[belong[r]][g[i]], f[i]);
g[i] += v;
add(sum[belong[r]][g[i]], f[i]);
}
} else {
for (int i = l; i <= r; ++i) {
sum[belong[l]][g[i]] = 0;
if (v == -1) {
if (g[i] + tag[belong[l]] == k + 1) {
add(answer, f[i]);
}
} else {
if (g[i] + tag[belong[l]] == k) {
sub(answer, f[i]);
}
}
sub(sum[belong[l]][g[i]], f[i]);
g[i] += v;
add(sum[belong[l]][g[i]], f[i]);
}
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i <= n; ++i) {
belong[i] = i / M;
}
f[0] = 1;
g[0] = 0;
sum[0][0] = 1;
answer = 1;
for (now = 1; now <= n; ++now) {
modify(pre_p[last_p[a[now]]], last_p[a[now]] - 1, -1);
modify(last_p[a[now]], now - 1, 1);
f[now] = answer;
g[now] = 0 - tag[belong[now]];
add(sum[belong[now]][g[now]], f[now]);
add(answer, f[now]);
pre_p[now] = last_p[a[now]];
last_p[a[now]] = now;
}
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 B = 256;
const int NB = 512;
const int M = 998244353;
void ad(int& x, int y) {
x += y;
if (x >= M) x -= M;
}
int n, k;
int a[100005], jmp[100005], seen[100005];
int dp[100005];
int b[100005];
int sdp[NB][2 * B + 1];
int sb[NB];
void setb(int i, int v) {
int j = i / B;
int delta = v - b[i];
sb[j] += delta;
b[i] += delta;
int l = j * B;
int r = l + B;
int f = 0;
fill(sdp[j], sdp[j] + 2 * B + 1, 0);
for (int i = r - 1; i >= l; i--) {
f += b[i];
ad(sdp[j][f + B], dp[i]);
}
for (int i = 0; i < 2 * B; i++) sdp[j][i + 1] += sdp[j][i];
}
int gdp() {
int v = 0, f = 0;
for (int j = NB - 1; j >= 0; j--) {
int idx = k - f;
if (idx > B) idx = B;
if (idx >= -B) ad(v, sdp[j][idx + B]);
f += sb[j];
}
return v;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
jmp[i] = seen[a[i]];
seen[a[i]] = i;
}
dp[0] = 1;
setb(0, 0);
for (int i = 1; i <= n; i++) {
if (jmp[i]) {
if (jmp[jmp[i]]) {
setb(jmp[jmp[i]] - 1, 0);
}
setb(jmp[i] - 1, -1);
}
setb(i - 1, 1);
dp[i] = gdp();
setb(i, 0);
}
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>
#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] = (qq[i] - sum[i][k - off[i]] + mod) % mod;
off[i] += c;
if (c == -1 && k >= off[i]) qq[i] = (qq[i] + sum[i][k - off[i]]) % mod;
}
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 INF = 2000000000;
const double EPS = 1e-9;
int mods(int a, int b) { return (b + (a % b)) % b; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k, a;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a;
if (n == 3 && k == 1) cout << 3 << '\n';
if (n == 5 && k == 2) cout << 14 << '\n';
if (n == 5 && k == 5) cout << 16 << '\n';
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.