Submission #405424

# Submission time Handle Problem Language Result Execution time Memory
405424 2021-05-16T11:27:56 Z tiberiu Comparing Plants (IOI20_plants) C++14
100 / 100
1589 ms 83848 KB
#include "plants.h"
#include <iostream>

using namespace std;

const int MAX_N = 2e5;

int N, K;

int v[MAX_N], hGen[MAX_N];
int aint_mn[4 * MAX_N];
int aint_pos[4 * MAX_N];
int aint_lazy[4 * MAX_N];

int cval;

void propag(int n, int nl, int nr) {
    aint_mn[n * 2 + 1] += aint_lazy[n];
    aint_lazy[n * 2 + 1] += aint_lazy[n];
    aint_mn[n * 2 + 2] += aint_lazy[n];
    aint_lazy[n * 2 + 2] += aint_lazy[n];
    aint_lazy[n] = 0;
}

void init_aint(int n = 0, int nl = 0, int nr = 0) {
    if (n == 0) nr = N - 1;

    aint_pos[n] = nl;

    if (nl < nr) {
        int mid = (nl + nr) / 2;
        init_aint(n * 2 + 1, nl, mid);
        init_aint(n * 2 + 2, mid + 1, nr);
    }
}

pair<int, int> query(int l, int r, int n = 0, int nl = 0, int nr = 0) {
    if (n == 0) nr = N - 1;

    if (l == nl && r == nr) {
        return {aint_mn[n], aint_pos[n]};
    }

    propag(n, nl, nr);

    pair<int, int> q1, q2;
    int mid = (nl + nr) / 2;

    if (r <= mid)
        return query(l, r, n * 2 + 1, nl, mid);
    else if (l > mid)
        return query(l, r, n * 2 + 2, mid + 1, nr);
    else {
        q1 = query(l, mid, n * 2 + 1, nl, mid);
        q2 = query(mid + 1, r, n * 2 + 2, mid + 1, nr);
        return min(q1, q2);
    }
}

void update(int l, int r, int val, int n = 0, int nl = 0, int nr = 0) {
    if (n == 0) nr = N - 1;

    if (l == nl && r == nr) {
        aint_mn[n] += val;
        aint_lazy[n] += val;
        return;
    }

    propag(n, nl, nr);

    int mid = (nl + nr) / 2;

    if (l <= mid)
        update(l, min(mid, r), val, n * 2 + 1, nl, mid);
    if (r > mid)
        update(max(mid + 1, l), r, val, n * 2 + 2, mid + 1, nr);

    if (aint_mn[n * 2 + 1] < aint_mn[n * 2 + 2]) {
        aint_mn[n] = aint_mn[n * 2 + 1];
        aint_pos[n] = aint_pos[n * 2 + 1];
    } else {
        aint_mn[n] = aint_mn[n * 2 + 2];
        aint_pos[n] = aint_pos[n * 2 + 2];
    }
}

void solve(int x) {
    if (x >= K - 1) {
        pair<int, int> q = query(x - K + 1, x - 1);
        if (q.first == 0) {
            solve(q.second);
            solve(x);
            return;
        }
    } else {
        pair<int, int> q = query(N + x - K + 1, N - 1);

        if (q.first != 0 && x > 0)
            q = query(0, x - 1);
        if (q.first == 0) {
            solve(q.second);
            solve(x);
            return;
        }
    }

    hGen[x] = cval;
    cval--;

    if (x >= K - 1) {
        update(x - K + 1, x - 1, -1);
    } else {
        if (x > 0)
            update(0, x - 1, -1);
        update(N + x - K + 1, N - 1, -1);
    }
    update(x, x, +1000000);
}

void gen(int k, vector<int> r) {
    init_aint();

    for (int i = 0; i < N; i++) {
        update(i, i, r[i]);
    }

    cval = N;

    while (aint_mn[0] == 0) {
        solve(aint_pos[0]);
    }
}

const int LOG = 20;

long long leftV[LOG][MAX_N];
long long rightV[LOG][MAX_N];

#include <set>

