Submission #602529

# Submission time Handle Problem Language Result Execution time Memory
602529 2022-07-23T07:34:22 Z KoD Comparing Plants (IOI20_plants) C++17
100 / 100
764 ms 44748 KB
#include "plants.h"
#include <bits/stdc++.h>

using std::array;
using std::pair;
using std::vector;

template <class F>
struct fixed : private F {
    explicit fixed(F&& f) : F(std::forward<F>(f)) {} 
    template <class... Args> decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

constexpr int INF = std::numeric_limits<int>::max() / 2;
constexpr int LOG = 18;

class lazysegtree {
    int size, logn;
    vector<pair<int, int>> data;
    vector<int> lazy;

    void apply(const int k, const int e) {
        data[k].first += e;
        if (k < size) {
            lazy[k] += e;
        }
    }
    void flush(const int k) {
        if (lazy[k] != 0) {
            apply(2 * k, lazy[k]);
            apply(2 * k + 1, lazy[k]);
            lazy[k] = 0;
        }
    }
    void push(const int k) {
        const int lsb = __builtin_ctz(k);
        for (int d = logn; d > lsb; --d) {
            flush(k >> d);
        }
    }

    void fetch(const int k) {
        data[k] = std::min(data[2 * k], data[2 * k + 1]);
    }
    void pull(int k) {
        k >>= __builtin_ctz(k);
        while (k > 1) {
            fetch(k >>= 1);
        }
    }

  public:
    explicit lazysegtree(const vector<int>& vec) {
        logn = 0;
        while ((1 << logn) < (int)vec.size()) {
            logn += 1;
        }
        size = 1 << logn;
        data.resize(2 * size, {INF, INF});
        for (int i = 0; i < (int)vec.size(); ++i) {
            data[size + i] = {vec[i], i};
        }
        for (int i = size - 1; i > 0; --i) {
            fetch(i);
        }
        lazy.resize(size);
    }

    void disable(int k) {
        k += size;
        for (int d = logn; d > 0; --d) {
            flush(k >> d);
        }
        data[k] = {INF, INF};
        while (k > 1) {
            fetch(k >>= 1);
        }
    }

    void subtract(int l, int r) {
        l += size;
        r += size;
        push(l);
        push(r);
        const int lc = l, rc = r;
        while (l < r) {
            if (l & 1) apply(l++, -1);
            if (r & 1) apply(--r, -1);
            l >>= 1;
            r >>= 1;
        }
        pull(lc);
        pull(rc);
    }

    pair<int, int> fold(int l, int r) {
        l += size;
        r += size;
        push(l);
        push(r);
        pair<int, int> ret = {INF, INF};
        while (l < r) {
            if (l & 1) ret = std::min(ret, data[l++]);
            if (r & 1) ret = std::min(ret, data[--r]);
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }

    pair<int, int> fold() const {
        return data[1];
    }
};

class rangemin {
    int size;
    vector<int> data;

  public:
    explicit rangemin(const int n) : size(n), data(2 * n, INF) {}

    void chmin(int i, const int x) {
        i += size;
        while (i > 0) {
            data[i] = std::min(data[i], x);
            i >>= 1;
        }
    }

