Submission #717191

# Submission time Handle Problem Language Result Execution time Memory
717191 2023-04-01T12:35:28 Z fatemetmhr Long Mansion (JOI17_long_mansion) C++17
100 / 100
479 ms 66684 KB
// ~ Be Name Khoda ~

#include <bits/stdc++.h>
//#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,O3")

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second

const int maxn  =  1e6   + 10;
const int maxn5 =  1e6   + 10;
const int maxnt =  4e6   + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;

int n, c[maxn5], nxt[maxn5], pre[maxn5];
int l[maxn5], r[maxn5], last[maxn5];
vector <int> av[maxn5], good;
pair <int, int> mn[maxnt];

void upd(int l, int r, int id, int val, int v){
    if(r - l == 1){
        mn[v] = {val, id};
        return;
    }
    int mid = (l + r) >> 1;
    if(id < mid)
        upd(l, mid, id, val, v * 2);
    else
        upd(mid, r, id, val, v * 2 + 1);
    mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
}

int get_first(int l, int r, int lq, int rq, int val, int v){
    if(rq <= l || r <= lq || mn[v].fi > val)
        return n;
    if(r - l == 1)
        return l;
    int mid = (l + r) >> 1;
    int ans = get_first(l, mid, lq, rq, val, v * 2);
    if(ans >= n)
        ans = get_first(mid, r, lq, rq, val, v * 2 + 1);
    return ans;
}

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

    cin >> n;
    for(int i = 0; i < n - 1; i++)
        cin >> c[i];
    memset(last, -1, sizeof last);
    for(int i = 0; i < n; i++){
        int b; cin >> b;
        while(b--){
            int a; cin >> a;
            av[i].pb(a);
            last[a] = i;
        }
        if(i < n - 1)
            pre[i] = last[c[i]];
    }
    fill(last, last + n + 5, n);
    for(int i = n - 1; i; i--){
        for(auto u : av[i])
            last[u] = i;
        nxt[i - 1] = last[c[i - 1]];       
    }
    fill(r, r + n + 5, n - 1);
    for(int i = 0; i < n - 1; i++)
        upd(0, n, i, pre[i], 1);
    upd(0, n, n - 1, -1, 1);
    good.pb(-1);
    int curr = get_first(0, n, 0, n, -1, 1);
    for(int i = 0; i < n; i++){
        l[i] = good.back() + 1;
        r[i] = curr;
        //cout << "ok " << i << ' ' << l[i] << ' ' << r[i] << ' ' << pre[i] << ' ' << nxt[i] << '\n';
        if(i == n - 1)
            break;
        upd(0, n, i, n + 5, 1);
        while(true){
            if(good.back() == -1){
                curr = get_first(0, n, 0, n, -1, 1);
                break;
            }
            int ind = get_first(0, n, 0, nxt[good.back()], good.back(), 1);
            if(ind < n){
                curr = ind;
                break;
            }
            good.pop_back();
        }
        int ind = get_first(0, n, 0, nxt[i], i, 1);
        //cout << "here " << i << ' ' << ind << '\n';
        if(ind < n){
            good.pb(i);
            curr = ind;
        }
    }

    int q; cin >> q;
    while(q--){
        int x, y; cin >> x >> y;
        x--; y--;
        cout << (l[x] <= y && y <= r[x] ? "YES" : "NO") << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 27912 KB Output is correct
2 Correct 16 ms 27980 KB Output is correct
3 Correct 18 ms 28188 KB Output is correct
4 Correct 17 ms 27924 KB Output is correct
5 Correct 17 ms 27936 KB Output is correct
6 Correct 17 ms 27932 KB Output is correct
7 Correct 17 ms 27988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 27912 KB Output is correct
2 Correct 16 ms 27980 KB Output is correct
3 Correct 18 ms 28188 KB Output is correct
4 Correct 17 ms 27924 KB Output is correct
5 Correct 17 ms 27936 KB Output is correct
6 Correct 17 ms 27932 KB Output is correct
7 Correct 17 ms 27988 KB Output is correct
8 Correct 113 ms 31020 KB Output is correct
9 Correct 109 ms 31080 KB Output is correct
10 Correct 125 ms 31264 KB Output is correct
11 Correct 113 ms 31564 KB Output is correct
12 Correct 117 ms 31200 KB Output is correct
13 Correct 104 ms 31252 KB Output is correct
14 Correct 101 ms 31296 KB Output is correct
15 Correct 118 ms 31316 KB Output is correct
16 Correct 108 ms 31524 KB Output is correct
17 Correct 104 ms 31184 KB Output is correct
18 Correct 112 ms 31284 KB Output is correct
19 Correct 107 ms 31236 KB Output is correct
20 Correct 106 ms 31380 KB Output is correct
21 Correct 111 ms 31676 KB Output is correct
22 Correct 115 ms 31280 KB Output is correct
23 Correct 117 ms 31188 KB Output is correct
24 Correct 147 ms 31116 KB Output is correct
25 Correct 115 ms 31152 KB Output is correct
26 Correct 115 ms 31048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 38828 KB Output is correct
2 Correct 234 ms 38796 KB Output is correct
3 Correct 251 ms 38724 KB Output is correct
4 Correct 228 ms 38768 KB Output is correct
5 Correct 277 ms 38772 KB Output is correct
6 Correct 192 ms 38348 KB Output is correct
7 Correct 215 ms 38528 KB Output is correct
8 Correct 177 ms 38548 KB Output is correct
9 Correct 216 ms 38468 KB Output is correct
10 Correct 186 ms 38568 KB Output is correct
11 Correct 186 ms 38560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 27912 KB Output is correct
2 Correct 16 ms 27980 KB Output is correct
3 Correct 18 ms 28188 KB Output is correct
4 Correct 17 ms 27924 KB Output is correct
5 Correct 17 ms 27936 KB Output is correct
6 Correct 17 ms 27932 KB Output is correct
7 Correct 17 ms 27988 KB Output is correct
8 Correct 113 ms 31020 KB Output is correct
9 Correct 109 ms 31080 KB Output is correct
10 Correct 125 ms 31264 KB Output is correct
11 Correct 113 ms 31564 KB Output is correct
12 Correct 117 ms 31200 KB Output is correct
13 Correct 104 ms 31252 KB Output is correct
14 Correct 101 ms 31296 KB Output is correct
15 Correct 118 ms 31316 KB Output is correct
16 Correct 108 ms 31524 KB Output is correct
17 Correct 104 ms 31184 KB Output is correct
18 Correct 112 ms 31284 KB Output is correct
19 Correct 107 ms 31236 KB Output is correct
20 Correct 106 ms 31380 KB Output is correct
21 Correct 111 ms 31676 KB Output is correct
22 Correct 115 ms 31280 KB Output is correct
23 Correct 117 ms 31188 KB Output is correct
24 Correct 147 ms 31116 KB Output is correct
25 Correct 115 ms 31152 KB Output is correct
26 Correct 115 ms 31048 KB Output is correct
27 Correct 241 ms 38828 KB Output is correct
28 Correct 234 ms 38796 KB Output is correct
29 Correct 251 ms 38724 KB Output is correct
30 Correct 228 ms 38768 KB Output is correct
31 Correct 277 ms 38772 KB Output is correct
32 Correct 192 ms 38348 KB Output is correct
33 Correct 215 ms 38528 KB Output is correct
34 Correct 177 ms 38548 KB Output is correct
35 Correct 216 ms 38468 KB Output is correct
36 Correct 186 ms 38568 KB Output is correct
37 Correct 186 ms 38560 KB Output is correct
38 Correct 413 ms 61148 KB Output is correct
39 Correct 479 ms 66684 KB Output is correct
40 Correct 357 ms 55624 KB Output is correct
41 Correct 448 ms 64880 KB Output is correct
42 Correct 181 ms 38556 KB Output is correct
43 Correct 181 ms 38288 KB Output is correct
44 Correct 272 ms 45484 KB Output is correct
45 Correct 272 ms 45512 KB Output is correct
46 Correct 264 ms 45424 KB Output is correct
47 Correct 176 ms 38300 KB Output is correct
48 Correct 194 ms 38476 KB Output is correct
49 Correct 270 ms 45440 KB Output is correct
50 Correct 272 ms 45520 KB Output is correct
51 Correct 273 ms 45388 KB Output is correct
52 Correct 285 ms 45636 KB Output is correct
53 Correct 373 ms 55288 KB Output is correct
54 Correct 457 ms 60332 KB Output is correct
55 Correct 403 ms 55100 KB Output is correct