Submission #302090

# Submission time Handle Problem Language Result Execution time Memory
302090 2020-09-18T12:44:04 Z kevinsogo Comparing Plants (IOI20_plants) C++17
100 / 100
1849 ms 138352 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;
    }
};

struct TreeTree {
    int k;
    vector<vector<int>> anc;
    vector<ll> pard, dists;
    vector<int> depth, height, down, upp, upi, lg;
    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), lg(par.size() + 1) {
        int n = par.size();
        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++) {
                int j = anc[kk][i];
                anc[kk + 1][i] = anc[kk][j];
            }
        }

        vector<vector<int>> adj(n);
        vector<int> pre;
        for (int i = 0; i < n; i++) {
            if (par[i] == i) pre.push_back(i);
            if (par[i] != i) 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());
        for (int i : pre) {
            if (down[i] != -1)
                height[i] = height[down[i]] + 1;
            else
                down[i] = i;
            if (par[i] != i && (down[par[i]] == -1 || height[down[par[i]]] < height[i]))
                down[par[i]] = i;
            down[i] = down[down[i]];
        }
        
        reverse(pre.begin(), pre.end());
        for (int s : pre) {
            if (upi[s] == -1) {
                int pi = ups.size();
                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++) {
                    int i = ups.back()[idx];
                    if (upi[i] == -1) {
                        upi[i] = pi;
                        upp[i] = idx;
                    }
                }
            }
        }

        for (int i = 2; i < lg.size(); i++)
            lg[i] = lg[i >> 1] + 1;
    }

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


