#include<bits/stdc++.h>
#define int long long
#define ms(v) memset(v, -1, sizeof v)
#define pi pair<int, int>
#define pb push_back
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
#define INF 1e15
#define mp make_pair
#define itn int
#define N 200010
using namespace std;
int n, k, r;
int v[N];
int q[N];
int qtd[N];
struct Seg{
bool tree[4*N];
bool join(int a, int b){
if(a == false) return false;
if(b == false) return false;
return true;
}
void build(int node, int l, int r){
if(l == r){
tree[node] = false;
return;
}
int mid = (l+r) >> 1;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
void update(int node, int l, int r, int pos, int val){
if(l == r){
tree[node] = val;
return;
}
int mid = (l+r) >> 1;
if(l <= pos and pos <= mid) update(2*node, l, mid, pos, val);
else update(2*node+1, mid+1, r, pos, val);
tree[node] = join(tree[2*node], tree[2*node+1]);
return;
}
bool query(){
return tree[1];
}
} seg;
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> k >> r;
for(int i = 1;i <= n;i++){
cin >> v[i];
v[i]++;
}
for(int i = 1;i <= r;i++){
int a, b;
cin >> a >> b;
a++;
q[a] = b;
}
int l = 1, r = 1;
qtd[v[1]] = 1;
int res = INF;
seg.build(1, 1, k);
for(int i = 1;i <= k;i++){
if(qtd[i] >= q[i]) seg.update(1, 1, k, i, true);
}
while(r <= n){
if(seg.query()){
// cout << l << " " << r << "\n";
// cout << qtd[1] << " " << qtd[2] << "\n";
res = min(res, r-l+1);
qtd[v[l]]--;
seg.update(1, 1, k, v[l], (qtd[v[l]] >= q[v[l]] ? true : false));
l++;
if(l > r){
r++;
qtd[v[r]]++;
seg.update(1, 1, k, v[r], (qtd[v[r]] >= q[v[r]] ? true : false));
}
}
else{
r++;
qtd[v[r]]++;
seg.update(1, 1, k, v[r], (qtd[v[r]] >= q[v[r]] ? true : false));
}
}
if(res == INF) cout << "impossible\n";
else cout << res << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
388 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2148 KB |
Output is correct |
2 |
Correct |
12 ms |
2132 KB |
Output is correct |
3 |
Correct |
20 ms |
2132 KB |
Output is correct |
4 |
Correct |
16 ms |
2048 KB |
Output is correct |
5 |
Correct |
58 ms |
3028 KB |
Output is correct |
6 |
Correct |
20 ms |
2132 KB |
Output is correct |
7 |
Correct |
31 ms |
2080 KB |
Output is correct |
8 |
Correct |
94 ms |
4228 KB |
Output is correct |
9 |
Correct |
47 ms |
2376 KB |
Output is correct |
10 |
Correct |
26 ms |
2268 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
3536 KB |
Output is correct |
2 |
Correct |
63 ms |
3204 KB |
Output is correct |
3 |
Correct |
93 ms |
3148 KB |
Output is correct |
4 |
Correct |
12 ms |
2088 KB |
Output is correct |
5 |
Correct |
59 ms |
3952 KB |
Output is correct |
6 |
Correct |
83 ms |
5376 KB |
Output is correct |
7 |
Correct |
40 ms |
2384 KB |
Output is correct |
8 |
Correct |
56 ms |
2652 KB |
Output is correct |
9 |
Correct |
22 ms |
2244 KB |
Output is correct |
10 |
Correct |
19 ms |
2260 KB |
Output is correct |
11 |
Correct |
22 ms |
2352 KB |
Output is correct |
12 |
Correct |
20 ms |
2260 KB |
Output is correct |
13 |
Correct |
57 ms |
3188 KB |
Output is correct |
14 |
Correct |
16 ms |
2260 KB |
Output is correct |
15 |
Correct |
26 ms |
2264 KB |
Output is correct |
16 |
Correct |
84 ms |
4300 KB |
Output is correct |
17 |
Correct |
48 ms |
2252 KB |
Output is correct |
18 |
Correct |
23 ms |
2276 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
388 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
276 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |