답안 #114064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114064 2019-05-29T18:51:09 Z popovicirobert Long Mansion (JOI17_long_mansion) C++14
100 / 100
1434 ms 112424 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

vector <char> lg;

inline int get_mx(int x, int y, vector < vector <int> > &rmq, vector <int> &arr) {
    int bit = lg[y - x + 1];
    return max(arr[rmq[x][bit]], arr[rmq[y - (1 << bit) + 1][bit]]);
}

inline int get_mn(int x, int y, vector < vector <int> > &rmq, vector <int> &arr) {
    int bit = lg[y - x + 1];
    return min(arr[rmq[x][bit]], arr[rmq[y - (1 << bit) + 1][bit]]);
}

vector <bool> sol;
int n, q;

inline void solve(vector <int> &c, vector < vector <int> > &key, vector < pair <int, int> > &qry) {

    vector <int> last(n + 1, -1), prv(n + 1, -1), nxt(n + 1, n + 1);
    int i;

    for(i = 1; i < n; i++) {
        for(auto it : key[i]) {
            last[it] = i;
        }
        prv[i + 1] = last[c[i]];
    }

    fill(last.begin(), last.end(), n + 1);

    for(i = n - 1; i >= 1; i--) {
        for(auto it : key[i + 1]) {
            last[it] = i + 1;
        }
        nxt[i] = last[c[i]];
    }

    vector < vector <int> > rmq(n + 1, vector <int>(19));
    for(i = 1; i <= n; i++) {
        rmq[i][0] = i;
    }

    for(int bit = 1; (1 << bit) <= n; bit++) {
        for(i = 1; i + (1 << bit) <= n + 1; i++) {
            int x = rmq[i][bit - 1], y = rmq[i + (1 << (bit - 1))][bit - 1];

            if(nxt[x] > nxt[y]) {
                rmq[i][bit] = x;
            }
            else {
                rmq[i][bit] = y;
            }
        }
    }

    vector <int> len(n + 1);

    for(i = 1; i <= n; i++) {
        if(prv[i] == -1) {
            len[i] = -1;
            continue;
        }

        int res = prv[i] - 1;
        for(int step = 1 << 18; step; step >>= 1) {
            if(res + step <= n && get_mx(prv[i], res + step, rmq, nxt) < i) {
                res += step;
            }
        }

        len[i] = res + 1;

    }

    for(int bit = 1; (1 << bit) <= n; bit++) {
        for(i = 1; i + (1 << bit) <= n + 1; i++) {
            int x = rmq[i][bit - 1], y = rmq[i + (1 << (bit - 1))][bit - 1];

            if(len[x] < len[y]) {
                rmq[i][bit] = x;
            }
            else {
                rmq[i][bit] = y;
            }
        }
    }


    for(i = 1; i <= q; i++) {
        int x = qry[i].first, y = qry[i].second;

        if(x > y) {
            continue;
        }

        sol[i] = (get_mn(x + 1, y, rmq, len) >= x);

    }
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    lg.resize(n + 1);
    for(i = 2; i <= n; i++) {
        lg[i] = lg[i >> 1] + 1;
    }

    vector <int> c(n);

    for(i = 1; i < n; i++) {
        cin >> c[i];
    }

    vector < vector <int> > key(n + 1);

    for(i = 1; i <= n; i++) {
        int x;
        cin >> x;

        while(x > 0) {
            x--;
            int id;
            cin >> id;
            key[i].push_back(id);
        }
    }

    cin >> q;

    vector < pair <int, int> > qry(q + 1);

    for(i = 1; i <= q; i++) {
        int x, y;
        cin >> x >> y;
        qry[i] = {x, y};
    }

    sol.resize(q + 1);

    solve(c, key, qry);

    for(i = 1; i <= q; i++) {
        qry[i].first = n - qry[i].first + 1;
        qry[i].second = n - qry[i].second + 1;
    }
    for(i = 1; i < n - i; i++) {
        swap(c[i], c[n - i]);
    }
    for(i = 1; i < n - i + 1; i++) {
        swap(key[i], key[n - i + 1]);
    }

    solve(c, key, qry);

    for(i = 1; i <= q; i++) {
        if(sol[i] == 0) {
            cout << "NO\n";
        }
        else {
            cout << "YES\n";
        }
    }

    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 768 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 8 ms 1408 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 768 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 8 ms 1408 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
8 Correct 104 ms 6100 KB Output is correct
9 Correct 102 ms 6112 KB Output is correct
10 Correct 106 ms 6364 KB Output is correct
11 Correct 110 ms 6600 KB Output is correct
12 Correct 96 ms 6268 KB Output is correct
13 Correct 98 ms 6368 KB Output is correct
14 Correct 94 ms 6368 KB Output is correct
15 Correct 99 ms 6356 KB Output is correct
16 Correct 97 ms 6624 KB Output is correct
17 Correct 95 ms 6240 KB Output is correct
18 Correct 93 ms 6368 KB Output is correct
19 Correct 104 ms 6340 KB Output is correct
20 Correct 98 ms 6368 KB Output is correct
21 Correct 97 ms 6612 KB Output is correct
22 Correct 101 ms 6368 KB Output is correct
23 Correct 99 ms 6112 KB Output is correct
24 Correct 104 ms 6244 KB Output is correct
25 Correct 100 ms 6240 KB Output is correct
26 Correct 100 ms 6244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 323 ms 24536 KB Output is correct
2 Correct 345 ms 24336 KB Output is correct
3 Correct 301 ms 24248 KB Output is correct
4 Correct 302 ms 24488 KB Output is correct
5 Correct 329 ms 24432 KB Output is correct
6 Correct 305 ms 23848 KB Output is correct
7 Correct 266 ms 29816 KB Output is correct
8 Correct 281 ms 29692 KB Output is correct
9 Correct 277 ms 29752 KB Output is correct
10 Correct 266 ms 29868 KB Output is correct
11 Correct 275 ms 29736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 768 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 8 ms 1408 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 5 ms 768 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 4 ms 896 KB Output is correct
8 Correct 104 ms 6100 KB Output is correct
9 Correct 102 ms 6112 KB Output is correct
10 Correct 106 ms 6364 KB Output is correct
11 Correct 110 ms 6600 KB Output is correct
12 Correct 96 ms 6268 KB Output is correct
13 Correct 98 ms 6368 KB Output is correct
14 Correct 94 ms 6368 KB Output is correct
15 Correct 99 ms 6356 KB Output is correct
16 Correct 97 ms 6624 KB Output is correct
17 Correct 95 ms 6240 KB Output is correct
18 Correct 93 ms 6368 KB Output is correct
19 Correct 104 ms 6340 KB Output is correct
20 Correct 98 ms 6368 KB Output is correct
21 Correct 97 ms 6612 KB Output is correct
22 Correct 101 ms 6368 KB Output is correct
23 Correct 99 ms 6112 KB Output is correct
24 Correct 104 ms 6244 KB Output is correct
25 Correct 100 ms 6240 KB Output is correct
26 Correct 100 ms 6244 KB Output is correct
27 Correct 323 ms 24536 KB Output is correct
28 Correct 345 ms 24336 KB Output is correct
29 Correct 301 ms 24248 KB Output is correct
30 Correct 302 ms 24488 KB Output is correct
31 Correct 329 ms 24432 KB Output is correct
32 Correct 305 ms 23848 KB Output is correct
33 Correct 266 ms 29816 KB Output is correct
34 Correct 281 ms 29692 KB Output is correct
35 Correct 277 ms 29752 KB Output is correct
36 Correct 266 ms 29868 KB Output is correct
37 Correct 275 ms 29736 KB Output is correct
38 Correct 1278 ms 93228 KB Output is correct
39 Correct 1434 ms 112424 KB Output is correct
40 Correct 900 ms 72804 KB Output is correct
41 Correct 1342 ms 110480 KB Output is correct
42 Correct 288 ms 31096 KB Output is correct
43 Correct 282 ms 31100 KB Output is correct
44 Correct 616 ms 53212 KB Output is correct
45 Correct 585 ms 53208 KB Output is correct
46 Correct 556 ms 53488 KB Output is correct
47 Correct 284 ms 31160 KB Output is correct
48 Correct 273 ms 30840 KB Output is correct
49 Correct 570 ms 53140 KB Output is correct
50 Correct 590 ms 53076 KB Output is correct
51 Correct 552 ms 53568 KB Output is correct
52 Correct 515 ms 52088 KB Output is correct
53 Correct 853 ms 73328 KB Output is correct
54 Correct 1105 ms 94288 KB Output is correct
55 Correct 850 ms 73496 KB Output is correct