Submission #300598

# Submission time Handle Problem Language Result Execution time Memory
300598 2020-09-17T10:05:31 Z imeimi2000 Comparing Plants (IOI20_plants) C++17
32 / 100
460 ms 14648 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 sum[200001], val[200001];
void init(int k, vector<int> r) {
    ::k = k;
    n = r.size();
    for (int i = 1; i <= n; ++i) {
        sum[i] = sum[i - 1] + r[i - 1];
    }
    if (k + k > n) {
        set<int> can, one;
        auto add = [&](int x) {
            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) {
            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);
                S::update(1, 1, n, x, x, 1e8);
            }
            x = *one.begin();
            S::update(1, 1, n, x - k + 1, x, -1);
            S::update(1, 1, n, n + x - k + 1, n + x, -1);
            del(x);
            val[x] = v;
        }
    }
}

int compare_plants(int x, int y) {
    if (k == 2) {
        int cnt = sum[y] - sum[x];
        int max = y - x;
        if (cnt == 0) return 1;
        if (cnt == max) return -1;
        cnt = sum[n] - cnt;
        max = n - max;
        if (cnt == 0) return -1;
        if (cnt == max) return 1;
        return 0;
    }
    if (val[x + 1] < val[y + 1]) return -1;
    if (val[x + 1] > val[y + 1]) return 1;
    return 0;
}

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:142:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  142 |             while (x = S::find(1, 1, n)) {
      |                    ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 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 63 ms 3120 KB Output is correct
7 Correct 86 ms 3320 KB Output is correct
8 Correct 108 ms 4984 KB Output is correct
9 Correct 107 ms 5112 KB Output is correct
10 Correct 103 ms 4984 KB Output is correct
11 Correct 102 ms 4984 KB Output is correct
12 Correct 114 ms 4988 KB Output is correct
13 Correct 100 ms 5112 KB Output is correct
14 Correct 99 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 77 ms 3448 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 76 ms 3448 KB Output is correct
11 Correct 76 ms 3448 KB Output is correct
12 Correct 87 ms 3452 KB Output is correct
13 Correct 80 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 77 ms 3448 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 76 ms 3448 KB Output is correct
11 Correct 76 ms 3448 KB Output is correct
12 Correct 87 ms 3452 KB Output is correct
13 Correct 80 ms 3448 KB Output is correct
14 Correct 102 ms 3992 KB Output is correct
15 Correct 460 ms 9848 KB Output is correct
16 Correct 105 ms 4088 KB Output is correct
17 Correct 449 ms 10056 KB Output is correct
18 Correct 437 ms 14648 KB Output is correct
19 Correct 337 ms 9880 KB Output is correct
20 Correct 436 ms 9848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Incorrect 0 ms 256 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Incorrect 1 ms 256 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 360 KB Output is correct
4 Incorrect 1 ms 384 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 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 63 ms 3120 KB Output is correct
7 Correct 86 ms 3320 KB Output is correct
8 Correct 108 ms 4984 KB Output is correct
9 Correct 107 ms 5112 KB Output is correct
10 Correct 103 ms 4984 KB Output is correct
11 Correct 102 ms 4984 KB Output is correct
12 Correct 114 ms 4988 KB Output is correct
13 Correct 100 ms 5112 KB Output is correct
14 Correct 99 ms 4984 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 77 ms 3448 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 76 ms 3448 KB Output is correct
25 Correct 76 ms 3448 KB Output is correct
26 Correct 87 ms 3452 KB Output is correct
27 Correct 80 ms 3448 KB Output is correct
28 Correct 102 ms 3992 KB Output is correct
29 Correct 460 ms 9848 KB Output is correct
30 Correct 105 ms 4088 KB Output is correct
31 Correct 449 ms 10056 KB Output is correct
32 Correct 437 ms 14648 KB Output is correct
33 Correct 337 ms 9880 KB Output is correct
34 Correct 436 ms 9848 KB Output is correct
35 Correct 1 ms 256 KB Output is correct
36 Incorrect 0 ms 256 KB Output isn't correct
37 Halted 0 ms 0 KB -