    int fold(int l, int r) const {
        l += size;
        r += size;
        int ret = INF;
        while (l < r) {
            if (l & 1) ret = std::min(ret, data[l++]);
            if (r & 1) ret = std::min(ret, data[--r]);
            l >>= 1;
            r >>= 1;
        }
        return ret;
    }
};

int N;
vector<int> order, rank;
array<vector<int>, 18> left, right;

int dist(const int i, const int j) {
    return i <= j ? j - i : j - i + N;
}

int move_left(const int i, const int x) {
    return i >= x ? i - x : i - x + N;
}

int move_right(const int i, const int x) {
    return i + x < N ? i + x : i + x - N;
}

void init(int K, vector<int> R) {
    N = (int)R.size();
    lazysegtree seg(R);
    order.reserve(N);
    while ((int)order.size() < N) {
        fixed([&](auto&& dfs, const int i) -> void {
            while (true) {
                int x, j;
                if (i >= K - 1) {
                    std::tie(x, j) = seg.fold(i - (K - 1), i);
                } else {
                    std::tie(x, j) = std::min(seg.fold(i - (K - 1) + N, N), seg.fold(0, i));
                }
                if (x == 0) {
                    dfs(j);
                } else {
                    break;
                }
            }
            if (i >= K - 1) {
                seg.subtract(i - (K - 1), i);
            } else {
                seg.subtract(i - (K - 1) + N, N);
                seg.subtract(0, i);
            }
            seg.disable(i);
            order.push_back(i);
        })(seg.fold().second);
    }
    rangemin min(N); 
    rank.resize(N);
    left[0].resize(N);
    right[0].resize(N);
    for (int k = N - 1; k >= 0; --k) {
        const int i = order[k];
        rank[i] = k;
        min.chmin(i, k); 
        {
            int x;
            if (i >= K - 1) {
                x = min.fold(i - (K - 1), i);
            } else {
                x = std::min(min.fold(i - (K - 1) + N, N), min.fold(0, i));
            }
            left[0][i] = x == INF ? 0 : dist(order[x], i);
        }
        {
            int x;
            if (i + K <= N) {
                x = min.fold(i + 1, i + K);
            } else {
                x = std::min(min.fold(0, i + K - N), min.fold(i + 1, N));
            }
            right[0][i] = x == INF ? 0 : dist(i, order[x]);
        }
    }
    for (int k = 0; k + 1 < LOG; ++k) {
        left[k + 1].resize(N);
        right[k + 1].resize(N);
        for (int i = 0; i < N; ++i) {
            left[k + 1][i] = std::min(N - 1, left[k][move_left(i, left[k][i])] + left[k][i]);
            right[k + 1][i] = std::min(N - 1, right[k][move_right(i, right[k][i])] + right[k][i]);
        }
    }
}

bool between(const int x, const int l, const int r) {
    if (l <= r) {
        return l <= x and x <= r;
    } else {
        return x <= r or l <= x;
    }
}

int compare_plants(int x, int y) {
    if (rank[x] > rank[y]) {
        return -compare_plants(y, x);
    }
    int a = x, b = x;
    for (int k = LOG - 1; k >= 0; --k) {
        if (const int t = (a - left[k][a] % N + N) % N; !between(y, t, a)) {
            a = t;
        }
        if (const int t = (b + right[k][b]) % N; !between(y, b, t)) {
            b = t;
        }
    }
    if (between(y, move_left(a, left[0][a]), a) and rank[a] < rank[y]) {
        return 1;
    }
    if (between(y, b, move_right(b, right[0][b])) and rank[b] < rank[y]) {
        return 1;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 100 ms 3028 KB Output is correct
7 Correct 146 ms 6576 KB Output is correct
8 Correct 545 ms 40696 KB Output is correct
9 Correct 537 ms 40736 KB Output is correct
10 Correct 489 ms 40776 KB Output is correct
11 Correct 565 ms 40680 KB Output is correct
12 Correct 498 ms 40672 KB Output is correct
13 Correct 551 ms 40652 KB Output is correct
14 Correct 513 ms 40676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 4 ms 468 KB Output is correct
7 Correct 116 ms 3884 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 129 ms 3980 KB Output is correct
11 Correct 116 ms 3880 KB Output is correct
12 Correct 118 ms 4080 KB Output is correct
13 Correct 114 ms 3952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 4 ms 468 KB Output is correct
7 Correct 116 ms 3884 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 129 ms 3980 KB Output is correct
11 Correct 116 ms 3880 KB Output is correct
12 Correct 118 ms 4080 KB Output is correct
13 Correct 114 ms 3952 KB Output is correct
14 Correct 162 ms 6476 KB Output is correct
15 Correct 744 ms 40696 KB Output is correct
16 Correct 151 ms 8736 KB Output is correct
17 Correct 745 ms 44468 KB Output is correct
18 Correct 608 ms 43952 KB Output is correct
19 Correct 588 ms 44484 KB Output is correct
20 Correct 751 ms 44632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 113 ms 3432 KB Output is correct
4 Correct 517 ms 41696 KB Output is correct
5 Correct 555 ms 40804 KB Output is correct
6 Correct 600 ms 40756 KB Output is correct
7 Correct 677 ms 40680 KB Output is correct
8 Correct 764 ms 40720 KB Output is correct
9 Correct 548 ms 43736 KB Output is correct
10 Correct 511 ms 43724 KB Output is correct
11 Correct 535 ms 43596 KB Output is correct
12 Correct 523 ms 43792 KB Output is correct
13 Correct 592 ms 43880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 372 KB Output is correct
7 Correct 22 ms 980 KB Output is correct
8 Correct 23 ms 888 KB Output is correct
9 Correct 27 ms 952 KB Output is correct
10 Correct 24 ms 968 KB Output is correct
11 Correct 23 ms 972 KB Output is correct
12 Correct 27 ms 884 KB Output is correct
13 Correct 24 ms 892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 270 ms 40696 KB Output is correct
7 Correct 481 ms 40688 KB Output is correct
8 Correct 407 ms 40860 KB Output is correct
9 Correct 715 ms 40684 KB Output is correct
10 Correct 250 ms 42956 KB Output is correct
11 Correct 405 ms 43532 KB Output is correct
12 Correct 336 ms 43988 KB Output is correct
13 Correct 285 ms 43060 KB Output is correct
14 Correct 382 ms 43084 KB Output is correct
15 Correct 594 ms 43348 KB Output is correct
16 Correct 291 ms 42768 KB Output is correct
17 Correct 301 ms 43024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 100 ms 3028 KB Output is correct
7 Correct 146 ms 6576 KB Output is correct
8 Correct 545 ms 40696 KB Output is correct
9 Correct 537 ms 40736 KB Output is correct
10 Correct 489 ms 40776 KB Output is correct
11 Correct 565 ms 40680 KB Output is correct
12 Correct 498 ms 40672 KB Output is correct
13 Correct 551 ms 40652 KB Output is correct
14 Correct 513 ms 40676 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 116 ms 3884 KB Output is correct
22 Correct 3 ms 340 KB Output is correct
23 Correct 4 ms 468 KB Output is correct
24 Correct 129 ms 3980 KB Output is correct
25 Correct 116 ms 3880 KB Output is correct
26 Correct 118 ms 4080 KB Output is correct
27 Correct 114 ms 3952 KB Output is correct
28 Correct 162 ms 6476 KB Output is correct
29 Correct 744 ms 40696 KB Output is correct
30 Correct 151 ms 8736 KB Output is correct
31 Correct 745 ms 44468 KB Output is correct
32 Correct 608 ms 43952 KB Output is correct
33 Correct 588 ms 44484 KB Output is correct
34 Correct 751 ms 44632 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 113 ms 3432 KB Output is correct
38 Correct 517 ms 41696 KB Output is correct
39 Correct 555 ms 40804 KB Output is correct
40 Correct 600 ms 40756 KB Output is correct
41 Correct 677 ms 40680 KB Output is correct
42 Correct 764 ms 40720 KB Output is correct
43 Correct 548 ms 43736 KB Output is correct
44 Correct 511 ms 43724 KB Output is correct
45 Correct 535 ms 43596 KB Output is correct
46 Correct 523 ms 43792 KB Output is correct
47 Correct 592 ms 43880 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 3 ms 372 KB Output is correct
54 Correct 22 ms 980 KB Output is correct
55 Correct 23 ms 888 KB Output is correct
56 Correct 27 ms 952 KB Output is correct
57 Correct 24 ms 968 KB Output is correct
58 Correct 23 ms 972 KB Output is correct
59 Correct 27 ms 884 KB Output is correct
60 Correct 24 ms 892 KB Output is correct
61 Correct 99 ms 5108 KB Output is correct
62 Correct 143 ms 8692 KB Output is correct
63 Correct 549 ms 43608 KB Output is correct
64 Correct 425 ms 43772 KB Output is correct
65 Correct 633 ms 44000 KB Output is correct
66 Correct 663 ms 44248 KB Output is correct
67 Correct 756 ms 44380 KB Output is correct
68 Correct 438 ms 43836 KB Output is correct
69 Correct 641 ms 44372 KB Output is correct
70 Correct 499 ms 44748 KB Output is correct
71 Correct 475 ms 43908 KB Output is correct
72 Correct 592 ms 43968 KB Output is correct
73 Correct 723 ms 44344 KB Output is correct
74 Correct 522 ms 43680 KB Output is correct
75 Correct 535 ms 43844 KB Output is correct