#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e5 + 2;
const int inf = 1e9;
int c[N], le[N], ri[N], l[N], r[N], lst[N];
vector<int> b[N];
bool Between(int L, int R, int x) {
return L <= x && x <= R;
}
bool Can(int L, int R, int x) {
return Between(L, R, le[x]) || Between(L, R, ri[x]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin >> n;
for (int i = 1; i < n; i++) cin >> c[i];
for (int i = 1; i <= n; i++) {
int sz; cin >> sz;
b[i].resize(sz);
for (int j = 0; j < sz; j++) cin >> b[i][j];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < b[i].size(); j++) lst[b[i][j]] = i;
le[i] = lst[c[i]];
}
for (int i = 1; i <= n; i++) lst[i] = 0;
for (int i = n; i > 1; i--) {
for (int j = 0; j < b[i].size(); j++) lst[b[i][j]] = i;
ri[i - 1] = lst[c[i - 1]];
}
for (int i = 1; i <= n; i++) {
l[i] = r[i] = i;
while (1) {
if (l[i] > 1 && Can(l[i], r[i], l[i] - 1)) {
smax(r[i], r[l[i] - 1]);
l[i] = l[l[i] - 1];
}
else if (r[i] < n && Can(l[i], r[i], r[i])) r[i]++;
else break;
}
}
int q; cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
if (Between(l[x], r[x], y)) cout << "YES" << en;
else cout << "NO" << en;
}
return 0;
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:31:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int j = 0; j < b[i].size(); j++) lst[b[i][j]] = i;
| ~~^~~~~~~~~~~~~
long_mansion.cpp:36:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j = 0; j < b[i].size(); j++) lst[b[i][j]] = i;
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
7 ms |
12244 KB |
Output is correct |
3 |
Correct |
8 ms |
12396 KB |
Output is correct |
4 |
Correct |
7 ms |
12220 KB |
Output is correct |
5 |
Correct |
7 ms |
12244 KB |
Output is correct |
6 |
Correct |
7 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
7 ms |
12244 KB |
Output is correct |
3 |
Correct |
8 ms |
12396 KB |
Output is correct |
4 |
Correct |
7 ms |
12220 KB |
Output is correct |
5 |
Correct |
7 ms |
12244 KB |
Output is correct |
6 |
Correct |
7 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12176 KB |
Output is correct |
8 |
Correct |
96 ms |
13644 KB |
Output is correct |
9 |
Correct |
97 ms |
13644 KB |
Output is correct |
10 |
Correct |
96 ms |
13844 KB |
Output is correct |
11 |
Correct |
92 ms |
14092 KB |
Output is correct |
12 |
Correct |
89 ms |
13916 KB |
Output is correct |
13 |
Correct |
95 ms |
13964 KB |
Output is correct |
14 |
Correct |
88 ms |
13848 KB |
Output is correct |
15 |
Correct |
90 ms |
14028 KB |
Output is correct |
16 |
Correct |
91 ms |
14156 KB |
Output is correct |
17 |
Correct |
90 ms |
13868 KB |
Output is correct |
18 |
Correct |
89 ms |
13900 KB |
Output is correct |
19 |
Correct |
91 ms |
13936 KB |
Output is correct |
20 |
Correct |
93 ms |
14040 KB |
Output is correct |
21 |
Correct |
91 ms |
14196 KB |
Output is correct |
22 |
Correct |
90 ms |
13892 KB |
Output is correct |
23 |
Correct |
92 ms |
13792 KB |
Output is correct |
24 |
Correct |
92 ms |
13764 KB |
Output is correct |
25 |
Correct |
92 ms |
13700 KB |
Output is correct |
26 |
Correct |
92 ms |
13720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
19196 KB |
Output is correct |
2 |
Correct |
143 ms |
19276 KB |
Output is correct |
3 |
Correct |
137 ms |
19332 KB |
Output is correct |
4 |
Correct |
142 ms |
19268 KB |
Output is correct |
5 |
Correct |
142 ms |
19288 KB |
Output is correct |
6 |
Correct |
127 ms |
19340 KB |
Output is correct |
7 |
Correct |
129 ms |
19444 KB |
Output is correct |
8 |
Correct |
124 ms |
19532 KB |
Output is correct |
9 |
Correct |
125 ms |
19468 KB |
Output is correct |
10 |
Correct |
129 ms |
19632 KB |
Output is correct |
11 |
Correct |
126 ms |
19580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12244 KB |
Output is correct |
2 |
Correct |
7 ms |
12244 KB |
Output is correct |
3 |
Correct |
8 ms |
12396 KB |
Output is correct |
4 |
Correct |
7 ms |
12220 KB |
Output is correct |
5 |
Correct |
7 ms |
12244 KB |
Output is correct |
6 |
Correct |
7 ms |
12244 KB |
Output is correct |
7 |
Correct |
7 ms |
12176 KB |
Output is correct |
8 |
Correct |
96 ms |
13644 KB |
Output is correct |
9 |
Correct |
97 ms |
13644 KB |
Output is correct |
10 |
Correct |
96 ms |
13844 KB |
Output is correct |
11 |
Correct |
92 ms |
14092 KB |
Output is correct |
12 |
Correct |
89 ms |
13916 KB |
Output is correct |
13 |
Correct |
95 ms |
13964 KB |
Output is correct |
14 |
Correct |
88 ms |
13848 KB |
Output is correct |
15 |
Correct |
90 ms |
14028 KB |
Output is correct |
16 |
Correct |
91 ms |
14156 KB |
Output is correct |
17 |
Correct |
90 ms |
13868 KB |
Output is correct |
18 |
Correct |
89 ms |
13900 KB |
Output is correct |
19 |
Correct |
91 ms |
13936 KB |
Output is correct |
20 |
Correct |
93 ms |
14040 KB |
Output is correct |
21 |
Correct |
91 ms |
14196 KB |
Output is correct |
22 |
Correct |
90 ms |
13892 KB |
Output is correct |
23 |
Correct |
92 ms |
13792 KB |
Output is correct |
24 |
Correct |
92 ms |
13764 KB |
Output is correct |
25 |
Correct |
92 ms |
13700 KB |
Output is correct |
26 |
Correct |
92 ms |
13720 KB |
Output is correct |
27 |
Correct |
141 ms |
19196 KB |
Output is correct |
28 |
Correct |
143 ms |
19276 KB |
Output is correct |
29 |
Correct |
137 ms |
19332 KB |
Output is correct |
30 |
Correct |
142 ms |
19268 KB |
Output is correct |
31 |
Correct |
142 ms |
19288 KB |
Output is correct |
32 |
Correct |
127 ms |
19340 KB |
Output is correct |
33 |
Correct |
129 ms |
19444 KB |
Output is correct |
34 |
Correct |
124 ms |
19532 KB |
Output is correct |
35 |
Correct |
125 ms |
19468 KB |
Output is correct |
36 |
Correct |
129 ms |
19632 KB |
Output is correct |
37 |
Correct |
126 ms |
19580 KB |
Output is correct |
38 |
Correct |
204 ms |
35404 KB |
Output is correct |
39 |
Correct |
217 ms |
40936 KB |
Output is correct |
40 |
Correct |
192 ms |
30028 KB |
Output is correct |
41 |
Correct |
218 ms |
47560 KB |
Output is correct |
42 |
Correct |
135 ms |
26700 KB |
Output is correct |
43 |
Correct |
133 ms |
26788 KB |
Output is correct |
44 |
Correct |
181 ms |
34764 KB |
Output is correct |
45 |
Correct |
179 ms |
34864 KB |
Output is correct |
46 |
Correct |
178 ms |
35108 KB |
Output is correct |
47 |
Correct |
142 ms |
26784 KB |
Output is correct |
48 |
Correct |
136 ms |
26592 KB |
Output is correct |
49 |
Correct |
178 ms |
34768 KB |
Output is correct |
50 |
Correct |
180 ms |
34896 KB |
Output is correct |
51 |
Correct |
178 ms |
35148 KB |
Output is correct |
52 |
Correct |
168 ms |
33688 KB |
Output is correct |
53 |
Correct |
196 ms |
40976 KB |
Output is correct |
54 |
Correct |
224 ms |
48076 KB |
Output is correct |
55 |
Correct |
194 ms |
41064 KB |
Output is correct |