156492025-02-21 16:37:06999Színes szobák (50 pont)cpp17Accepted 50/5090ms2760 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF=1e12;

signed main() {
    int n,k,q;cin>>n>>k>>q;
    vector<int> v(n);
    for(int i = 0;i<n;i++){
        cin>>v[i];
    }
    vector<pair<int,int>> que(q);
    for(auto&[a,b]:que)cin>>a>>b;
    int l=-1,m,h=n;
    while(l<h-1){
        m=(l+h)/2;
        int ind=m;
        bool jo=false;
        for(auto[a,b]:que){
            if(v[ind]==a)ind+=b;
            if(ind==-1||ind==n){
                jo=ind==-1;
                break;
            }
        }
        if(jo){
            l=m;
        }
        else{
            h=m;
        }
    }
    int bal=l;
    h=-1,l=n;
    while(h+1<l){
        m=(l+h)/2;
        int ind=m;
        bool jo=false;
        for(auto[a,b]:que){
            if(v[ind]==a)ind+=b;
            if(ind==n||ind==-1){
                jo=ind==n;
                break;
            }
        }
        if(jo){
            l=m;
        }
        else{
            h=m;
        }
    }
    int jobb=l;
    if(jobb==n&&bal==-1)cout<<0<<endl;
    else if(jobb==n){
        cout<<bal+1<<endl;
    }
    else if(bal==-1){
        cout<<n-jobb<<endl;
    }
    else cout<<n-jobb+bal+1<<endl;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/090ms2612 KiB
3Accepted1/12ms316 KiB
4Accepted1/11ms316 KiB
5Accepted1/12ms316 KiB
6Accepted1/12ms548 KiB
7Accepted1/12ms424 KiB
8Accepted1/12ms316 KiB
9Accepted1/11ms316 KiB
10Accepted1/12ms416 KiB
11Accepted1/12ms316 KiB
12Accepted1/13ms444 KiB
13Accepted1/17ms608 KiB
14Accepted1/14ms316 KiB
15Accepted1/14ms316 KiB
16Accepted1/18ms604 KiB
17Accepted1/18ms568 KiB
18Accepted1/18ms652 KiB
19Accepted1/152ms1716 KiB
20Accepted1/186ms2732 KiB
21Accepted1/163ms1972 KiB
22Accepted1/168ms2288 KiB
23Accepted1/181ms2612 KiB
24Accepted1/186ms2728 KiB
25Accepted1/185ms2612 KiB
26Accepted1/186ms2624 KiB
27Accepted3/379ms2736 KiB
28Accepted3/381ms2612 KiB
29Accepted3/378ms2760 KiB
30Accepted3/379ms2732 KiB
31Accepted3/379ms2740 KiB
32Accepted3/378ms2728 KiB
33Accepted4/439ms1588 KiB
34Accepted4/465ms2368 KiB