답안 #526669

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
526669 2022-02-16T00:58:20 Z PurpleCrayon 식물 비교 (IOI20_plants) C++17
100 / 100
1706 ms 145012 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
const int INF = 1e9+10;

struct S {
    int n;
    vector<pair<int, int>> t;
    vector<int> lzy;

    void build(int v, int tl, int tr, const vector<int>& a) {
        if (tl == tr) {
            t[v] = make_pair(a[tl], tl);
        } else {
            int tm = (tl + tr) / 2;
            build(2*v, tl, tm, a), build(2*v+1, tm+1, tr, a);
            t[v] = min(t[2*v], t[2*v+1]);
        }
    }

    S() {}
    S(vector<int> a) {
        n = sz(a);
        t.resize(4 * n);
        lzy.assign(4 * n, 0);
        build(1, 0, n-1, a);
    }
    S(int _n): n(_n) {
        vector<int> a(n, INF);
        t.resize(4 * n);
        lzy.assign(4 * n, 0);
        build(1, 0, n-1, a);
    }
    void push(int v, int tl, int tr, int x) {
        if (!x) return;
        t[v].first += x;
        if (tl != tr) {
            lzy[2*v] += x;
            lzy[2*v+1] += x;
        }
    }
    void app(int v, int tl, int tr) {
        push(v, tl, tr, lzy[v]);
        lzy[v] = 0;
    }
    void upd(int v, int tl, int tr, int l, int r, int x) {
        app(v, tl, tr);
        if (l > r || l > tr || r < tl) return;
        if (l <= tl && tr <= r) {
            push(v, tl, tr, x);
            return;
        }
        int tm = (tl + tr) / 2;
        upd(2*v, tl, tm, l, r, x), upd(2*v+1, tm+1, tr, l, r, x);
        t[v] = min(t[2*v], t[2*v+1]);
    }
    void upd(int l, int r, int x) {
        if (l > r) {
            upd(1, 0, n-1, 0, r, +x);
            upd(1, 0, n-1, l, n-1, +x);
        } else {
            upd(1, 0, n-1, l, r, +x);
        }
    }
    pair<int, int> qry(int v, int tl, int tr, int l, int r) {
        app(v, tl, tr);
        if (l > r || l > tr || r < tl) return {INF, INF};
        if (l <= tl && tr <= r) return t[v];
        int tm = (tl + tr) / 2;
        return min(qry(2*v, tl, tm, l, r), qry(2*v+1, tm+1, tr, l, r));
    }
    pair<int, int> qry(int l, int r) {
        if (l > r) {
            return min(qry(1, 0, n-1, 0, r), qry(1, 0, n-1, l, n-1));
        } else {
            return qry(1, 0, n-1, l, r);
        }
    }
    void set(int pos, int x) {
        upd(pos, pos, -qry(pos, pos).first + x);
    }
};

const int L = 19;
vector<vector<int>> build_lift(vector<int> par) {
    int n = sz(par);
    vector<vector<int>> up(L, vector<int>(n, -1));
    up[0] = par;
    for (int k = 1; k < L; k++) {
        for (int i = 0; i < n; i++) {
            up[k][i] = up[k-1][i] == -1 ? -1 : up[k-1][up[k-1][i]];
        }
    }
    return up;
}
int search_lift(const vector<vector<int>>& up, int c, const auto& f) {
    for (int k = L-1; k >= 0; k--) {
        if (f(up[k][c])) {
            c = up[k][c];
        }
    }
    return c;
}

int n, k;
vector<int> a, b;
vector<int> nxt, prv;

vector<vector<int>> up_nxt, up_prv;

