답안 #302134

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
302134 2020-09-18T13:21:53 Z kevinsogo 식물 비교 (IOI20_plants) C++17
100 / 100
1769 ms 132732 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int mod(int a, int n) {
    if ((a %= n) < 0) a += n;
    return a;
}

int n;
int dist(int i, int j) {
    return mod(j - i - 1, n) + 1;
}

struct MinTree {
    int i, j;
    int m, a = 0;
    MinTree *l, *r;
    MinTree(const vector<int>& v, int i, int j): i(i), j(j) {
        if (j - i == 1) {
            m = v[i];
            l = r = nullptr;
        } else {
            int k = i + j >> 1;
            l = new MinTree(v, i, k);
            r = new MinTree(v, k, j);
            m = min(l->m, r->m);
        }
    }

    void visit() {
        if (a) {
            m += a;
            if (l) l->a += a, r->a += a;
            a = 0;
        }
    }
    
    void inc(int I, int J, int A) {
        if (I <= i && j <= J) {
            a += A;
            visit();
        } else {
            visit();
            if (!(J <= i || j <= I)) {
                l->inc(I, J, A);
                r->inc(I, J, A);
                m = min(l->m, r->m);
            }
        }
    }

    void get0s(int I, int J, vector<int>& t) {
        visit();
        if (m > 0 || J <= i || j <= I) return;
        if (l) {
            l->get0s(I, J, t);
            r->get0s(I, J, t);
        } else {
            t.push_back(i);
        }
    }

    void rinc(int I, int J, int A) {
        while (I < j) {
            I += j - i;
            J += j - i;
        }
        while (i < I) {
            I -= j - i;
            J -= j - i;
            inc(I, J, A);
        }
    }

    void rget0s(int I, int J, vector<int>& t) {
        while (I < j) {
            I += j - i;
            J += j - i;
        }
        while (i < I) {
            I -= j - i;
            J -= j - i;
            get0s(I, J, t);
        }
    }
};

struct MaxTree {
    int i, j;
    pair<int,int> m;
    MaxTree *l, *r;
    MaxTree(int v, int i, int j): i(i), j(j), m(v, i) {
        if (j - i == 1) {
            l = r = nullptr;
        } else {
            int k = i + j >> 1;
            l = new MaxTree(v, i, k);
            r = new MaxTree(v, k, j);
        }
    }

    void write(int I, int v) {
        if (i <= I && I < j) {
            if (l) {
                l->write(I, v);
                r->write(I, v);
                m = std::max(l->m, r->m);
            } else {
                m = {v, i};
            }
        }
    }

    pair<int,int> max(int I, int J) {
        if (I <= i && j <= J) {
            return m;
        } else if (J <= i || j <= I) {
            return {-3, -3};
        } else {
            return std::max(l->max(I, J), r->max(I, J));
        }
    }

    pair<int,int> rmax(int I, int J) {
        while (I < j) {
            I += j - i;
            J += j - i;
        }
        pair<int,int> ans(-3, -3);
        while (i < I) {
            I -= j - i;
            J -= j - i;
            ans = std::max(ans, max(I, J));
        }
        return ans;
    }
};


vector<int> _lg(2);
int lg(int n) {
    for (int i; (i = _lg.size()) <= n;) _lg.push_back(_lg[i >> 1] + 1);
    return _lg[n];
}

struct TreeTree {
    vector<vector<int>> anc;
    vector<ll> pard, dists;
    vector<int> depth, height, down, upp, upi;
    vector<vector<int>> ups;
    TreeTree() {}
    TreeTree(const vector<int>& par, const vector<ll>& pard):
            pard(pard), dists(par.size()), depth(par.size()), height(par.size()),
            down(par.size(), -1), upp(par.size(), -1), upi(par.size(), -1) {
        int n = par.size(), k;
        for (k = 0; 1 << k <= n; k++);
        anc = vector<vector<int>>(k, vector<int>(n, -1));
        anc[0] = par;
        for (int kk = 0; kk < k - 1; kk++) {
            for (int i = 0; i < n; i++) anc[kk + 1][i] = anc[kk][anc[kk][i]];
        }

        vector<vector<int>> adj(n);
        vector<int> pre;
        for (int i = 0; i < n; i++) {
            (par[i] == i ? pre : adj[par[i]]).push_back(i);
        }

        for (int f = 0; f < pre.size(); f++) {
            int i = pre[f];
            for (int j : adj[i]) {
                depth[j] = depth[i] + 1;
                dists[j] = dists[i] + pard[j];
                pre.push_back(j);
            }
        }

        reverse(pre.begin(), pre.end());
        iota(down.begin(), down.end(), 0);
        for (int i : pre) {
            if (down[i] != i) height[i] = height[down[i]] + 1;
            if (par[i] != i && height[down[par[i]]] <= height[i]) down[par[i]] = i;
            down[i] = down[down[i]];
        }
        
        reverse(pre.begin(), pre.end());
        ups.reserve(n);
        for (int s : pre) {
            if (upi[s] == -1) {
                ups.emplace_back(1, down[s]);
                for (int it = 2*(height[s] + 1); it--;) {
                    ups.back().push_back(par[ups.back().back()]);
                }
                for (int idx = 0; idx < ups.back().size(); idx++) {
                    if (int i = ups.back()[idx]; upi[i] == -1) {
                        upi[i] = ups.size() - 1;
                        upp[i] = idx;
                    }
                }
            }
        }
    }

