#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int rd() {
int x = 0, w = 1;
char ch = getchar();
for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') w = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x * w;
}
const int maxn = 3e3 + 4;
int n;
vector<int> adj[maxn];
vector<char> color;
vector<int> parent;
int cycle_start, cycle_end;
bool dfs(int v) {
color[v] = 1;
for (int u : adj[v]) {
if (color[u] == 0) {
parent[u] = v;
if (dfs(u))
return true;
}
else if (color[u] == 1) {
cycle_end = v;
cycle_start = u;
return true;
}
}
color[v] = 2;
return false;
}
vector<int> find_cycle() {
color.assign(n, 0);
parent.assign(n, -1);
cycle_start = -1;
for (int v = 0; v < n; v++) {
if (color[v] == 0 && dfs(v))
break;
}
if (cycle_start == -1) {
return { -1 };
}
else {
vector<int> cycle;
cycle.push_back(cycle_start);
for (int v = cycle_end; v != cycle_start; v = parent[v])
cycle.push_back(v);
cycle.push_back(cycle_start);
reverse(cycle.begin(), cycle.end());
return cycle;
}
}
void solve() {
int k, q; cin >> n >> k >> q;
vector<int> a(n);
for (auto& x : a) cin >> x;
if (k == 2) {
int ok[n + 1][n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ok[i][j] = 0;
}
}
for (int i = 0; i < n; i++) {
ok[i][i] = 1;
for (int j = i + 1; j < n; j++) {
if (a[j] != a[j - 1]) ok[i][j] |= ok[i][j - 1];
else {
ok[i][j] = 0; break;
}
}
}
for (int queries = 0; queries < q; queries++) {
int l, r; cin >> l >> r;
l--; r--;
if (ok[l][r]) cout << "YES\n";
else cout << "NO\n";
}
}
else if (n <= 500 && k <= 5) {
int ok[n + 5][n + 5];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ok[i][j] = 0;
for (int f = 0; f < n; f++) {
for (int s = f + 1; s < n; s++) {
for (int i = 0; i < n; i++) adj[i].clear();
int cnt = 0;
int l = f, r = s;
for (int i = l; i <= r; i++) {
if (cnt % 2 == 1) {
if (i - 1 >= l)adj[a[i - 1]].push_back(a[i]);
if (i + 1 <= r) adj[a[i + 1]].push_back(a[i]);
}
else {
if (i + 1 <= r) adj[a[i]].push_back(a[i + 1]);
if (i - 1 >= l) adj[a[i]].push_back(a[i - 1]);
}
cnt++;
}
vector<int> meow = find_cycle();
int yeaaaa = (meow[0] == -1);
if (yeaaaa)ok[l][r] = 1;
}
}
for (int queries = 0; queries < q; queries++) {
int l, r; cin >> l >> r;
l--; r--;
if (ok[l][r]) cout << "YES\n";
else cout << "NO\n";
}
}
else {
for (int queries = 0; queries < q; queries++) {
int l, r; cin >> l >> r;
l--; r--;
for (int i = 0; i < n; i++) adj[i].clear();
int cnt = 0;
for (int i = l; i <= r; i++) {
if (cnt % 2 == 1) {
if (i - 1 >= l)adj[a[i - 1]].push_back(a[i]);
if (i + 1 <= r) adj[a[i + 1]].push_back(a[i]);
}
else {
if (i + 1 <= r) adj[a[i]].push_back(a[i + 1]);
if (i - 1 >= l) adj[a[i]].push_back(a[i - 1]);
}
cnt++;
}
vector<int> meow = find_cycle();
int yeaaaa = (meow[0] == -1);
if (yeaaaa) cout << "YES\n";
else cout << "NO\n";
}
}
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int tt = 1; //cin>>tt;
while (tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
73924 KB |
Output is correct |
2 |
Correct |
275 ms |
73932 KB |
Output is correct |
3 |
Correct |
266 ms |
73708 KB |
Output is correct |
4 |
Correct |
124 ms |
3344 KB |
Output is correct |
5 |
Correct |
128 ms |
3484 KB |
Output is correct |
6 |
Correct |
265 ms |
73560 KB |
Output is correct |
7 |
Correct |
276 ms |
73420 KB |
Output is correct |
8 |
Correct |
279 ms |
73804 KB |
Output is correct |
9 |
Correct |
273 ms |
73736 KB |
Output is correct |
10 |
Correct |
296 ms |
73932 KB |
Output is correct |
11 |
Correct |
286 ms |
74536 KB |
Output is correct |
12 |
Correct |
266 ms |
74796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
513 ms |
5476 KB |
Output is correct |
2 |
Correct |
523 ms |
13080 KB |
Output is correct |
3 |
Correct |
155 ms |
12908 KB |
Output is correct |
4 |
Correct |
113 ms |
8240 KB |
Output is correct |
5 |
Correct |
143 ms |
9500 KB |
Output is correct |
6 |
Correct |
521 ms |
12904 KB |
Output is correct |
7 |
Correct |
509 ms |
12996 KB |
Output is correct |
8 |
Correct |
155 ms |
13032 KB |
Output is correct |
9 |
Correct |
637 ms |
13460 KB |
Output is correct |
10 |
Correct |
813 ms |
13952 KB |
Output is correct |
11 |
Correct |
768 ms |
13900 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
54 ms |
548 KB |
Output is correct |
3 |
Correct |
56 ms |
552 KB |
Output is correct |
4 |
Correct |
37 ms |
70788 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
52 ms |
468 KB |
Output is correct |
8 |
Correct |
64 ms |
552 KB |
Output is correct |
9 |
Correct |
72 ms |
560 KB |
Output is correct |
10 |
Correct |
79 ms |
548 KB |
Output is correct |
11 |
Correct |
83 ms |
552 KB |
Output is correct |
12 |
Correct |
76 ms |
564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
73924 KB |
Output is correct |
2 |
Correct |
275 ms |
73932 KB |
Output is correct |
3 |
Correct |
266 ms |
73708 KB |
Output is correct |
4 |
Correct |
124 ms |
3344 KB |
Output is correct |
5 |
Correct |
128 ms |
3484 KB |
Output is correct |
6 |
Correct |
265 ms |
73560 KB |
Output is correct |
7 |
Correct |
276 ms |
73420 KB |
Output is correct |
8 |
Correct |
279 ms |
73804 KB |
Output is correct |
9 |
Correct |
273 ms |
73736 KB |
Output is correct |
10 |
Correct |
296 ms |
73932 KB |
Output is correct |
11 |
Correct |
286 ms |
74536 KB |
Output is correct |
12 |
Correct |
266 ms |
74796 KB |
Output is correct |
13 |
Correct |
513 ms |
5476 KB |
Output is correct |
14 |
Correct |
523 ms |
13080 KB |
Output is correct |
15 |
Correct |
155 ms |
12908 KB |
Output is correct |
16 |
Correct |
113 ms |
8240 KB |
Output is correct |
17 |
Correct |
143 ms |
9500 KB |
Output is correct |
18 |
Correct |
521 ms |
12904 KB |
Output is correct |
19 |
Correct |
509 ms |
12996 KB |
Output is correct |
20 |
Correct |
155 ms |
13032 KB |
Output is correct |
21 |
Correct |
637 ms |
13460 KB |
Output is correct |
22 |
Correct |
813 ms |
13952 KB |
Output is correct |
23 |
Correct |
768 ms |
13900 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
54 ms |
548 KB |
Output is correct |
27 |
Correct |
56 ms |
552 KB |
Output is correct |
28 |
Correct |
37 ms |
70788 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
52 ms |
468 KB |
Output is correct |
32 |
Correct |
64 ms |
552 KB |
Output is correct |
33 |
Correct |
72 ms |
560 KB |
Output is correct |
34 |
Correct |
79 ms |
548 KB |
Output is correct |
35 |
Correct |
83 ms |
552 KB |
Output is correct |
36 |
Correct |
76 ms |
564 KB |
Output is correct |
37 |
Execution timed out |
1065 ms |
1320 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |