답안 #991239

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
991239 2024-06-01T15:51:05 Z _callmelucian Long Mansion (JOI17_long_mansion) C++14
100 / 100
598 ms 117700 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 5e5 + 5;
int spt[mn][19], req[mn], last[mn], reachLeft[mn], reachRight[mn], ans[mn];
vector<int> key[mn], violate[mn];

int spQry (int l, int r) {
    int p = 31 - __builtin_clz(r - l + 1);
    return max(spt[l][p], spt[r - (1 << p) + 1][p]);
}

struct query {
    int x, y, id;

    query() : x(0), y(0), id(0) {}
    query (int a, int b, int c) : x(a), y(b), id(c) {}

    bool operator < (const query &o) const {
        return x < o.x;
    }

    bool type() { return x < y; }

    query rever (int n) {
        return query(n - x + 1, n - y + 1, id);
    }
};

void solve (int n, vector<query> &qry) {
    // calculate reachLeft
    for (int i = 1; i <= n; i++) last[i] = 0;
    for (int i = 1; i <= n; i++) {
        reachLeft[i] = last[req[i]];
        for (int u : key[i]) last[u] = i;
    }
    reachLeft[n + 1] = INT_MIN;

    // calculate reachRight
    for (int i = 1; i <= n; i++) last[i] = n + 1;
    for (int i = n; i >= 1; i--) {
        reachRight[i] = last[req[i + 1]];
        for (int u : key[i]) last[u] = i;
    }
    reachRight[0] = INT_MAX;

    // build sparse table
    for (int i = 0; i <= n; i++) spt[i][0] = reachRight[i];
    for (int s = 1; (1 << s) <= n + 1; s++) {
        for (int i = 0; i + (1 << s) - 1 <= n; i++) {
            int p = s - 1;
            spt[i][s] = max(spt[i][p], spt[i + (1 << p)][p]);
        }
    }

    for (int i = 1; i <= n; i++) violate[i].clear();
    for (int i = 2; i <= n; i++) {
        int k = reachLeft[i];
        for (int step = n; step >= 1; step /= 2) {
            while (k + step < i && spQry(reachLeft[i], k + step - 1) < i) k += step;
        }
        //cout << "pos " << i << " first to violate " << k + 1 << "\n";
        violate[k + 1].push_back(i);
    }

    int pos = -1;
    priority_queue<int> pq;
    for (query cur : qry) {
        while (pos < cur.x) {
            pos++;
            for (int u : violate[pos]) pq.push(-u);
        }
        while (pq.size() && -pq.top() <= cur.x) pq.pop();
        int bound = (pq.size() ? -pq.top() : INT_MAX);
        //cout << "prep query " << cur.id << " " << bound << "\n";
        ans[cur.id] = (cur.y < bound ? 1 : 0);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    for (int i = 2; i <= n; i++) cin >> req[i];
    for (int i = 1; i <= n; i++) {
        int k; cin >> k;
        key[i] = vector<int>(k);
        for (int j = 0; j < k; j++) cin >> key[i][j];
    }

    int q; cin >> q;
    vector<query> ltr, rtl;

    for (int i = 0; i < q; i++) {
        int x, y; cin >> x >> y;
        query cur(x, y, i);
        if (cur.type()) ltr.push_back(cur);
        else rtl.push_back(cur.rever(n));
    }
    sort(all(ltr)); sort(all(rtl));

    solve(n, ltr);

    reverse(req + 1, req + 1 + n);
    reverse(key + 1, key + 1 + n);
    for (int i = n; i >= 1; i--)
        req[i] = req[i - 1];

    solve(n, rtl);

    for (int i = 0; i < q; i++)
        cout << (ans[i] ? "YES" : "NO") << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24408 KB Output is correct
2 Correct 12 ms 24412 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 11 ms 24240 KB Output is correct
7 Correct 12 ms 24412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24408 KB Output is correct
2 Correct 12 ms 24412 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 11 ms 24240 KB Output is correct
7 Correct 12 ms 24412 KB Output is correct
8 Correct 104 ms 38160 KB Output is correct
9 Correct 108 ms 38232 KB Output is correct
10 Correct 102 ms 38500 KB Output is correct
11 Correct 110 ms 38984 KB Output is correct
12 Correct 96 ms 37416 KB Output is correct
13 Correct 96 ms 38468 KB Output is correct
14 Correct 96 ms 38200 KB Output is correct
15 Correct 98 ms 38188 KB Output is correct
16 Correct 103 ms 38032 KB Output is correct
17 Correct 97 ms 38188 KB Output is correct
18 Correct 102 ms 38096 KB Output is correct
19 Correct 98 ms 38192 KB Output is correct
20 Correct 98 ms 38236 KB Output is correct
21 Correct 99 ms 37928 KB Output is correct
22 Correct 100 ms 38196 KB Output is correct
23 Correct 102 ms 38208 KB Output is correct
24 Correct 105 ms 38076 KB Output is correct
25 Correct 110 ms 37932 KB Output is correct
26 Correct 100 ms 38248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 172 ms 48044 KB Output is correct
2 Correct 182 ms 55312 KB Output is correct
3 Correct 175 ms 55496 KB Output is correct
4 Correct 176 ms 55456 KB Output is correct
5 Correct 198 ms 55476 KB Output is correct
6 Correct 165 ms 55204 KB Output is correct
7 Correct 168 ms 55116 KB Output is correct
8 Correct 159 ms 54920 KB Output is correct
9 Correct 154 ms 55076 KB Output is correct
10 Correct 147 ms 55052 KB Output is correct
11 Correct 151 ms 54884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24408 KB Output is correct
2 Correct 12 ms 24412 KB Output is correct
3 Correct 12 ms 24668 KB Output is correct
4 Correct 10 ms 24412 KB Output is correct
5 Correct 11 ms 24412 KB Output is correct
6 Correct 11 ms 24240 KB Output is correct
7 Correct 12 ms 24412 KB Output is correct
8 Correct 104 ms 38160 KB Output is correct
9 Correct 108 ms 38232 KB Output is correct
10 Correct 102 ms 38500 KB Output is correct
11 Correct 110 ms 38984 KB Output is correct
12 Correct 96 ms 37416 KB Output is correct
13 Correct 96 ms 38468 KB Output is correct
14 Correct 96 ms 38200 KB Output is correct
15 Correct 98 ms 38188 KB Output is correct
16 Correct 103 ms 38032 KB Output is correct
17 Correct 97 ms 38188 KB Output is correct
18 Correct 102 ms 38096 KB Output is correct
19 Correct 98 ms 38192 KB Output is correct
20 Correct 98 ms 38236 KB Output is correct
21 Correct 99 ms 37928 KB Output is correct
22 Correct 100 ms 38196 KB Output is correct
23 Correct 102 ms 38208 KB Output is correct
24 Correct 105 ms 38076 KB Output is correct
25 Correct 110 ms 37932 KB Output is correct
26 Correct 100 ms 38248 KB Output is correct
27 Correct 172 ms 48044 KB Output is correct
28 Correct 182 ms 55312 KB Output is correct
29 Correct 175 ms 55496 KB Output is correct
30 Correct 176 ms 55456 KB Output is correct
31 Correct 198 ms 55476 KB Output is correct
32 Correct 165 ms 55204 KB Output is correct
33 Correct 168 ms 55116 KB Output is correct
34 Correct 159 ms 54920 KB Output is correct
35 Correct 154 ms 55076 KB Output is correct
36 Correct 147 ms 55052 KB Output is correct
37 Correct 151 ms 54884 KB Output is correct
38 Correct 598 ms 102932 KB Output is correct
39 Correct 532 ms 117036 KB Output is correct
40 Correct 351 ms 87136 KB Output is correct
41 Correct 332 ms 117700 KB Output is correct
42 Correct 171 ms 56188 KB Output is correct
43 Correct 169 ms 56272 KB Output is correct
44 Correct 215 ms 74072 KB Output is correct
45 Correct 204 ms 74240 KB Output is correct
46 Correct 216 ms 74284 KB Output is correct
47 Correct 159 ms 56104 KB Output is correct
48 Correct 153 ms 55852 KB Output is correct
49 Correct 204 ms 73980 KB Output is correct
50 Correct 190 ms 74136 KB Output is correct
51 Correct 205 ms 74524 KB Output is correct
52 Correct 244 ms 69596 KB Output is correct
53 Correct 310 ms 84076 KB Output is correct
54 Correct 381 ms 98472 KB Output is correct
55 Correct 323 ms 84140 KB Output is correct