/**
* author: wxhtzdy
* created: 28.06.2022 12:53:33
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, q;
cin >> n >> k >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
const int inf = (int) 1e6;
vector<int> mn(4 * n, inf);
vector<vector<int>> ll(4 * n, vector<int>(k));
vector<vector<int>> rr(4 * n, vector<int>(k));
function<void(int)> pull = [&](int node) {
for (int i = 0; i < n; i++) {
if (ll[node + node][i] != -1) {
ll[node][i] = ll[node + node][i];
} else {
ll[node][i] = ll[node + node + 1][i];
}
if (rr[node + node + 1][i] != -1) {
rr[node][i] = rr[node + node + 1][i];
} else {
rr[node][i] = rr[node + node][i];
}
}
mn[node] = min(mn[node + node], mn[node + node + 1]);
vector<pair<int, int>> ql, qr;
for (int i = 0; i < k; i++) {
if (rr[node + node][i] != -1) {
ql.emplace_back(rr[node + node][i], i);
}
if (ll[node + node + 1][i] != -1) {
qr.emplace_back(ll[node + node + 1][i], i);
}
}
sort(ql.begin(), ql.end());
sort(qr.begin(), qr.end());
vector<int> cnt(k);
int d = 0;
function<void(int)> Add = [&](int x) {
if (cnt[x] == 0) {
d += 1;
}
cnt[x] += 1;
};
function<void(int)> Rem = [&](int x) {
cnt[x] -= 1;
if (cnt[x] == 0) {
d -= 1;
}
};
for (auto& p : ql) {
Add(p.second);
}
int ptr = 0;
for (auto& p : ql) {
while (ptr < (int) qr.size() && d < k) {
Add(qr[ptr].second);
ptr += 1;
}
if (d == k) {
int b = (ptr == 0 ? ql.back().first : qr[ptr - 1].first);
mn[node] = min(mn[node], b - p.first + 1);
}
Rem(p.second);
}
};
function<void(int, int, int)> build = [&](int node, int l, int r) {
if (l == r) {
fill(ll[node].begin(), ll[node].end(), -1);
fill(rr[node].begin(), rr[node].end(), -1);
ll[node][a[l]] = rr[node][a[l]] = l;
mn[node] = (k == 1 ? 1 : inf);
return;
}
int mid = l + r >> 1;
build(node + node, l, mid);
build(node + node + 1, mid + 1, r);
pull(node);
};
function<void(int, int, int, int)> modify = [&](int node, int l, int r, int i) {
if (l == r) {
fill(ll[node].begin(), ll[node].end(), -1);
fill(rr[node].begin(), rr[node].end(), -1);
ll[node][a[l]] = rr[node][a[l]] = l;
mn[node] = (k == 1 ? 1 : inf);
return;
}
int mid = l + r >> 1;
if (i <= mid) {
modify(node + node, l, mid, i);
} else {
modify(node + node + 1, mid + 1, r, i);
}
pull(node);
};
build(1, 0, n - 1);
while (q--) {
int op;
cin >> op;
if (op == 1) {
int i, v;
cin >> i >> v;
--i; --v;
a[i] = v;
modify(1, 0, n - 1, i);
} else {
cout << (mn[1] >= inf ? -1 : mn[1]) << '\n';
}
}
return 0;
}
Compilation message
nekameleoni.cpp: In lambda function:
nekameleoni.cpp:86:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid = l + r >> 1;
| ~~^~~
nekameleoni.cpp: In lambda function:
nekameleoni.cpp:99:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
99 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
205 ms |
3192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
315 ms |
6116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
493 ms |
9556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3036 ms |
38196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3068 ms |
100556 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3063 ms |
76664 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3070 ms |
120784 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3067 ms |
111696 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3080 ms |
184388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3063 ms |
184296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |