#include<iostream>
#include<algorithm>
#include<time.h>
#include<vector>
#include<stack>
#include<iomanip>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<string.h>
#include<climits>
#include<utility>
#define ull unsigned long long
#define mk make_pair
#define pb push_back
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define pcc pair<char,char>
#define vi vector<ll int>
#define pll pair<ll int,ll int>
#define F first
#define S second
#define kill_ cout<<0<<endl,exit(0);
using namespace std;
int n,q,k,x,y;
const int N = 500002;
vector <int> kotagh[N];
int lans[N],rans[N],keys[N];
void reachable__(int x)
{
bool fl = true;
while(fl)
{
fl = false;
int tmn;
tmn = (lans[x] == 1 ? N : *lower_bound( kotagh[keys[lans[x]-1]].begin(), kotagh[keys[lans[x]-1]].end(), lans[x]));
if (tmn <= rans[x])
{
int tmp = lans[x]--;
lans[x] = min(lans[x], lans[ lans[x] ]),rans[x] = max(rans[x] ,rans[ lans[x]]),fl = true;
}
else
{
int tml = (rans[x] == n ? N : *lower_bound( kotagh[keys[rans[x]]].begin(), kotagh[keys[rans[x]]].end(), lans[x]));
if (tml <= rans[x])
{
rans[x]++;
reachable__(rans[x]);
lans[x] = min(lans[x], lans[ rans[x] ]),rans[x] = max(rans[x] ,rans[ rans[x]]),fl = 1;
}
}
}
}
void input()
{
cin >> n;
for (int i = 1; i <= n-1; i++)cin >> keys[i];
for (int i = 1; i <= n; i++)
{
int ted;cin >> ted;
for (int j = 1; j <= ted; j++){cin >> k;kotagh[k].push_back(i);}
}
for (int i = 1; i <= n; i++)
{
lans[i] = rans[i] = i;
kotagh[i].push_back(N);
}
}
void solve()
{
for (int i = 1; i <= n; i++)reachable__(i);
cin >> q;
while(q--)
{
cin >> x >> y;
cout<<((lans[x] <= y && rans[x] >= y)?"YES\n":"NO\n");
}
}
void optimize(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
int main(){optimize();input();solve();}
Compilation message
long_mansion.cpp: In function 'void reachable__(int)':
long_mansion.cpp:43:17: warning: unused variable 'tmp' [-Wunused-variable]
43 | int tmp = lans[x]--;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12244 KB |
Output is correct |
2 |
Correct |
10 ms |
12372 KB |
Output is correct |
3 |
Correct |
9 ms |
12592 KB |
Output is correct |
4 |
Correct |
8 ms |
12216 KB |
Output is correct |
5 |
Correct |
8 ms |
12360 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12244 KB |
Output is correct |
2 |
Correct |
10 ms |
12372 KB |
Output is correct |
3 |
Correct |
9 ms |
12592 KB |
Output is correct |
4 |
Correct |
8 ms |
12216 KB |
Output is correct |
5 |
Correct |
8 ms |
12360 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
89 ms |
18112 KB |
Output is correct |
9 |
Correct |
89 ms |
18044 KB |
Output is correct |
10 |
Correct |
97 ms |
18484 KB |
Output is correct |
11 |
Correct |
90 ms |
18880 KB |
Output is correct |
12 |
Correct |
90 ms |
17640 KB |
Output is correct |
13 |
Correct |
86 ms |
18236 KB |
Output is correct |
14 |
Correct |
93 ms |
18264 KB |
Output is correct |
15 |
Correct |
86 ms |
18240 KB |
Output is correct |
16 |
Correct |
85 ms |
18192 KB |
Output is correct |
17 |
Correct |
91 ms |
18212 KB |
Output is correct |
18 |
Correct |
107 ms |
18220 KB |
Output is correct |
19 |
Correct |
86 ms |
18344 KB |
Output is correct |
20 |
Correct |
82 ms |
18380 KB |
Output is correct |
21 |
Correct |
89 ms |
18124 KB |
Output is correct |
22 |
Correct |
93 ms |
18204 KB |
Output is correct |
23 |
Correct |
89 ms |
18068 KB |
Output is correct |
24 |
Correct |
91 ms |
18020 KB |
Output is correct |
25 |
Correct |
95 ms |
17968 KB |
Output is correct |
26 |
Correct |
90 ms |
17996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
27980 KB |
Output is correct |
2 |
Correct |
175 ms |
28140 KB |
Output is correct |
3 |
Correct |
156 ms |
29328 KB |
Output is correct |
4 |
Correct |
172 ms |
27848 KB |
Output is correct |
5 |
Correct |
197 ms |
27968 KB |
Output is correct |
6 |
Correct |
141 ms |
28364 KB |
Output is correct |
7 |
Correct |
150 ms |
31316 KB |
Output is correct |
8 |
Correct |
167 ms |
31424 KB |
Output is correct |
9 |
Correct |
132 ms |
31472 KB |
Output is correct |
10 |
Correct |
153 ms |
31636 KB |
Output is correct |
11 |
Correct |
132 ms |
31604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
12244 KB |
Output is correct |
2 |
Correct |
10 ms |
12372 KB |
Output is correct |
3 |
Correct |
9 ms |
12592 KB |
Output is correct |
4 |
Correct |
8 ms |
12216 KB |
Output is correct |
5 |
Correct |
8 ms |
12360 KB |
Output is correct |
6 |
Correct |
8 ms |
12244 KB |
Output is correct |
7 |
Correct |
8 ms |
12244 KB |
Output is correct |
8 |
Correct |
89 ms |
18112 KB |
Output is correct |
9 |
Correct |
89 ms |
18044 KB |
Output is correct |
10 |
Correct |
97 ms |
18484 KB |
Output is correct |
11 |
Correct |
90 ms |
18880 KB |
Output is correct |
12 |
Correct |
90 ms |
17640 KB |
Output is correct |
13 |
Correct |
86 ms |
18236 KB |
Output is correct |
14 |
Correct |
93 ms |
18264 KB |
Output is correct |
15 |
Correct |
86 ms |
18240 KB |
Output is correct |
16 |
Correct |
85 ms |
18192 KB |
Output is correct |
17 |
Correct |
91 ms |
18212 KB |
Output is correct |
18 |
Correct |
107 ms |
18220 KB |
Output is correct |
19 |
Correct |
86 ms |
18344 KB |
Output is correct |
20 |
Correct |
82 ms |
18380 KB |
Output is correct |
21 |
Correct |
89 ms |
18124 KB |
Output is correct |
22 |
Correct |
93 ms |
18204 KB |
Output is correct |
23 |
Correct |
89 ms |
18068 KB |
Output is correct |
24 |
Correct |
91 ms |
18020 KB |
Output is correct |
25 |
Correct |
95 ms |
17968 KB |
Output is correct |
26 |
Correct |
90 ms |
17996 KB |
Output is correct |
27 |
Correct |
165 ms |
27980 KB |
Output is correct |
28 |
Correct |
175 ms |
28140 KB |
Output is correct |
29 |
Correct |
156 ms |
29328 KB |
Output is correct |
30 |
Correct |
172 ms |
27848 KB |
Output is correct |
31 |
Correct |
197 ms |
27968 KB |
Output is correct |
32 |
Correct |
141 ms |
28364 KB |
Output is correct |
33 |
Correct |
150 ms |
31316 KB |
Output is correct |
34 |
Correct |
167 ms |
31424 KB |
Output is correct |
35 |
Correct |
132 ms |
31472 KB |
Output is correct |
36 |
Correct |
153 ms |
31636 KB |
Output is correct |
37 |
Correct |
132 ms |
31604 KB |
Output is correct |
38 |
Correct |
283 ms |
44836 KB |
Output is correct |
39 |
Correct |
261 ms |
49108 KB |
Output is correct |
40 |
Correct |
221 ms |
39832 KB |
Output is correct |
41 |
Correct |
336 ms |
64176 KB |
Output is correct |
42 |
Correct |
156 ms |
25956 KB |
Output is correct |
43 |
Correct |
142 ms |
26404 KB |
Output is correct |
44 |
Correct |
278 ms |
33664 KB |
Output is correct |
45 |
Correct |
266 ms |
33848 KB |
Output is correct |
46 |
Correct |
226 ms |
33836 KB |
Output is correct |
47 |
Correct |
193 ms |
29324 KB |
Output is correct |
48 |
Correct |
141 ms |
29516 KB |
Output is correct |
49 |
Correct |
217 ms |
35632 KB |
Output is correct |
50 |
Correct |
240 ms |
36432 KB |
Output is correct |
51 |
Correct |
227 ms |
36248 KB |
Output is correct |
52 |
Correct |
186 ms |
33024 KB |
Output is correct |
53 |
Correct |
197 ms |
40588 KB |
Output is correct |
54 |
Correct |
261 ms |
44836 KB |
Output is correct |
55 |
Correct |
212 ms |
38920 KB |
Output is correct |