#include<bits/stdc++.h>
using namespace std;
const int MN = 500010;
int N, Q;
int C[MN];
vector<int> A[MN], B[MN];
int L[MN], R[MN], X[MN], Y[MN];
struct BIT {
vector<int> tree;
void init() {
tree = vector<int>(4 * N, -1e9);
}
void upd(int idx, int val, int l, int r, int n) {
if(idx < l || r < idx) return;
if(l == r) {
tree[n] = val;
return;
}
int m = (l + r)>>1;
upd(idx, val, l, m, 2*n);
upd(idx, val, m + 1, r, 2*n + 1);
tree[n] = max(tree[2*n], tree[2*n + 1]);
}
int quer(int a, int b, int l, int r, int n) {
if(b < l || r < a) return -1e9;
if(a <= l && r <= b) return tree[n];
int m = (l + r)>>1;
int L = quer(a, b, l, m, 2*n);
int R = quer(a, b, m + 1, r, 2*n + 1);
return max(L, R);
}
} bit1, bit2, bit3, bit4;
int main() {
scanf("%d", &N);
for(int i = 0; i < N - 1; i++) {
scanf("%d", &C[i]);
C[i]--;
}
for(int i = 0; i < N; i++) {
int k; scanf("%d", &k);
A[i].resize(k);
for(int j = 0; j < k; j++) {
scanf("%d", &A[i][j]);
A[i][j]--;
}
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < A[i].size(); j++) {
B[ A[i][j] ].push_back(i);
}
}
for(int i = 0; i < N - 1; i++) {
int s = 0, e = (int)B[ C[i] ].size() - 1, p = -1;
while(s <= e) {
int m = (s + e)>>1;
if(B[ C[i] ][m] <= i) {
p = B[ C[i] ][m];
s = m + 1;
}
else e = m - 1;
}
L[i] = p;
}
for(int i = N - 1; i >= 1; i--) {
int s = 0, e = (int)B[ C[i - 1] ].size() - 1, p = N;
while(s <= e) {
int m = (s + e)>>1;
if(B[ C[i - 1] ][m] >= i) {
p = B[ C[i - 1] ][m];
e = m - 1;
}
else s = m + 1;
}
R[i] = p;
}
L[N - 1] = -1;
R[0] = N;
bit1.init();
bit2.init();
for(int i = 0; i < N; i++) {
bit1.upd(i, R[i], 0, N - 1, 1);
bit2.upd(i, -L[i], 0, N - 1, 1);
}
for(int i = 0; i < N - 1; i++) {
int s = L[i] + 1, e = i, p = N;
while(s <= e) {
int m = (s + e)>>1;
if(bit1.quer(L[i] + 1, m, 0, N - 1, 1) > i) {
p = m;
e = m - 1;
}
else s = m + 1;
}
X[i] = p;
}
for(int i = N - 1; i >= 1; i--) {
int s = i, e = R[i] - 1, p = -1;
while(s <= e) {
int m = (s + e)>>1;
if(-bit2.quer(m, R[i] - 1, 0, N - 1, 1) < i) {
p = m;
s = m + 1;
}
else e = m - 1;
}
Y[i] = p;
}
bit3.init();
bit4.init();
for(int i = 0; i < N; i++) {
bit3.upd(i, -X[i], 0, N - 1, 1);
bit4.upd(i, Y[i], 0, N - 1, 1);
}
scanf("%d", &Q);
for(int i = 0; i < Q; i++) {
int x, y; scanf("%d %d", &x, &y);
x--; y--;
if(x < y) {
if(-bit3.quer(x, y - 1, 0, N - 1, 1) <= x) printf("NO\n");
else printf("YES\n");
}
else {
if(bit4.quer(y + 1, x, 0, N - 1, 1) >= x) printf("NO\n");
else printf("YES\n");
}
}
}
Compilation message
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:55:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < A[i].size(); j++) {
~~^~~~~~~~~~~~~
long_mansion.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
long_mansion.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
long_mansion.cpp:46:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int k; scanf("%d", &k);
~~~~~^~~~~~~~~~
long_mansion.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i][j]);
~~~~~^~~~~~~~~~~~~~~~
long_mansion.cpp:128:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
long_mansion.cpp:131:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24184 KB |
Output is correct |
2 |
Correct |
29 ms |
24372 KB |
Output is correct |
3 |
Correct |
30 ms |
24840 KB |
Output is correct |
4 |
Correct |
26 ms |
24840 KB |
Output is correct |
5 |
Correct |
26 ms |
24840 KB |
Output is correct |
6 |
Correct |
27 ms |
24840 KB |
Output is correct |
7 |
Correct |
34 ms |
25032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24184 KB |
Output is correct |
2 |
Correct |
29 ms |
24372 KB |
Output is correct |
3 |
Correct |
30 ms |
24840 KB |
Output is correct |
4 |
Correct |
26 ms |
24840 KB |
Output is correct |
5 |
Correct |
26 ms |
24840 KB |
Output is correct |
6 |
Correct |
27 ms |
24840 KB |
Output is correct |
7 |
Correct |
34 ms |
25032 KB |
Output is correct |
8 |
Correct |
318 ms |
30800 KB |
Output is correct |
9 |
Correct |
312 ms |
35080 KB |
Output is correct |
10 |
Correct |
346 ms |
39972 KB |
Output is correct |
11 |
Correct |
393 ms |
45012 KB |
Output is correct |
12 |
Correct |
361 ms |
48460 KB |
Output is correct |
13 |
Correct |
263 ms |
52904 KB |
Output is correct |
14 |
Correct |
264 ms |
57280 KB |
Output is correct |
15 |
Correct |
266 ms |
61636 KB |
Output is correct |
16 |
Correct |
262 ms |
65692 KB |
Output is correct |
17 |
Correct |
269 ms |
69848 KB |
Output is correct |
18 |
Correct |
276 ms |
74332 KB |
Output is correct |
19 |
Correct |
262 ms |
78712 KB |
Output is correct |
20 |
Correct |
254 ms |
82948 KB |
Output is correct |
21 |
Correct |
262 ms |
86984 KB |
Output is correct |
22 |
Correct |
263 ms |
91072 KB |
Output is correct |
23 |
Correct |
249 ms |
95196 KB |
Output is correct |
24 |
Correct |
284 ms |
99340 KB |
Output is correct |
25 |
Correct |
254 ms |
103628 KB |
Output is correct |
26 |
Correct |
251 ms |
108040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
623 ms |
129104 KB |
Output is correct |
2 |
Correct |
630 ms |
135900 KB |
Output is correct |
3 |
Correct |
618 ms |
142832 KB |
Output is correct |
4 |
Correct |
680 ms |
150548 KB |
Output is correct |
5 |
Correct |
656 ms |
157424 KB |
Output is correct |
6 |
Correct |
480 ms |
162896 KB |
Output is correct |
7 |
Correct |
562 ms |
169184 KB |
Output is correct |
8 |
Correct |
551 ms |
175204 KB |
Output is correct |
9 |
Correct |
494 ms |
181408 KB |
Output is correct |
10 |
Correct |
499 ms |
187292 KB |
Output is correct |
11 |
Correct |
459 ms |
193324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
24184 KB |
Output is correct |
2 |
Correct |
29 ms |
24372 KB |
Output is correct |
3 |
Correct |
30 ms |
24840 KB |
Output is correct |
4 |
Correct |
26 ms |
24840 KB |
Output is correct |
5 |
Correct |
26 ms |
24840 KB |
Output is correct |
6 |
Correct |
27 ms |
24840 KB |
Output is correct |
7 |
Correct |
34 ms |
25032 KB |
Output is correct |
8 |
Correct |
318 ms |
30800 KB |
Output is correct |
9 |
Correct |
312 ms |
35080 KB |
Output is correct |
10 |
Correct |
346 ms |
39972 KB |
Output is correct |
11 |
Correct |
393 ms |
45012 KB |
Output is correct |
12 |
Correct |
361 ms |
48460 KB |
Output is correct |
13 |
Correct |
263 ms |
52904 KB |
Output is correct |
14 |
Correct |
264 ms |
57280 KB |
Output is correct |
15 |
Correct |
266 ms |
61636 KB |
Output is correct |
16 |
Correct |
262 ms |
65692 KB |
Output is correct |
17 |
Correct |
269 ms |
69848 KB |
Output is correct |
18 |
Correct |
276 ms |
74332 KB |
Output is correct |
19 |
Correct |
262 ms |
78712 KB |
Output is correct |
20 |
Correct |
254 ms |
82948 KB |
Output is correct |
21 |
Correct |
262 ms |
86984 KB |
Output is correct |
22 |
Correct |
263 ms |
91072 KB |
Output is correct |
23 |
Correct |
249 ms |
95196 KB |
Output is correct |
24 |
Correct |
284 ms |
99340 KB |
Output is correct |
25 |
Correct |
254 ms |
103628 KB |
Output is correct |
26 |
Correct |
251 ms |
108040 KB |
Output is correct |
27 |
Correct |
623 ms |
129104 KB |
Output is correct |
28 |
Correct |
630 ms |
135900 KB |
Output is correct |
29 |
Correct |
618 ms |
142832 KB |
Output is correct |
30 |
Correct |
680 ms |
150548 KB |
Output is correct |
31 |
Correct |
656 ms |
157424 KB |
Output is correct |
32 |
Correct |
480 ms |
162896 KB |
Output is correct |
33 |
Correct |
562 ms |
169184 KB |
Output is correct |
34 |
Correct |
551 ms |
175204 KB |
Output is correct |
35 |
Correct |
494 ms |
181408 KB |
Output is correct |
36 |
Correct |
499 ms |
187292 KB |
Output is correct |
37 |
Correct |
459 ms |
193324 KB |
Output is correct |
38 |
Execution timed out |
3046 ms |
240472 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |