답안 #829764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
829764 2023-08-18T14:50:14 Z vjudge1 Long Mansion (JOI17_long_mansion) C++17
0 / 100
241 ms 104548 KB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 5e5 + 10;

template<typename T> 
struct SparseTable {
    vector<vector<T>> st;
    T (*F) (T, T);
    int len;
    void init(vector<T> &a, T(*f)(T, T)) {
        len = (int)a.size();
        int mxpow = 32 - __builtin_clz(len);
        F = f;
        st.resize(mxpow);
        st[0] = a;
        for(int k = 1; k < mxpow; k++) {
            st[k].resize(len - (1 << k) + 1);
            for(int i = 0; i < (int)st[k].size(); i++) 
                st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
        }
    }
    T get(int l, int r) {
        if(l > r || l < 0 || r >= len) return -1;
        int k = 31 - __builtin_clz(r - l + 1);
        return F(st[k][l], st[k][r - (1 << k) + 1]);
    }
};

int n;
int C[N];
vector<int> keys[N];
vector<int> Rb(N), Lb(N);

void precalc() {
    vector<int> lst(N, -1);
    for(int i = 1; i < n; i++) {
        for(int x : keys[i]) lst[x] = i;
        Lb[i] = lst[C[i]];
    }
    lst.assign(N, N);
    for(int i = n; i > 1; i--) {
        for(int x : keys[i]) lst[x] = i;
        Rb[i - 1] = lst[C[i - 1]];
    }
}

SparseTable<int> mn, mx;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1; i < n; i++) 
        cin >> C[i];
    for(int i = 1; i <= n; i++) {
        int k;
        cin >> k;
        keys[i].resize(k);
        for(int &c : keys[i]) {
            cin >> c;
        }
    }
    precalc();
    mn.init(Lb, [](int x, int y){return (x < y ? x : y);});
    mx.init(Rb, [](int x, int y){return (x > y ? x : y);});
    vector<int> G(n + 1), T(n + 1);
    // for(int i = 1; i < n; i++) {
    //     cout << Lb[i] << ' ';
    // }
    // cout << '\n';
    // for(int i = 1; i < n; i++) {
    //     cout << Rb[i] << ' ';
    // }
    // cout << '\n';
    for(int i = 1; i < n; i++) {
        int l = max(Lb[i] - 1, 0), r = i;
        while(r - l > 1) {
            int mid = (l + r) / 2;
            if(mx.get(max(Lb[i], 1), mid - 1) > i) r = mid;
            else l = mid;
        }
        T[i] = r;
        l = i, r = min(Rb[i], n + 1);
        while(r - l > 1) {
            int mid = (l + r) / 2;
            if(mn.get(mid, min(Rb[i] - 1, n)) <= i) l = mid;
            else r = mid;
        }
        G[i] = l;
    }
    // for(int i = 1; i < n; i++) {
    //     cout << G[i] << ' ';
    // }
    // cout << '\n';
    // for(int i = 1; i < n; i++) {
    //     cout << T[i] << ' ';
    // }
    // cout << '\n';
    
    SparseTable<int> gg, tt;
    gg.init(G, [](int x, int y){return (x > y ? x : y);});
    tt.init(T, [](int x, int y){return (x < y ? x : y);});

    int q;
    cin >> q;
    while(q--) {
        int s, t;
        cin >> s >> t;
        if(s < t) {
            cout << ((tt.get(s, t - 1) >= s && mn.get(s, t - 1) >= 1) ? "YES\n" : "NO\n");
        } else {
            cout << ((gg.get(t, s - 1) >= s || mx.get(t, s - 1) > n) ? "NO\n" : "YES\n");
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 72 ms 86492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 72 ms 86492 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 241 ms 104548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 72 ms 86492 KB Output isn't correct
2 Halted 0 ms 0 KB -