#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct event
{
    int type,a,b;
};
int key[500'002];
int rep_[500'002];
int LC[500'002];
int RC[500'002];
bool is_dead[500'002];
set<int> keys[500'001];
pii ans[500'001];
set<int> unset;
queue<event> que;
void upd_seg(int x)
{
    x = rep_[x];
    int l = LC[x];
    int r = RC[x];
    bool is_l = is_dead[x] || (keys[x].find(key[l-1]) != keys[x].end());
    bool is_r = is_dead[x] || (keys[x].find(key[r]) != keys[x].end());
    if(!is_l && !is_r)
    {
        que.push({-1,x,x});
    }
    else
    {
        if(is_l && ((is_dead[rep_[l-1]] && !is_dead[x]) || keys[rep_[l-1]].find(key[l-1]) != keys[rep_[l-1]].end()))
        {
            que.push({1,l-1,l});
        }
        if(is_r && ((is_dead[rep_[r+1]] && !is_dead[x]) || keys[rep_[r+1]].find(key[r]) != keys[rep_[r+1]].end()))
        {
            que.push({1,r,r+1});
        }
    }
}
void merge_segs(int a, int b)
{
    if(rep_[a] == rep_[b]) return;
    a = rep_[a];
    b = rep_[b];
    if(!is_dead[a] && !is_dead[b])
    {
        if(RC[a]+1 == LC[b])
        {
            if(keys[a].find(key[RC[a]]) == keys[a].end() || keys[b].find(key[RC[a]]) == keys[b].end()) return;
        }
        else
        {
            if(keys[b].find(key[RC[b]]) == keys[b].end() || keys[a].find(key[RC[b]]) == keys[a].end()) return;
        }
    }
    if((RC[a] - LC[a]) > (RC[b] - LC[b])) swap(a,b);
    rep2(i,LC[a],RC[a]) rep_[i] = b;
    forall(it,keys[a]) keys[b].insert(it);
    LC[b] = min(LC[b],LC[a]); 
    RC[b] = max(RC[b],RC[a]); 
    is_dead[b] = 0;
    upd_seg(b);
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    cin >> n;
    rep2(i,1,n) rep_[i] = i;
    rep_[n+1] = n+1;
    rep2(i,1,n) unset.insert(i);
    rep2(i,1,n-1) cin >> key[i];
    key[0] = -1;
    key[n] = -1;
    rep2(i,1,n)
    {
        LC[i] = i;
        RC[i] = i;
        int b;
        cin >> b;
        rep(j,b)
        {
            int x;
            cin >> x;
            keys[i].insert(x);
        }
    }
    rep2(i,1,n) upd_seg(i);
    while(!que.empty())
    {
        event t = que.front();
        que.pop();
        if(t.type == 1) merge_segs(t.a,t.b);
        else
        {
            int c = rep_[t.a];
            while(true)
            {
                auto it = unset.lower_bound(LC[c]);
                if(it == unset.end() || (*it) > RC[c]) break;
                ans[*it] = {LC[c],RC[c]};
                unset.erase(it);
            }
            is_dead[c] = 1;
            upd_seg(c);
        }
    }
    int q;
    cin >> q;
    rep(qq,q)
    {
        int a,b;
        cin >> a >> b;
        if(ans[a].ff <= b && ans[a].ss >= b) cout << "YES\n";
        else cout << "NO\n";
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |