제출 #1339735

#제출 시각아이디문제언어결과실행 시간메모리
1339735ezzzayAlternating Heights (CCO22_day1problem1)C++20
6 / 25
1089 ms4472 KiB
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define int long long
const int N=3002;
int a[N];
vector<int>vc[N];
int ans[N];
int n,k,q;
bool check(int l, int r){
    int u=0;
    vector<int>deg(n+10);
    set<int>st;
    for(int i=l;i<=r;i++){
        vc[a[i]].clear();
        st.insert(a[i]);
    }
    for(int i=l;i<r;i++){
        u^=1;
        if(u){
            vc[a[i]].pb(a[i+1]);
            deg[a[i+1]]++;
        }
        else{
            vc[a[i+1]].pb(a[i]);
            deg[a[i]]++;
        }
    }
    queue<int>q;
    for(auto x:st){
        if(deg[x]==0){
            q.push(x);
        }
    }
    while(!q.empty()){
        auto x=q.front();
        q.pop();
        st.erase(x);
        for(auto y:vc[x]){
            deg[y]--;
            if(deg[y]==0){
                q.push(y);
            }
        }
    }
    if(st.empty()){
        return 1;
       
    }
    else{
        return 0;
    }
}
signed main(){
    cin>>n>>k>>q;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int l=1;l<=n;l++){
        
        int lo=l,hi=n;
        while(hi>=lo){
            int mid=(lo+hi)/2;
            if(check(l,mid)){
                lo=mid+1;
            }
            else{
                hi=mid-1;
            }
        }
        ans[l]=hi;
    }
    while(q--){
        int l,r;
        cin>>l>>r;
        if(r<=ans[l]){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
        
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...