#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 500000;
int const lgmax = 20;
vector<int> keys[5 + nmax];
int path[5 + nmax];
int firstleft[5 + nmax], firstright[5 + nmax];
namespace RMQ{
int lg[1 + nmax];
int rmq[1 + lgmax][1 + nmax];
void initialize(int n){
for(int i = 2; i <= n; i++)
lg[i] = lg[i / 2] + 1;
for(int i = 1;i <= n; i++)
rmq[0][i] = firstleft[i];
for(int h = 1; h <= lgmax; h++)
for(int i = 1;i <= n - (1 << h) + 1; i++)
rmq[h][i] = min(rmq[h - 1][i], rmq[h - 1][i + (1 << (h - 1))]);
}
int extract(int x, int y){
int h = lg[y - x];
return min(rmq[h][x], rmq[h][y - (1 << h) + 1]);
}
}
map<int,int> frec;
void _addset(int room){
for(int i = 0; i < keys[room].size(); i++)
frec[keys[room][i]] = room;
}
int sol[1 + nmax][2];
void expandright(int &x, int &y, int n){
for(int jump = (1 << 20); 0 < jump; jump /= 2)
if(y + jump <= n && x <= RMQ::extract(y + 1, y + jump))
y += jump;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i = 1;i < n; i++)
cin >> path[i];
for(int i = 1;i <= n; i++){
int k;
cin >> k;
for(int j = 1;j <= k; j++){
int val;
cin >> val;
keys[i].push_back(val);
}
}
for(int i = 1; i <= n; i++) {
firstleft[i] = frec[path[i - 1]];
_addset(i);
}
frec.clear();
for(int i = n; 1 <= i; i--){
firstright[i] = frec[path[i]];
if(firstright[i] == 0)
firstright[i] = n + 1;
_addset(i);
}
RMQ::initialize(n);
for(int i = 1; i <= n; i++)
sol[i][0] = sol[i][1] = i;
stack<int> st;
for(int i = 1; i <= n; i++){
expandright(sol[i][0], sol[i][1], n);
while(0 < st.size() && firstright[st.top()] <= sol[i][1]) {
sol[i][0] = min(sol[i][0], sol[st.top()][0]);
sol[i][1] = max(sol[i][1], sol[st.top()][1]);
expandright(sol[i][0], sol[i][1], n);
st.pop();
}
st.push(i);
}
int q;
cin >> q;
for(int i = 1; i <= q; i++){
int x, y;
cin >> x >> y;
if(sol[x][0] <= y && y <= sol[x][1])
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
Compilation message
long_mansion.cpp: In function 'void _addset(int)':
long_mansion.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < keys[room].size(); i++)
~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12416 KB |
Output is correct |
2 |
Correct |
14 ms |
12672 KB |
Output is correct |
3 |
Correct |
15 ms |
12800 KB |
Output is correct |
4 |
Correct |
15 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12544 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12416 KB |
Output is correct |
2 |
Correct |
14 ms |
12672 KB |
Output is correct |
3 |
Correct |
15 ms |
12800 KB |
Output is correct |
4 |
Correct |
15 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12544 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12544 KB |
Output is correct |
8 |
Correct |
126 ms |
18336 KB |
Output is correct |
9 |
Correct |
129 ms |
18424 KB |
Output is correct |
10 |
Correct |
130 ms |
18680 KB |
Output is correct |
11 |
Correct |
129 ms |
19192 KB |
Output is correct |
12 |
Correct |
116 ms |
17912 KB |
Output is correct |
13 |
Correct |
121 ms |
18552 KB |
Output is correct |
14 |
Correct |
134 ms |
18868 KB |
Output is correct |
15 |
Correct |
128 ms |
18552 KB |
Output is correct |
16 |
Correct |
122 ms |
18272 KB |
Output is correct |
17 |
Correct |
120 ms |
18424 KB |
Output is correct |
18 |
Correct |
123 ms |
18552 KB |
Output is correct |
19 |
Correct |
124 ms |
18556 KB |
Output is correct |
20 |
Correct |
135 ms |
18548 KB |
Output is correct |
21 |
Correct |
121 ms |
18296 KB |
Output is correct |
22 |
Correct |
142 ms |
18424 KB |
Output is correct |
23 |
Correct |
121 ms |
18296 KB |
Output is correct |
24 |
Correct |
127 ms |
18444 KB |
Output is correct |
25 |
Correct |
130 ms |
18296 KB |
Output is correct |
26 |
Correct |
127 ms |
18296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
31736 KB |
Output is correct |
2 |
Correct |
264 ms |
33528 KB |
Output is correct |
3 |
Correct |
278 ms |
33144 KB |
Output is correct |
4 |
Correct |
265 ms |
33400 KB |
Output is correct |
5 |
Correct |
275 ms |
33400 KB |
Output is correct |
6 |
Correct |
243 ms |
32120 KB |
Output is correct |
7 |
Correct |
215 ms |
31864 KB |
Output is correct |
8 |
Correct |
225 ms |
32140 KB |
Output is correct |
9 |
Correct |
208 ms |
31992 KB |
Output is correct |
10 |
Correct |
215 ms |
31992 KB |
Output is correct |
11 |
Correct |
212 ms |
31864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12416 KB |
Output is correct |
2 |
Correct |
14 ms |
12672 KB |
Output is correct |
3 |
Correct |
15 ms |
12800 KB |
Output is correct |
4 |
Correct |
15 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12544 KB |
Output is correct |
6 |
Correct |
15 ms |
12536 KB |
Output is correct |
7 |
Correct |
14 ms |
12544 KB |
Output is correct |
8 |
Correct |
126 ms |
18336 KB |
Output is correct |
9 |
Correct |
129 ms |
18424 KB |
Output is correct |
10 |
Correct |
130 ms |
18680 KB |
Output is correct |
11 |
Correct |
129 ms |
19192 KB |
Output is correct |
12 |
Correct |
116 ms |
17912 KB |
Output is correct |
13 |
Correct |
121 ms |
18552 KB |
Output is correct |
14 |
Correct |
134 ms |
18868 KB |
Output is correct |
15 |
Correct |
128 ms |
18552 KB |
Output is correct |
16 |
Correct |
122 ms |
18272 KB |
Output is correct |
17 |
Correct |
120 ms |
18424 KB |
Output is correct |
18 |
Correct |
123 ms |
18552 KB |
Output is correct |
19 |
Correct |
124 ms |
18556 KB |
Output is correct |
20 |
Correct |
135 ms |
18548 KB |
Output is correct |
21 |
Correct |
121 ms |
18296 KB |
Output is correct |
22 |
Correct |
142 ms |
18424 KB |
Output is correct |
23 |
Correct |
121 ms |
18296 KB |
Output is correct |
24 |
Correct |
127 ms |
18444 KB |
Output is correct |
25 |
Correct |
130 ms |
18296 KB |
Output is correct |
26 |
Correct |
127 ms |
18296 KB |
Output is correct |
27 |
Correct |
277 ms |
31736 KB |
Output is correct |
28 |
Correct |
264 ms |
33528 KB |
Output is correct |
29 |
Correct |
278 ms |
33144 KB |
Output is correct |
30 |
Correct |
265 ms |
33400 KB |
Output is correct |
31 |
Correct |
275 ms |
33400 KB |
Output is correct |
32 |
Correct |
243 ms |
32120 KB |
Output is correct |
33 |
Correct |
215 ms |
31864 KB |
Output is correct |
34 |
Correct |
225 ms |
32140 KB |
Output is correct |
35 |
Correct |
208 ms |
31992 KB |
Output is correct |
36 |
Correct |
215 ms |
31992 KB |
Output is correct |
37 |
Correct |
212 ms |
31864 KB |
Output is correct |
38 |
Correct |
638 ms |
77200 KB |
Output is correct |
39 |
Correct |
559 ms |
90104 KB |
Output is correct |
40 |
Correct |
485 ms |
62336 KB |
Output is correct |
41 |
Correct |
451 ms |
85984 KB |
Output is correct |
42 |
Correct |
307 ms |
35192 KB |
Output is correct |
43 |
Correct |
293 ms |
34040 KB |
Output is correct |
44 |
Correct |
514 ms |
52216 KB |
Output is correct |
45 |
Correct |
544 ms |
52984 KB |
Output is correct |
46 |
Correct |
554 ms |
53656 KB |
Output is correct |
47 |
Correct |
320 ms |
35296 KB |
Output is correct |
48 |
Correct |
288 ms |
33316 KB |
Output is correct |
49 |
Correct |
486 ms |
50040 KB |
Output is correct |
50 |
Correct |
497 ms |
51576 KB |
Output is correct |
51 |
Correct |
555 ms |
54564 KB |
Output is correct |
52 |
Correct |
422 ms |
55928 KB |
Output is correct |
53 |
Correct |
540 ms |
74104 KB |
Output is correct |
54 |
Correct |
670 ms |
94652 KB |
Output is correct |
55 |
Correct |
532 ms |
75480 KB |
Output is correct |