#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const int INF = 987654321;
int N, QQ;
int C[MAXN+10];
vector<int> A[MAXN+10];
int L[MAXN+10], R[MAXN+10];
int P[MAXN+10], Q[MAXN+10];
struct SEG1
{
int tree[MAXN*4+10];
void update(int node, int tl, int tr, int p, int v)
{
if(tl==tr)
{
tree[node]=v;
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p, v);
else update(node*2+1, mid+1, tr, p, v);
tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query1(int node, int tl, int tr, int l, int r)
{
int mid=tl+tr>>1;
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return -INF;
return max(query1(node*2, tl, mid, l, r), query1(node*2+1, mid+1, tr, l, r));
}
int query2(int node, int tl, int tr, int l, int r, int p)
{
if(r<tl || tr<l) return -1;
if(tree[node]<=p) return -1;
if(tl==tr) return tl;
int mid=tl+tr>>1;
int t=query2(node*2, tl, mid, l, r, p);
if(t!=-1) return t;
t=query2(node*2+1, mid+1, tr, l, r, p);
if(t!=-1) return t;
return -1;
}
}seg1, seg4;
struct SEG2
{
int tree[MAXN*4+10];
void update(int node, int tl, int tr, int p, int v)
{
if(tl==tr)
{
tree[node]=v;
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p, v);
else update(node*2+1, mid+1, tr, p, v);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
int query1(int node, int tl, int tr, int l, int r)
{
int mid=tl+tr>>1;
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return INF;
return min(query1(node*2, tl, mid, l, r), query1(node*2+1, mid+1, tr, l, r));
}
int query2(int node, int tl, int tr, int l, int r, int p)
{
if(r<tl || tr<l) return -1;
if(tree[node]>=p) return -1;
if(tl==tr) return tl;
int mid=tl+tr>>1;
int t=query2(node*2+1, mid+1, tr, l, r, p);
if(t!=-1) return t;
t=query2(node*2, tl, mid, l, r, p);
if(t!=-1) return t;
return -1;
}
}seg2, seg3;
int main()
{
scanf("%d", &N);
for(int i=1; i<N; i++) scanf("%d", &C[i]);
for(int i=1; i<=N; i++) A[i].push_back(0);
for(int i=1; i<=N; i++)
{
int t;
scanf("%d", &t);
while(t--)
{
int p;
scanf("%d", &p);
A[p].push_back(i);
}
}
for(int i=1; i<=N; i++) A[i].push_back(N+1);
for(int i=1; i<N; i++)
{
int col=C[i];
L[i]=*(--upper_bound(A[col].begin(), A[col].end(), i));
R[i]=*lower_bound(A[col].begin(), A[col].end(), i+1);
}
L[0]=0; R[0]=N+1;
L[N]=0; R[N]=N+1;
for(int i=1; i<=N; i++)
{
seg1.update(1, 1, N, i, R[i-1]);
}
for(int i=1; i<N; i++)
{
if(seg1.query1(1, 1, N, L[i]+1, i)<=i)
{
P[i]=N+1;
}
else
{
P[i]=seg1.query2(1, 1, N, L[i]+1, i, i);
}
seg3.update(1, 1, N, i, P[i]);
}
for(int i=1; i<N; i++)
{
seg2.update(1, 1, N, i, L[i]);
}
for(int i=2; i<=N; i++)
{
if(seg2.query1(1, 1, N, i, R[i-1]-1)>=i)
{
Q[i]=0;
}
else
{
Q[i]=seg2.query2(1, 1, N, i, R[i-1]-1, i);
}
seg4.update(1, 1, N, i-1, Q[i]);
}
scanf("%d", &QQ);
while(QQ--)
{
int x, y;
scanf("%d%d", &x, &y);
if(x<y)
{
if(seg3.query1(1, 1, N, x, y-1)<=x) printf("NO\n");
else printf("YES\n");
}
else
{
if(seg4.query1(1, 1, N, y, x-1)>=x) printf("NO\n");
else printf("YES\n");
}
}
}
Compilation message
long_mansion.cpp: In member function 'void SEG1::update(int, int, int, int, int)':
long_mansion.cpp:28:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'int SEG1::query1(int, int, int, int, int)':
long_mansion.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'int SEG1::query2(int, int, int, int, int, int)':
long_mansion.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'void SEG2::update(int, int, int, int, int)':
long_mansion.cpp:64:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'int SEG2::query1(int, int, int, int, int)':
long_mansion.cpp:71:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'int SEG2::query2(int, int, int, int, int, int)':
long_mansion.cpp:81:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
92 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:93:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
93 | for(int i=1; i<N; i++) scanf("%d", &C[i]);
| ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:98:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
98 | scanf("%d", &t);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:102:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:154:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
154 | scanf("%d", &QQ);
| ~~~~~^~~~~~~~~~~
long_mansion.cpp:158:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
158 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12396 KB |
Output is correct |
2 |
Correct |
18 ms |
12524 KB |
Output is correct |
3 |
Correct |
17 ms |
12780 KB |
Output is correct |
4 |
Correct |
13 ms |
12396 KB |
Output is correct |
5 |
Correct |
13 ms |
12396 KB |
Output is correct |
6 |
Correct |
13 ms |
12396 KB |
Output is correct |
7 |
Correct |
13 ms |
12396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12396 KB |
Output is correct |
2 |
Correct |
18 ms |
12524 KB |
Output is correct |
3 |
Correct |
17 ms |
12780 KB |
Output is correct |
4 |
Correct |
13 ms |
12396 KB |
Output is correct |
5 |
Correct |
13 ms |
12396 KB |
Output is correct |
6 |
Correct |
13 ms |
12396 KB |
Output is correct |
7 |
Correct |
13 ms |
12396 KB |
Output is correct |
8 |
Correct |
264 ms |
18276 KB |
Output is correct |
9 |
Correct |
267 ms |
18276 KB |
Output is correct |
10 |
Correct |
281 ms |
18788 KB |
Output is correct |
11 |
Correct |
297 ms |
19204 KB |
Output is correct |
12 |
Correct |
251 ms |
17764 KB |
Output is correct |
13 |
Correct |
237 ms |
18404 KB |
Output is correct |
14 |
Correct |
236 ms |
18408 KB |
Output is correct |
15 |
Correct |
236 ms |
18532 KB |
Output is correct |
16 |
Correct |
219 ms |
18276 KB |
Output is correct |
17 |
Correct |
236 ms |
18404 KB |
Output is correct |
18 |
Correct |
241 ms |
18532 KB |
Output is correct |
19 |
Correct |
259 ms |
18532 KB |
Output is correct |
20 |
Correct |
250 ms |
18440 KB |
Output is correct |
21 |
Correct |
224 ms |
18276 KB |
Output is correct |
22 |
Correct |
242 ms |
18532 KB |
Output is correct |
23 |
Correct |
216 ms |
18276 KB |
Output is correct |
24 |
Correct |
217 ms |
18148 KB |
Output is correct |
25 |
Correct |
220 ms |
18268 KB |
Output is correct |
26 |
Correct |
220 ms |
18148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
629 ms |
32948 KB |
Output is correct |
2 |
Correct |
619 ms |
32144 KB |
Output is correct |
3 |
Correct |
592 ms |
32040 KB |
Output is correct |
4 |
Correct |
602 ms |
32332 KB |
Output is correct |
5 |
Correct |
627 ms |
32628 KB |
Output is correct |
6 |
Correct |
453 ms |
30820 KB |
Output is correct |
7 |
Correct |
451 ms |
30564 KB |
Output is correct |
8 |
Correct |
450 ms |
30564 KB |
Output is correct |
9 |
Correct |
461 ms |
30564 KB |
Output is correct |
10 |
Correct |
453 ms |
30820 KB |
Output is correct |
11 |
Correct |
453 ms |
30564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12396 KB |
Output is correct |
2 |
Correct |
18 ms |
12524 KB |
Output is correct |
3 |
Correct |
17 ms |
12780 KB |
Output is correct |
4 |
Correct |
13 ms |
12396 KB |
Output is correct |
5 |
Correct |
13 ms |
12396 KB |
Output is correct |
6 |
Correct |
13 ms |
12396 KB |
Output is correct |
7 |
Correct |
13 ms |
12396 KB |
Output is correct |
8 |
Correct |
264 ms |
18276 KB |
Output is correct |
9 |
Correct |
267 ms |
18276 KB |
Output is correct |
10 |
Correct |
281 ms |
18788 KB |
Output is correct |
11 |
Correct |
297 ms |
19204 KB |
Output is correct |
12 |
Correct |
251 ms |
17764 KB |
Output is correct |
13 |
Correct |
237 ms |
18404 KB |
Output is correct |
14 |
Correct |
236 ms |
18408 KB |
Output is correct |
15 |
Correct |
236 ms |
18532 KB |
Output is correct |
16 |
Correct |
219 ms |
18276 KB |
Output is correct |
17 |
Correct |
236 ms |
18404 KB |
Output is correct |
18 |
Correct |
241 ms |
18532 KB |
Output is correct |
19 |
Correct |
259 ms |
18532 KB |
Output is correct |
20 |
Correct |
250 ms |
18440 KB |
Output is correct |
21 |
Correct |
224 ms |
18276 KB |
Output is correct |
22 |
Correct |
242 ms |
18532 KB |
Output is correct |
23 |
Correct |
216 ms |
18276 KB |
Output is correct |
24 |
Correct |
217 ms |
18148 KB |
Output is correct |
25 |
Correct |
220 ms |
18268 KB |
Output is correct |
26 |
Correct |
220 ms |
18148 KB |
Output is correct |
27 |
Correct |
629 ms |
32948 KB |
Output is correct |
28 |
Correct |
619 ms |
32144 KB |
Output is correct |
29 |
Correct |
592 ms |
32040 KB |
Output is correct |
30 |
Correct |
602 ms |
32332 KB |
Output is correct |
31 |
Correct |
627 ms |
32628 KB |
Output is correct |
32 |
Correct |
453 ms |
30820 KB |
Output is correct |
33 |
Correct |
451 ms |
30564 KB |
Output is correct |
34 |
Correct |
450 ms |
30564 KB |
Output is correct |
35 |
Correct |
461 ms |
30564 KB |
Output is correct |
36 |
Correct |
453 ms |
30820 KB |
Output is correct |
37 |
Correct |
453 ms |
30564 KB |
Output is correct |
38 |
Correct |
1443 ms |
64928 KB |
Output is correct |
39 |
Correct |
1573 ms |
70080 KB |
Output is correct |
40 |
Correct |
1146 ms |
59148 KB |
Output is correct |
41 |
Correct |
1341 ms |
67620 KB |
Output is correct |
42 |
Correct |
484 ms |
31612 KB |
Output is correct |
43 |
Correct |
498 ms |
31792 KB |
Output is correct |
44 |
Correct |
727 ms |
44644 KB |
Output is correct |
45 |
Correct |
735 ms |
44900 KB |
Output is correct |
46 |
Correct |
742 ms |
44900 KB |
Output is correct |
47 |
Correct |
491 ms |
31844 KB |
Output is correct |
48 |
Correct |
479 ms |
31588 KB |
Output is correct |
49 |
Correct |
716 ms |
44644 KB |
Output is correct |
50 |
Correct |
747 ms |
44772 KB |
Output is correct |
51 |
Correct |
769 ms |
45028 KB |
Output is correct |
52 |
Correct |
691 ms |
41828 KB |
Output is correct |
53 |
Correct |
898 ms |
57056 KB |
Output is correct |
54 |
Correct |
1125 ms |
63676 KB |
Output is correct |
55 |
Correct |
906 ms |
57188 KB |
Output is correct |