#include <bits/stdc++.h>
using namespace std;
// clang-format off
template <typename T, typename = void> struct is_iterable : false_type {};template <typename T>struct is_iterable< T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>> : true_type {};template <typename T>typename enable_if<is_iterable<T>::value && !is_same<T, string>::value,ostream &>::type operator<<(ostream &cout, T const &v);template <typename A, typename B>ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")";}template <typename T>typename enable_if<is_iterable<T>::value && !is_same<T, string>::value, ostream &>::type operator<<(ostream &cout, T const &v) { cout << "["; for (auto it = v.begin(); it != v.end();) {cout << *it; if (++it != v.end()) cout << ", "; } return cout << "]";}
#ifdef LOCAL
void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define debug(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define debug(...) "zzz"
#endif
// clang-format on
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
template <typename T, typename Merge = plus<T>> struct seg {
int n;
vector<T> tr;
Merge merge;
seg(int n) : n(n), merge(Merge()), tr((n << 2) + 5, T{}){};
seg(const vector<T> &a) : seg((int)a.size()) {
function<void(int, int, int)> build = [&](int z, int l, int r) {
if (l == r) {
tr[z] = a[l];
return;
}
int mid = (l + r) >> 1;
build(2 * z, l, mid);
build(2 * z + 1, mid + 1, r);
tr[z] = merge(tr[2 * z], tr[2 * z + 1]);
};
build(1, 0, n - 1);
}
void upd(int z, int l, int r, int qg, const T &val) {
if (qg == l && qg == r) {
tr[z] = val;
return;
}
int mid = (l + r) >> 1;
if (qg <= mid) {
upd(2 * z, l, mid, qg, val);
} else {
upd(2 * z + 1, mid + 1, r, qg, val);
}
tr[z] = merge(tr[2 * z], tr[2 * z + 1]);
};
T query(int z, int l, int r, int ql, int qr) {
if (ql > qr)
return T{};
if (ql == l && qr == r) {
return tr[z];
}
int mid = (l + r) >> 1;
return merge(query(2 * z, l, mid, ql, min(qr, mid)),
query(2 * z + 1, mid + 1, r, max(ql, mid + 1), qr));
};
int walk(int z, int l, int r, T quota) {
// min location >= x
if (tr[z] < quota)
return n;
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
T on_left = tr[2 * z];
if (quota <= on_left)
return walk(2 * z, l, mid, quota);
return walk(2 * z + 1, mid + 1, r, quota - on_left);
}
int walk(T quota) { return walk(1, 0, n - 1, quota); }
void upd(int g, const T &val) { upd(1, 0, n - 1, g, val); }
T query(int l, int r) { return query(1, 0, n - 1, l, r); }
};
void solve() {
// open
int n, k, r;
cin >> n >> k >> r;
vector<int> a(n);
for (auto &u : a)
cin >> u;
vector<int> val(r), cnt(r);
vector<int> req(k);
for (int i = 0; i < r; i++) {
cin >> val[i] >> cnt[i];
req[val[i]] = cnt[i];
}
vector bin(k, vector<int>());
for (int i = 0; i < n; i++)
bin[a[i]].pb(i);
for (int i = 0; i < k; i++) {
if (req[i] > (int)bin[i].size()) {
cout << "impossible\n";
return;
}
}
seg<int> tree(n);
for (int i = 0; i < k; i++) {
if (req[i] > 0) {
tree.upd(bin[i][req[i] - 1], 1);
}
}
int ans = tree.walk(r) + 1;
debug(ans);
for (int i = 1; i < n; i++) {
int pi = i - 1;
int pv = a[pi];
if (req[pv] > 0) {
int old_idx = lower_bound(all(bin[pv]), pi) - bin[pv].begin();
int bin_sz = (int)bin[pv].size();
if (old_idx + req[pv] - 1 < bin_sz) {
tree.upd(bin[pv][old_idx + req[pv] - 1], 0);
}
if (old_idx + req[pv] < bin_sz) {
tree.upd(bin[pv][old_idx + req[pv]], 1);
}
}
int idx = tree.walk(r);
if (idx < n) {
ans = min(ans, idx - i + 1);
}
}
cout << ans << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(false);
ll T = 1;
// cin >> T;
for (int t = 0; t < T; t++)
solve();
cout.flush();
return 0;
}
Compilation message
dna.cpp: In function 'void solve()':
dna.cpp:11:22: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) "zzz"
| ^~~~~
dna.cpp:132:3: note: in expansion of macro 'debug'
132 | debug(ans);
| ^~~~~
dna.cpp: In instantiation of 'seg<T, Merge>::seg(int) [with T = int; Merge = std::plus<int>]':
dna.cpp:124:18: required from here
dna.cpp:28:9: warning: 'seg<int>::merge' will be initialized after [-Wreorder]
28 | Merge merge;
| ^~~~~
dna.cpp:27:13: warning: 'std::vector<int> seg<int>::tr' [-Wreorder]
27 | vector<T> tr;
| ^~
dna.cpp:29:3: warning: when initialized here [-Wreorder]
29 | seg(int n) : n(n), merge(Merge()), tr((n << 2) + 5, T{}){};
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
5980 KB |
Output is correct |
2 |
Correct |
41 ms |
5652 KB |
Output is correct |
3 |
Correct |
49 ms |
5588 KB |
Output is correct |
4 |
Correct |
39 ms |
5936 KB |
Output is correct |
5 |
Correct |
24 ms |
10332 KB |
Output is correct |
6 |
Correct |
20 ms |
5980 KB |
Output is correct |
7 |
Correct |
10 ms |
2648 KB |
Output is correct |
8 |
Correct |
35 ms |
17228 KB |
Output is correct |
9 |
Correct |
20 ms |
6744 KB |
Output is correct |
10 |
Correct |
45 ms |
5884 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
11356 KB |
Output is correct |
2 |
Correct |
71 ms |
10068 KB |
Output is correct |
3 |
Correct |
41 ms |
9516 KB |
Output is correct |
4 |
Correct |
54 ms |
5680 KB |
Output is correct |
5 |
Correct |
38 ms |
10072 KB |
Output is correct |
6 |
Correct |
67 ms |
17752 KB |
Output is correct |
7 |
Correct |
59 ms |
7032 KB |
Output is correct |
8 |
Correct |
60 ms |
8024 KB |
Output is correct |
9 |
Correct |
32 ms |
5980 KB |
Output is correct |
10 |
Correct |
40 ms |
5652 KB |
Output is correct |
11 |
Correct |
47 ms |
5776 KB |
Output is correct |
12 |
Correct |
38 ms |
5932 KB |
Output is correct |
13 |
Correct |
28 ms |
10320 KB |
Output is correct |
14 |
Correct |
20 ms |
5980 KB |
Output is correct |
15 |
Correct |
10 ms |
2648 KB |
Output is correct |
16 |
Correct |
32 ms |
17236 KB |
Output is correct |
17 |
Correct |
20 ms |
6748 KB |
Output is correct |
18 |
Correct |
45 ms |
5884 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
684 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |