Submission #47427

# Submission time Handle Problem Language Result Execution time Memory
47427 2018-05-02T21:46:08 Z qoo2p5 Long Mansion (JOI17_long_mansion) C++17
100 / 100
1089 ms 262144 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = (int) 1e9 + 1e6;
const ll LINF = (ll) 1e18 + 1e9;
const ll MOD = (ll) 1e9 + 7;

#define sz(x) (int) (x).size()
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb(s, t, x) (int) (lower_bound(s, t, x) - s)
#define ub(s, t, x) (int) (upper_bound(s, t, x) - s)
#define rep(i, f, t) for (auto i = f; i < t; ++(i))
#define per(i, f, t) for (auto i = (f); i >= (t); --(i))

ll power(ll x, ll y, ll mod = MOD) {
    if (y == 0) {
        return 1;
    }
    if (y & 1) {
        return power(x, y - 1, mod) * x % mod;
    } else {
        ll tmp = power(x, y / 2, mod);
        return tmp * tmp % mod;
    }
}

template<typename A, typename B> bool mini(A &x, const B &y) {
    if (y < x) {
        x = y;
        return true;
    }
    return false;
}

template<typename A, typename B> bool maxi(A &x, const B &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

void add(ll &x, ll y) {
    x += y;
    if (x >= MOD) x -= MOD;
    if (x < 0) x += MOD;
}

void run();

#define TASK ""

int main() {
#ifdef LOCAL
    if (strlen(TASK) > 0) {
        cerr << "Reminder: you are using file i/o, filename: " TASK "!" << endl << endl;
    }
#endif
#ifndef LOCAL
    if (strlen(TASK)) {
        freopen(TASK ".in", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    cout << fixed << setprecision(12);
    run();
    return 0;
}

// == SOLUTION == //

const int N = 1 << 19;

int n, q;
int c[N];
vector<int> what[N];
vector<int> color[N];
int L[N], R[N];

struct Q {
    int x, y, id;
};

bool ans[N];
Q qu[N];

int tree[2 * N];
int val[N];

void upd(int i, int val) {
    i += N;
    tree[i] = val;
    i /= 2;
    while (i > 0) {
        tree[i] = max(tree[2 * i], tree[2 * i + 1]);
        i /= 2;
    }
}

int get(int i, int tl, int tr, int l, int r) {
    if (tl >= r || tr <= l) {
        return -INF;
    }
    if (l <= tl && tr <= r) return tree[i];
    int tm = (tl + tr) / 2;
    return max(get(2 * i, tl, tm, l, r), get(2 * i + 1, tm, tr, l, r));
}

int find(int i, int tl, int tr, int l, int r, int val) {
    if (tl >= r || tr <= l) {
        return N;
    }
    int tm = (tl + tr) / 2;
    if (l <= tl && tr <= r) {
        if (tree[i] <= val) {
            return N;
        }
        if (tl == tr - 1) {
            return tl;
        }
        if (tree[2 * i] > val) {
            return find(2 * i, tl, tm, l, r, val);
        } else {
            return find(2 * i + 1, tm, tr, l, r, val);
        }
    }
    int res = find(2 * i, tl, tm, l, r, val);
    if (res != N) {
        return res;
    }
    return find(2 * i + 1, tm, tr, l, r, val);
}

void run() {
    cin >> n;
    rep(i, 0, n - 1) {
        cin >> c[i];
    }
    rep(i, 0, n) {
        int k;
        cin >> k;
        rep(j, 0, k) {
            int v;
            cin >> v;
            what[i].pb(v);
        }
    }
    cin >> q;
    rep(i, 0, q) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        qu[i] = {x, y, i};
    }
    
    rep(iter, 0, 2) {
        rep(i, 0, N) color[i].clear();
        rep(i, 0, N) color[i].pb(-INF);
        rep(i, 0, n) {
            for (int j : what[i]) {
                color[j].pb(i);
            }
        }
        rep(i, 0, N) color[i].pb(INF);
        
        L[0] = INF;
        R[n - 1] = -INF;
        rep(i, 0, n - 1) {
            int w = c[i];
            L[i + 1] = *lower_bound(all(color[w]), i + 1);
            R[i] = *(--lower_bound(all(color[w]), i + 1));
        }
        rep(i, 0, n) {
            upd(i, L[i]);
        }
        rep(i, 0, n) {
            val[i] = find(1, 0, N, R[i] + 1, i + 1, i);
        }
        rep(i, 0, n) {
            upd(i, -val[i]);
        }
        rep(i, 0, q) {
            if (qu[i].x < qu[i].y) {
                ans[qu[i].id] = -get(1, 0, N, qu[i].x, qu[i].y) > qu[i].x;
            }
        }
        
        per(i, n - 1, 0) {
            c[i] = c[i - 1];
        }
        reverse(c, c + n);
        reverse(what, what + n);
        rep(i, 0, q) {
            qu[i].x = n - 1 - qu[i].x;
            qu[i].y = n - 1 - qu[i].y;
        }
    }
    
    rep(i, 0, q) {
        cout << (ans[i] ? "YES" : "NO") << "\n";
    }
}

Compilation message

long_mansion.cpp: In function 'int main()':
long_mansion.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(TASK ".in", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
long_mansion.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(TASK ".out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 100 ms 41816 KB Output is correct
2 Correct 102 ms 42172 KB Output is correct
3 Correct 99 ms 42372 KB Output is correct
4 Correct 96 ms 42372 KB Output is correct
5 Correct 98 ms 42372 KB Output is correct
6 Correct 94 ms 42372 KB Output is correct
7 Correct 96 ms 42592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 41816 KB Output is correct
2 Correct 102 ms 42172 KB Output is correct
3 Correct 99 ms 42372 KB Output is correct
4 Correct 96 ms 42372 KB Output is correct
5 Correct 98 ms 42372 KB Output is correct
6 Correct 94 ms 42372 KB Output is correct
7 Correct 96 ms 42592 KB Output is correct
8 Correct 369 ms 54820 KB Output is correct
9 Correct 375 ms 59160 KB Output is correct
10 Correct 394 ms 63912 KB Output is correct
11 Correct 384 ms 68832 KB Output is correct
12 Correct 358 ms 72452 KB Output is correct
13 Correct 319 ms 76796 KB Output is correct
14 Correct 342 ms 81360 KB Output is correct
15 Correct 348 ms 85604 KB Output is correct
16 Correct 351 ms 89696 KB Output is correct
17 Correct 327 ms 93876 KB Output is correct
18 Correct 341 ms 98240 KB Output is correct
19 Correct 347 ms 102664 KB Output is correct
20 Correct 345 ms 106904 KB Output is correct
21 Correct 359 ms 110940 KB Output is correct
22 Correct 343 ms 115024 KB Output is correct
23 Correct 324 ms 119160 KB Output is correct
24 Correct 322 ms 123404 KB Output is correct
25 Correct 334 ms 127648 KB Output is correct
26 Correct 359 ms 131832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 147776 KB Output is correct
2 Correct 591 ms 154632 KB Output is correct
3 Correct 587 ms 161496 KB Output is correct
4 Correct 591 ms 169280 KB Output is correct
5 Correct 591 ms 176408 KB Output is correct
6 Correct 507 ms 181424 KB Output is correct
7 Correct 529 ms 187644 KB Output is correct
8 Correct 510 ms 193852 KB Output is correct
9 Correct 508 ms 199680 KB Output is correct
10 Correct 513 ms 205884 KB Output is correct
11 Correct 596 ms 211864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 41816 KB Output is correct
2 Correct 102 ms 42172 KB Output is correct
3 Correct 99 ms 42372 KB Output is correct
4 Correct 96 ms 42372 KB Output is correct
5 Correct 98 ms 42372 KB Output is correct
6 Correct 94 ms 42372 KB Output is correct
7 Correct 96 ms 42592 KB Output is correct
8 Correct 369 ms 54820 KB Output is correct
9 Correct 375 ms 59160 KB Output is correct
10 Correct 394 ms 63912 KB Output is correct
11 Correct 384 ms 68832 KB Output is correct
12 Correct 358 ms 72452 KB Output is correct
13 Correct 319 ms 76796 KB Output is correct
14 Correct 342 ms 81360 KB Output is correct
15 Correct 348 ms 85604 KB Output is correct
16 Correct 351 ms 89696 KB Output is correct
17 Correct 327 ms 93876 KB Output is correct
18 Correct 341 ms 98240 KB Output is correct
19 Correct 347 ms 102664 KB Output is correct
20 Correct 345 ms 106904 KB Output is correct
21 Correct 359 ms 110940 KB Output is correct
22 Correct 343 ms 115024 KB Output is correct
23 Correct 324 ms 119160 KB Output is correct
24 Correct 322 ms 123404 KB Output is correct
25 Correct 334 ms 127648 KB Output is correct
26 Correct 359 ms 131832 KB Output is correct
27 Correct 598 ms 147776 KB Output is correct
28 Correct 591 ms 154632 KB Output is correct
29 Correct 587 ms 161496 KB Output is correct
30 Correct 591 ms 169280 KB Output is correct
31 Correct 591 ms 176408 KB Output is correct
32 Correct 507 ms 181424 KB Output is correct
33 Correct 529 ms 187644 KB Output is correct
34 Correct 510 ms 193852 KB Output is correct
35 Correct 508 ms 199680 KB Output is correct
36 Correct 513 ms 205884 KB Output is correct
37 Correct 596 ms 211864 KB Output is correct
38 Correct 1055 ms 241048 KB Output is correct
39 Correct 1089 ms 258228 KB Output is correct
40 Correct 900 ms 258228 KB Output is correct
41 Correct 1081 ms 262144 KB Output is correct
42 Correct 491 ms 262144 KB Output is correct
43 Correct 516 ms 262144 KB Output is correct
44 Correct 709 ms 262144 KB Output is correct
45 Correct 747 ms 262144 KB Output is correct
46 Correct 761 ms 262144 KB Output is correct
47 Correct 553 ms 262144 KB Output is correct
48 Correct 542 ms 262144 KB Output is correct
49 Correct 750 ms 262144 KB Output is correct
50 Correct 773 ms 262144 KB Output is correct
51 Correct 829 ms 262144 KB Output is correct
52 Correct 609 ms 262144 KB Output is correct
53 Correct 689 ms 262144 KB Output is correct
54 Correct 778 ms 262144 KB Output is correct
55 Correct 676 ms 262144 KB Output is correct