Submission #45461

# Submission time Handle Problem Language Result Execution time Memory
45461 2018-04-14T07:54:26 Z choikiwon Long Mansion (JOI17_long_mansion) C++17
100 / 100
1609 ms 241772 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 500010;

int N, Q;
int C[MN];
vector<int> A[MN], B[MN];
int L[MN], R[MN], X[MN], Y[MN];

struct BIT {
    vector<int> tree;
    void init() {
        tree = vector<int>(4 * N, -1e9);
    }
    void upd(int idx, int val, int l, int r, int n) {
        if(idx < l || r < idx) return;
        if(l == r) {
            tree[n] = val;
            return;
        }
        int m = (l + r)>>1;
        upd(idx, val, l, m, 2*n);
        upd(idx, val, m + 1, r, 2*n + 1);
        tree[n] = max(tree[2*n], tree[2*n + 1]);
    }
    int quer(int a, int b, int l, int r, int n) {
        if(b < l || r < a) return -1e9;
        if(a <= l && r <= b) return tree[n];
        int m = (l + r)>>1;
        int L = quer(a, b, l, m, 2*n);
        int R = quer(a, b, m + 1, r, 2*n + 1);
        return max(L, R);
    }
} bit1, bit2, bit3, bit4;

vector<pii> ord;

int main() {
    scanf("%d", &N);

    for(int i = 0; i < N - 1; i++) {
        scanf("%d", &C[i]);
        C[i]--;
    }

    for(int i = 0; i < N; i++) {
        int k; scanf("%d", &k);
        A[i].resize(k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &A[i][j]);
            A[i][j]--;
        }
    }

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < A[i].size(); j++) {
            B[ A[i][j] ].push_back(i);
        }
    }
    for(int i = 0; i < N - 1; i++) {
        int s = 0, e = (int)B[ C[i] ].size() - 1, p = -1;
        while(s <= e) {
            int m = (s + e)>>1;

            if(B[ C[i] ][m] <= i) {
                p = B[ C[i] ][m];
                s = m + 1;
            }
            else e = m - 1;
        }
        L[i] = p;
    }
    for(int i = N - 1; i >= 1; i--) {
        int s = 0, e = (int)B[ C[i - 1] ].size() - 1, p = N;
        while(s <= e) {
            int m = (s + e)>>1;

            if(B[ C[i - 1] ][m] >= i) {
                p = B[ C[i - 1] ][m];
                e = m - 1;
            }
            else s = m + 1;
        }
        R[i] = p;
    }
    L[N - 1] = -1;
    R[0] = N;

    bit1.init();
    bit2.init();
    for(int i = 0; i < N; i++) {
        ord.push_back({ R[i], i });
    }
    sort(ord.begin(), ord.end());

    int pos = N - 1;
    for(int i = N - 2; i >= 0; i--) {
        while(pos >= 0 && ord[pos].first > i) {
            bit1.upd(ord[pos].second, -ord[pos].second, 0, N - 1, 1);
            pos--;
        }
        X[i] = -bit1.quer(L[i] + 1, i, 0, N - 1, 1);
    }

    ord.clear();
    for(int i = 0; i < N; i++) {
        ord.push_back({ L[i], i });
    }
    sort(ord.begin(), ord.end());

    pos = 0;
    for(int i = 1; i < N; i++) {
        while(pos < N && ord[pos].first < i) {
            bit2.upd(ord[pos].second, ord[pos].second, 0, N - 1, 1);
            pos++;
        }
        Y[i] = bit2.quer(i, R[i] - 1, 0, N - 1, 1);
    }

    bit3.init();
    bit4.init();
    for(int i = 0; i < N; i++) {
        bit3.upd(i, -X[i], 0, N - 1, 1);
        bit4.upd(i, Y[i], 0, N - 1, 1);
    }

    scanf("%d", &Q);

    for(int i = 0; i < Q; i++) {
        int x, y; scanf("%d %d", &x, &y);
        x--; y--;

        if(x < y) {
            if(-bit3.quer(x, y - 1, 0, N - 1, 1) <= x) printf("NO\n");
            else printf("YES\n");
        }
        else {
            if(bit4.quer(y + 1, x, 0, N - 1, 1) >= x) printf("NO\n");
            else printf("YES\n");
        }
    }
}

Compilation message

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:59:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 0; j < A[i].size(); j++) {
                        ~~^~~~~~~~~~~~~
long_mansion.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
long_mansion.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &C[i]);
         ~~~~~^~~~~~~~~~~~~
long_mansion.cpp:50:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int k; scanf("%d", &k);
                ~~~~~^~~~~~~~~~
long_mansion.cpp:53:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &A[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
long_mansion.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &Q);
     ~~~~~^~~~~~~~~~
long_mansion.cpp:133:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         int x, y; scanf("%d %d", &x, &y);
                   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24184 KB Output is correct
2 Correct 28 ms 24408 KB Output is correct
3 Correct 31 ms 24744 KB Output is correct
4 Correct 27 ms 24744 KB Output is correct
5 Correct 25 ms 24744 KB Output is correct
6 Correct 26 ms 24744 KB Output is correct
7 Correct 26 ms 24744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24184 KB Output is correct
2 Correct 28 ms 24408 KB Output is correct
3 Correct 31 ms 24744 KB Output is correct
4 Correct 27 ms 24744 KB Output is correct
5 Correct 25 ms 24744 KB Output is correct
6 Correct 26 ms 24744 KB Output is correct
7 Correct 26 ms 24744 KB Output is correct
8 Correct 321 ms 26196 KB Output is correct
9 Correct 315 ms 26196 KB Output is correct
10 Correct 337 ms 26196 KB Output is correct
11 Correct 346 ms 26660 KB Output is correct
12 Correct 346 ms 26660 KB Output is correct
13 Correct 259 ms 26660 KB Output is correct
14 Correct 287 ms 26660 KB Output is correct
15 Correct 271 ms 26660 KB Output is correct
16 Correct 260 ms 26660 KB Output is correct
17 Correct 258 ms 26660 KB Output is correct
18 Correct 268 ms 26660 KB Output is correct
19 Correct 263 ms 26660 KB Output is correct
20 Correct 268 ms 26660 KB Output is correct
21 Correct 268 ms 26660 KB Output is correct
22 Correct 271 ms 26660 KB Output is correct
23 Correct 253 ms 26660 KB Output is correct
24 Correct 282 ms 26660 KB Output is correct
25 Correct 246 ms 26660 KB Output is correct
26 Correct 255 ms 26660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 729 ms 40448 KB Output is correct
2 Correct 644 ms 40448 KB Output is correct
3 Correct 625 ms 40448 KB Output is correct
4 Correct 642 ms 40448 KB Output is correct
5 Correct 634 ms 40448 KB Output is correct
6 Correct 500 ms 40448 KB Output is correct
7 Correct 521 ms 40448 KB Output is correct
8 Correct 523 ms 40448 KB Output is correct
9 Correct 513 ms 40448 KB Output is correct
10 Correct 519 ms 40448 KB Output is correct
11 Correct 513 ms 40448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 24184 KB Output is correct
2 Correct 28 ms 24408 KB Output is correct
3 Correct 31 ms 24744 KB Output is correct
4 Correct 27 ms 24744 KB Output is correct
5 Correct 25 ms 24744 KB Output is correct
6 Correct 26 ms 24744 KB Output is correct
7 Correct 26 ms 24744 KB Output is correct
8 Correct 321 ms 26196 KB Output is correct
9 Correct 315 ms 26196 KB Output is correct
10 Correct 337 ms 26196 KB Output is correct
11 Correct 346 ms 26660 KB Output is correct
12 Correct 346 ms 26660 KB Output is correct
13 Correct 259 ms 26660 KB Output is correct
14 Correct 287 ms 26660 KB Output is correct
15 Correct 271 ms 26660 KB Output is correct
16 Correct 260 ms 26660 KB Output is correct
17 Correct 258 ms 26660 KB Output is correct
18 Correct 268 ms 26660 KB Output is correct
19 Correct 263 ms 26660 KB Output is correct
20 Correct 268 ms 26660 KB Output is correct
21 Correct 268 ms 26660 KB Output is correct
22 Correct 271 ms 26660 KB Output is correct
23 Correct 253 ms 26660 KB Output is correct
24 Correct 282 ms 26660 KB Output is correct
25 Correct 246 ms 26660 KB Output is correct
26 Correct 255 ms 26660 KB Output is correct
27 Correct 729 ms 40448 KB Output is correct
28 Correct 644 ms 40448 KB Output is correct
29 Correct 625 ms 40448 KB Output is correct
30 Correct 642 ms 40448 KB Output is correct
31 Correct 634 ms 40448 KB Output is correct
32 Correct 500 ms 40448 KB Output is correct
33 Correct 521 ms 40448 KB Output is correct
34 Correct 523 ms 40448 KB Output is correct
35 Correct 513 ms 40448 KB Output is correct
36 Correct 519 ms 40448 KB Output is correct
37 Correct 513 ms 40448 KB Output is correct
38 Correct 1387 ms 77380 KB Output is correct
39 Correct 1609 ms 100940 KB Output is correct
40 Correct 1219 ms 100940 KB Output is correct
41 Correct 1345 ms 120360 KB Output is correct
42 Correct 565 ms 120360 KB Output is correct
43 Correct 524 ms 120360 KB Output is correct
44 Correct 779 ms 120360 KB Output is correct
45 Correct 752 ms 120764 KB Output is correct
46 Correct 837 ms 131316 KB Output is correct
47 Correct 540 ms 131316 KB Output is correct
48 Correct 532 ms 131456 KB Output is correct
49 Correct 786 ms 155260 KB Output is correct
50 Correct 809 ms 165868 KB Output is correct
51 Correct 1022 ms 176956 KB Output is correct
52 Correct 950 ms 187716 KB Output is correct
53 Correct 1117 ms 213752 KB Output is correct
54 Correct 1215 ms 241772 KB Output is correct
55 Correct 886 ms 241772 KB Output is correct