Submission #781896

# Submission time Handle Problem Language Result Execution time Memory
781896 2023-07-13T12:49:35 Z boris_mihov Comparing Plants (IOI20_plants) C++17
32 / 100
264 ms 22760 KB
#include "plants.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int n, k;
struct SegmentTree
{
    struct Node
    {
        int max;
        int lazy;
        int leftPos;
        int rightPos;

        Node()
        {
            max = lazy = leftPos = rightPos = 0;
        }
    };

    Node tree[4*MAXN];
    Node combine(Node left, Node right)
    {
        if (left.max == right.max)
        {
            Node res;
            res.max = left.max;
            res.leftPos = left.leftPos;
            res.rightPos = right.rightPos;
            return res;
        }

        if (left.max > right.max)
        {
            return left;
        } else
        {
            return right;
        }
    }

    void push(int node, int l, int r)
    {
        if (tree[node].lazy == 0)
        {
            return;
        }

        tree[node].max += tree[node].lazy;
        if (l < r)
        {
            tree[2*node].lazy += tree[node].lazy;
            tree[2*node + 1].lazy += tree[node].lazy;
        }

        tree[node].lazy = 0;
    }

    void build(int l, int r, int node, int vals[])
    {
        if (l == r)
        {
            tree[node].max = vals[l];
            tree[node].leftPos = l;
            tree[node].rightPos = l;
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, vals);
        build(mid + 1, r, 2*node + 1, vals);
        tree[node] = combine(tree[2*node], tree[2*node + 1]);
    }

    void pointUpdate(int l, int r, int node, int queryPos)
    {
        push(node, l, r);
        if (queryPos < l || r < queryPos)
        {
            return;
        }

        if (l == r)
        {
            tree[node].max = -INF;
            return;
        }

        int mid = (l + r) / 2;
        pointUpdate(l, mid, 2*node, queryPos);
        pointUpdate(mid + 1 , r, 2*node + 1, queryPos);
        tree[node] = combine(tree[2*node], tree[2*node + 1]);
    }

    void update(int l, int r, int node, int queryL, int queryR)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy++;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR);
        update(mid + 1 , r, 2*node + 1, queryL, queryR);
        tree[node] = combine(tree[2*node], tree[2*node + 1]);
    }

    int findFirstEqual(int l, int r, int node, int queryPos, int max)
    {
        push(node, l, r);
        if (r < queryPos || tree[node].max < max)
        {
            return 0;
        }

        if (l == r)
        {
            return l;
        }

        int mid = (l + r) / 2;
        if (l >= queryPos)
        {
            push(2*node, l, mid);
            if (tree[2*node].max == max) return findFirstEqual(l, mid, 2*node, queryPos, max);
            else return findFirstEqual(mid + 1, r, 2*node + 1, queryPos, max);
        }
    
        int res = findFirstEqual(l, mid, 2*node, queryPos, max);
        if (res != 0) return res;
        return findFirstEqual(mid + 1, r, 2*node + 1, queryPos, max);
    }

    void build(int vals[])
    {
        build(1, n, 1, vals);
    }

    void pointUpdate(int idx)
    {
        pointUpdate(1, n, 1, idx);
    }

    void update(int idx)
    {
        if (idx >= k)
        {
            update(1, n, 1, idx - k + 1, idx - 1);
        } else
        {
            if (idx > 1) update(1, n, 1, 1, idx - 1);
            update(1, n, 1, n - (k - idx) + 1, n);
        }
    }

    int findMAX()
    {
        if (tree[1].leftPos + k > tree[1].rightPos) return tree[1].leftPos;
        return findFirstEqual(1, n, 1, tree[1].leftPos + k, tree[1].max);
    }
};

int p[MAXN];
int r[MAXN];
int prefix[MAXN];
int prefix2[MAXN];
SegmentTree tree;

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

    tree.build(r);
    for (int currTry = 1 ; currTry <= n ; ++currTry)
    {
        int max = tree.findMAX();
        p[max] = currTry;
        tree.pointUpdate(max);
        tree.update(max);
    }
}

int getSum(int x, int y)
{
    if (x < y) return prefix[y - 1] - prefix[x - 1];
    return prefix[n] - (prefix[x - 1] - prefix[y - 1]);
}

int getSum2(int x, int y)
{
    if (x < y) return prefix2[y - 1] - prefix2[x - 1];
    return prefix2[n] - (prefix2[x - 1] - prefix2[y - 1]);
}

