#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, Q, C[MAXN+10], A[MAXN+10], B[MAXN+10], L[MAXN+10], R[MAXN+10];
vector<int> V[MAXN+10];
struct MINSEG
{
int tree[MAXN*4+10], val[MAXN*4+10];
MINSEG() {}
void init(int node, int tl, int tr, int *S)
{
if(tl==tr) { tree[node]=S[tl]; return; }
int mid=tl+tr>>1;
init(node*2, tl, mid, S);
init(node*2+1, mid+1, tr, S);
tree[node]=min(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return INF;
int mid=tl+tr>>1;
return min(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
void query2(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) { val[node]=tree[node]; return; }
if(r<tl || tr<l) { val[node]=INF; return; }
int mid=tl+tr>>1;
query2(node*2, tl, mid, l, r);
query2(node*2+1, mid+1, tr, l, r);
val[node]=min(val[node*2], val[node*2+1]);
}
int query3(int node, int tl, int tr, int l, int r, int k)
{
if(tl==tr) return tl;
if(l<=tl && tr<=r) { val[node*2]=tree[node*2]; val[node*2+1]=tree[node*2+1]; }
int mid=tl+tr>>1;
if(val[node*2+1]<=k) return query3(node*2+1, mid+1, tr, l, r, k);
else return query3(node*2, tl, mid, l, r, k);
}
};
struct MAXSEG
{
int tree[MAXN*4+10], val[MAXN*4+10];
MAXSEG() {}
void init(int node, int tl, int tr, int *S)
{
if(tl==tr) { tree[node]=S[tl]; return; }
int mid=tl+tr>>1;
init(node*2, tl, mid, S);
init(node*2+1, mid+1, tr, S);
tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return -INF;
int mid=tl+tr>>1;
return max(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
void query2(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) { val[node]=tree[node]; return; }
if(r<tl || tr<l) { val[node]=-INF; return; }
int mid=tl+tr>>1;
query2(node*2, tl, mid, l, r);
query2(node*2+1, mid+1, tr, l, r);
val[node]=max(val[node*2], val[node*2+1]);
}
int query3(int node, int tl, int tr, int l, int r, int k)
{
if(tl==tr) return tl;
if(l<=tl && tr<=r) { val[node*2]=tree[node*2]; val[node*2+1]=tree[node*2+1]; }
int mid=tl+tr>>1;
if(val[node*2]>=k) return query3(node*2, tl, mid, l, r, k);
else return query3(node*2+1, mid+1, tr, l, r, k);
}
};
MINSEG SA, SL;
MAXSEG SB, SR;
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<N; i++) scanf("%d", &C[i]);
for(i=1; i<=N; i++) V[i].push_back(0);
for(i=1; i<=N; i++)
{
int t;
scanf("%d", &t);
while(t--)
{
int p;
scanf("%d", &p);
V[p].push_back(i);
}
}
for(i=1; i<=N; i++) V[i].push_back(N+1);
for(i=1; i<N; i++)
{
auto it=upper_bound(V[C[i]].begin(), V[C[i]].end(), i);
B[i+1]=*it-1;
A[i]=*(--it)+1;
}
A[N]=1; B[1]=N;
//for(i=1; i<=N; i++) printf("%d ", A[i]); printf("\n");
//for(i=1; i<=N; i++) printf("%d ", B[i]); printf("\n");
SB.init(1, 1, N, B);
for(i=1; i<=N; i++)
{
SB.query2(1, 1, N, A[i], N);
L[i]=SB.query3(1, 1, N, A[i], N, i);
//printf("%d %d %d : %d\n", A[i], N, i, L[i]);
}
SA.init(1, 1, N, A);
for(i=1; i<=N; i++)
{
SA.query2(1, 1, N, 1, B[i]);
R[i]=SA.query3(1, 1, N, 1, B[i], i);
}
SR.init(1, 1, N, R);
SL.init(1, 1, N, L);
scanf("%d", &Q);
while(Q--)
{
int X, Y;
scanf("%d%d", &X, &Y);
if(X<Y)
{
if(SL.query(1, 1, N, X, Y-1)<=X) printf("NO\n");
else printf("YES\n");
}
else
{
if(SR.query(1, 1, N, Y+1, X)>=X) printf("NO\n");
else printf("YES\n");
}
}
}
Compilation message
long_mansion.cpp: In member function 'void MINSEG::init(int, int, int, int*)':
long_mansion.cpp:21:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'int MINSEG::query(int, int, int, int, int)':
long_mansion.cpp:30:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'void MINSEG::query2(int, int, int, int, int)':
long_mansion.cpp:37:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'int MINSEG::query3(int, int, int, int, int, int)':
long_mansion.cpp:46:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'void MAXSEG::init(int, int, int, int*)':
long_mansion.cpp:59:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'int MAXSEG::query(int, int, int, int, int)':
long_mansion.cpp:68:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'void MAXSEG::query2(int, int, int, int, int)':
long_mansion.cpp:75:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In member function 'int MAXSEG::query3(int, int, int, int, int, int)':
long_mansion.cpp:84:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
long_mansion.cpp: In function 'int main()':
long_mansion.cpp:95:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
long_mansion.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
long_mansion.cpp:98:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<N; i++) scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
long_mansion.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &t);
~~~~~^~~~~~~~~~
long_mansion.cpp:107:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p);
~~~~~^~~~~~~~~~
long_mansion.cpp:141:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
long_mansion.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &X, &Y);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12408 KB |
Output is correct |
2 |
Correct |
18 ms |
12536 KB |
Output is correct |
3 |
Correct |
20 ms |
12792 KB |
Output is correct |
4 |
Correct |
17 ms |
12408 KB |
Output is correct |
5 |
Correct |
16 ms |
12408 KB |
Output is correct |
6 |
Correct |
17 ms |
12408 KB |
Output is correct |
7 |
Correct |
18 ms |
12408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12408 KB |
Output is correct |
2 |
Correct |
18 ms |
12536 KB |
Output is correct |
3 |
Correct |
20 ms |
12792 KB |
Output is correct |
4 |
Correct |
17 ms |
12408 KB |
Output is correct |
5 |
Correct |
16 ms |
12408 KB |
Output is correct |
6 |
Correct |
17 ms |
12408 KB |
Output is correct |
7 |
Correct |
18 ms |
12408 KB |
Output is correct |
8 |
Correct |
285 ms |
13944 KB |
Output is correct |
9 |
Correct |
299 ms |
14072 KB |
Output is correct |
10 |
Correct |
303 ms |
14200 KB |
Output is correct |
11 |
Correct |
333 ms |
14712 KB |
Output is correct |
12 |
Correct |
268 ms |
14072 KB |
Output is correct |
13 |
Correct |
254 ms |
14072 KB |
Output is correct |
14 |
Correct |
247 ms |
14204 KB |
Output is correct |
15 |
Correct |
256 ms |
14204 KB |
Output is correct |
16 |
Correct |
230 ms |
14384 KB |
Output is correct |
17 |
Correct |
244 ms |
14200 KB |
Output is correct |
18 |
Correct |
257 ms |
14200 KB |
Output is correct |
19 |
Correct |
253 ms |
14148 KB |
Output is correct |
20 |
Correct |
253 ms |
14328 KB |
Output is correct |
21 |
Correct |
235 ms |
14328 KB |
Output is correct |
22 |
Correct |
250 ms |
14200 KB |
Output is correct |
23 |
Correct |
246 ms |
13944 KB |
Output is correct |
24 |
Correct |
243 ms |
13960 KB |
Output is correct |
25 |
Correct |
242 ms |
13944 KB |
Output is correct |
26 |
Correct |
233 ms |
13944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
577 ms |
27384 KB |
Output is correct |
2 |
Correct |
557 ms |
27256 KB |
Output is correct |
3 |
Correct |
541 ms |
27120 KB |
Output is correct |
4 |
Correct |
571 ms |
27248 KB |
Output is correct |
5 |
Correct |
567 ms |
27128 KB |
Output is correct |
6 |
Correct |
437 ms |
26444 KB |
Output is correct |
7 |
Correct |
424 ms |
26616 KB |
Output is correct |
8 |
Correct |
428 ms |
26872 KB |
Output is correct |
9 |
Correct |
428 ms |
26564 KB |
Output is correct |
10 |
Correct |
438 ms |
26744 KB |
Output is correct |
11 |
Correct |
428 ms |
26616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12408 KB |
Output is correct |
2 |
Correct |
18 ms |
12536 KB |
Output is correct |
3 |
Correct |
20 ms |
12792 KB |
Output is correct |
4 |
Correct |
17 ms |
12408 KB |
Output is correct |
5 |
Correct |
16 ms |
12408 KB |
Output is correct |
6 |
Correct |
17 ms |
12408 KB |
Output is correct |
7 |
Correct |
18 ms |
12408 KB |
Output is correct |
8 |
Correct |
285 ms |
13944 KB |
Output is correct |
9 |
Correct |
299 ms |
14072 KB |
Output is correct |
10 |
Correct |
303 ms |
14200 KB |
Output is correct |
11 |
Correct |
333 ms |
14712 KB |
Output is correct |
12 |
Correct |
268 ms |
14072 KB |
Output is correct |
13 |
Correct |
254 ms |
14072 KB |
Output is correct |
14 |
Correct |
247 ms |
14204 KB |
Output is correct |
15 |
Correct |
256 ms |
14204 KB |
Output is correct |
16 |
Correct |
230 ms |
14384 KB |
Output is correct |
17 |
Correct |
244 ms |
14200 KB |
Output is correct |
18 |
Correct |
257 ms |
14200 KB |
Output is correct |
19 |
Correct |
253 ms |
14148 KB |
Output is correct |
20 |
Correct |
253 ms |
14328 KB |
Output is correct |
21 |
Correct |
235 ms |
14328 KB |
Output is correct |
22 |
Correct |
250 ms |
14200 KB |
Output is correct |
23 |
Correct |
246 ms |
13944 KB |
Output is correct |
24 |
Correct |
243 ms |
13960 KB |
Output is correct |
25 |
Correct |
242 ms |
13944 KB |
Output is correct |
26 |
Correct |
233 ms |
13944 KB |
Output is correct |
27 |
Correct |
577 ms |
27384 KB |
Output is correct |
28 |
Correct |
557 ms |
27256 KB |
Output is correct |
29 |
Correct |
541 ms |
27120 KB |
Output is correct |
30 |
Correct |
571 ms |
27248 KB |
Output is correct |
31 |
Correct |
567 ms |
27128 KB |
Output is correct |
32 |
Correct |
437 ms |
26444 KB |
Output is correct |
33 |
Correct |
424 ms |
26616 KB |
Output is correct |
34 |
Correct |
428 ms |
26872 KB |
Output is correct |
35 |
Correct |
428 ms |
26564 KB |
Output is correct |
36 |
Correct |
438 ms |
26744 KB |
Output is correct |
37 |
Correct |
428 ms |
26616 KB |
Output is correct |
38 |
Correct |
984 ms |
61480 KB |
Output is correct |
39 |
Correct |
1072 ms |
78224 KB |
Output is correct |
40 |
Correct |
877 ms |
67576 KB |
Output is correct |
41 |
Correct |
1000 ms |
75824 KB |
Output is correct |
42 |
Correct |
468 ms |
33656 KB |
Output is correct |
43 |
Correct |
449 ms |
33760 KB |
Output is correct |
44 |
Correct |
712 ms |
48632 KB |
Output is correct |
45 |
Correct |
673 ms |
48760 KB |
Output is correct |
46 |
Correct |
692 ms |
49272 KB |
Output is correct |
47 |
Correct |
458 ms |
33784 KB |
Output is correct |
48 |
Correct |
447 ms |
33788 KB |
Output is correct |
49 |
Correct |
610 ms |
48888 KB |
Output is correct |
50 |
Correct |
669 ms |
48932 KB |
Output is correct |
51 |
Correct |
715 ms |
49216 KB |
Output is correct |
52 |
Correct |
515 ms |
44152 KB |
Output is correct |
53 |
Correct |
616 ms |
61796 KB |
Output is correct |
54 |
Correct |
699 ms |
67956 KB |
Output is correct |
55 |
Correct |
873 ms |
61632 KB |
Output is correct |