Submission #515975

# Submission time Handle Problem Language Result Execution time Memory
515975 2022-01-20T08:51:00 Z apostoldaniel854 Distributing Candies (IOI21_candies) C++17
100 / 100
368 ms 43272 KB
#include "candies.h"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define dbg(x) cerr << #x << " " << x << "\n

struct node_info {
    ll sum;
    ll sum_max;
    ll sum_min;
};

class SegTree {
private:
    vector <node_info> seg;
    node_info join(node_info L, node_info R) {
        node_info RES;
        RES.sum = L.sum + R.sum;
        RES.sum_max = max(L.sum_max, R.sum_max + L.sum);
        RES.sum_min = min(L.sum_min, R.sum_min + L.sum);
        return RES;
    }
public:
    SegTree(int n) {
        seg.resize(1 + 4 * n);
    }
    void update_pos(int node, int lb, int rb, int pos, int val) {
        if (lb == rb) {
            seg[node].sum_max = max(0, val);
            seg[node].sum_min = min(0, val);
            seg[node].sum = val;
            return;
        }
        int mid = (lb + rb) / 2, lnode = node * 2, rnode = node * 2 + 1;
        if (pos <= mid)
            update_pos(lnode, lb, mid, pos, val);
        else
            update_pos(rnode, mid + 1, rb, pos, val);
        seg[node] = join(seg[lnode], seg[rnode]);
    }
    ll go(int node, int lb, int rb, ll cap) {
        if (lb == rb)
            return min(max(seg[node].sum, 0ll), cap);
        int mid = (lb + rb) / 2, lnode = node * 2, rnode = node * 2 + 1;
        if (seg[rnode].sum_max - seg[rnode].sum_min > cap)
            return go(rnode, mid + 1, rb, cap);
        ll value = go(lnode, lb, mid, cap);
        if (value + seg[rnode].sum_max > cap)
            return cap - (seg[rnode].sum_max - seg[rnode].sum);
        if (value + seg[rnode].sum_min < 0)
            return seg[rnode].sum - seg[rnode].sum_min;
        return value + seg[rnode].sum;
    }
};

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    int n = c.size(), q = v.size();
    vector <vector <int>> L(n), R(n);
    for (int i = 0; i < q; i++) {
        L[l[i]].push_back(i + 1);
        if (r[i] + 1 < n)
            R[r[i] + 1].push_back(i + 1);
    }
    SegTree DS_time(q);
    std::vector<int> s(n);
    for (int i = 0; i < n; i++) {
        for (int id : L[i]) {
            /// activate id
            DS_time.update_pos(1, 1, q, id, v[id - 1]);
        }
        for (int id : R[i]) {
            /// deactivate id
            DS_time.update_pos(1, 1, q, id, 0);
        }
        s[i] = DS_time.go(1, 1, q, c[i]);
    }
    return s;
}
/**
3
10 15 13
2
0 2 20
0 1 -11

**/

Compilation message

candies.cpp:7:42: warning: missing terminating " character
    7 | #define dbg(x) cerr << #x << " " << x << "\n
      |                                          ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 444 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 43224 KB Output is correct
2 Correct 320 ms 43240 KB Output is correct
3 Correct 320 ms 43220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 159 ms 27296 KB Output is correct
3 Correct 54 ms 12868 KB Output is correct
4 Correct 354 ms 43220 KB Output is correct
5 Correct 324 ms 43204 KB Output is correct
6 Correct 368 ms 43272 KB Output is correct
7 Correct 325 ms 43132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 80 ms 25300 KB Output is correct
4 Correct 54 ms 12684 KB Output is correct
5 Correct 131 ms 36864 KB Output is correct
6 Correct 129 ms 36912 KB Output is correct
7 Correct 135 ms 36940 KB Output is correct
8 Correct 127 ms 36964 KB Output is correct
9 Correct 140 ms 36988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 444 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 326 ms 43224 KB Output is correct
7 Correct 320 ms 43240 KB Output is correct
8 Correct 320 ms 43220 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 159 ms 27296 KB Output is correct
11 Correct 54 ms 12868 KB Output is correct
12 Correct 354 ms 43220 KB Output is correct
13 Correct 324 ms 43204 KB Output is correct
14 Correct 368 ms 43272 KB Output is correct
15 Correct 325 ms 43132 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 80 ms 25300 KB Output is correct
19 Correct 54 ms 12684 KB Output is correct
20 Correct 131 ms 36864 KB Output is correct
21 Correct 129 ms 36912 KB Output is correct
22 Correct 135 ms 36940 KB Output is correct
23 Correct 127 ms 36964 KB Output is correct
24 Correct 140 ms 36988 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 54 ms 12840 KB Output is correct
27 Correct 164 ms 27344 KB Output is correct
28 Correct 314 ms 43172 KB Output is correct
29 Correct 346 ms 43236 KB Output is correct
30 Correct 334 ms 43216 KB Output is correct
31 Correct 312 ms 43172 KB Output is correct
32 Correct 327 ms 43200 KB Output is correct