    vector<pair<int,ll>> walk_up(int i, ll d, int t) {
        d -= dists[i];
        if ((t = max(0, min(depth[i], t))) > 0) {
            i = anc[lg(t)][i];
            i = ups[upi[i]][upp[i] + t - (1 << lg(t))];
        }
        d += dists[i];
        vector<pair<int,ll>> res;
        for (; d >= pard[i]; d -= pard[i], i = anc[0][i]);
        res.emplace_back(i, d);
        d -= pard[i], i = anc[0][i];
        res.emplace_back(i, d);
        return res;
    }
};


vector<ll> depths;
int k;
void _jumpify(vector<int>& par, vector<ll>& dist, int i) {
    if (depths[i] == -1) {
        if (par[i] == i) {
            depths[i] = 0;
        } else {
            _jumpify(par, dist, par[i]);
            depths[i] = dist[i] + depths[par[i]];
            while (par[i] != par[par[i]] && i / k == par[i] / k) {
                dist[i] += dist[par[i]];
                par[i] = par[par[i]];
            }
        }
    }
}

void jumpify(vector<int>& par, vector<ll>& dist) {
    depths = vector<ll>(par.size(), -1);
    for (int i = 0; i < par.size(); i++) _jumpify(par, dist, i);
}

vector<int> vals;
TreeTree rttree, lftree;
void init(int k, vector<int> r) {
    ::k = k;
    n = r.size();
    vals = vector<int>(n);

    MinTree curr(r, 0, n);
    vector<int> likod(n), harap(n);
    unordered_set<int> oks;

    vector<int> cands;
    for (int i = 0; i < n; i++) if (!r[i]) cands.push_back(i);
    for (int x2 = 0, x1 = cands.size() - 1; x2 < cands.size(); x1 = x2++) {
        int j1 = cands[x1], j2 = cands[x2];
        harap[j1] = j2;
        likod[j2] = j1;
        if (dist(j1, j2) >= k) oks.insert(j2);
    }
    
    for (int v = n; v--;) {
        int j = *oks.begin();
        int j1 = likod[j], j2 = harap[j];
        oks.erase(j);
        vals[j] = v;
        curr.inc(j, j + 1, n + 1);
        curr.rinc(j - k + 1, j, -1);

        vector<int> bagos;
        curr.rget0s(j - k + 1, j, bagos);

        if (v > 0) {
            if (j == j1 && j == j2) {
                j1 = bagos.back();
                j2 = bagos.front();
            }

            bagos.insert(bagos.begin(), j1);
            bagos.push_back(j2);
            for (int x1 = 0, x2 = 1; x2 < bagos.size(); x1++, x2++) {
                int j1 = bagos[x1], j2 = bagos[x2];
                harap[j1] = j2;
                likod[j2] = j1;
                if (dist(j1, j2) >= k) {
                    oks.insert(j2);
                } else if (oks.count(j2)) {
                    oks.erase(j2);
                }
            }
        }
    }

    vector<int> lfs(n, -1), rts(n, -1);
    MaxTree wals(-2, 0, n);
    vector<int> inds(n);
    for (int i = 0; i < n; i++) inds[vals[i]] = i;
    for (int x : inds) {
        wals.write(x, -1);
        int m;
        tie(m, lfs[x]) = wals.rmax(x - k + 1, x + 1);
        tie(m, rts[x]) = wals.rmax(x, x + k);
        wals.write(x, vals[x]);
    }

    vector<ll> lfdists(n), rtdists(n);
    for (int i = 0; i < n; i++) {
        lfdists[i] = dist(lfs[i], i);
        rtdists[i] = dist(i, rts[i]);
    }

    jumpify(lfs, lfdists);
    jumpify(rts, rtdists);

    lftree = TreeTree(lfs, lfdists);
    rttree = TreeTree(rts, rtdists);
}

