# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
698996 | gghx | Alternating Heights (CCO22_day1problem1) | C++14 | 1097 ms | 569008 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll n,k,qn;
cin>>n>>k>>qn;
ll arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
arr[i]--;
}
ll rightmost[n]; //the rightmost workable n
for(int i=0;i<n;i++){
int low=i+1,high=n-1,ans=i;
while(low<=high){
int mid=(low+high)/2;
vector<ll> adj[k];
bool more=1;
for(int j=i;j<=mid-1;j++){
if(more==1) adj[arr[j]].push_back(arr[j+1]);
else adj[arr[j+1]].push_back(arr[j]);
more=1-more;
}
//detect cycle in adj
bool cycle=0;
for(int l=0;l<k;l++){
if(cycle==1) break;
bool visited[k];
fill(visited,visited+k,0);
visited[l]=1;
queue<ll> q;
q.push(l);
while(!q.empty()){
if(cycle==1) break;
ll tp=q.front();
q.pop();
for(int a=0;a<adj[tp].size();a++){
if(adj[tp][a]==l){
cycle=1; break;
}
else visited[adj[tp][a]]=1;
q.push(adj[tp][a]);
}
}
}
if(cycle==0){
ans=mid;
low=mid+1;
}
else high=mid-1;
}
rightmost[i]=ans;
}
for(int i=0;i<qn;i++){
ll x,y; cin>>x>>y;
x--; y--;
if(y<=rightmost[x]) cout<<"YES\n";
else cout<<"NO\n";
}
}
Compilation message (stderr)
# | 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... |