Submission #422164

# Submission time Handle Problem Language Result Execution time Memory
422164 2021-06-09T19:50:57 Z idk321 Comparing Plants (IOI20_plants) C++17
32 / 100
508 ms 11116 KB
#include "plants.h"

#include <bits/stdc++.h>
using namespace std;

int k;
vector<int> r;
vector<int> prefR;
const int N = 200005;
int tree[4 * N];
int lazy[4 * N];
const int M = 1000000000;


int n;
int getSum(int from, int to)
{
    int res = prefR[to + 1];
    res -= prefR[from];
    return res;
}

void push(int node)
{
    for (int i = 0; i <= 1; i++)
    {
        tree[node * 2 +i] += lazy[node];
        lazy[node * 2 + i] += lazy[node];
    }
    lazy[node] = 0;
}

void add(int num, int from, int to, int a, int b, int node)
{
    if (from <= a && b <= to)
    {
        tree[node] += num;
        lazy[node] += num;
        return;
    }

    push(node);
    int mid = (a + b) / 2;
    if (from <= mid) add(num, from, to, a, mid, node * 2);
    if (to > mid) add(num, from, to, mid + 1, b, node * 2+ 1);

    if (tree[node * 2] <= tree[node * 2 +1])
    {

        tree[node] = tree[node * 2];
    } else
    {

        tree[node] = tree[node * 2 + 1];
    }
}

int minIndOn(int from, int to, int a, int b, int node)
{
    if(tree[node] != 0) return -1;
    if (a == b)
    {
        return a;
    }

    push(node);
    int mid = (a + b) / 2;
    int res = -1;
    if (from <= mid) res = minIndOn(from, to, a, mid, node * 2);
    if (res == -1 && to > mid) res = minIndOn(from, to, mid + 1, b, node * 2 + 1);

    return res;
}

vector<int> order;

void init(int K, std::vector<int> R) {
    k = K;
    r = R;
    n = r.size();
    prefR.resize(r.size() + 1);
    for (int i = 1; i <= r.size(); i++)
    {
        prefR[i] = r[i - 1];
        prefR[i] += prefR[i - 1];
    }

    order.resize(n);


    if (k == 2) return;

    for (int i = 0; i < n; i++)
    {
        add(r[i], i, i, 0, n - 1, 1);
    }

    for (int t = 0; t < n; t++)
    {
        int res = -1;

        int res1 = minIndOn(0, n - 1, 0, n - 1, 1);


        if (res1 + k <= n - 1)
        {
            int res2 = minIndOn(res1 + k, n - 1, 0, n - 1, 1);

            if (res2 != -1) res = res2;
            else res = res1;
        } else res = res1;



        order[res] = t;
        add(M, res, res, 0, n - 1, 1);

        if (res != 0) add(-1, max(0, res - k + 1), res - 1, 0, n - 1, 1);
        int still = k - (res - max(0, res - k + 1) + 1);
        if (still > 0)
        {
            add(-1, n - still, n - 1, 0, n - 1, 1);
        }


    }



	return;
}

int compare_plants(int x, int y) {
    if (k == 2)
    {
        if (getSum(x, y - 1) == (y - x)) return -1;
        if (getSum(x, y - 1) == 0) return 1;
        int sum1 = getSum(y, n -1);
        if (x != 0) sum1 += getSum(0, x - 1);
        if (sum1 == (n - y) + x) return 1;
        if (sum1 == 0) return -1;
        return 0;
    }


    if (order[x] < order[y])
    {
        return 1;
    }
    if (order[x] > order[y]) return -1;

	return 0;
}

//3 2 1 4 0
/*
5 3 1
0 1 1 0 2
0 1
*/

Compilation message

plants.cpp: In function 'void init(int, std::vector<int>)':
plants.cpp:82:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |     for (int i = 1; i <= r.size(); i++)
      |                     ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 86 ms 3424 KB Output is correct
7 Correct 71 ms 3764 KB Output is correct
8 Correct 100 ms 6888 KB Output is correct
9 Correct 117 ms 6892 KB Output is correct
10 Correct 98 ms 6888 KB Output is correct
11 Correct 94 ms 6892 KB Output is correct
12 Correct 93 ms 6892 KB Output is correct
13 Correct 96 ms 6888 KB Output is correct
14 Correct 94 ms 9408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 102 ms 3696 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 4 ms 460 KB Output is correct
10 Correct 72 ms 3648 KB Output is correct
11 Correct 68 ms 3576 KB Output is correct
12 Correct 68 ms 3748 KB Output is correct
13 Correct 76 ms 3728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 102 ms 3696 KB Output is correct
8 Correct 3 ms 332 KB Output is correct
9 Correct 4 ms 460 KB Output is correct
10 Correct 72 ms 3648 KB Output is correct
11 Correct 68 ms 3576 KB Output is correct
12 Correct 68 ms 3748 KB Output is correct
13 Correct 76 ms 3728 KB Output is correct
14 Correct 102 ms 4384 KB Output is correct
15 Correct 497 ms 10952 KB Output is correct
16 Correct 97 ms 4332 KB Output is correct
17 Correct 501 ms 11116 KB Output is correct
18 Correct 379 ms 11064 KB Output is correct
19 Correct 380 ms 11068 KB Output is correct
20 Correct 508 ms 11112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Runtime error 1 ms 588 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Runtime error 2 ms 588 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Runtime error 2 ms 588 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 86 ms 3424 KB Output is correct
7 Correct 71 ms 3764 KB Output is correct
8 Correct 100 ms 6888 KB Output is correct
9 Correct 117 ms 6892 KB Output is correct
10 Correct 98 ms 6888 KB Output is correct
11 Correct 94 ms 6892 KB Output is correct
12 Correct 93 ms 6892 KB Output is correct
13 Correct 96 ms 6888 KB Output is correct
14 Correct 94 ms 9408 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 3 ms 460 KB Output is correct
21 Correct 102 ms 3696 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 4 ms 460 KB Output is correct
24 Correct 72 ms 3648 KB Output is correct
25 Correct 68 ms 3576 KB Output is correct
26 Correct 68 ms 3748 KB Output is correct
27 Correct 76 ms 3728 KB Output is correct
28 Correct 102 ms 4384 KB Output is correct
29 Correct 497 ms 10952 KB Output is correct
30 Correct 97 ms 4332 KB Output is correct
31 Correct 501 ms 11116 KB Output is correct
32 Correct 379 ms 11064 KB Output is correct
33 Correct 380 ms 11068 KB Output is correct
34 Correct 508 ms 11112 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Runtime error 1 ms 588 KB Execution killed with signal 11
37 Halted 0 ms 0 KB -