/*
I find it wholesome to be alone the greater part of the time.
To be in company, even with the best, is soon wearisome and dissipating.
I love to be alone.
I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 5e5 + 10;
//const int Mod = 1e9 + 7;
//const int inf =
int n;
vector <int> S[maxN];
int L[maxN], R[maxN];
int l[maxN], r[maxN];
int last[maxN];
int c[maxN];
bool inside(int l, int r, int x){
return l <= x && x <= r;
}
bool can(int l, int r, int x){
return inside(l, r, L[x]) || inside(l, r, R[x]);
}
void Init(){
cin >> n;
for (int i = 1; i < n; ++i) cin >> c[i];
for (int i = 1; i <= n; ++i){
int b; cin >> b;
while (b--){
int x; cin >> x;
S[i].pb(x);
}
}
for (int i = 2; i <= n; ++i){
for (int j: S[i - 1]){
last[j] = i - 1;
}
L[i] = last[c[i - 1]];
}
fill(last + 1, last + n + 1, n + 1);
R[n] = n + 1;
for (int i = n - 1; i >= 1; --i){
for (int j: S[i + 1]){
last[j] = i + 1;
}
R[i] = last[c[i]];
}
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)){
r[i] = max(r[i], r[l[i] - 1]);
l[i] = l[l[i] - 1];
}
else{
if (r[i] < n && can(l[i], r[i], r[i] + 1))
++r[i];
else break;
}
}
}
int q; cin >> q;
while (q--){
int x, y;
cin >> x >> y;
cout << (inside(l[x], r[x], y) ? "YES\n" : "NO\n");
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12340 KB |
Output is correct |
3 |
Correct |
9 ms |
12372 KB |
Output is correct |
4 |
Correct |
8 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12336 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12340 KB |
Output is correct |
3 |
Correct |
9 ms |
12372 KB |
Output is correct |
4 |
Correct |
8 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12336 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
98 ms |
18044 KB |
Output is correct |
9 |
Correct |
89 ms |
18000 KB |
Output is correct |
10 |
Correct |
89 ms |
18380 KB |
Output is correct |
11 |
Correct |
92 ms |
18908 KB |
Output is correct |
12 |
Correct |
79 ms |
17708 KB |
Output is correct |
13 |
Correct |
84 ms |
18224 KB |
Output is correct |
14 |
Correct |
84 ms |
18232 KB |
Output is correct |
15 |
Correct |
86 ms |
18328 KB |
Output is correct |
16 |
Correct |
86 ms |
18092 KB |
Output is correct |
17 |
Correct |
85 ms |
18312 KB |
Output is correct |
18 |
Correct |
91 ms |
18232 KB |
Output is correct |
19 |
Correct |
84 ms |
18304 KB |
Output is correct |
20 |
Correct |
93 ms |
18244 KB |
Output is correct |
21 |
Correct |
96 ms |
18100 KB |
Output is correct |
22 |
Correct |
84 ms |
18124 KB |
Output is correct |
23 |
Correct |
89 ms |
17992 KB |
Output is correct |
24 |
Correct |
94 ms |
17996 KB |
Output is correct |
25 |
Correct |
91 ms |
18100 KB |
Output is correct |
26 |
Correct |
86 ms |
18004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
25676 KB |
Output is correct |
2 |
Correct |
153 ms |
26996 KB |
Output is correct |
3 |
Correct |
145 ms |
26812 KB |
Output is correct |
4 |
Correct |
153 ms |
27084 KB |
Output is correct |
5 |
Correct |
161 ms |
27072 KB |
Output is correct |
6 |
Correct |
128 ms |
25804 KB |
Output is correct |
7 |
Correct |
123 ms |
25532 KB |
Output is correct |
8 |
Correct |
127 ms |
25548 KB |
Output is correct |
9 |
Correct |
132 ms |
25728 KB |
Output is correct |
10 |
Correct |
125 ms |
25572 KB |
Output is correct |
11 |
Correct |
128 ms |
25588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12244 KB |
Output is correct |
2 |
Correct |
9 ms |
12340 KB |
Output is correct |
3 |
Correct |
9 ms |
12372 KB |
Output is correct |
4 |
Correct |
8 ms |
12244 KB |
Output is correct |
5 |
Correct |
8 ms |
12244 KB |
Output is correct |
6 |
Correct |
9 ms |
12336 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
98 ms |
18044 KB |
Output is correct |
9 |
Correct |
89 ms |
18000 KB |
Output is correct |
10 |
Correct |
89 ms |
18380 KB |
Output is correct |
11 |
Correct |
92 ms |
18908 KB |
Output is correct |
12 |
Correct |
79 ms |
17708 KB |
Output is correct |
13 |
Correct |
84 ms |
18224 KB |
Output is correct |
14 |
Correct |
84 ms |
18232 KB |
Output is correct |
15 |
Correct |
86 ms |
18328 KB |
Output is correct |
16 |
Correct |
86 ms |
18092 KB |
Output is correct |
17 |
Correct |
85 ms |
18312 KB |
Output is correct |
18 |
Correct |
91 ms |
18232 KB |
Output is correct |
19 |
Correct |
84 ms |
18304 KB |
Output is correct |
20 |
Correct |
93 ms |
18244 KB |
Output is correct |
21 |
Correct |
96 ms |
18100 KB |
Output is correct |
22 |
Correct |
84 ms |
18124 KB |
Output is correct |
23 |
Correct |
89 ms |
17992 KB |
Output is correct |
24 |
Correct |
94 ms |
17996 KB |
Output is correct |
25 |
Correct |
91 ms |
18100 KB |
Output is correct |
26 |
Correct |
86 ms |
18004 KB |
Output is correct |
27 |
Correct |
150 ms |
25676 KB |
Output is correct |
28 |
Correct |
153 ms |
26996 KB |
Output is correct |
29 |
Correct |
145 ms |
26812 KB |
Output is correct |
30 |
Correct |
153 ms |
27084 KB |
Output is correct |
31 |
Correct |
161 ms |
27072 KB |
Output is correct |
32 |
Correct |
128 ms |
25804 KB |
Output is correct |
33 |
Correct |
123 ms |
25532 KB |
Output is correct |
34 |
Correct |
127 ms |
25548 KB |
Output is correct |
35 |
Correct |
132 ms |
25728 KB |
Output is correct |
36 |
Correct |
125 ms |
25572 KB |
Output is correct |
37 |
Correct |
128 ms |
25588 KB |
Output is correct |
38 |
Correct |
211 ms |
47052 KB |
Output is correct |
39 |
Correct |
225 ms |
52364 KB |
Output is correct |
40 |
Correct |
201 ms |
40484 KB |
Output is correct |
41 |
Correct |
232 ms |
50772 KB |
Output is correct |
42 |
Correct |
127 ms |
26728 KB |
Output is correct |
43 |
Correct |
153 ms |
26752 KB |
Output is correct |
44 |
Correct |
175 ms |
34764 KB |
Output is correct |
45 |
Correct |
179 ms |
35000 KB |
Output is correct |
46 |
Correct |
200 ms |
35196 KB |
Output is correct |
47 |
Correct |
137 ms |
26740 KB |
Output is correct |
48 |
Correct |
129 ms |
26588 KB |
Output is correct |
49 |
Correct |
188 ms |
34836 KB |
Output is correct |
50 |
Correct |
179 ms |
34868 KB |
Output is correct |
51 |
Correct |
198 ms |
35232 KB |
Output is correct |
52 |
Correct |
178 ms |
33728 KB |
Output is correct |
53 |
Correct |
191 ms |
40936 KB |
Output is correct |
54 |
Correct |
215 ms |
48088 KB |
Output is correct |
55 |
Correct |
198 ms |
41212 KB |
Output is correct |