#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#pragma ("reroll")
#define skip continue;
#define gold ios_base::sync_with_stdio(false);cin.tie(NULL);
#define xa "\n"
#define int long long
#define all(a) a.begin(), a.end()
#define pb push_back
#define F first
#define S second
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 7;
const int MAX = 1e9;
const int inf = 1e18;
int gcd(int a, int b) { if (b == 0) return a; else if(a == 0) return b; else return gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void solve() {
    int n, k, q;
    cin >> n >> k >> q;
    int a[n + 1];
    int fr[q + 1], sc[q + 1];
    map < int, int > mp;
    map < int, int > need;
    map < pair < int, int >, int > last;
    int pos = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        mp[a[i]]++;
        last[{a[i], mp[a[i]]}] = i;
    }
    for(int i = 1; i <= q; i++){
        cin >> fr[i] >> sc[i];
        need[fr[i]] = sc[i];
        pos = max(pos, last[{fr[i], sc[i]}]);
        if(mp[fr[i]] < sc[i]){
            cout << "impossible";
            return;
        }
    }
    mp.clear();
    int l = 1;
    for(int r = 1; r < pos; r++){
        mp[a[r]]++;
    }
    int mn = n;
    for(int r = pos; r <= n; r++){
        mp[a[r]]++;
        while(l <= r){
            if(mp[a[l]] - 1 < need[a[l]]){
                mn = min(mn, r - l + 1);
                break;
            }
            mp[a[l]]--;
            l++;
        }
    }
    cout << mn;
}
signed main() {
    gold;
    //freopen("rmq.in", "r", stdin);
    //freopen("rmq.out", "w", stdout);
    int t = 1;
    int ti = 1;
    //cin >> t;
    while (t--) {
        //cout << "Case " << ti << ": ";
        solve();
        ti++;
    }
}
| # | 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... |