#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));
}
}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));
}
}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
{
int lo=L[i], hi=i;
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(seg1.query1(1, 1, N, L[i]+1, mid)>i) hi=mid;
else lo=mid;
}
P[i]=hi;
}
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
{
int lo=i, hi=R[i-1];
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(seg2.query1(1, 1, N, mid, R[i-1]-1)<i) lo=mid;
else hi=mid;
}
Q[i]=lo;
}
seg4.update(1, 1, N, i-1, Q[i]);
}
//for(int i=1; i<N; i++) printf("%d ", L[i]); printf(" : L\n");
//for(int i=1; i<N; i++) printf("%d ", R[i]); printf(" : R\n");
//for(int i=1; i<N; i++) printf("%d ", P[i]); printf(" : P\n");
//for(int i=2; i<=N; i++) printf("%d ", Q[i]); printf(" : Q\n");
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 'void SEG2::update(int, int, int, int, int)':
long_mansion.cpp:52:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In member function 'int SEG2::query1(int, int, int, int, int)':
long_mansion.cpp:59:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int mid=tl+tr>>1;
| ~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:110:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | int mid=lo+hi>>1;
| ~~^~~
long_mansion.cpp:135:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
135 | int mid=lo+hi>>1;
| ~~^~~
long_mansion.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:69:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | for(int i=1; i<N; i++) scanf("%d", &C[i]);
| ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf("%d", &t);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:78:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
78 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
long_mansion.cpp:149:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
149 | scanf("%d", &QQ);
| ~~~~~^~~~~~~~~~~
long_mansion.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
153 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12556 KB |
Output is correct |
2 |
Correct |
17 ms |
12656 KB |
Output is correct |
3 |
Correct |
18 ms |
12780 KB |
Output is correct |
4 |
Correct |
14 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12396 KB |
Output is correct |
6 |
Correct |
14 ms |
12392 KB |
Output is correct |
7 |
Correct |
17 ms |
12396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12556 KB |
Output is correct |
2 |
Correct |
17 ms |
12656 KB |
Output is correct |
3 |
Correct |
18 ms |
12780 KB |
Output is correct |
4 |
Correct |
14 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12396 KB |
Output is correct |
6 |
Correct |
14 ms |
12392 KB |
Output is correct |
7 |
Correct |
17 ms |
12396 KB |
Output is correct |
8 |
Correct |
279 ms |
18276 KB |
Output is correct |
9 |
Correct |
276 ms |
18276 KB |
Output is correct |
10 |
Correct |
289 ms |
18660 KB |
Output is correct |
11 |
Correct |
334 ms |
19300 KB |
Output is correct |
12 |
Correct |
266 ms |
17892 KB |
Output is correct |
13 |
Correct |
243 ms |
18404 KB |
Output is correct |
14 |
Correct |
242 ms |
18404 KB |
Output is correct |
15 |
Correct |
239 ms |
18556 KB |
Output is correct |
16 |
Correct |
225 ms |
18280 KB |
Output is correct |
17 |
Correct |
238 ms |
18404 KB |
Output is correct |
18 |
Correct |
245 ms |
18580 KB |
Output is correct |
19 |
Correct |
249 ms |
18532 KB |
Output is correct |
20 |
Correct |
232 ms |
18404 KB |
Output is correct |
21 |
Correct |
224 ms |
18276 KB |
Output is correct |
22 |
Correct |
253 ms |
18460 KB |
Output is correct |
23 |
Correct |
225 ms |
18148 KB |
Output is correct |
24 |
Correct |
220 ms |
18304 KB |
Output is correct |
25 |
Correct |
224 ms |
18192 KB |
Output is correct |
26 |
Correct |
224 ms |
18148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
674 ms |
32920 KB |
Output is correct |
2 |
Correct |
645 ms |
32284 KB |
Output is correct |
3 |
Correct |
631 ms |
32032 KB |
Output is correct |
4 |
Correct |
660 ms |
32632 KB |
Output is correct |
5 |
Correct |
640 ms |
32600 KB |
Output is correct |
6 |
Correct |
492 ms |
30952 KB |
Output is correct |
7 |
Correct |
479 ms |
30740 KB |
Output is correct |
8 |
Correct |
480 ms |
30564 KB |
Output is correct |
9 |
Correct |
467 ms |
30820 KB |
Output is correct |
10 |
Correct |
468 ms |
30620 KB |
Output is correct |
11 |
Correct |
473 ms |
30564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12556 KB |
Output is correct |
2 |
Correct |
17 ms |
12656 KB |
Output is correct |
3 |
Correct |
18 ms |
12780 KB |
Output is correct |
4 |
Correct |
14 ms |
12544 KB |
Output is correct |
5 |
Correct |
14 ms |
12396 KB |
Output is correct |
6 |
Correct |
14 ms |
12392 KB |
Output is correct |
7 |
Correct |
17 ms |
12396 KB |
Output is correct |
8 |
Correct |
279 ms |
18276 KB |
Output is correct |
9 |
Correct |
276 ms |
18276 KB |
Output is correct |
10 |
Correct |
289 ms |
18660 KB |
Output is correct |
11 |
Correct |
334 ms |
19300 KB |
Output is correct |
12 |
Correct |
266 ms |
17892 KB |
Output is correct |
13 |
Correct |
243 ms |
18404 KB |
Output is correct |
14 |
Correct |
242 ms |
18404 KB |
Output is correct |
15 |
Correct |
239 ms |
18556 KB |
Output is correct |
16 |
Correct |
225 ms |
18280 KB |
Output is correct |
17 |
Correct |
238 ms |
18404 KB |
Output is correct |
18 |
Correct |
245 ms |
18580 KB |
Output is correct |
19 |
Correct |
249 ms |
18532 KB |
Output is correct |
20 |
Correct |
232 ms |
18404 KB |
Output is correct |
21 |
Correct |
224 ms |
18276 KB |
Output is correct |
22 |
Correct |
253 ms |
18460 KB |
Output is correct |
23 |
Correct |
225 ms |
18148 KB |
Output is correct |
24 |
Correct |
220 ms |
18304 KB |
Output is correct |
25 |
Correct |
224 ms |
18192 KB |
Output is correct |
26 |
Correct |
224 ms |
18148 KB |
Output is correct |
27 |
Correct |
674 ms |
32920 KB |
Output is correct |
28 |
Correct |
645 ms |
32284 KB |
Output is correct |
29 |
Correct |
631 ms |
32032 KB |
Output is correct |
30 |
Correct |
660 ms |
32632 KB |
Output is correct |
31 |
Correct |
640 ms |
32600 KB |
Output is correct |
32 |
Correct |
492 ms |
30952 KB |
Output is correct |
33 |
Correct |
479 ms |
30740 KB |
Output is correct |
34 |
Correct |
480 ms |
30564 KB |
Output is correct |
35 |
Correct |
467 ms |
30820 KB |
Output is correct |
36 |
Correct |
468 ms |
30620 KB |
Output is correct |
37 |
Correct |
473 ms |
30564 KB |
Output is correct |
38 |
Execution timed out |
3051 ms |
55280 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |