Submission #563385

# Submission time Handle Problem Language Result Execution time Memory
563385 2022-05-17T05:08:22 Z 12anicul Distributing Candies (IOI21_candies) C++17
100 / 100
512 ms 50836 KB
#include "candies.h"
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;
using ll = long long;
const int N = 2e5 + 10;
//subtask 1
/*
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();
    std::vector<int> s(n);
    int q = l.size();
    for (int i = 0; i < q; i ++) {
        for (int j = l[i]; j <= r[i]; j ++) {
            s[j] = max(0, min(s[j] + v[i], c[j]));
        }
    }
    return s;

}
*/
//subtask 2
/*
const int INF = 1e9 + 7;
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();
    std::vector<int> s(n);
    int q = l.size();
    vector<long long> helper(n + 1);
    for (int i = 0; i < q; i++) {
        helper[l[i]] += v[i];
        helper[r[i] + 1] -= v[i];
    }
    for (int i = 1; i < n; i++) {
        helper[i] += helper[i - 1];
    }
    for (int i = 0; i < n; i++) {
        s[i] = (int) (min(1LL * c[i], helper[i]));
    }
    return s;

}
*/
int find_mid(int l, int r) {
    return l + r >> 1;
}
//subtask 3
struct Node {
    pair<ll, int> ma;
    pair<ll, int> mi;
    ll lazy;
    void apply(ll val) {
        ma.st += val;
        mi.st += val;
        lazy += val;
    }
}node[4 * N];

void push(int id, int l, int r) {
    if (l == r || node[id].lazy == 0) return;
    int l_node = id << 1, r_node = id << 1 | 1;
    node[l_node].apply(node[id].lazy);
    node[r_node].apply(node[id].lazy);
    node[id].lazy = 0;
}

void update(int id, int l, int r, int up_l, int up_r, int val) {
    if (l > up_r || r < up_l) return;
    push(id, l, r);
    if (l >= up_l && r <= up_r) {
        node[id].apply(val);
        return;
    }
    int mid = find_mid(l, r), l_node = id << 1, r_node = id << 1 | 1;
    update(l_node, l    , mid, up_l, up_r, val);
    update(r_node, mid + 1, r, up_l, up_r, val);
    node[id].ma = (node[l_node].ma.st > node[r_node].ma.st) ? node[l_node].ma : node[r_node].ma;
    node[id].mi = (node[l_node].mi.st < node[r_node].mi.st) ? node[l_node].mi : node[r_node].mi;
    return;
}

ll query_sum(int id, int l, int r, int pos) {
    push(id, l, r);
    if (l == pos && r == pos) return node[id].ma.st;
    int mid = find_mid(l, r), l_node = id << 1, r_node = id << 1 | 1;
    if (mid >= pos) return query_sum(l_node, l, mid, pos); 
    else return query_sum(r_node, mid +1, r, pos);
}

pair<pair<ll, int>, pair<ll, int>> max_excluded(int id, int l, int r, ll val, ll ma, ll mi) {
    push(id, l, r);
    if ((l ==r) || max(node[id].ma.st, ma) - min(node[id].mi.st, mi) < val) return make_pair(node[id].ma, node[id].mi);
    int mid = find_mid(l, r), l_node = id << 1, r_node = id << 1 | 1;
    pair<pair<ll,int>, pair<ll,int>> p = max_excluded(r_node, mid +1, r, val, ma, mi);
    ma = max(p.st.st, ma), mi = min(p.nd.st, mi);
    if (ma - mi >= val) return p;
    pair<pair<ll,int>, pair<ll,int>> q = max_excluded(l_node, l, mid, val, ma, mi);
    if (q.st.st > p.st.st) p.st = q.st;
    if (q.nd.st < p.nd.st) p.nd = q.nd;
    return p;
}

void init(int id, int l, int r) {
    node[id].ma = node[id].mi = make_pair(0, r);
    node[id].lazy = 0;
    if (l == r) return;
    int mid = find_mid(l, r), l_node = id << 1, r_node = id << 1 | 1;
    init(l_node, l, mid), init(r_node, mid +1, r);
    return;
}   

struct Event {
    int pos;
    int val;
    int ord;
    bool operator < (const Event &o) const {
        return pos < o.pos;
    }
    Event (int pos, int val, int ord) : pos(pos), val(val), ord(ord){}
};


