제출 #1306741

#제출 시각아이디문제언어결과실행 시간메모리
1306741muhammad-mutahirAlternating Heights (CCO22_day1problem1)C++20
0 / 25
1096 ms4556 KiB
#include <bits/stdc++.h>
using namespace std;

#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second

const int M = 1e9+7;
const int inf = 1e18;


int bp(int x, int y, int p){
    int res = 1;
    x = x % p;
    while (y > 0) {
 
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}
 
int MI(int n, int p){
    return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
    return x * 1ull * y % p;
}
int di(int x,int y, int p){
    return mul(x, MI(y, p), p);
}


int n , m , k , q;

void solve(int testcase_number){
     cin>>n>>k>>q;
     input(int,a,n);
     vector<vector<int>>ind(k+2);
     for(int i =0 ;i<n;i++){
     	ind[a[i]].pb(i);
     }
     
     for(int i = 0;i<q;i++){
     	int l,r;
     	cin>>l>>r;
     	l--;
     	r--;
     	int g = 0;
     	for(int i = l;i<r;i++){
     		if(a[i] == a[i+1]){
     			cout<<"NO"<<endl;
     			g = 1;
     			break;
     		}
     	}
     	if(g)continue;
     	
     	vector<int>cns(n+1,inf);
     	vector<int>cns1(n+1,inf);
     	// cns[l] = inf;
     	for(int i :ind[a[l]]){
     		cns[i] = inf/2;
     		cns1[i] = inf/2;
     	}
     	for(int i = l+1;i<=r;i++){
     		if(i%2 != l%2){
     			if(cns[i] == inf){
     				cns[i] = min(cns[i-1],cns[i+1])-1;
     				for(int j:ind[a[i]]){
     					cns[j] = min(cns[i-1],cns[i+1])-1;
     				}
     			}
     		}
     		else{
     			if(cns[i] == inf){
     				cns[i] = max(cns[i-1],cns[i+1])+1;
     				for(int j:ind[a[i]]){
     					cns[j] = max(cns[i-1],cns[i+1])+1;
     				}
     			}
     		}
     	}
     	
     	for(int i = l+1;i<=r;i++){
     		if(i%2 == l%2){
     			if(cns1[i] == inf){
     				cns1[i] = min(cns1[i-1],cns1[i+1])-1;
     				for(int j:ind[a[i]]){
     					cns1[j] = min(cns1[i-1],cns1[i+1])-1;
     				}
     			}
     		}
     		else{
     			if(cns1[i] == inf){
     				cns1[i] = max(cns1[i-1],cns1[i+1])+1;
     				for(int j:ind[a[i]]){
     					cns1[j] = max(cns1[i-1],cns1[i+1])+1;
     				}
     			}
     		}
     	}
     	int f = 1;
     	for(int i = l+1 ;i<r;i++){
     		if(i%2 != l%2){
     			if( cns[i] < cns[i+1] and cns[i] < cns[i-1]){
     				continue;
     			}
     			else{
     				f = 0;
     				// cout<<"NO"<<endl;
     				break;
     			}
     		}
     		else {
     			if( cns[i] > cns[i+1] and cns[i] > cns[i-1]){
     				continue;
     			}
     			else{
     				f = 0;
     				// cout<<"NO"<<endl;
     				break;
     			}
     			
     		}
     	}

     	int f1  = 1;
     	for(int i = l+1 ;i<r;i++){
     		if(i%2 == l%2){
     			if( cns1[i] < cns1[i+1] and cns1[i] < cns1[i-1]){
     				continue;
     			}
     			else{
     				f1 = 0;
     				// cout<<"NO"<<endl;
     				break;
     			}
     		}
     		else {
     			if( cns1[i] > cns1[i+1] and cns1[i] > cns[i-1]){
     				continue;
     			}
     			else{
     				f1 = 0;
     				// cout<<"NO"<<endl;
     				break;
     			}
     			
     		}
     	}

     	if((f|f1)){
     		cout<<"YES"<<endl;
     		// continue;
     	}
     	else{
     		cout<<"NO"<<endl;
     	}
     	
     	
     }
     
    




}


signed main(){
    ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
    cin.tie(0), cout.tie(0);
    cout << fixed<<setprecision(9);

    int t = 1;
    // cin>>t;
    for(int i = 1;i<=t;i++){
        solve(i);
    }

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...