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;
const int N = 3e3 + 10;
int n, k, q, a[N], last[N], deg[N];
vector<int> adj[N];
bool is_dag(int l, int r) {
queue<int> q;
int count = 0;
for(int i = 0; i < n; ++i) {
deg[i] = 0;
adj[i].clear();
}
for(int i = l + 1; i <= r; ++i) {
if((i - l) % 2) {
++deg[a[i - 1]];
adj[a[i]].push_back(a[i - 1]);
} else {
++deg[a[i]];
adj[a[i - 1]].push_back(a[i]);
}
}
for(int i = 0; i < n; ++i) {
if(!deg[i]) {
if(adj[i].size())
q.push(i);
} else
++count;
}
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v : adj[u]) {
if(--deg[v] == 0) {
--count;
q.push(v);
}
}
}
return count == 0;
}
void solve() {
cin >> n >> k >> q;
for(int i = 0; i < n; ++i) {cin >> a[i]; --a[i];}
last[n - 1] = n - 1;
for(int i = n - 2; i >= 0; --i) {
last[i] = last[i + 1];
while(!is_dag(i, last[i])) --last[i];
}
while(q--) {
int l, r;
cin >> l >> r;
--l, --r;
cout << (last[l] >= r ? "YES\n" : "NO\n");
}
}
int main() {
int t = 1;
ios_base :: sync_with_stdio(false);
cin.tie(0);
//cin >> t;
while(t--) solve();
return 0;
}
# | 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... |