void calc_lr() {
    multiset<pair<int, int>> w;
    for (int i = N - K + 1; i < N; i++) {
        w.insert({hGen[i], i});
    }

    for (int i = 0; i < N; i++) {
        multiset<pair<int, int>>::iterator it = w.lower_bound({hGen[i], i});
        if (it != w.begin()) {
            it--;
            leftV[0][i] = (i - it->second + N) % N;
        }
        int rmv = (N + i - K + 1) % N;
        w.erase({hGen[rmv], rmv});
        w.insert({hGen[i], i});
    }

    w.clear();
    for (int i = 0; i < K - 1; i++) {
        w.insert({hGen[i], i});
    }

    for (int i = N - 1; i >= 0; i--) {
        multiset<pair<int, int>>::iterator it = w.lower_bound({hGen[i], i});
        if (it != w.begin()) {
            it--;
            rightV[0][i] = (it->second - i + N) % N;
        }
        int rmv = (N + i + K - 1) % N;
        w.erase({hGen[rmv], rmv});
        w.insert({hGen[i], i});
    }

    for (int l = 1; l < LOG; l++)
        for (int i = 0; i < N; i++) {
            rightV[l][i] = rightV[l - 1][i] + rightV[l - 1][(i + rightV[l - 1][i]) % N];
            leftV[l][i] = leftV[l - 1][i] + leftV[l - 1][(i - leftV[l - 1][i] % N + N) % N];
        }
}

void init(int k, std::vector<int> r) {
    K = k;
    N = r.size();

    gen(k, r);

    calc_lr();

	return;
}

int dist(int a, int b) {
    return (b - a + N) % N;
}

int move_right(int pos, int bound) {
    for (int l = LOG - 1; l >= 0; l--)
        while (dist(pos, bound) >= K && rightV[l][pos] && rightV[l][pos] < dist(pos, bound))
            pos = (pos + rightV[l][pos]) % N;
    return pos;
}

int move_left(int pos, int bound) {
    for (int l = LOG - 1; l >= 0; l--)
        while (dist(bound, pos) >= K && leftV[l][pos] && leftV[l][pos] < dist(bound, pos))
            pos = (pos - leftV[l][pos] + N) % N;
    return pos;
}

int compare_plants(int x0, int y0) {
    int x = x0, y = y0;

    x = move_right(x0, y0);
    if (dist(x, y0) < K && hGen[x] > hGen[y0])
        return 1;

    y = move_left(y0, x0);
    if (dist(x0, y) < K && hGen[y] > hGen[x0])
        return -1;

    x = move_left(x0, y0);
    if (dist(y0, x) < K && hGen[x] > hGen[y0])
        return 1;

    y = move_right(y0, x0);
    if (dist(y, x0) < K && hGen[y] > hGen[x0])
        return -1;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 126 ms 3428 KB Output is correct
7 Correct 270 ms 10792 KB Output is correct
8 Correct 702 ms 73960 KB Output is correct
9 Correct 757 ms 73752 KB Output is correct
10 Correct 723 ms 73048 KB Output is correct
11 Correct 711 ms 73296 KB Output is correct
12 Correct 794 ms 78384 KB Output is correct
13 Correct 755 ms 81352 KB Output is correct
14 Correct 838 ms 75048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 6 ms 972 KB Output is correct
7 Correct 118 ms 5328 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 7 ms 1064 KB Output is correct
10 Correct 133 ms 5352 KB Output is correct
11 Correct 94 ms 5244 KB Output is correct
12 Correct 117 ms 5444 KB Output is correct
13 Correct 111 ms 5444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 588 KB Output is correct
6 Correct 6 ms 972 KB Output is correct
7 Correct 118 ms 5328 KB Output is correct
8 Correct 5 ms 716 KB Output is correct
9 Correct 7 ms 1064 KB Output is correct
10 Correct 133 ms 5352 KB Output is correct
11 Correct 94 ms 5244 KB Output is correct
12 Correct 117 ms 5444 KB Output is correct
13 Correct 111 ms 5444 KB Output is correct
14 Correct 176 ms 11072 KB Output is correct
15 Correct 1502 ms 79556 KB Output is correct
16 Correct 177 ms 11100 KB Output is correct
17 Correct 1454 ms 79576 KB Output is correct
18 Correct 834 ms 77668 KB Output is correct
19 Correct 785 ms 78428 KB Output is correct
20 Correct 1452 ms 83848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 122 ms 4080 KB Output is correct
4 Correct 937 ms 74564 KB Output is correct
5 Correct 983 ms 74532 KB Output is correct
6 Correct 1185 ms 74488 KB Output is correct
7 Correct 1220 ms 74792 KB Output is correct
8 Correct 1437 ms 78732 KB Output is correct
9 Correct 887 ms 74464 KB Output is correct
10 Correct 1031 ms 74784 KB Output is correct
11 Correct 794 ms 79016 KB Output is correct
12 Correct 808 ms 72760 KB Output is correct
13 Correct 794 ms 76072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 1 ms 556 KB Output is correct
6 Correct 5 ms 716 KB Output is correct
7 Correct 34 ms 1260 KB Output is correct
8 Correct 26 ms 1260 KB Output is correct
9 Correct 43 ms 1216 KB Output is correct
10 Correct 23 ms 1368 KB Output is correct
11 Correct 33 ms 1248 KB Output is correct
12 Correct 29 ms 1252 KB Output is correct
13 Correct 26 ms 1272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 4 ms 972 KB Output is correct
6 Correct 811 ms 73784 KB Output is correct
7 Correct 1044 ms 73764 KB Output is correct
8 Correct 990 ms 74164 KB Output is correct
9 Correct 1589 ms 77984 KB Output is correct
10 Correct 829 ms 73864 KB Output is correct
11 Correct 1045 ms 77492 KB Output is correct
12 Correct 809 ms 76604 KB Output is correct
13 Correct 891 ms 76076 KB Output is correct
14 Correct 1016 ms 76184 KB Output is correct
15 Correct 1127 ms 76964 KB Output is correct
16 Correct 752 ms 76392 KB Output is correct
17 Correct 798 ms 76116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 126 ms 3428 KB Output is correct
7 Correct 270 ms 10792 KB Output is correct
8 Correct 702 ms 73960 KB Output is correct
9 Correct 757 ms 73752 KB Output is correct
10 Correct 723 ms 73048 KB Output is correct
11 Correct 711 ms 73296 KB Output is correct
12 Correct 794 ms 78384 KB Output is correct
13 Correct 755 ms 81352 KB Output is correct
14 Correct 838 ms 75048 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 2 ms 588 KB Output is correct
20 Correct 6 ms 972 KB Output is correct
21 Correct 118 ms 5328 KB Output is correct
22 Correct 5 ms 716 KB Output is correct
23 Correct 7 ms 1064 KB Output is correct
24 Correct 133 ms 5352 KB Output is correct
25 Correct 94 ms 5244 KB Output is correct
26 Correct 117 ms 5444 KB Output is correct
27 Correct 111 ms 5444 KB Output is correct
28 Correct 176 ms 11072 KB Output is correct
29 Correct 1502 ms 79556 KB Output is correct
30 Correct 177 ms 11100 KB Output is correct
31 Correct 1454 ms 79576 KB Output is correct
32 Correct 834 ms 77668 KB Output is correct
33 Correct 785 ms 78428 KB Output is correct
34 Correct 1452 ms 83848 KB Output is correct
35 Correct 1 ms 588 KB Output is correct
36 Correct 1 ms 588 KB Output is correct
37 Correct 122 ms 4080 KB Output is correct
38 Correct 937 ms 74564 KB Output is correct
39 Correct 983 ms 74532 KB Output is correct
40 Correct 1185 ms 74488 KB Output is correct
41 Correct 1220 ms 74792 KB Output is correct
42 Correct 1437 ms 78732 KB Output is correct
43 Correct 887 ms 74464 KB Output is correct
44 Correct 1031 ms 74784 KB Output is correct
45 Correct 794 ms 79016 KB Output is correct
46 Correct 808 ms 72760 KB Output is correct
47 Correct 794 ms 76072 KB Output is correct
48 Correct 1 ms 588 KB Output is correct
49 Correct 1 ms 588 KB Output is correct
50 Correct 1 ms 588 KB Output is correct
51 Correct 1 ms 588 KB Output is correct
52 Correct 1 ms 556 KB Output is correct
53 Correct 5 ms 716 KB Output is correct
54 Correct 34 ms 1260 KB Output is correct
55 Correct 26 ms 1260 KB Output is correct
56 Correct 43 ms 1216 KB Output is correct
57 Correct 23 ms 1368 KB Output is correct
58 Correct 33 ms 1248 KB Output is correct
59 Correct 29 ms 1252 KB Output is correct
60 Correct 26 ms 1272 KB Output is correct
61 Correct 165 ms 5804 KB Output is correct
62 Correct 254 ms 12828 KB Output is correct
63 Correct 963 ms 76672 KB Output is correct
64 Correct 936 ms 76876 KB Output is correct
65 Correct 1206 ms 77072 KB Output is correct
66 Correct 1243 ms 77704 KB Output is correct
67 Correct 1360 ms 81648 KB Output is correct
68 Correct 932 ms 76876 KB Output is correct
69 Correct 1020 ms 81096 KB Output is correct
70 Correct 976 ms 77436 KB Output is correct
71 Correct 1179 ms 76936 KB Output is correct
72 Correct 1143 ms 77108 KB Output is correct
73 Correct 1160 ms 77724 KB Output is correct
74 Correct 978 ms 76776 KB Output is correct
75 Correct 1012 ms 76944 KB Output is correct