답안 #388326

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
388326 2021-04-10T22:51:29 Z alexxela12345 식물 비교 (IOI20_plants) C++17
100 / 100
2003 ms 68956 KB
#include <bits/stdc++.h>
#include "plants.h"

#define left left1
#define right right1

using namespace std;

int n;
int k;
vector<int> arr;
vector<int> h;

void Set(int, int);
void genSome();
void buildLeft();
void buildRight();

void init(int k_, std::vector<int> r_) {
	k = k_;
	arr = r_;
	n = arr.size();
    genSome();
    buildRight();
    buildLeft();
}

int cur;

vector<pair<int, int>> tree;
vector<int> mod;

void build(int v, int l, int r) {
    if (l + 1 == r) {
        tree[v] = {arr[l], l};
    } else {
        int m = (l + r) / 2;
        build(2 * v + 1, l, m);
        build(2 * v + 2, m, r);
        tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
    }
}

void build() {
    tree.resize(4 * n);
    mod.resize(4 * n);
    build(0, 0, n);
}

void push(int v, int l, int r) {
    tree[v].first += mod[v];
    if (l + 1 != r) {
        mod[2 * v + 1] += mod[v];
        mod[2 * v + 2] += mod[v];
    }
    mod[v] = 0;
}

pair<int, int> GetMin(int v, int l, int r, int ql, int qr) {
    if (ql >= r || qr <= l) {
        return {n + 1, -1};
    }
    push(v, l, r);
    if (ql <= l && r <= qr) {
        return tree[v];
    }
    int m = (l + r) / 2;
    return min(GetMin(2 * v + 1, l, m, ql, qr),
               GetMin(2 * v + 2, m, r, ql, qr));
}

pair<int, int> GetMin(int l, int r) {
    // l might be negative
    if (l >= 0) {
        if (r > n) {
            return min(GetMin(l, n), GetMin(0, r - n));
        }
        return GetMin(0, 0, n, l, r);
    }
    return min(GetMin(0, 0, n, 0, r), GetMin(0, 0, n, n + l, n));
}

void Set(int v, int l, int r, int ind, int x) {
    push(v, l, r);
    if (ind < l || ind >= r)
        return;
    if (l + 1 == r) {
        tree[v] = {x, ind};
    } else {
        int m = (l + r) / 2;
        Set(2 * v + 1, l, m, ind, x);
        Set(2 * v + 2, m, r, ind, x);
        tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
    }
}

void Set(int ind, int x) {
    Set(0, 0, n, ind, x);
}

void Add(int v, int l, int r, int ql, int qr, int x) {
    push(v, l, r);
    if (ql >= r || qr <= l)
        return;
    if (ql <= l && r <= qr) {
        mod[v] = x;
        push(v, l, r);
        return;
    }
    int m = (l + r) / 2;
    Add(2 * v + 1, l, m, ql, qr, x);
    Add(2 * v + 2, m, r, ql, qr, x);
    tree[v] = min(tree[2 * v + 1], tree[2 * v + 2]);
}

void Add(int l, int r, int x) {
    if (l >= 0) {
        return Add(0, 0, n, l, r, x);
    }
    Add(0, 0, n, 0, r, x);
    Add(0, 0, n, n + l, n, x);
}

void rec(int ind) {
    while (true) {
        auto pp = GetMin(ind - k + 1, ind);
        if (pp.first == 0) {
            rec(pp.second);
        } else {
            break;
        }
    }
    h[ind] = cur--;
    Set(ind, n);
    Add(ind - k + 1, ind, -1);
}

void genSome() {
    h.resize(n, -1);
    build();
    cur = n;
    while (true) {
        auto pp = GetMin(0, n);
        if (pp.first == 0) {
            rec(pp.second);
        } else {
            break;
        }
    }
}

vector<int> left, right;
vector<vector<int>> leftb, rightb;

const int LOG = 19;

void buildRight() {
    for (int i = 0; i < n; i++) {
        Set(i, n + 1);
    }
    vector<pair<int, int>> arr(n);
    for (int i = 0; i < n; i++) {
        arr[i] = {h[i], i};
    }
    sort(arr.rbegin(), arr.rend());
    right.resize(n, n);
    rightb.resize(n, vector<int> (LOG));
    for (auto el : arr) {
        int i = el.second;
        int l = i + 1;
        int r = i + k;
        auto pp = GetMin(l, r);
        if (pp.first <= n) {
            right[i] = (pp.second - i + n) % n;
        }
        Set(i, h[i]);
    }
    for (int i = n - 1; i >= 0; i--) {
        rightb[i][0] = right[i];
        for (int j = 1; j < LOG; j++) {
            rightb[i][j] = rightb[(i + rightb[i][j - 1]) % n][j - 1] + rightb[i][j - 1];
            if (rightb[i][j] > n) {
                rightb[i][j] = n;
            }
        }
    }
}

void buildLeft() {
    for (int i = 0; i < n; i++) {
        Set(i, n + 1);
    }
    vector<pair<int, int>> arr(n);
    for (int i = 0; i < n; i++) {
        arr[i] = {h[i], i};
    }
    sort(arr.rbegin(), arr.rend());
    left.resize(n, n);
    leftb.resize(n, vector<int> (LOG));
    for (auto el : arr) {
        int i = el.second;
        int l = i - k + 1;
        int r = i;
        auto pp = GetMin(l, r);
        if (pp.first <= n) {
            left[i] = (i - pp.second + n) % n;
        }
        Set(i, h[i]);
    }
    for (int i = 0; i < n; i++) {
        leftb[i][0] = left[i];
        for (int j = 1; j < LOG; j++) {
            leftb[i][j] = leftb[((i - leftb[i][j - 1]) % n + n) % n][j - 1] + leftb[i][j - 1];
            if (leftb[i][j] > n) {
                leftb[i][j] = n;
            }
        }
    }
}

bool binupLeft(int x, int y) {
    int dist = (y - x + n) % n;
    for (int i = LOG - 1; i >= 0; i--) {
        if (leftb[y][i] < dist) {
            dist -= leftb[y][i];
            y -= leftb[y][i];
            y += n;
            y %= n;
        }
    }
    return leftb[y][0] != n && h[x] >= h[y];
}

bool binupRight(int x, int y) {
    int dist = (y - x + n) % n;
    for (int i = LOG - 1; i >= 0; i--) {
        if (rightb[x][i] < dist) {
            dist -= rightb[x][i];
            x += rightb[x][i];
            x += n;
            x %= n;
        }
    }
    return rightb[x][0] != n && h[y] >= h[x];
}

int compare_plants(int x, int y) {
    if (x > y)
        return -compare_plants(y, x);
    if (binupLeft(x, y)) {
        return 1;
    }
    if (binupLeft(y, x)) {
        return -1;
    }
    if (binupRight(x, y)) {
        return -1;
    }
    if (binupRight(y, x)) {
        return 1;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 83 ms 3028 KB Output is correct
7 Correct 217 ms 9668 KB Output is correct
8 Correct 1142 ms 65280 KB Output is correct
9 Correct 1232 ms 65208 KB Output is correct
10 Correct 1282 ms 65280 KB Output is correct
11 Correct 1299 ms 65284 KB Output is correct
12 Correct 1320 ms 65308 KB Output is correct
13 Correct 1239 ms 65276 KB Output is correct
14 Correct 1383 ms 65316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 6 ms 588 KB Output is correct
7 Correct 103 ms 4588 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 8 ms 588 KB Output is correct
10 Correct 103 ms 4676 KB Output is correct
11 Correct 118 ms 4584 KB Output is correct
12 Correct 139 ms 4776 KB Output is correct
13 Correct 103 ms 4656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 6 ms 588 KB Output is correct
7 Correct 103 ms 4588 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 8 ms 588 KB Output is correct
10 Correct 103 ms 4676 KB Output is correct
11 Correct 118 ms 4584 KB Output is correct
12 Correct 139 ms 4776 KB Output is correct
13 Correct 103 ms 4656 KB Output is correct
14 Correct 208 ms 9500 KB Output is correct
15 Correct 1943 ms 65364 KB Output is correct
16 Correct 210 ms 9356 KB Output is correct
17 Correct 1928 ms 65428 KB Output is correct
18 Correct 1573 ms 65448 KB Output is correct
19 Correct 1721 ms 65276 KB Output is correct
20 Correct 1883 ms 65304 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 123 ms 3652 KB Output is correct
4 Correct 1551 ms 65544 KB Output is correct
5 Correct 1600 ms 65432 KB Output is correct
6 Correct 1824 ms 65284 KB Output is correct
7 Correct 2003 ms 65308 KB Output is correct
8 Correct 1956 ms 65304 KB Output is correct
9 Correct 1344 ms 65348 KB Output is correct
10 Correct 1426 ms 65324 KB Output is correct
11 Correct 1267 ms 65316 KB Output is correct
12 Correct 1578 ms 65304 KB Output is correct
13 Correct 1553 ms 65196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 24 ms 1240 KB Output is correct
8 Correct 18 ms 1228 KB Output is correct
9 Correct 26 ms 1324 KB Output is correct
10 Correct 18 ms 1252 KB Output is correct
11 Correct 24 ms 1332 KB Output is correct
12 Correct 24 ms 1304 KB Output is correct
13 Correct 17 ms 1328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 5 ms 588 KB Output is correct
6 Correct 1462 ms 67504 KB Output is correct
7 Correct 1679 ms 67604 KB Output is correct
8 Correct 1867 ms 67900 KB Output is correct
9 Correct 1955 ms 68084 KB Output is correct
10 Correct 1351 ms 67360 KB Output is correct
11 Correct 1672 ms 68004 KB Output is correct
12 Correct 1455 ms 67612 KB Output is correct
13 Correct 1615 ms 67452 KB Output is correct
14 Correct 1677 ms 67716 KB Output is correct
15 Correct 1937 ms 67904 KB Output is correct
16 Correct 1275 ms 67496 KB Output is correct
17 Correct 1373 ms 67600 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 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 83 ms 3028 KB Output is correct
7 Correct 217 ms 9668 KB Output is correct
8 Correct 1142 ms 65280 KB Output is correct
9 Correct 1232 ms 65208 KB Output is correct
10 Correct 1282 ms 65280 KB Output is correct
11 Correct 1299 ms 65284 KB Output is correct
12 Correct 1320 ms 65308 KB Output is correct
13 Correct 1239 ms 65276 KB Output is correct
14 Correct 1383 ms 65316 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 6 ms 588 KB Output is correct
21 Correct 103 ms 4588 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 8 ms 588 KB Output is correct
24 Correct 103 ms 4676 KB Output is correct
25 Correct 118 ms 4584 KB Output is correct
26 Correct 139 ms 4776 KB Output is correct
27 Correct 103 ms 4656 KB Output is correct
28 Correct 208 ms 9500 KB Output is correct
29 Correct 1943 ms 65364 KB Output is correct
30 Correct 210 ms 9356 KB Output is correct
31 Correct 1928 ms 65428 KB Output is correct
32 Correct 1573 ms 65448 KB Output is correct
33 Correct 1721 ms 65276 KB Output is correct
34 Correct 1883 ms 65304 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 123 ms 3652 KB Output is correct
38 Correct 1551 ms 65544 KB Output is correct
39 Correct 1600 ms 65432 KB Output is correct
40 Correct 1824 ms 65284 KB Output is correct
41 Correct 2003 ms 65308 KB Output is correct
42 Correct 1956 ms 65304 KB Output is correct
43 Correct 1344 ms 65348 KB Output is correct
44 Correct 1426 ms 65324 KB Output is correct
45 Correct 1267 ms 65316 KB Output is correct
46 Correct 1578 ms 65304 KB Output is correct
47 Correct 1553 ms 65196 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 0 ms 204 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 3 ms 332 KB Output is correct
54 Correct 24 ms 1240 KB Output is correct
55 Correct 18 ms 1228 KB Output is correct
56 Correct 26 ms 1324 KB Output is correct
57 Correct 18 ms 1252 KB Output is correct
58 Correct 24 ms 1332 KB Output is correct
59 Correct 24 ms 1304 KB Output is correct
60 Correct 17 ms 1328 KB Output is correct
61 Correct 119 ms 5336 KB Output is correct
62 Correct 228 ms 11684 KB Output is correct
63 Correct 1364 ms 68204 KB Output is correct
64 Correct 1545 ms 68496 KB Output is correct
65 Correct 1785 ms 68596 KB Output is correct
66 Correct 1979 ms 68800 KB Output is correct
67 Correct 1931 ms 68956 KB Output is correct
68 Correct 1552 ms 68396 KB Output is correct
69 Correct 1818 ms 68896 KB Output is correct
70 Correct 1587 ms 68624 KB Output is correct
71 Correct 1731 ms 68372 KB Output is correct
72 Correct 1901 ms 68564 KB Output is correct
73 Correct 1982 ms 68816 KB Output is correct
74 Correct 1354 ms 68264 KB Output is correct
75 Correct 1354 ms 68504 KB Output is correct