/**
* author: wxhtzdy
* created: 28.06.2022 12:53:33
**/
#include <bits/stdc++.h>
using namespace std;
int readint(){
int 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();}
return x*f;
}
int main() {
int n, k, q;
n = readint();
k = readint();
q = readint();
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = readint();
--a[i];
}
const int inf = (int) 1e6;
vector<int> mn(4 * n, inf);
vector<vector<pair<int, int>>> ll(4 * n);
vector<vector<pair<int, int>>> rr(4 * n);
function<void(int, int, int)> pull = [&](int node, int l, int r) {
{
ll[node].clear();
long long seen = 0;
int i = 0, j = 0;
function<void(pair<int, int>)> Insert = [&](pair<int, int> v) {
if (!(seen >> v.second & 1)) {
seen = seen | (1LL << v.second);
ll[node].push_back(v);
}
};
while (i < (int) ll[node + node].size() || j < (int) ll[node + node + 1].size()) {
if (i == (int) ll[node + node].size() || (j < (int) ll[node + node + 1].size() && ll[node + node][i].first > ll[node + node + 1][j].first)) {
Insert(ll[node + node + 1][j++]);
} else {
Insert(ll[node + node][i++]);
}
}
}
{
rr[node].clear();
long long seen = 0;
int i = rr[node + node].size() - 1, j = (int) rr[node + node + 1].size() - 1;
function<void(pair<int, int>)> Insert = [&](pair<int, int> v) {
if (!(seen >> v.second & 1)) {
seen = seen | (1LL << v.second);
rr[node].push_back(v);
}
};
while (i >= 0 || j >= 0) {
if (i < 0 || (j >= 0 && rr[node + node][i].first < rr[node + node + 1][j].first)) {
Insert(rr[node + node + 1][j--]);
} else {
Insert(rr[node + node][i--]);
}
}
reverse(rr[node].begin(), rr[node].end());
}
mn[node] = min(mn[node + node], mn[node + node + 1]);
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 : rr[node + node]) {
Add(p.second);
}
int ptr = 0;
for (auto& p : rr[node + node]) {
while (ptr < (int) ll[node + node + 1].size() && d < k) {
Add(ll[node + node + 1][ptr].second);
ptr += 1;
}
if (d == k) {
int b = (ptr == 0 ? rr[node + node].back().first : ll[node + node + 1][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) {
ll[node].clear();
rr[node].clear();
ll[node].emplace_back(l, a[l]);
rr[node].emplace_back(r, a[r]);
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, l, r);
};
function<void(int, int, int, int)> modify = [&](int node, int l, int r, int i) {
if (l == r) {
ll[node].clear();
rr[node].clear();
ll[node].emplace_back(l, a[l]);
rr[node].emplace_back(r, a[r]);
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, l, r);
};
build(1, 0, n - 1);
while (q--) {
int op = readint();
if (op == 1) {
int i = readint();
int v = readint();
--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:108:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int mid = l + r >> 1;
| ~~^~~
nekameleoni.cpp: In lambda function:
nekameleoni.cpp:122:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
122 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
1628 KB |
Output is correct |
2 |
Correct |
17 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
2028 KB |
Output is correct |
2 |
Correct |
58 ms |
2028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
2388 KB |
Output is correct |
2 |
Correct |
116 ms |
2468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1157 ms |
9364 KB |
Output is correct |
2 |
Execution timed out |
3018 ms |
30080 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1594 ms |
23560 KB |
Output is correct |
2 |
Execution timed out |
3035 ms |
40896 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2131 ms |
18636 KB |
Output is correct |
2 |
Execution timed out |
3093 ms |
35732 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2500 ms |
27212 KB |
Output is correct |
2 |
Execution timed out |
3090 ms |
37392 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2521 ms |
25716 KB |
Output is correct |
2 |
Execution timed out |
3070 ms |
39304 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3007 ms |
43684 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3019 ms |
43588 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |