#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)]);
}
int q;
cin>>q;
while(q--){
int x, y;
cin>>x>>y;
if(y>R[find(x)] || y<L[find(x)]){
cout<<"NO\n";
}
else cout<<"YES\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
16972 KB |
Output is correct |
2 |
Correct |
47 ms |
17064 KB |
Output is correct |
3 |
Correct |
78 ms |
17404 KB |
Output is correct |
4 |
Correct |
38 ms |
16984 KB |
Output is correct |
5 |
Correct |
40 ms |
17108 KB |
Output is correct |
6 |
Correct |
36 ms |
17032 KB |
Output is correct |
7 |
Correct |
36 ms |
16980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
16972 KB |
Output is correct |
2 |
Correct |
47 ms |
17064 KB |
Output is correct |
3 |
Correct |
78 ms |
17404 KB |
Output is correct |
4 |
Correct |
38 ms |
16984 KB |
Output is correct |
5 |
Correct |
40 ms |
17108 KB |
Output is correct |
6 |
Correct |
36 ms |
17032 KB |
Output is correct |
7 |
Correct |
36 ms |
16980 KB |
Output is correct |
8 |
Correct |
833 ms |
22752 KB |
Output is correct |
9 |
Correct |
811 ms |
22744 KB |
Output is correct |
10 |
Correct |
873 ms |
23304 KB |
Output is correct |
11 |
Correct |
875 ms |
24044 KB |
Output is correct |
12 |
Correct |
828 ms |
22380 KB |
Output is correct |
13 |
Correct |
845 ms |
22936 KB |
Output is correct |
14 |
Correct |
827 ms |
23056 KB |
Output is correct |
15 |
Correct |
840 ms |
23076 KB |
Output is correct |
16 |
Correct |
817 ms |
23032 KB |
Output is correct |
17 |
Correct |
890 ms |
23012 KB |
Output is correct |
18 |
Correct |
829 ms |
22972 KB |
Output is correct |
19 |
Correct |
890 ms |
23192 KB |
Output is correct |
20 |
Correct |
824 ms |
23292 KB |
Output is correct |
21 |
Correct |
843 ms |
23040 KB |
Output is correct |
22 |
Correct |
807 ms |
23068 KB |
Output is correct |
23 |
Correct |
815 ms |
22896 KB |
Output is correct |
24 |
Correct |
827 ms |
22928 KB |
Output is correct |
25 |
Correct |
830 ms |
22860 KB |
Output is correct |
26 |
Correct |
812 ms |
22816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1879 ms |
36244 KB |
Output is correct |
2 |
Correct |
1856 ms |
36936 KB |
Output is correct |
3 |
Correct |
1854 ms |
39272 KB |
Output is correct |
4 |
Correct |
1827 ms |
36436 KB |
Output is correct |
5 |
Correct |
1857 ms |
36780 KB |
Output is correct |
6 |
Correct |
1821 ms |
38124 KB |
Output is correct |
7 |
Correct |
1845 ms |
42832 KB |
Output is correct |
8 |
Correct |
1810 ms |
43300 KB |
Output is correct |
9 |
Correct |
1808 ms |
43232 KB |
Output is correct |
10 |
Correct |
1795 ms |
43648 KB |
Output is correct |
11 |
Correct |
1847 ms |
43540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
16972 KB |
Output is correct |
2 |
Correct |
47 ms |
17064 KB |
Output is correct |
3 |
Correct |
78 ms |
17404 KB |
Output is correct |
4 |
Correct |
38 ms |
16984 KB |
Output is correct |
5 |
Correct |
40 ms |
17108 KB |
Output is correct |
6 |
Correct |
36 ms |
17032 KB |
Output is correct |
7 |
Correct |
36 ms |
16980 KB |
Output is correct |
8 |
Correct |
833 ms |
22752 KB |
Output is correct |
9 |
Correct |
811 ms |
22744 KB |
Output is correct |
10 |
Correct |
873 ms |
23304 KB |
Output is correct |
11 |
Correct |
875 ms |
24044 KB |
Output is correct |
12 |
Correct |
828 ms |
22380 KB |
Output is correct |
13 |
Correct |
845 ms |
22936 KB |
Output is correct |
14 |
Correct |
827 ms |
23056 KB |
Output is correct |
15 |
Correct |
840 ms |
23076 KB |
Output is correct |
16 |
Correct |
817 ms |
23032 KB |
Output is correct |
17 |
Correct |
890 ms |
23012 KB |
Output is correct |
18 |
Correct |
829 ms |
22972 KB |
Output is correct |
19 |
Correct |
890 ms |
23192 KB |
Output is correct |
20 |
Correct |
824 ms |
23292 KB |
Output is correct |
21 |
Correct |
843 ms |
23040 KB |
Output is correct |
22 |
Correct |
807 ms |
23068 KB |
Output is correct |
23 |
Correct |
815 ms |
22896 KB |
Output is correct |
24 |
Correct |
827 ms |
22928 KB |
Output is correct |
25 |
Correct |
830 ms |
22860 KB |
Output is correct |
26 |
Correct |
812 ms |
22816 KB |
Output is correct |
27 |
Correct |
1879 ms |
36244 KB |
Output is correct |
28 |
Correct |
1856 ms |
36936 KB |
Output is correct |
29 |
Correct |
1854 ms |
39272 KB |
Output is correct |
30 |
Correct |
1827 ms |
36436 KB |
Output is correct |
31 |
Correct |
1857 ms |
36780 KB |
Output is correct |
32 |
Correct |
1821 ms |
38124 KB |
Output is correct |
33 |
Correct |
1845 ms |
42832 KB |
Output is correct |
34 |
Correct |
1810 ms |
43300 KB |
Output is correct |
35 |
Correct |
1808 ms |
43232 KB |
Output is correct |
36 |
Correct |
1795 ms |
43648 KB |
Output is correct |
37 |
Correct |
1847 ms |
43540 KB |
Output is correct |
38 |
Execution timed out |
3070 ms |
50500 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |