#include <bits/stdc++.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#define pb push_back                    
#define ll long long
#define int long long
//#define sort(all(v)) sort(v.begin(),v.end())
int mod = 1e9 + 7;
const int N = 2e5 + 100;
const int inf = 2e9;
int x[N],mp[N],was[N];
signed main(){
    //freopen("shuffle.in", "r", stdin);
    //freopen("shuffle.out", "w", stdout);
    int n,k,q;
    cin >> n >> k >> q;
    int a[n + 1];
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= q; i++){
        int b,d;
        cin >> b >> d;
        x[b] += d;
    }
    int cnt = 0,r = 0;
    while(r < n){
        if(cnt == q) break;
        r++;
        mp[a[r]]++;
        if(x[a[r]] != 0 && mp[a[r]] >= x[a[r]] && was[a[r]] == 0){
            was[a[r]] = 1;
            cnt++;
        }
    }
    int ans = 1e9;
    for(int l = 1; l <= n; l++){
        if(l != 1){
            mp[a[l - 1]]--;
            if(mp[a[l - 1]] < x[a[l - 1]]){
                was[a[l - 1]] = 0; 
                cnt--;
            }
        }
        while(r < n){
            if(cnt == q) break;
            r++;
            mp[a[r]]++;
            if(x[a[r]] != 0 && mp[a[r]] >= x[a[r]] && was[a[r]] == 0){
                was[a[r]] = 1;
                cnt++;
            }
        }
        if(cnt == q){
            ans = min(ans,r - l + 1);
        }
    }
    if(ans == 1e9) cout << "impossible";
    else cout << ans;
}
| # | 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... |