Submission #1021132

# Submission time Handle Problem Language Result Execution time Memory
1021132 2024-07-12T14:39:50 Z mdn2002 Distributing Candies (IOI21_candies) C++17
3 / 100
5000 ms 1304920 KB
/*
Mayoeba Yabureru
*/
#include<bits/stdc++.h>
using namespace std;

struct Node {
    int valuel = 0, valuer = 0, lazyadd = 0, lazyset = -1;
    int left = -1, right = -1;
    Node() {}
};
Node st[55000006];
int cnt;
void push(int node, int l, int r) {
    int left = st[node].left, right = st[node].right;
    if (st[node].lazyset != -1) {
        //cout << '*' << l << ' ' << r << ' ' << st[node].lazyset << ' ' << st[node].lazyadd << endl;
        if (st[node].lazyset == 0) {
            st[node].valuel = 0;
            st[node].valuer = 0;
        }
        else {
            st[node].valuel = l;
            st[node].valuer = r;
        }
        st[node].valuel += st[node].lazyadd;
        st[node].valuer += st[node].lazyadd;
        if (l != r) {
            st[left].lazyset = st[right].lazyset = st[node].lazyset;
            st[left].lazyadd = st[right].lazyadd = 0;
            st[left].lazyadd += st[node].lazyadd;
            st[right].lazyadd += st[node].lazyadd;
        }
        st[node].lazyset = -1;
        st[node].lazyadd = 0;
    }
    else if (st[node].lazyadd != 0) {
        st[left].lazyadd += st[node].lazyadd;
        st[right].lazyadd += st[node].lazyadd;
        st[node].valuel += st[node].lazyadd;
        st[node].valuer += st[node].lazyadd;
        st[node].lazyadd = 0;
    }
}
void update(int node, int l, int r, int val) {
    if (st[node].left == -1) {
        st[node].left = ++ cnt;
        st[cnt] = Node();
    }
    if (st[node].right == -1) {
        st[node].right = ++ cnt;
        st[cnt] = Node();
    }
    //cout << ' ' << l << ' ' << r << ' ' << st[node].lazyset << endl;
    push(node, l, r);
    long long vall = st[node].valuel, valr = st[node].valuer;
    //cout << ' ' << l << ' ' << r << ' ' << vall << ' ' << valr << ' ' << val << endl;

    if (val >= 0 && vall + val > l && valr + val > r) { // all the boxes with cap inbetween l and r are set to equal the cap
        st[node].lazyadd = 0;
        st[node].lazyset = 1;
        push(node, l, r);
        return;
    }

    if (val >= 0 && vall + val <= l && valr + val <= r) { // all the boxes with cap inbetween l and r are added val
        st[node].lazyadd = val;
        push(node, l, r);
        return;
    }

    if (val < 0 && vall + val < 0 && valr + val < 0) { // all the boxes with cap inbetween l and r are set to equal the 0
        st[node].lazyadd = 0;
        st[node].lazyset = 0;
        push(node, l, r);
        return;
    }

    if (val < 0 && vall + val >= 0 && valr + val >= 0) { // all the boxes with cap inbetween l and r are added val
        st[node].lazyadd = val;
        push(node, l, r);
        return;
    }

    int mid = (l + r) / 2;

    int left = st[node].left, right = st[node].right;
    update(left, l, mid, val);
    update(right, mid + 1, r, val);
    st[node].valuel = st[left].valuel;
    st[node].valuer = st[right].valuer;
}

int query(int node, int l, int r, int x) {
    if (st[node].left == -1) {
        st[node].left = ++ cnt;
        st[cnt] = Node();
    }
    if (st[node].right == -1) {
        st[node].right = ++ cnt;
        st[cnt] = Node();
    }
    push(node, l, r);
    if (l == x && r == x) return st[node].valuel;
    int mid = (l + r) / 2;
    if (x <= mid) return query(st[node].left, l, mid, x);
    return query(st[node].right, mid + 1, r, x);
}

vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    int n = c.size();
    st[0] = Node();
    vector<int> s(n);
    for (int i = 0; i < v.size(); i ++) {
        for (int j = l[i]; j <= r[i]; j ++) {
            s[j] += v[i];
            s[j] = min(s[j], c[j]);
            s[j] = max(s[j], 0);
        }
    }
    return s;
}

/*
3
10 15 13
2
0 2 20
0 2 -11
*/

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:115:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  115 |     for (int i = 0; i < v.size(); i ++) {
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 403 ms 1291856 KB Output is correct
2 Correct 415 ms 1291856 KB Output is correct
3 Correct 439 ms 1291856 KB Output is correct
4 Correct 414 ms 1292000 KB Output is correct
5 Correct 407 ms 1292112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5113 ms 1303824 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 402 ms 1291960 KB Output is correct
2 Correct 552 ms 1299536 KB Output is correct
3 Correct 533 ms 1297732 KB Output is correct
4 Execution timed out 5117 ms 1304920 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 431 ms 1291860 KB Output is correct
2 Correct 408 ms 1291860 KB Output is correct
3 Correct 755 ms 1299220 KB Output is correct
4 Correct 732 ms 1295616 KB Output is correct
5 Execution timed out 5064 ms 1302488 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 403 ms 1291856 KB Output is correct
2 Correct 415 ms 1291856 KB Output is correct
3 Correct 439 ms 1291856 KB Output is correct
4 Correct 414 ms 1292000 KB Output is correct
5 Correct 407 ms 1292112 KB Output is correct
6 Execution timed out 5113 ms 1303824 KB Time limit exceeded
7 Halted 0 ms 0 KB -