vector<ll> depths;
void _jumpify(vector<int>& par, vector<ll>& dist, int k, int i) {
    if (depths[i] == -1) {
        if (par[i] == i) {
            depths[i] = 0;
        } else {
            _jumpify(par, dist, k, 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, int k) {
    depths = vector<ll>(par.size(), -1);
    for (int i = 0; i < par.size(); i++) _jumpify(par, dist, k, i);
}

vector<int> vals;
TreeTree rttree, lftree;
int k;
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, k);
    jumpify(rts, rtdists, k);

    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, 4)) {
        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, 4)) {
        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:169:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |         for (int f = 0; f < pre.size(); f++) {
      |                         ~~^~~~~~~~~~~~
plants.cpp:197:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  197 |                 for (int idx = 0; idx < ups.back().size(); idx++) {
      |                                   ~~~~^~~~~~~~~~~~~~~~~~~
plants.cpp:207:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  207 |         for (int i = 2; i < lg.size(); i++)
      |                         ~~^~~~~~~~~~~
plants.cpp: In function 'void jumpify(std::vector<int>&, std::vector<long long int>&, int)':
plants.cpp:245:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  245 |     for (int i = 0; i < par.size(); i++) _jumpify(par, dist, k, i);
      |                     ~~^~~~~~~~~~~~
plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:262:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  262 |     for (int x2 = 0, x1 = cands.size() - 1; x2 < cands.size(); x1 = x2++) {
      |                                             ~~~^~~~~~~~~~~~~~
plants.cpp:288:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  288 |             for (int x1 = 0, x2 = 1; x2 < bagos.size(); x1++, x2++) {
      |                                      ~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 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 127 ms 3192 KB Output is correct
7 Correct 216 ms 14092 KB Output is correct
8 Correct 931 ms 122548 KB Output is correct
9 Correct 903 ms 122612 KB Output is correct
10 Correct 876 ms 122872 KB Output is correct
11 Correct 783 ms 125556 KB Output is correct
12 Correct 779 ms 130568 KB Output is correct
13 Correct 732 ms 138096 KB Output is correct
14 Correct 837 ms 118916 KB Output is correct
# Verdict Execution time Memory 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 6 ms 1024 KB Output is correct
7 Correct 132 ms 6264 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 6 ms 896 KB Output is correct
10 Correct 130 ms 6136 KB Output is correct
11 Correct 116 ms 6268 KB Output is correct
12 Correct 138 ms 6136 KB Output is correct
13 Correct 134 ms 6392 KB Output is correct
# Verdict Execution time Memory 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 6 ms 1024 KB Output is correct
7 Correct 132 ms 6264 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 6 ms 896 KB Output is correct
10 Correct 130 ms 6136 KB Output is correct
11 Correct 116 ms 6268 KB Output is correct
12 Correct 138 ms 6136 KB Output is correct
13 Correct 134 ms 6392 KB Output is correct
14 Correct 208 ms 14588 KB Output is correct
15 Correct 1685 ms 123240 KB Output is correct
16 Correct 203 ms 14460 KB Output is correct
17 Correct 1714 ms 123084 KB Output is correct
18 Correct 780 ms 136576 KB Output is correct
19 Correct 838 ms 121724 KB Output is correct
20 Correct 1263 ms 129916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 136 ms 4216 KB Output is correct
4 Correct 907 ms 126472 KB Output is correct
5 Correct 1067 ms 119752 KB Output is correct
6 Correct 1439 ms 120704 KB Output is correct
7 Correct 1656 ms 121860 KB Output is correct
8 Correct 1697 ms 125692 KB Output is correct
9 Correct 874 ms 122884 KB Output is correct
10 Correct 938 ms 124924 KB Output is correct
11 Correct 740 ms 138352 KB Output is correct
12 Correct 842 ms 123388 KB Output is correct
13 Correct 795 ms 138096 KB Output is correct
# Verdict Execution time Memory 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 4 ms 384 KB Output is correct
7 Correct 35 ms 1152 KB Output is correct
8 Correct 25 ms 1152 KB Output is correct
9 Correct 30 ms 1152 KB Output is correct
10 Correct 25 ms 1152 KB Output is correct
11 Correct 32 ms 1144 KB Output is correct
12 Correct 32 ms 1144 KB Output is correct
13 Correct 25 ms 1152 KB Output is correct
# Verdict Execution time Memory 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 5 ms 896 KB Output is correct
6 Correct 1045 ms 118432 KB Output is correct
7 Correct 1269 ms 118536 KB Output is correct
8 Correct 1512 ms 119304 KB Output is correct
9 Correct 1646 ms 120072 KB Output is correct
10 Correct 837 ms 122624 KB Output is correct
11 Correct 1064 ms 126604 KB Output is correct
12 Correct 859 ms 127196 KB Output is correct
13 Correct 954 ms 119816 KB Output is correct
14 Correct 1357 ms 120328 KB Output is correct
15 Correct 1652 ms 121472 KB Output is correct
16 Correct 864 ms 124732 KB Output is correct
17 Correct 891 ms 123804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 127 ms 3192 KB Output is correct
7 Correct 216 ms 14092 KB Output is correct
8 Correct 931 ms 122548 KB Output is correct
9 Correct 903 ms 122612 KB Output is correct
10 Correct 876 ms 122872 KB Output is correct
11 Correct 783 ms 125556 KB Output is correct
12 Correct 779 ms 130568 KB Output is correct
13 Correct 732 ms 138096 KB Output is correct
14 Correct 837 ms 118916 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 256 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 1024 KB Output is correct
21 Correct 132 ms 6264 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 6 ms 896 KB Output is correct
24 Correct 130 ms 6136 KB Output is correct
25 Correct 116 ms 6268 KB Output is correct
26 Correct 138 ms 6136 KB Output is correct
27 Correct 134 ms 6392 KB Output is correct
28 Correct 208 ms 14588 KB Output is correct
29 Correct 1685 ms 123240 KB Output is correct
30 Correct 203 ms 14460 KB Output is correct
31 Correct 1714 ms 123084 KB Output is correct
32 Correct 780 ms 136576 KB Output is correct
33 Correct 838 ms 121724 KB Output is correct
34 Correct 1263 ms 129916 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Correct 1 ms 256 KB Output is correct
37 Correct 136 ms 4216 KB Output is correct
38 Correct 907 ms 126472 KB Output is correct
39 Correct 1067 ms 119752 KB Output is correct
40 Correct 1439 ms 120704 KB Output is correct
41 Correct 1656 ms 121860 KB Output is correct
42 Correct 1697 ms 125692 KB Output is correct
43 Correct 874 ms 122884 KB Output is correct
44 Correct 938 ms 124924 KB Output is correct
45 Correct 740 ms 138352 KB Output is correct
46 Correct 842 ms 123388 KB Output is correct
47 Correct 795 ms 138096 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 288 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 35 ms 1152 KB Output is correct
55 Correct 25 ms 1152 KB Output is correct
56 Correct 30 ms 1152 KB Output is correct
57 Correct 25 ms 1152 KB Output is correct
58 Correct 32 ms 1144 KB Output is correct
59 Correct 32 ms 1144 KB Output is correct
60 Correct 25 ms 1152 KB Output is correct
61 Correct 155 ms 4216 KB Output is correct
62 Correct 246 ms 14076 KB Output is correct
63 Correct 1126 ms 118408 KB Output is correct
64 Correct 1174 ms 118408 KB Output is correct
65 Correct 1485 ms 118516 KB Output is correct
66 Correct 1849 ms 119068 KB Output is correct
67 Correct 1812 ms 123856 KB Output is correct
68 Correct 1043 ms 123272 KB Output is correct
69 Correct 1166 ms 125192 KB Output is correct
70 Correct 989 ms 126836 KB Output is correct
71 Correct 1148 ms 120436 KB Output is correct
72 Correct 1484 ms 120588 KB Output is correct
73 Correct 1706 ms 121476 KB Output is correct
74 Correct 1090 ms 118724 KB Output is correct
75 Correct 985 ms 123904 KB Output is correct