Submission #241666

#TimeUsernameProblemLanguageResultExecution timeMemory
241666osaaateiasavtnlLong Mansion (JOI17_long_mansion)C++17
10 / 100
3096 ms11512 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif


    int n;
    cin >> n;

    vector <int> c(n-1);
    for (int i = 0; i < n - 1; ++i)
        cin >> c[i];

    vector <vector <int> > a(n);
    for (int i = 0; i < n; ++i) {
        int k;
        cin >> k;
        a[i].resize(k);
        for (int j = 0; j < k; ++j)
            cin >> a[i][j];
    }   

    vector <int> l(n), r(n);
    for (int i = 0; i < n; ++i) {
        l[i] = r[i] = i;

        set <int> cur;
        for (auto e : a[i])
            cur.insert(e);

        while (1) {
            if (l[i] && cur.find(c[l[i]-1]) != cur.end()) {
                --l[i];
                for (auto e : a[l[i]])
                    cur.insert(e);
            }   
            else if (r[i] < n - 1 && cur.find(c[r[i]]) != cur.end()) {
                ++r[i];
                for (auto e : a[r[i]])
                    cur.insert(e);
            }   
            else {
                break;
            }   
        }   
    }   

    int q;
    cin >> q;
    while (q--) {
        int i, j;
        cin >> i >> j;
        --i; --j;
        if (l[i] <= j && j <= r[i]) {
            cout << "YES" << endl;
        }   
        else {
            cout << "NO" << endl;
        }   
    }   
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...