Submission #1069453

# Submission time Handle Problem Language Result Execution time Memory
1069453 2024-08-22T00:46:54 Z GusterGoose27 Distributing Candies (IOI21_candies) C++17
100 / 100
262 ms 51656 KB
#include "candies.h"

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

const int MAXN = 2e5+5;
const int SEG = 262144;
typedef long long ll;
const ll inf = 1e18;

struct seg_info {
    ll mn_pre, mx_pre, mn_sum, mx_sum, sum;
    seg_info() {
        mn_pre = 0;
        mx_pre = 0;
        mn_sum = 0;
        mx_sum = 0;
        sum = 0;
    }
    seg_info(seg_info l, seg_info r) {
        reupd(l, r);
    }
    void reupd(seg_info &l, seg_info &r) {
        mn_pre = min(l.mn_pre, l.sum+r.mn_pre);
        mx_pre = max(l.mx_pre, l.sum+r.mx_pre);
        mn_sum = min(min(l.mn_sum, r.mn_sum), r.mn_pre + l.sum - l.mx_pre);
        mx_sum = max(max(l.mx_sum, r.mx_sum), r.mx_pre + l.sum - l.mn_pre);
        sum = l.sum + r.sum;
    }
    void upd(int v) {
        mn_pre = min(0, v);
        mx_pre = max(0, v);
        mn_sum = mn_pre;
        mx_sum = mx_pre;
        sum = v;
    }
};

seg_info stree[2*SEG];

seg_info collect(int l, int r) { // inclusive
    l += SEG;
    r += SEG+1;
    seg_info li, ri;
    for (; r > l; l /= 2, r /= 2) {
        if (l & 1) {
            li = seg_info(li, stree[l++]);
        }
        if (r & 1) {
            ri = seg_info(stree[--r], ri);
        }
    }
    return seg_info(li, ri);
}

void upd(int p, int v) {
    p += SEG;
    stree[p].upd(v);
    for (p /= 2; p > 0; p /= 2) {
        stree[p].reupd(stree[2*p], stree[2*p+1]);
    }
}

seg_info violate_pos(int v, int cur = 1, seg_info r_info = seg_info()) {
    if (cur >= SEG) return seg_info(stree[cur], r_info);
    seg_info rcomb(stree[2*cur+1], r_info);
    if (max(rcomb.mx_sum, -rcomb.mn_sum) > v) return violate_pos(v, 2*cur+1, r_info);
    return violate_pos(v, 2*cur, rcomb);
}

int get_final(int v) {
    seg_info viol_info = (max(stree[1].mx_sum, -stree[1].mn_sum) <= v) ? stree[1] : violate_pos(v);
    if (viol_info.mx_sum > v) return v+viol_info.sum-viol_info.mx_pre;
    else return viol_info.sum-viol_info.mn_pre;
}

vector<int> ins[MAXN];
vector<int> del[MAXN];

vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    int n = c.size();
    int q = l.size();
    vector<int> ans;
    for (int i = 0; i < q; i++) {
        ins[l[i]].push_back(i);
        del[r[i]+1].push_back(i);
    }
    for (int i = 0; i < n; i++) {
        for (int j: ins[i]) upd(j, v[j]);
        for (int j: del[i]) upd(j, 0);
        ans.push_back(get_final(c[i]));
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 30300 KB Output is correct
2 Correct 5 ms 30300 KB Output is correct
3 Correct 6 ms 30260 KB Output is correct
4 Correct 6 ms 30336 KB Output is correct
5 Correct 8 ms 30296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 240 ms 44876 KB Output is correct
2 Correct 235 ms 44744 KB Output is correct
3 Correct 226 ms 44744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 30296 KB Output is correct
2 Correct 153 ms 40760 KB Output is correct
3 Correct 61 ms 36160 KB Output is correct
4 Correct 262 ms 50876 KB Output is correct
5 Correct 246 ms 51144 KB Output is correct
6 Correct 254 ms 51656 KB Output is correct
7 Correct 220 ms 50888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 30552 KB Output is correct
2 Correct 4 ms 30300 KB Output is correct
3 Correct 64 ms 39400 KB Output is correct
4 Correct 54 ms 34260 KB Output is correct
5 Correct 115 ms 43196 KB Output is correct
6 Correct 118 ms 43708 KB Output is correct
7 Correct 122 ms 44452 KB Output is correct
8 Correct 119 ms 42940 KB Output is correct
9 Correct 138 ms 44480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 30300 KB Output is correct
2 Correct 5 ms 30300 KB Output is correct
3 Correct 6 ms 30260 KB Output is correct
4 Correct 6 ms 30336 KB Output is correct
5 Correct 8 ms 30296 KB Output is correct
6 Correct 240 ms 44876 KB Output is correct
7 Correct 235 ms 44744 KB Output is correct
8 Correct 226 ms 44744 KB Output is correct
9 Correct 5 ms 30296 KB Output is correct
10 Correct 153 ms 40760 KB Output is correct
11 Correct 61 ms 36160 KB Output is correct
12 Correct 262 ms 50876 KB Output is correct
13 Correct 246 ms 51144 KB Output is correct
14 Correct 254 ms 51656 KB Output is correct
15 Correct 220 ms 50888 KB Output is correct
16 Correct 5 ms 30552 KB Output is correct
17 Correct 4 ms 30300 KB Output is correct
18 Correct 64 ms 39400 KB Output is correct
19 Correct 54 ms 34260 KB Output is correct
20 Correct 115 ms 43196 KB Output is correct
21 Correct 118 ms 43708 KB Output is correct
22 Correct 122 ms 44452 KB Output is correct
23 Correct 119 ms 42940 KB Output is correct
24 Correct 138 ms 44480 KB Output is correct
25 Correct 4 ms 30296 KB Output is correct
26 Correct 48 ms 34316 KB Output is correct
27 Correct 155 ms 40380 KB Output is correct
28 Correct 252 ms 49352 KB Output is correct
29 Correct 253 ms 49800 KB Output is correct
30 Correct 254 ms 49864 KB Output is correct
31 Correct 247 ms 50056 KB Output is correct
32 Correct 257 ms 50364 KB Output is correct