답안 #300638

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
300638 2020-09-17T10:46:13 Z imeimi2000 식물 비교 (IOI20_plants) C++17
100 / 100
921 ms 47864 KB
#ifdef imeimi

#include <vector>

void init(int k, std::vector<int> r);
int compare_plants(int x, int y);

#include <cstdio>
#include <cassert>
#include <vector>
#define n N
#define k K

static int n, k, q;
static std::vector<int> r;
static std:: vector<int> x;
static std:: vector<int> y;
static std:: vector<int> answer;
int main() {
	assert(scanf("%d%d%d", &n, &k, &q) == 3);
	r.resize(n);
	answer.resize(q);
	for (int i = 0; i < n; i++) {
		int value;
		assert(scanf("%d", &value) == 1);
		r[i] = value;
	}
	x.resize(q);
	y.resize(q);
	for (int i = 0; i < q; i++) {
		assert(scanf("%d%d", &x[i], &y[i]) == 2);
	}
	fclose(stdin);

	init(k, r);
	for (int i = 0; i < q; i++) {
		answer[i] = compare_plants(x[i], y[i]);
	}

	for (int i = 0; i < q; i++) {
		printf("%d\n", answer[i]);
	}

	fclose(stdout);

	return 0;
}

#undef n
#undef k
#else

#include "plants.h"

#endif

#include <bits/stdc++.h>

using namespace std;

namespace S {
    int seg[1 << 19], add[1 << 19];
    void update(int i, int s, int e, int x, int y, int v) {
        if (e < x || y < s) return;
        if (x <= s && e <= y) {
            seg[i] += v;
            add[i] += v;
            return;
        }
        int m = (s + e) / 2;
        update(i + i, s, m, x, y, v);
        update(i + i + 1, m + 1, e, x, y, v);
        seg[i] = min(seg[i + i], seg[i + i + 1]) + add[i];
    }
    int find(int i, int s, int e, int a = 0) {
        if (seg[i] + a) return 0;
        if (s == e) return s;
        a += add[i];
        int m = (s + e) / 2;
        int ret = find(i + i, s, m, a);
        if (ret) return ret;
        return find(i + i + 1, m + 1, e, a);
    }
}

static int n, k;
static int val[200001], rev[200001];
static int fen[200001], L[200001][18], R[200001][18];

static void update(int i, int x) {
    while (i <= n) {
        fen[i] += x;
        i += i & -i;
    }
}

static int query(int i) {
    int ret = 0;
    while (i) {
        ret += fen[i];
        i -= i & -i;
    }
    return ret;
}

static int find(int v) {
    int x = 0;
    for (int i = 18; i--; ) {
        int y = x + (1 << i);
        if (y <= n && fen[y] <= v) v -= fen[x = y];
    }
    return x + 1;
}

void init(int k, vector<int> r) {
    ::k = k;
    n = r.size();
    set<int> can, one;
    auto add = [&](int x) {
        S::update(1, 1, n, x, x, 1e8);
        auto it = can.insert(x).first;
        if (int(can.size()) == 1) {
            one.insert(x);
            return;
        }
        auto it1 = it;
        if (it1 == can.begin()) it1 = prev(can.end());
        else --it1;
        int d1 = (x - *it1 + n) % n;
        auto it2 = next(it);
        if (it2 == can.end()) it2 = can.begin();
        int d2 = (*it2 - x + n) % n;
        if (it1 == it2) {
            one.erase(*it2);
            if (d1 >= k) one.insert(x);
            if (d2 >= k) one.insert(*it2);
            return;
        }
        if (d1 + d2 >= k) one.erase(*it2);
        if (d1 >= k) one.insert(x);
        if (d2 >= k) one.insert(*it2);
    };
    auto del = [&](int x) {
        S::update(1, 1, n, x - k + 1, x, -1);
        S::update(1, 1, n, n + x - k + 1, n + x, -1);
        auto it1 = can.upper_bound(x);
        auto it2 = it1;
        can.erase(x);
        if (one.count(x)) one.erase(x);
        if (can.empty()) return;
        if (it1 == can.begin()) it1 = prev(can.end());
        else --it1;
        int d1 = (x - *it1 + n) % n;
        if (it2 == can.end()) it2 = can.begin();
        int d2 = (*it2 - x + n) % n;
        if (it1 == it2) {
            one.insert(*it2);
            return;
        }
        if (d2 >= k) one.erase(*it2);
        if (d1 + d2 >= k) one.insert(*it2);
    };
    for (int i = 1; i <= n; ++i) {
        S::update(1, 1, n, i, i, r[i - 1]);
    }
    for (int v = n; v > 0; --v) {
        int x;
        while (x = S::find(1, 1, n)) add(x);
        x = *one.begin();
        del(x);
        val[x] = v;
        rev[v] = x;
    }
    for (int i = n - k + 1; i <= n; ++i) update(val[i], 1);
    for (int i = 1; i <= n; ++i) {
        update(val[(i - k + n - 1) % n + 1], -1);
        int x = find(query(val[i]));
        L[i][0] = x <= n ? (i - rev[x] + n) % n : n;
        update(val[i], 1);
    }
    memset(fen, 0, sizeof(fen));
    for (int i = 1; i <= k; ++i) update(val[i], 1);
    for (int i = n; i > 0; --i) {
        update(val[(i + k + n - 1) % n + 1], -1);
        int x = find(query(val[i]));
        R[i][0] = x <= n ? (rev[x] - i + n) % n : n;
        update(val[i], 1);
    }
    for (int l = 1; l < 18; ++l) {
        for (int i = 1; i <= n; ++i) {
            int x = L[i][l - 1];
            L[i][l] = min(x + L[(i - x + n - 1) % n + 1][l - 1], n);
            x = R[i][l - 1];
            R[i][l] = min(x + R[(i + x - 1) % n + 1][l - 1], n);
        }
    }
}