void init(int K, vector<int> r) {
    n = sz(r), k = K;
    vector<bool> done(n);

    a.resize(n);
    nxt.assign(2*n, -1), prv.assign(2*n, -1);

    set<int> cand;
    set<int> bad_cand;
    auto prv_cand = [&](int x, bool which) {
        const auto& use = (which ? cand : bad_cand);
        if (!sz(use)) return INF;
        auto it = use.lower_bound(x);
        if (it != use.begin()) return *prev(it);
        return *use.rbegin();
    };
    auto nxt_cand = [&](int x, bool which) {
        const auto& use = (which ? cand : bad_cand);
        if (!sz(use)) return INF;
        auto it = use.upper_bound(x);
        if (it == use.end()) return *use.begin();
        return *it;
    };
    auto forward_dist = [&](int a, int b) {
        if (a == INF || b == INF) return INF;
        if (a < b) return b - a;
        return n - a + b;
    };

    S seg(r);
    vector<bool> has_add(n);
    for (int i = 0; i < n; i++) if (!r[i]) {
        cand.insert(i), has_add[i] = 1;
        seg.upd(i, i, INF);
    }

    vector<set<int>> stop(n);
    vector<int> stopped(n, -1);

    auto add_stop = [&](int x, int y) {
        if (stopped[y] != -1) {
            stop[stopped[y]].erase(y);
        }
        stop[x].insert(y);
        stopped[y] = x;
    };

    for (int i = 0; i < n; i++) if (!r[i]) {
        int p = prv_cand(i, true);
        if (forward_dist(p, i) < k) {
            add_stop(p, i);
            // stop[p].insert(i);
            bad_cand.insert(i);
        }
    }
    for (int x : bad_cand) cand.erase(x);

    auto prv_cand_both = [&](int x) {
        int one = prv_cand(x, true), two = prv_cand(x, false);
        if (forward_dist(one, x) < forward_dist(two, x)) return one;
        return two;
    };
    auto nxt_cand_both = [&](int x) {
        int one = nxt_cand(x, true), two = nxt_cand(x, false);
        if (forward_dist(x, one) < forward_dist(x, two)) return one;
        return two;
    };

    auto add_cand = [&](int x) {
        int p = prv_cand_both(x);
        if (p != INF && forward_dist(p, x) < k) {
            add_stop(p, x);
            // stop[p].push_back(x);
            cand.erase(x);
            bad_cand.insert(x);
        } else {
            bad_cand.erase(x);
            cand.insert(x);
        }
        int q = nxt_cand_both(x);
        if (q != INF && forward_dist(x, q) < k) {
            add_stop(x, q);
            // stop[x].push_back(q);
            cand.erase(q);
            bad_cand.insert(q);
        }
    };

    for (int rep = 0; rep < n; rep++) {
        int me = *cand.begin();

        a[me] = n - 1 - rep;
        done[me] = 1;
        cand.erase(me);

        for (int x : stop[me]) if (!done[x]) {
            add_cand(x);
        }

        int L = ((me - k + 1) + n) % n, R = (me - 1 + n) % n;
        seg.upd(L, R, -1);
        for (auto C = seg.qry(L, R); C.first == 0; C = seg.qry(L, R)) {
            int c = C.second;
            add_cand(c);
            seg.upd(c, c, +INF);
        }
    }
    for (int rep : {0, 1}) for (int x : a) b.push_back(x);

    assert(sz(b) == 2*n);
    vector<vector<int>> loc(n);
    for (int i = 0; i < 2*n; i++) loc[b[i]].push_back(i);

    S b_seg(2*n);
    for (int l = n-1; l >= 0; l--) for (int i : loc[l]) {
        {
            int L = i+1, R = min(2*n, i+k)-1;
            if (L <= R) {
                auto c = b_seg.qry(L, R);
                if (c.first != INF) nxt[i] = c.second;
            }
        }
        {
            int L = max(0, i-k+1), R = i-1;
            if (L <= R) {
                auto c = b_seg.qry(L, R);
                if (c.first != INF) prv[i] = c.second;
            }
        }
        if (nxt[i] == INF) nxt[i] = -1;
        if (prv[i] == INF) prv[i] = -1;

        b_seg.set(i, b[i]);
    }

    // for (int x : b) cerr << x << ' '; cerr << endl;
    // for (int x : nxt) cerr << x << ' '; cerr << endl;
    // for (int x : prv) cerr << x << ' '; cerr << endl;

    /*
    for (int i = 0; i < 2*n; i++) {
        int c_nxt = -1;
        for (int j = i+1; j < min(2*n, i+k); j++) { // range min
            if (b[j] > b[i] && (c_nxt == -1 || b[c_nxt] > b[j]))
                c_nxt = j;
        }
        // cerr << "nxt: " << i << ' ' << c_nxt << ' ' << nxt[i] << endl;
        assert(c_nxt == nxt[i]);
        int c_prv = -1;
        for (int j = max(0, i-k+1); j < i; j++) { // range min
            if (b[j] > b[i] && (c_prv == -1 || b[c_prv] > b[j]))
                c_prv = j;
        }
        // cerr << "prv: " << i << ' ' << c_prv << ' ' << prv[i] << endl;
        assert(c_prv == prv[i]);
    }
    */

    up_nxt = build_lift(nxt);
    up_prv = build_lift(prv);
}

