제출 #1255102

#제출 시각아이디문제언어결과실행 시간메모리
1255102CrabCNHLong Mansion (JOI17_long_mansion)C++20
100 / 100
376 ms76276 KiB
#include <bits/stdc++.h>

#define task     "BriantheCrab"

#define int    long long
#define pii    pair <int, int>
#define fi     first
#define se     second
#define szf    sizeof
#define sz(s)  (int)((s).size())
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

template <class T> void minimize (T &t, T f) {if (t > f) t = f;}
template <class T> void maximize (T &t, T f) {if (t < f) t = f;}

const int maxN = 5e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;

// khong tu code thi khong kha len duoc dau
// biet sol roi thi tu lam not di

int n;
int c[maxN];
vector <int> pos[maxN];
int visited[maxN];
vector <int> tmp;

struct DSU {
    int par[maxN], sz[maxN];
    int l[maxN], r[maxN];
    
    inline void init () {
        for (int i = 1; i <= n; i ++) {
            par[i] = i;
            sz[i] = 1;
            l[i] = r[i] = i;
        }
    }
    
    int getRoot (int u) {
        if (par[u] == u) {
            return u;
        }
        return (par[u] = getRoot (par[u]));
    }
    
    inline void merge (int u, int v) {
        u = getRoot (u);
        v = getRoot (v);
        if (u == v) {
            return;
        }
        if (sz[u] < sz[v]) {
            swap (u, v);
        }
        minimize (l[u], l[v]);
        maximize (r[u], r[v]);
        sz[u] += sz[v];
        par[v] = u;
        return;
    }
} D;

bool valid (int cc, int L, int R) {
    auto it1 = lower_bound (all (pos[cc]), L) - pos[cc].begin ();
    auto it2 = upper_bound (all (pos[cc]), R) - pos[cc].begin ();
    // for (auto it : pos[cc]) {
        // cout << it << '\n';
    // }
    //cout << it1 << ' ' << it2 << ' ' << L << ' ' << R << '\n';
    return (it2 - it1 > 0);
}

void dfs (int u, int p) {
    //cout << u << '\n';
    int cur = D.getRoot (u);
    int par = D.getRoot (p);
    if (visited[cur] == 2) {
        if (cur != par) {
            minimize (D.l[par], D.l[cur]);
            maximize (D.r[par], D.r[cur]);
        }
        return;
    }
    if (visited[cur] == 1) {
        D.merge (cur, p);
        return;
    }
    visited[cur] = 1;
    while (true) {
        cur = D.getRoot (cur);
        par = D.getRoot (p);
        int L = D.l[cur], R = D.r[cur];
        bool cc = false;
        if (L >= 2 && valid (c[L - 1], L, R)) {
            dfs (L - 1, cur);
            cc = true;
        }
        cur = D.getRoot (cur);
        L = D.l[cur], R = D.r[cur];
        if (R <= n - 1 && valid (c[R], L, R)) {
            dfs (R + 1, cur);
            cc = true;
        }
        if (!cc) {
            break;
        }
    }
    visited[cur] = 2;
    return;
}

void solve () {
    cin >> n;
    for (int i = 1; i <= n - 1; i ++) {
        cin >> c[i];
    }
    for (int i = 1; i <= n; i ++) {
        int b;
        cin >> b;
        while (b --) {
            int x;
            cin >> x;
            pos[x].push_back (i);
        }
    }
    D.init ();
    for (int i = 1; i <= n; i ++) {
        dfs (i, i);
    }
    int q;
    cin >> q;
    for (int i = 1; i <= q; i ++) {
        int x, y;
        cin >> x >> y;
        x = D.getRoot (x);
        //cout << D.l[x] << ' ' << D.r[x] << '\n';
        if (D.l[x] <= y && y <= D.r[x]) {
            cout << "YES\n";
        }
        else {
            cout << "NO\n";
        }
    }
    return;
}

signed main () {
    cin.tie (nullptr) -> sync_with_stdio (false);
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    int t = 1;
    //cin >> t;
    while (t --) {
        solve ();
    } 
    return 0;
}
// thfv

컴파일 시 표준 에러 (stderr) 메시지

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:158:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:159:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...