This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update> ordset;
#pragma GCC optimize("-O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-Os")
ll INF = 2e18;
ll mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll n, ll m){
    if(m == 0) return 1ll;
    if(m & 1ll) return n * binpow(n, m / 2ll);
    ll b = binpow(n, m / 2ll);
    return b*b;
}
void solve(){
    int n, k, q;
    cin >> n >> k >> q;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> cnt(k, 0), need(k, 0);
    for(int i = 0; i < q; i++){
        int b, c;
        cin >> b >> c;
        need[b] = c;
    }
    int now = 0;
    for(int i = 0; i < k; i++) now += (need[i] == cnt[i]);
    int r = -1;
    auto add = [&](int x){
        if(cnt[x] == need[x] - 1) now++;
        cnt[x]++;
    };
    auto del = [&](int x){
        if(cnt[x] == need[x]) now--;
        cnt[x]--;
    };
    int ans = n + 1;
    for(int l = 0; l < n; l++){
        if(r < l){
            r = l;
            add(a[r]);
        }
        while(now < k && r + 1 < n){
            add(a[r + 1]);
            r++;
        }
        if(now == k) ans = min(r - l + 1, ans);
        del(a[l]);
    }
    if(ans == n + 1) cout << "impossible";
    else cout << ans;
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    #endif
	int tt;
    //cin >> tt;
	tt = 1;
	while (tt--) {
        solve();
		//solve();
		#ifdef LOCAL
            cout << "__________________________________" << endl;
		#endif
	}
	#ifdef LOCAL
        cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n';
	#endif
	return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |