#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
const int MAX_N = 5005;
vector <int> edg[MAX_N];
stack <int> added;
int arr[MAX_N], ans[MAX_N], cur;
bool marked[MAX_N], marked2[MAX_N], flag;
int n, k;
void dfs(int x){
marked[x] = true; marked2[x] = true;
added.push(x);
for (auto edge : edg[x]){
if (marked2[edge]) flag = true; //cerr << edge << ' ' << x << '\n';
if (!marked[edge]) dfs(edge);
}
}
bool check(int x){
//if (cur == 1) cerr << cur << ' ' << x << '\n';
for (int i = 0; i <= k; i++) edg[i].clear();
fill(marked, marked + MAX_N, 0);
fill(marked2, marked2 + MAX_N, 0);
for (int i = cur; i < x; i++){
if ((i - cur) % 2) edg[arr[i]].pb(arr[i + 1]);
else edg[arr[i + 1]].pb(arr[i]);
/*if (cur == 1){
if ((i - cur) % 2) cerr << arr[i] << ' ' << arr[i + 1] << '\n';
else cerr << arr[i + 1] << ' ' << arr[i] << '\n';
}*/
}
flag = false;
for (int i = 0; i < n; i++){
if (!marked[i]) dfs(i);
while (!added.empty()){
marked2[added.top()] = 0;
added.pop();
}
}
return !flag;
//reverse(topo.begin(), topo.end());
//if (cur == 1) for (auto tmp : topo) cerr << tmp << ' ';
//if (cur == 1) cerr << '\n';
/*fill(marked, marked + MAX_N, 0);
flag = false;
for (auto nod : topo){
if (!marked[nod]){
dfs2(nod);*/
/*if (cur == 1){
cerr << nod << ' ' << flag << '\n';
for (int j = 0; j < 3; j++) cerr << marked[j] << ' ';
cerr << '\n';
}*/
//}
//}
//if (cur == 1) cerr << '\n';
}
void init(){
for (int i = 0; i < n; i++){
int lo = i, hi = n, mi; cur = i;
/*if (i == 1){
cerr << "Checking 1: \n"; check(4);
cerr << "DONE\n";
}*/
while (lo + 1 != hi){
mi = (lo + hi) / 2;
if (check(mi)) lo = mi;
else hi = mi;
//cerr << i << ' ' << lo << ' ' << hi << '\n';
}
ans[i] = lo;
}
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int q, x, y; cin >> n >> k >> q;
for (int i = 0; i < n; i++) cin >> arr[i], arr[i] -= 1;
init();
//for (int i = 0; i < n; i++) cerr << "Highest: " << ans[i] << '\n';
for (int i = 0; i < q; i++){
cin >> x >> y;
x -= 1; y -= 1;
if (y <= ans[x]) cout << "YES\n";
else cout << "NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
856 ms |
3748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
156 ms |
3660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1002 ms |
552 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
856 ms |
3748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |