#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
int n, q;
int a[maxn];
int l[maxn];
int r[maxn];
vector<int> keysPos[maxn];
bool isPossible(int leftPos, int rightPos, int key)
{
if (keysPos[key].empty()) return false;
int pos = lower_bound(keysPos[key].begin(), keysPos[key].end(), leftPos) - keysPos[key].begin();
return pos < keysPos[key].size() && keysPos[key][pos] <= rightPos;
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n - 1; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= n; ++i)
{
int cnt;
cin >> cnt;
for (int j = 1; j <= cnt; ++j)
{
int key;
cin >> key;
keysPos[key].push_back(i);
}
}
for (int i = 1; i <= n; ++i)
{
l[i] = r[i] = i;
//cout << "on " << i << endl;
while (true)
{
//cout << "left " << isPossible(l[i], r[i], a[l[i] - 1]) << "\n";
if (l[i] > 1 && isPossible(l[i], r[i], a[l[i] - 1]) == true)
{
r[i] = max(r[i], r[l[i] - 1]);
l[i] = l[l[i] - 1];
}
else if (r[i] < n && isPossible(l[i], r[i], a[r[i]]) == true)
{
++r[i];
}
else
{
break;
}
}
//cout << i << " -> " << l[i] << " " << r[i] << endl;
}
cin >> q;
for (int i = 1; i <= q; ++i)
{
int x, y;
cin >> x >> y;
if (l[x] <= y && y <= r[x])
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
return 0;
}
Compilation message
long_mansion.cpp: In function 'bool isPossible(int, int, int)':
long_mansion.cpp:19:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | return pos < keysPos[key].size() && keysPos[key][pos] <= rightPos;
| ~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12164 KB |
Output is correct |
3 |
Correct |
6 ms |
12380 KB |
Output is correct |
4 |
Correct |
6 ms |
12380 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12164 KB |
Output is correct |
3 |
Correct |
6 ms |
12380 KB |
Output is correct |
4 |
Correct |
6 ms |
12380 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12392 KB |
Output is correct |
8 |
Correct |
60 ms |
18000 KB |
Output is correct |
9 |
Correct |
59 ms |
18000 KB |
Output is correct |
10 |
Correct |
70 ms |
18304 KB |
Output is correct |
11 |
Correct |
61 ms |
18612 KB |
Output is correct |
12 |
Correct |
55 ms |
17748 KB |
Output is correct |
13 |
Correct |
55 ms |
18256 KB |
Output is correct |
14 |
Correct |
67 ms |
18364 KB |
Output is correct |
15 |
Correct |
56 ms |
18260 KB |
Output is correct |
16 |
Correct |
54 ms |
18000 KB |
Output is correct |
17 |
Correct |
61 ms |
18256 KB |
Output is correct |
18 |
Correct |
56 ms |
18256 KB |
Output is correct |
19 |
Correct |
57 ms |
18260 KB |
Output is correct |
20 |
Correct |
57 ms |
18256 KB |
Output is correct |
21 |
Correct |
55 ms |
18044 KB |
Output is correct |
22 |
Correct |
56 ms |
18260 KB |
Output is correct |
23 |
Correct |
60 ms |
18180 KB |
Output is correct |
24 |
Correct |
61 ms |
18048 KB |
Output is correct |
25 |
Correct |
64 ms |
17996 KB |
Output is correct |
26 |
Correct |
59 ms |
18004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
120 ms |
21072 KB |
Output is correct |
2 |
Correct |
132 ms |
24308 KB |
Output is correct |
3 |
Correct |
107 ms |
23996 KB |
Output is correct |
4 |
Correct |
114 ms |
24556 KB |
Output is correct |
5 |
Correct |
112 ms |
24420 KB |
Output is correct |
6 |
Correct |
98 ms |
22648 KB |
Output is correct |
7 |
Correct |
86 ms |
22612 KB |
Output is correct |
8 |
Correct |
84 ms |
22608 KB |
Output is correct |
9 |
Correct |
86 ms |
22496 KB |
Output is correct |
10 |
Correct |
81 ms |
22612 KB |
Output is correct |
11 |
Correct |
81 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12164 KB |
Output is correct |
3 |
Correct |
6 ms |
12380 KB |
Output is correct |
4 |
Correct |
6 ms |
12380 KB |
Output is correct |
5 |
Correct |
5 ms |
12124 KB |
Output is correct |
6 |
Correct |
5 ms |
12152 KB |
Output is correct |
7 |
Correct |
5 ms |
12392 KB |
Output is correct |
8 |
Correct |
60 ms |
18000 KB |
Output is correct |
9 |
Correct |
59 ms |
18000 KB |
Output is correct |
10 |
Correct |
70 ms |
18304 KB |
Output is correct |
11 |
Correct |
61 ms |
18612 KB |
Output is correct |
12 |
Correct |
55 ms |
17748 KB |
Output is correct |
13 |
Correct |
55 ms |
18256 KB |
Output is correct |
14 |
Correct |
67 ms |
18364 KB |
Output is correct |
15 |
Correct |
56 ms |
18260 KB |
Output is correct |
16 |
Correct |
54 ms |
18000 KB |
Output is correct |
17 |
Correct |
61 ms |
18256 KB |
Output is correct |
18 |
Correct |
56 ms |
18256 KB |
Output is correct |
19 |
Correct |
57 ms |
18260 KB |
Output is correct |
20 |
Correct |
57 ms |
18256 KB |
Output is correct |
21 |
Correct |
55 ms |
18044 KB |
Output is correct |
22 |
Correct |
56 ms |
18260 KB |
Output is correct |
23 |
Correct |
60 ms |
18180 KB |
Output is correct |
24 |
Correct |
61 ms |
18048 KB |
Output is correct |
25 |
Correct |
64 ms |
17996 KB |
Output is correct |
26 |
Correct |
59 ms |
18004 KB |
Output is correct |
27 |
Correct |
120 ms |
21072 KB |
Output is correct |
28 |
Correct |
132 ms |
24308 KB |
Output is correct |
29 |
Correct |
107 ms |
23996 KB |
Output is correct |
30 |
Correct |
114 ms |
24556 KB |
Output is correct |
31 |
Correct |
112 ms |
24420 KB |
Output is correct |
32 |
Correct |
98 ms |
22648 KB |
Output is correct |
33 |
Correct |
86 ms |
22612 KB |
Output is correct |
34 |
Correct |
84 ms |
22608 KB |
Output is correct |
35 |
Correct |
86 ms |
22496 KB |
Output is correct |
36 |
Correct |
81 ms |
22612 KB |
Output is correct |
37 |
Correct |
81 ms |
22356 KB |
Output is correct |
38 |
Correct |
162 ms |
33108 KB |
Output is correct |
39 |
Correct |
165 ms |
33876 KB |
Output is correct |
40 |
Correct |
135 ms |
30800 KB |
Output is correct |
41 |
Correct |
232 ms |
31408 KB |
Output is correct |
42 |
Correct |
94 ms |
24172 KB |
Output is correct |
43 |
Correct |
99 ms |
23888 KB |
Output is correct |
44 |
Correct |
136 ms |
29640 KB |
Output is correct |
45 |
Correct |
139 ms |
30036 KB |
Output is correct |
46 |
Correct |
142 ms |
30288 KB |
Output is correct |
47 |
Correct |
122 ms |
24148 KB |
Output is correct |
48 |
Correct |
95 ms |
23632 KB |
Output is correct |
49 |
Correct |
147 ms |
29312 KB |
Output is correct |
50 |
Correct |
148 ms |
29700 KB |
Output is correct |
51 |
Correct |
148 ms |
30804 KB |
Output is correct |
52 |
Correct |
111 ms |
31060 KB |
Output is correct |
53 |
Correct |
139 ms |
36816 KB |
Output is correct |
54 |
Correct |
151 ms |
43088 KB |
Output is correct |
55 |
Correct |
140 ms |
37304 KB |
Output is correct |