bool goL(int x, int y) {
    int d = (x - y + n) % n;
    for (int i = 18; i--; ) {
        if (L[x][i] <= d) {
            d -= L[x][i];
            x = (x - L[x][i] + n - 1) % n + 1;
        }
    }
    return d < k && val[x] <= val[y];
}

bool goR(int x, int y) {
    int d = (y - x + n) % n;
    for (int i = 18; i--; ) {
        if (R[x][i] <= d) {
            d -= R[x][i];
            x = (x + R[x][i] - 1) % n + 1;
        }
    }
    return d < k && val[x] <= val[y];
}

bool go(int x, int y) {
    return goL(x, y) || goR(x, y);
}

int compare_plants(int x, int y) {
    if (go(x + 1, y + 1)) return -1;
    if (go(y + 1, x + 1)) return 1;
    return 0;
}

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:168:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  168 |         while (x = S::find(1, 1, n)) add(x);
      |                ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 83 ms 3960 KB Output is correct
7 Correct 166 ms 7704 KB Output is correct
8 Correct 595 ms 43516 KB Output is correct
9 Correct 622 ms 42872 KB Output is correct
10 Correct 663 ms 43128 KB Output is correct
11 Correct 714 ms 43644 KB Output is correct
12 Correct 730 ms 43256 KB Output is correct
13 Correct 790 ms 47736 KB Output is correct
14 Correct 591 ms 38392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 4 ms 1408 KB Output is correct
7 Correct 98 ms 4984 KB Output is correct
8 Correct 3 ms 1280 KB Output is correct
9 Correct 6 ms 1408 KB Output is correct
10 Correct 95 ms 4984 KB Output is correct
11 Correct 143 ms 4984 KB Output is correct
12 Correct 105 ms 4984 KB Output is correct
13 Correct 93 ms 4856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 4 ms 1408 KB Output is correct
7 Correct 98 ms 4984 KB Output is correct
8 Correct 3 ms 1280 KB Output is correct
9 Correct 6 ms 1408 KB Output is correct
10 Correct 95 ms 4984 KB Output is correct
11 Correct 143 ms 4984 KB Output is correct
12 Correct 105 ms 4984 KB Output is correct
13 Correct 93 ms 4856 KB Output is correct
14 Correct 137 ms 7672 KB Output is correct
15 Correct 694 ms 39036 KB Output is correct
16 Correct 139 ms 7756 KB Output is correct
17 Correct 705 ms 38904 KB Output is correct
18 Correct 832 ms 43640 KB Output is correct
19 Correct 615 ms 39032 KB Output is correct
20 Correct 657 ms 39160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 128 ms 4344 KB Output is correct
4 Correct 803 ms 41848 KB Output is correct
5 Correct 796 ms 39544 KB Output is correct
6 Correct 838 ms 39032 KB Output is correct
7 Correct 812 ms 38904 KB Output is correct
8 Correct 716 ms 39032 KB Output is correct
9 Correct 746 ms 41208 KB Output is correct
10 Correct 765 ms 39544 KB Output is correct
11 Correct 795 ms 47864 KB Output is correct
12 Correct 634 ms 38892 KB Output is correct
13 Correct 829 ms 45432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 4 ms 1280 KB Output is correct
7 Correct 25 ms 1792 KB Output is correct
8 Correct 20 ms 1792 KB Output is correct
9 Correct 26 ms 1792 KB Output is correct
10 Correct 20 ms 1792 KB Output is correct
11 Correct 24 ms 1792 KB Output is correct
12 Correct 24 ms 1792 KB Output is correct
13 Correct 19 ms 1792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 3 ms 1280 KB Output is correct
6 Correct 691 ms 39160 KB Output is correct
7 Correct 666 ms 38904 KB Output is correct
8 Correct 737 ms 39032 KB Output is correct
9 Correct 651 ms 38904 KB Output is correct
10 Correct 642 ms 41212 KB Output is correct
11 Correct 705 ms 39416 KB Output is correct
12 Correct 698 ms 41848 KB Output is correct
13 Correct 765 ms 39544 KB Output is correct
14 Correct 709 ms 38904 KB Output is correct
15 Correct 687 ms 38860 KB Output is correct
16 Correct 642 ms 40568 KB Output is correct
17 Correct 610 ms 39120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 83 ms 3960 KB Output is correct
7 Correct 166 ms 7704 KB Output is correct
8 Correct 595 ms 43516 KB Output is correct
9 Correct 622 ms 42872 KB Output is correct
10 Correct 663 ms 43128 KB Output is correct
11 Correct 714 ms 43644 KB Output is correct
12 Correct 730 ms 43256 KB Output is correct
13 Correct 790 ms 47736 KB Output is correct
14 Correct 591 ms 38392 KB Output is correct
15 Correct 1 ms 1152 KB Output is correct
16 Correct 1 ms 1152 KB Output is correct
17 Correct 1 ms 1152 KB Output is correct
18 Correct 1 ms 1152 KB Output is correct
19 Correct 1 ms 1152 KB Output is correct
20 Correct 4 ms 1408 KB Output is correct
21 Correct 98 ms 4984 KB Output is correct
22 Correct 3 ms 1280 KB Output is correct
23 Correct 6 ms 1408 KB Output is correct
24 Correct 95 ms 4984 KB Output is correct
25 Correct 143 ms 4984 KB Output is correct
26 Correct 105 ms 4984 KB Output is correct
27 Correct 93 ms 4856 KB Output is correct
28 Correct 137 ms 7672 KB Output is correct
29 Correct 694 ms 39036 KB Output is correct
30 Correct 139 ms 7756 KB Output is correct
31 Correct 705 ms 38904 KB Output is correct
32 Correct 832 ms 43640 KB Output is correct
33 Correct 615 ms 39032 KB Output is correct
34 Correct 657 ms 39160 KB Output is correct
35 Correct 1 ms 1152 KB Output is correct
36 Correct 1 ms 1152 KB Output is correct
37 Correct 128 ms 4344 KB Output is correct
38 Correct 803 ms 41848 KB Output is correct
39 Correct 796 ms 39544 KB Output is correct
40 Correct 838 ms 39032 KB Output is correct
41 Correct 812 ms 38904 KB Output is correct
42 Correct 716 ms 39032 KB Output is correct
43 Correct 746 ms 41208 KB Output is correct
44 Correct 765 ms 39544 KB Output is correct
45 Correct 795 ms 47864 KB Output is correct
46 Correct 634 ms 38892 KB Output is correct
47 Correct 829 ms 45432 KB Output is correct
48 Correct 1 ms 1152 KB Output is correct
49 Correct 1 ms 1152 KB Output is correct
50 Correct 1 ms 1152 KB Output is correct
51 Correct 1 ms 1152 KB Output is correct
52 Correct 1 ms 1152 KB Output is correct
53 Correct 4 ms 1280 KB Output is correct
54 Correct 25 ms 1792 KB Output is correct
55 Correct 20 ms 1792 KB Output is correct
56 Correct 26 ms 1792 KB Output is correct
57 Correct 20 ms 1792 KB Output is correct
58 Correct 24 ms 1792 KB Output is correct
59 Correct 24 ms 1792 KB Output is correct
60 Correct 19 ms 1792 KB Output is correct
61 Correct 115 ms 4216 KB Output is correct
62 Correct 174 ms 7584 KB Output is correct
63 Correct 655 ms 40184 KB Output is correct
64 Correct 774 ms 39032 KB Output is correct
65 Correct 848 ms 39160 KB Output is correct
66 Correct 811 ms 38860 KB Output is correct
67 Correct 724 ms 38944 KB Output is correct
68 Correct 787 ms 41124 KB Output is correct
69 Correct 855 ms 39344 KB Output is correct
70 Correct 830 ms 41848 KB Output is correct
71 Correct 921 ms 39672 KB Output is correct
72 Correct 864 ms 38904 KB Output is correct
73 Correct 811 ms 38904 KB Output is correct
74 Correct 684 ms 40044 KB Output is correct
75 Correct 693 ms 39136 KB Output is correct