bool is_fixed_right(int x, int y) {
    for (auto [X, rem] : rttree.walk_up(x, dist(x, y), dist(x, y) / k - 1)) {
        if (abs(rem) < k && vals[X] >= vals[y]) return true;
    }
    return false;
}

bool is_fixed_left(int x, int y) {
    for (auto [X, rem] : lftree.walk_up(x, dist(y, x), dist(y, x) / k - 1)) {
        if (abs(rem) < k && vals[X] >= vals[y]) return true;
    }
    return false;
}

int compare_plants(int x, int y) {
    int mul = 1;
    if (vals[x] < vals[y]) {
        swap(x, y);
        mul = -1;
    }

    return (is_fixed_right(x, y) || is_fixed_left(x, y)) * mul;
}

Compilation message

plants.cpp: In constructor 'MinTree::MinTree(const std::vector<int>&, int, int)':
plants.cpp:25:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   25 |             int k = i + j >> 1;
      |                     ~~^~~
plants.cpp: In constructor 'MaxTree::MaxTree(int, int, int)':
plants.cpp:98:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   98 |             int k = i + j >> 1;
      |                     ~~^~~
plants.cpp: In constructor 'TreeTree::TreeTree(const std::vector<int>&, const std::vector<long long int>&)':
plants.cpp:171:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for (int f = 0; f < pre.size(); f++) {
      |                         ~~^~~~~~~~~~~~
plants.cpp:196:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  196 |                 for (int idx = 0; idx < ups.back().size(); idx++) {
      |                                   ~~~~^~~~~~~~~~~~~~~~~~~
plants.cpp: In function 'void jumpify(std::vector<int>&, std::vector<long long int>&)':
plants.cpp:242:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  242 |     for (int i = 0; i < par.size(); i++) _jumpify(par, dist, i);
      |                     ~~^~~~~~~~~~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:258:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  258 |     for (int x2 = 0, x1 = cands.size() - 1; x2 < cands.size(); x1 = x2++) {
      |                                             ~~~^~~~~~~~~~~~~~
plants.cpp:284:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  284 |             for (int x1 = 0, x2 = 1; x2 < bagos.size(); x1++, x2++) {
      |                                      ~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 110 ms 3192 KB Output is correct
7 Correct 213 ms 14016 KB Output is correct
8 Correct 922 ms 118476 KB Output is correct
9 Correct 858 ms 118524 KB Output is correct
10 Correct 841 ms 118720 KB Output is correct
11 Correct 777 ms 120572 KB Output is correct
12 Correct 762 ms 124756 KB Output is correct
13 Correct 704 ms 132600 KB Output is correct
14 Correct 845 ms 116188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 288 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 6 ms 896 KB Output is correct
7 Correct 118 ms 6072 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 6 ms 896 KB Output is correct
10 Correct 120 ms 6136 KB Output is correct
11 Correct 110 ms 6040 KB Output is correct
12 Correct 122 ms 6008 KB Output is correct
13 Correct 121 ms 6204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 288 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 6 ms 896 KB Output is correct
7 Correct 118 ms 6072 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 6 ms 896 KB Output is correct
10 Correct 120 ms 6136 KB Output is correct
11 Correct 110 ms 6040 KB Output is correct
12 Correct 122 ms 6008 KB Output is correct
13 Correct 121 ms 6204 KB Output is correct
14 Correct 194 ms 14272 KB Output is correct
15 Correct 1713 ms 120580 KB Output is correct
16 Correct 192 ms 14280 KB Output is correct
17 Correct 1769 ms 120304 KB Output is correct
18 Correct 786 ms 130696 KB Output is correct
19 Correct 860 ms 120072 KB Output is correct
20 Correct 1357 ms 126192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 134 ms 4216 KB Output is correct
4 Correct 894 ms 121340 KB Output is correct
5 Correct 1062 ms 117896 KB Output is correct
6 Correct 1482 ms 118720 KB Output is correct
7 Correct 1670 ms 119788 KB Output is correct
8 Correct 1697 ms 123016 KB Output is correct
9 Correct 856 ms 118660 KB Output is correct
10 Correct 906 ms 121384 KB Output is correct
11 Correct 713 ms 132732 KB Output is correct
12 Correct 816 ms 120948 KB Output is correct
13 Correct 771 ms 132604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 29 ms 1024 KB Output is correct
8 Correct 23 ms 1280 KB Output is correct
9 Correct 28 ms 1144 KB Output is correct
10 Correct 23 ms 1144 KB Output is correct
11 Correct 29 ms 1152 KB Output is correct
12 Correct 28 ms 1152 KB Output is correct
13 Correct 21 ms 1152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 4 ms 896 KB Output is correct
6 Correct 984 ms 116996 KB Output is correct
7 Correct 1273 ms 117128 KB Output is correct
8 Correct 1547 ms 117800 KB Output is correct
9 Correct 1658 ms 118280 KB Output is correct
10 Correct 822 ms 118272 KB Output is correct
11 Correct 1051 ms 122632 KB Output is correct
12 Correct 865 ms 121980 KB Output is correct
13 Correct 914 ms 118024 KB Output is correct
14 Correct 1280 ms 118408 KB Output is correct
15 Correct 1568 ms 119304 KB Output is correct
16 Correct 825 ms 120696 KB Output is correct
17 Correct 853 ms 120456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 110 ms 3192 KB Output is correct
7 Correct 213 ms 14016 KB Output is correct
8 Correct 922 ms 118476 KB Output is correct
9 Correct 858 ms 118524 KB Output is correct
10 Correct 841 ms 118720 KB Output is correct
11 Correct 777 ms 120572 KB Output is correct
12 Correct 762 ms 124756 KB Output is correct
13 Correct 704 ms 132600 KB Output is correct
14 Correct 845 ms 116188 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 1 ms 288 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 118 ms 6072 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 6 ms 896 KB Output is correct
24 Correct 120 ms 6136 KB Output is correct
25 Correct 110 ms 6040 KB Output is correct
26 Correct 122 ms 6008 KB Output is correct
27 Correct 121 ms 6204 KB Output is correct
28 Correct 194 ms 14272 KB Output is correct
29 Correct 1713 ms 120580 KB Output is correct
30 Correct 192 ms 14280 KB Output is correct
31 Correct 1769 ms 120304 KB Output is correct
32 Correct 786 ms 130696 KB Output is correct
33 Correct 860 ms 120072 KB Output is correct
34 Correct 1357 ms 126192 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 134 ms 4216 KB Output is correct
38 Correct 894 ms 121340 KB Output is correct
39 Correct 1062 ms 117896 KB Output is correct
40 Correct 1482 ms 118720 KB Output is correct
41 Correct 1670 ms 119788 KB Output is correct
42 Correct 1697 ms 123016 KB Output is correct
43 Correct 856 ms 118660 KB Output is correct
44 Correct 906 ms 121384 KB Output is correct
45 Correct 713 ms 132732 KB Output is correct
46 Correct 816 ms 120948 KB Output is correct
47 Correct 771 ms 132604 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 1 ms 256 KB Output is correct
50 Correct 1 ms 256 KB Output is correct
51 Correct 1 ms 256 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 4 ms 384 KB Output is correct
54 Correct 29 ms 1024 KB Output is correct
55 Correct 23 ms 1280 KB Output is correct
56 Correct 28 ms 1144 KB Output is correct
57 Correct 23 ms 1144 KB Output is correct
58 Correct 29 ms 1152 KB Output is correct
59 Correct 28 ms 1152 KB Output is correct
60 Correct 21 ms 1152 KB Output is correct
61 Correct 135 ms 4088 KB Output is correct
62 Correct 219 ms 13760 KB Output is correct
63 Correct 1071 ms 117004 KB Output is correct
64 Correct 1105 ms 117000 KB Output is correct
65 Correct 1400 ms 117256 KB Output is correct
66 Correct 1633 ms 117488 KB Output is correct
67 Correct 1675 ms 121480 KB Output is correct
68 Correct 1006 ms 118580 KB Output is correct
69 Correct 1082 ms 121544 KB Output is correct
70 Correct 907 ms 121728 KB Output is correct
71 Correct 1085 ms 118384 KB Output is correct
72 Correct 1433 ms 118664 KB Output is correct
73 Correct 1647 ms 119436 KB Output is correct
74 Correct 1047 ms 117132 KB Output is correct
75 Correct 936 ms 120580 KB Output is correct