bool can_nxt(int x, int y) { // slow
    int c = x;
    int need = y < x ? y + n : y;
    c = search_lift(up_nxt, c, [&](int v){ return v != -1 && v <= need; });
    /*
    while (nxt[c] != -1 && nxt[c] <= need) {
        assert(b[nxt[c]] > b[c]);
        c = nxt[c];
    }
    */
    assert(b[need] == a[y]);
    return need - c < k && b[need] >= b[c];
}
bool can_prv(int x, int y) { // slow
    int c = x > y ? x : x + n;
    int need = y;
    c = search_lift(up_prv, c, [&](int v){ return v != -1 && v >= need; });
    /*
    while (prv[c] != -1 && prv[c] >= need) {
        assert(b[prv[c]] > b[c]);
        c = prv[c];
    }
    */
    assert(b[need] == a[y]);
    return c - need < k && b[need] >= b[c];
}
bool can_reach(int x, int y) {
    return can_nxt(x, y) || can_prv(x, y);
}

int compare_plants(int x, int y) {
    if (can_reach(x, y)) { // y is greater than x
        assert(a[y] > a[x]);
        return -1;
    }
    if (can_reach(y, x)) { // x is greater than y
        assert(a[x] > a[y]);
        return 1;
    }
    return 0;
}

Compilation message