std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
    vector<Event> events;
    // fprintf(stderr, "%d \n", sizeof (node[0].ma.st));
    for (int i = 0; i < l.size(); i++) {
        events.emplace_back(l[i], v[i], i +1);
        events.emplace_back(r[i] +1, -v[i], i +1);
    }
    sort(events.begin(), events.end());
    int pt = 0, n = c.size(), m = v.size();
    vector<int> ans(n);
    init(1, 0, m);
    for (int i = 0; i < n; i++) {
        int val = c[i];
        for (;pt < events.size() && events[pt].pos == i; pt++) {
            update(1, 0, m, events[pt].ord, m,events[pt].val);
        }
        pair<pair<ll, int>, pair<ll, int>> mx = max_excluded(1, 0, m, 1ll * val, node[1].mi.st, node[1].ma.st);
        if (mx.st.st - mx.nd.st < val) {
            ans[i] = (int) query_sum(1, 0, m, m);
            if (mx.nd.st < 0) {
                ans[i] -= (int) query_sum(1, 0, m, mx.nd.nd); 
            }
        } else {
            int mx_pos = max(mx.st.nd, mx.nd.nd);
            // fprintf(stderr, "at %d max_pos = %d ((%lld, %d), (%lld, %d))\n", i, mx_pos, mx.st.st, mx.st.nd, mx.nd.st, mx.nd.nd);
            ans[i] = mx_pos == mx.st.nd ? val : 0;
            if (mx_pos < m) {
                ans[i] += (int) (query_sum(1, 0, m, m) - query_sum(1, 0, m, mx_pos));
            }
        }
        // for (int j = 0; j <= m; j++) fprintf(stderr, "%lld ", query_sum(1, 0, m, j));
        // fprintf(stderr, "\n");
    }
    return ans;
}


Compilation message

candies.cpp: In function 'int find_mid(int, int)':
candies.cpp:49:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   49 |     return l + r >> 1;
      |            ~~^~~
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:131:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |     for (int i = 0; i < l.size(); i++) {
      |                     ~~^~~~~~~~~~
candies.cpp:141:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |         for (;pt < events.size() && events[pt].pos == i; pt++) {
      |               ~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 14 ms 31700 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 423 ms 44076 KB Output is correct
2 Correct 420 ms 48240 KB Output is correct
3 Correct 451 ms 48028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31572 KB Output is correct
2 Correct 304 ms 42536 KB Output is correct
3 Correct 126 ms 37428 KB Output is correct
4 Correct 512 ms 50164 KB Output is correct
5 Correct 498 ms 50416 KB Output is correct
6 Correct 421 ms 50836 KB Output is correct
7 Correct 409 ms 50248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 31572 KB Output is correct
2 Correct 12 ms 31508 KB Output is correct
3 Correct 140 ms 42524 KB Output is correct
4 Correct 88 ms 34172 KB Output is correct
5 Correct 242 ms 44120 KB Output is correct
6 Correct 241 ms 44080 KB Output is correct
7 Correct 235 ms 44160 KB Output is correct
8 Correct 239 ms 47464 KB Output is correct
9 Correct 277 ms 49052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 31572 KB Output is correct
2 Correct 13 ms 31572 KB Output is correct
3 Correct 14 ms 31700 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31680 KB Output is correct
6 Correct 423 ms 44076 KB Output is correct
7 Correct 420 ms 48240 KB Output is correct
8 Correct 451 ms 48028 KB Output is correct
9 Correct 15 ms 31572 KB Output is correct
10 Correct 304 ms 42536 KB Output is correct
11 Correct 126 ms 37428 KB Output is correct
12 Correct 512 ms 50164 KB Output is correct
13 Correct 498 ms 50416 KB Output is correct
14 Correct 421 ms 50836 KB Output is correct
15 Correct 409 ms 50248 KB Output is correct
16 Correct 13 ms 31572 KB Output is correct
17 Correct 12 ms 31508 KB Output is correct
18 Correct 140 ms 42524 KB Output is correct
19 Correct 88 ms 34172 KB Output is correct
20 Correct 242 ms 44120 KB Output is correct
21 Correct 241 ms 44080 KB Output is correct
22 Correct 235 ms 44160 KB Output is correct
23 Correct 239 ms 47464 KB Output is correct
24 Correct 277 ms 49052 KB Output is correct
25 Correct 12 ms 31572 KB Output is correct
26 Correct 93 ms 35272 KB Output is correct
27 Correct 284 ms 45116 KB Output is correct
28 Correct 426 ms 48704 KB Output is correct
29 Correct 428 ms 49084 KB Output is correct
30 Correct 434 ms 49252 KB Output is correct
31 Correct 424 ms 49492 KB Output is correct
32 Correct 425 ms 49692 KB Output is correct