int compare_plants(int x, int y)
{
    if (x > y)
    {
        return -compare_plants(y, x);
    }

    x++;
    y++;
	if (k == 2)
    {
        if (getSum(x, y) == 0 || getSum2(y, x) == 0) return 1;
        if (getSum2(x, y) == 0 || getSum(y, x) == 0) return -1;
        return 0;
    }

    if (p[x] < p[y]) return -1;
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 6 ms 12884 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 6 ms 12756 KB Output is correct
5 Correct 7 ms 12736 KB Output is correct
6 Correct 45 ms 16596 KB Output is correct
7 Correct 76 ms 18252 KB Output is correct
8 Correct 156 ms 22760 KB Output is correct
9 Correct 159 ms 22756 KB Output is correct
10 Correct 158 ms 22720 KB Output is correct
11 Correct 152 ms 22760 KB Output is correct
12 Correct 154 ms 22700 KB Output is correct
13 Correct 146 ms 22744 KB Output is correct
14 Correct 133 ms 22744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 7 ms 12756 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 6 ms 12852 KB Output is correct
6 Correct 10 ms 12884 KB Output is correct
7 Correct 50 ms 15728 KB Output is correct
8 Correct 7 ms 12884 KB Output is correct
9 Correct 9 ms 12884 KB Output is correct
10 Correct 49 ms 15724 KB Output is correct
11 Correct 45 ms 15700 KB Output is correct
12 Correct 46 ms 15868 KB Output is correct
13 Correct 53 ms 15700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 7 ms 12756 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 7 ms 12756 KB Output is correct
5 Correct 6 ms 12852 KB Output is correct
6 Correct 10 ms 12884 KB Output is correct
7 Correct 50 ms 15728 KB Output is correct
8 Correct 7 ms 12884 KB Output is correct
9 Correct 9 ms 12884 KB Output is correct
10 Correct 49 ms 15724 KB Output is correct
11 Correct 45 ms 15700 KB Output is correct
12 Correct 46 ms 15868 KB Output is correct
13 Correct 53 ms 15700 KB Output is correct
14 Correct 65 ms 16048 KB Output is correct
15 Correct 264 ms 19816 KB Output is correct
16 Correct 67 ms 16236 KB Output is correct
17 Correct 259 ms 19944 KB Output is correct
18 Correct 177 ms 19984 KB Output is correct
19 Correct 171 ms 19940 KB Output is correct
20 Correct 224 ms 20004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 6 ms 12756 KB Output is correct
3 Incorrect 48 ms 15624 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12756 KB Output is correct
2 Correct 7 ms 12784 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Incorrect 6 ms 12800 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 12780 KB Output is correct
2 Correct 8 ms 12756 KB Output is correct
3 Correct 7 ms 12756 KB Output is correct
4 Incorrect 6 ms 12840 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12756 KB Output is correct
2 Correct 6 ms 12884 KB Output is correct
3 Correct 5 ms 12756 KB Output is correct
4 Correct 6 ms 12756 KB Output is correct
5 Correct 7 ms 12736 KB Output is correct
6 Correct 45 ms 16596 KB Output is correct
7 Correct 76 ms 18252 KB Output is correct
8 Correct 156 ms 22760 KB Output is correct
9 Correct 159 ms 22756 KB Output is correct
10 Correct 158 ms 22720 KB Output is correct
11 Correct 152 ms 22760 KB Output is correct
12 Correct 154 ms 22700 KB Output is correct
13 Correct 146 ms 22744 KB Output is correct
14 Correct 133 ms 22744 KB Output is correct
15 Correct 5 ms 12756 KB Output is correct
16 Correct 7 ms 12756 KB Output is correct
17 Correct 5 ms 12756 KB Output is correct
18 Correct 7 ms 12756 KB Output is correct
19 Correct 6 ms 12852 KB Output is correct
20 Correct 10 ms 12884 KB Output is correct
21 Correct 50 ms 15728 KB Output is correct
22 Correct 7 ms 12884 KB Output is correct
23 Correct 9 ms 12884 KB Output is correct
24 Correct 49 ms 15724 KB Output is correct
25 Correct 45 ms 15700 KB Output is correct
26 Correct 46 ms 15868 KB Output is correct
27 Correct 53 ms 15700 KB Output is correct
28 Correct 65 ms 16048 KB Output is correct
29 Correct 264 ms 19816 KB Output is correct
30 Correct 67 ms 16236 KB Output is correct
31 Correct 259 ms 19944 KB Output is correct
32 Correct 177 ms 19984 KB Output is correct
33 Correct 171 ms 19940 KB Output is correct
34 Correct 224 ms 20004 KB Output is correct
35 Correct 6 ms 12756 KB Output is correct
36 Correct 6 ms 12756 KB Output is correct
37 Incorrect 48 ms 15624 KB Output isn't correct
38 Halted 0 ms 0 KB -