plants.cpp:98:61: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   98 | int search_lift(const vector<vector<int>>& up, int c, const auto& f) {
      |                                                             ^~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:220:14: warning: unused variable 'rep' [-Wunused-variable]
  220 |     for (int rep : {0, 1}) for (int x : a) b.push_back(x);
      |              ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 1 ms 432 KB Output is correct
6 Correct 59 ms 4096 KB Output is correct
7 Correct 164 ms 17232 KB Output is correct
8 Correct 903 ms 130780 KB Output is correct
9 Correct 954 ms 131140 KB Output is correct
10 Correct 976 ms 131228 KB Output is correct
11 Correct 1006 ms 133308 KB Output is correct
12 Correct 1061 ms 132704 KB Output is correct
13 Correct 1274 ms 145012 KB Output is correct
14 Correct 793 ms 124704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 5 ms 972 KB Output is correct
7 Correct 90 ms 7516 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 5 ms 940 KB Output is correct
10 Correct 101 ms 7520 KB Output is correct
11 Correct 132 ms 7760 KB Output is correct
12 Correct 75 ms 7492 KB Output is correct
13 Correct 91 ms 7504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 5 ms 972 KB Output is correct
7 Correct 90 ms 7516 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 5 ms 940 KB Output is correct
10 Correct 101 ms 7520 KB Output is correct
11 Correct 132 ms 7760 KB Output is correct
12 Correct 75 ms 7492 KB Output is correct
13 Correct 91 ms 7504 KB Output is correct
14 Correct 207 ms 16824 KB Output is correct
15 Correct 1606 ms 127216 KB Output is correct
16 Correct 179 ms 16848 KB Output is correct
17 Correct 1628 ms 127864 KB Output is correct
18 Correct 1365 ms 133744 KB Output is correct
19 Correct 1024 ms 123600 KB Output is correct
20 Correct 1518 ms 123668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 90 ms 6016 KB Output is correct
4 Correct 1198 ms 128300 KB Output is correct
5 Correct 1267 ms 126288 KB Output is correct
6 Correct 1556 ms 127040 KB Output is correct
7 Correct 1676 ms 127728 KB Output is correct
8 Correct 1706 ms 128368 KB Output is correct
9 Correct 1244 ms 128416 KB Output is correct
10 Correct 1272 ms 127988 KB Output is correct
11 Correct 1248 ms 143992 KB Output is correct
12 Correct 909 ms 123588 KB Output is correct
13 Correct 1419 ms 136628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 284 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 4 ms 336 KB Output is correct
7 Correct 18 ms 1320 KB Output is correct
8 Correct 15 ms 1320 KB Output is correct
9 Correct 17 ms 1320 KB Output is correct
10 Correct 15 ms 1324 KB Output is correct
11 Correct 18 ms 1320 KB Output is correct
12 Correct 18 ms 1360 KB Output is correct
13 Correct 14 ms 1360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 4 ms 848 KB Output is correct
6 Correct 1151 ms 126820 KB Output is correct
7 Correct 1321 ms 127316 KB Output is correct
8 Correct 1542 ms 128248 KB Output is correct
9 Correct 1457 ms 129168 KB Output is correct
10 Correct 1008 ms 128260 KB Output is correct
11 Correct 1240 ms 131720 KB Output is correct
12 Correct 951 ms 128284 KB Output is correct
13 Correct 1120 ms 126816 KB Output is correct
14 Correct 1362 ms 127640 KB Output is correct
15 Correct 1471 ms 128400 KB Output is correct
16 Correct 1042 ms 127904 KB Output is correct
17 Correct 1140 ms 128448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 288 KB Output is correct
5 Correct 1 ms 432 KB Output is correct
6 Correct 59 ms 4096 KB Output is correct
7 Correct 164 ms 17232 KB Output is correct
8 Correct 903 ms 130780 KB Output is correct
9 Correct 954 ms 131140 KB Output is correct
10 Correct 976 ms 131228 KB Output is correct
11 Correct 1006 ms 133308 KB Output is correct
12 Correct 1061 ms 132704 KB Output is correct
13 Correct 1274 ms 145012 KB Output is correct
14 Correct 793 ms 124704 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 5 ms 972 KB Output is correct
21 Correct 90 ms 7516 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 5 ms 940 KB Output is correct
24 Correct 101 ms 7520 KB Output is correct
25 Correct 132 ms 7760 KB Output is correct
26 Correct 75 ms 7492 KB Output is correct
27 Correct 91 ms 7504 KB Output is correct
28 Correct 207 ms 16824 KB Output is correct
29 Correct 1606 ms 127216 KB Output is correct
30 Correct 179 ms 16848 KB Output is correct
31 Correct 1628 ms 127864 KB Output is correct
32 Correct 1365 ms 133744 KB Output is correct
33 Correct 1024 ms 123600 KB Output is correct
34 Correct 1518 ms 123668 KB Output is correct
35 Correct 1 ms 288 KB Output is correct
36 Correct 1 ms 288 KB Output is correct
37 Correct 90 ms 6016 KB Output is correct
38 Correct 1198 ms 128300 KB Output is correct
39 Correct 1267 ms 126288 KB Output is correct
40 Correct 1556 ms 127040 KB Output is correct
41 Correct 1676 ms 127728 KB Output is correct
42 Correct 1706 ms 128368 KB Output is correct
43 Correct 1244 ms 128416 KB Output is correct
44 Correct 1272 ms 127988 KB Output is correct
45 Correct 1248 ms 143992 KB Output is correct
46 Correct 909 ms 123588 KB Output is correct
47 Correct 1419 ms 136628 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 284 KB Output is correct
51 Correct 1 ms 208 KB Output is correct
52 Correct 1 ms 208 KB Output is correct
53 Correct 4 ms 336 KB Output is correct
54 Correct 18 ms 1320 KB Output is correct
55 Correct 15 ms 1320 KB Output is correct
56 Correct 17 ms 1320 KB Output is correct
57 Correct 15 ms 1324 KB Output is correct
58 Correct 18 ms 1320 KB Output is correct
59 Correct 18 ms 1360 KB Output is correct
60 Correct 14 ms 1360 KB Output is correct
61 Correct 89 ms 5992 KB Output is correct
62 Correct 168 ms 16884 KB Output is correct
63 Correct 1036 ms 127228 KB Output is correct
64 Correct 1258 ms 127644 KB Output is correct
65 Correct 1584 ms 128184 KB Output is correct
66 Correct 1669 ms 129088 KB Output is correct
67 Correct 1673 ms 130028 KB Output is correct
68 Correct 1229 ms 129232 KB Output is correct
69 Correct 1389 ms 132252 KB Output is correct
70 Correct 1207 ms 129168 KB Output is correct
71 Correct 1429 ms 127552 KB Output is correct
72 Correct 1579 ms 128296 KB Output is correct
73 Correct 1647 ms 129288 KB Output is correct
74 Correct 1062 ms 127756 KB Output is correct
75 Correct 1263 ms 129420 KB Output is correct