#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<19, INF=1e9+5, mod=1e9+7, mod2=998244353;
struct modint{
int n=0;
modint(){}
modint(ll x){
n=x%mod;
if(n<0)n+=mod;
}
operator int(){
return n;
}
modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
modint operator+=(modint a){return (*this)=(*this)+a;}
modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
modint operator-=(modint a){return (*this)=(*this)-a;}
modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
modint operator*=(modint a){return (*this)=(*this)*a;}
modint operator^(const ll &m)const{
modint a(1);
if(m==0)return a;
if(m==1)return (*this);
a=(*this)^(m/2);
a*=a;
return a*((*this)^(m&1));
}
modint odw(){
return (*this)^((ll)mod-2);
}
modint operator/(modint a){return (*this)*a.odw();}
modint operator/=(modint a){return (*this)=(*this)/a;}
bool operator==(modint a){return a.n==n;}
friend ostream& operator<<(ostream& os, modint m) {
return os << m.n;
}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
fact[0]=1;
for(int i=1; i<N; i++){
fact[i]=(fact[i-1]*modint(i))%mod;
}
fact2[N-1]=fact[N-1].odw();
for(int i=N-2; i>=0; i--){
fact2[i]=(fact2[i+1]*modint(i+1))%mod;
}
}
modint npok(int _n, int _k){
return fact[_n]*fact2[_k]*fact2[_n-_k];
}
int co[N], vis[N];
vi gdzie[N];
int L[N], R[N];
int r[N];
int find(int v){
if(v==r[v])return v;
return r[v]=find(r[v]);
}
void Union(int a, int b){
a=find(a);
b=find(b);
r[a]=b;
}
void dfs(int v){
vis[v]=1;
L[v]=R[v]=v;
r[v]=v;
while(true){
int RR=R[find(v)], LL=L[find(v)];
if(lower_bound(all(gdzie[co[LL-1]]), LL)!=upper_bound(all(gdzie[co[LL-1]]), RR)){
L[find(v)]=L[find(LL-1)];
if(vis[LL-1]==1 && find(v)!=find(LL-1)){
Union(v, LL-1);
R[find(v)]=R[v];
}
}
if(lower_bound(all(gdzie[co[RR]]), LL)!=upper_bound(all(gdzie[co[RR]]), RR)){
if(vis[RR+1]==0){
dfs(RR+1);
}
R[find(v)]=R[find(RR+1)];
}
if(RR==R[find(v)] && LL==L[find(v)])break;
}
vis[v]=2;
}
int main(){
//factinit();
BOOST;
int n;
cin>>n;
for(int i=1; i<n; i++){
cin>>co[i];
}
for(int i=1; i<=n; i++){
int k;
cin>>k;
for(int j=0; j<k; j++){
int x;
cin>>x;
gdzie[x].pb(i);
}
}
for(int i=1; i<=n; i++){
if(!vis[i])dfs(i);
}
for(int i=1; i<=n; i++){
//deb(find(i), L[find(i)], R[find(i)]);
L[i]=L[find(i)];
R[i]=R[find(i)];
}
int q;
cin>>q;
while(q--){
int x, y;
cin>>x>>y;
if(y>R[x] || y<L[x]){
cout<<"NO\n";
}
else cout<<"YES\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16852 KB |
Output is correct |
2 |
Correct |
11 ms |
16980 KB |
Output is correct |
3 |
Correct |
11 ms |
17236 KB |
Output is correct |
4 |
Correct |
10 ms |
16852 KB |
Output is correct |
5 |
Correct |
10 ms |
16980 KB |
Output is correct |
6 |
Correct |
10 ms |
16824 KB |
Output is correct |
7 |
Correct |
10 ms |
16852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16852 KB |
Output is correct |
2 |
Correct |
11 ms |
16980 KB |
Output is correct |
3 |
Correct |
11 ms |
17236 KB |
Output is correct |
4 |
Correct |
10 ms |
16852 KB |
Output is correct |
5 |
Correct |
10 ms |
16980 KB |
Output is correct |
6 |
Correct |
10 ms |
16824 KB |
Output is correct |
7 |
Correct |
10 ms |
16852 KB |
Output is correct |
8 |
Correct |
104 ms |
18376 KB |
Output is correct |
9 |
Correct |
87 ms |
18356 KB |
Output is correct |
10 |
Correct |
89 ms |
18636 KB |
Output is correct |
11 |
Correct |
92 ms |
19020 KB |
Output is correct |
12 |
Correct |
80 ms |
18500 KB |
Output is correct |
13 |
Correct |
84 ms |
18504 KB |
Output is correct |
14 |
Correct |
84 ms |
18560 KB |
Output is correct |
15 |
Correct |
89 ms |
18660 KB |
Output is correct |
16 |
Correct |
90 ms |
19192 KB |
Output is correct |
17 |
Correct |
84 ms |
18456 KB |
Output is correct |
18 |
Correct |
85 ms |
18636 KB |
Output is correct |
19 |
Correct |
84 ms |
18572 KB |
Output is correct |
20 |
Correct |
83 ms |
18936 KB |
Output is correct |
21 |
Correct |
84 ms |
19020 KB |
Output is correct |
22 |
Correct |
87 ms |
18700 KB |
Output is correct |
23 |
Correct |
86 ms |
18480 KB |
Output is correct |
24 |
Correct |
87 ms |
18428 KB |
Output is correct |
25 |
Correct |
86 ms |
18384 KB |
Output is correct |
26 |
Correct |
91 ms |
18488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
23712 KB |
Output is correct |
2 |
Correct |
173 ms |
24444 KB |
Output is correct |
3 |
Correct |
169 ms |
27056 KB |
Output is correct |
4 |
Correct |
172 ms |
23884 KB |
Output is correct |
5 |
Correct |
176 ms |
24208 KB |
Output is correct |
6 |
Correct |
150 ms |
26572 KB |
Output is correct |
7 |
Correct |
144 ms |
31992 KB |
Output is correct |
8 |
Correct |
144 ms |
32556 KB |
Output is correct |
9 |
Correct |
139 ms |
32324 KB |
Output is correct |
10 |
Correct |
145 ms |
32788 KB |
Output is correct |
11 |
Correct |
143 ms |
32728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16852 KB |
Output is correct |
2 |
Correct |
11 ms |
16980 KB |
Output is correct |
3 |
Correct |
11 ms |
17236 KB |
Output is correct |
4 |
Correct |
10 ms |
16852 KB |
Output is correct |
5 |
Correct |
10 ms |
16980 KB |
Output is correct |
6 |
Correct |
10 ms |
16824 KB |
Output is correct |
7 |
Correct |
10 ms |
16852 KB |
Output is correct |
8 |
Correct |
104 ms |
18376 KB |
Output is correct |
9 |
Correct |
87 ms |
18356 KB |
Output is correct |
10 |
Correct |
89 ms |
18636 KB |
Output is correct |
11 |
Correct |
92 ms |
19020 KB |
Output is correct |
12 |
Correct |
80 ms |
18500 KB |
Output is correct |
13 |
Correct |
84 ms |
18504 KB |
Output is correct |
14 |
Correct |
84 ms |
18560 KB |
Output is correct |
15 |
Correct |
89 ms |
18660 KB |
Output is correct |
16 |
Correct |
90 ms |
19192 KB |
Output is correct |
17 |
Correct |
84 ms |
18456 KB |
Output is correct |
18 |
Correct |
85 ms |
18636 KB |
Output is correct |
19 |
Correct |
84 ms |
18572 KB |
Output is correct |
20 |
Correct |
83 ms |
18936 KB |
Output is correct |
21 |
Correct |
84 ms |
19020 KB |
Output is correct |
22 |
Correct |
87 ms |
18700 KB |
Output is correct |
23 |
Correct |
86 ms |
18480 KB |
Output is correct |
24 |
Correct |
87 ms |
18428 KB |
Output is correct |
25 |
Correct |
86 ms |
18384 KB |
Output is correct |
26 |
Correct |
91 ms |
18488 KB |
Output is correct |
27 |
Correct |
178 ms |
23712 KB |
Output is correct |
28 |
Correct |
173 ms |
24444 KB |
Output is correct |
29 |
Correct |
169 ms |
27056 KB |
Output is correct |
30 |
Correct |
172 ms |
23884 KB |
Output is correct |
31 |
Correct |
176 ms |
24208 KB |
Output is correct |
32 |
Correct |
150 ms |
26572 KB |
Output is correct |
33 |
Correct |
144 ms |
31992 KB |
Output is correct |
34 |
Correct |
144 ms |
32556 KB |
Output is correct |
35 |
Correct |
139 ms |
32324 KB |
Output is correct |
36 |
Correct |
145 ms |
32788 KB |
Output is correct |
37 |
Correct |
143 ms |
32728 KB |
Output is correct |
38 |
Correct |
229 ms |
35592 KB |
Output is correct |
39 |
Correct |
253 ms |
42444 KB |
Output is correct |
40 |
Correct |
216 ms |
37776 KB |
Output is correct |
41 |
Correct |
362 ms |
70460 KB |
Output is correct |
42 |
Correct |
146 ms |
29516 KB |
Output is correct |
43 |
Correct |
141 ms |
29404 KB |
Output is correct |
44 |
Correct |
212 ms |
35960 KB |
Output is correct |
45 |
Correct |
209 ms |
36300 KB |
Output is correct |
46 |
Correct |
221 ms |
36684 KB |
Output is correct |
47 |
Correct |
147 ms |
35048 KB |
Output is correct |
48 |
Correct |
137 ms |
34560 KB |
Output is correct |
49 |
Correct |
204 ms |
37744 KB |
Output is correct |
50 |
Correct |
216 ms |
40044 KB |
Output is correct |
51 |
Correct |
237 ms |
41448 KB |
Output is correct |
52 |
Correct |
169 ms |
39512 KB |
Output is correct |
53 |
Correct |
202 ms |
48248 KB |
Output is correct |
54 |
Correct |
234 ms |
52984 KB |
Output is correct |
55 |
Correct |
198 ms |
46324 KB |
Output is correct |