Submission #1063479

# Submission time Handle Problem Language Result Execution time Memory
1063479 2024-08-17T19:15:05 Z RaresFelix Distributing Candies (IOI21_candies) C++17
100 / 100
342 ms 44624 KB
#include "candies.h"

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using ii = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
using pl = pair<ll, ll>;

const ll INF2 = 1e18;
const int INF = 1e9;

struct AINT {
    int n;
    vll Mi, Ma, Lz;
    AINT(int N) : n(N), Mi(4 * N, 0), Ma(4 * N, 0), Lz(4 * N, 0) {}

    void prop(int u, int s, int d) {
        if(!Lz[u]) return;
        Mi[u] += Lz[u];
        Ma[u] += Lz[u];
        if(s != d) {
            Lz[u << 1] += Lz[u];
            Lz[u << 1 | 1] += Lz[u];
        }
        Lz[u] = 0;
    }

    void update(int l, int r, ll v, int u, int s, int d) {
        prop(u, s, d);
        if(d < l || r < s) return;
        if(l <= s && d  <= r) {
            Lz[u] += v;
            prop(u, s, d);
            return;
        }
        update(l, r, v, u << 1, s, (s + d) >> 1);
        update(l, r, v, u << 1 | 1, ((s + d) >> 1) + 1, d);
        Mi[u] = min(Mi[u << 1], Mi[u << 1 | 1]);
        Ma[u] = max(Ma[u << 1], Ma[u << 1 | 1]);
    }

    void update(int l, int r, ll v) { update(l, r, v, 1, 0, n - 1); }

    bool query(int c, ll &mi, ll &ma, pl &prev, int u, int s, int d) {
        prop(u, s, d);
        if(max(Ma[u], ma) - min(Mi[u], mi) <= c) {
            mi = min(mi, Mi[u]);
            ma = max(ma, Ma[u]);
            return false;
        }
        if(s == d) {
            prev = {Mi[u], Ma[u]};
            return true;
        }
        if(!query(c, mi, ma, prev, u << 1 | 1, ((s + d) >> 1) + 1, d))
            query(c, mi, ma, prev, u << 1, s, (s + d) >> 1);
        return true;
    }

    void afis(int u, int s, int d) {
        prop(u, s, d);
        if(s == d) {
            cout << Mi[u] << " ";
            return;
        }
        afis(u << 1, s, (s + d) >> 1);
        afis(u << 1 | 1, ((s + d) >> 1) + 1, d);
    }

    void afis(string s = "") {
        cout << s;
        afis(1, 0, n - 1);
        cout << "\n";
    }

    void query(int c, ll &mi, ll &ma, pl &prev) {
        mi = INF2;
        ma = -INF2;
        query(c, mi, ma, prev, 1, 0, n - 1);
    }
};

vi distribute_candies(vi c, vi l, vi r, vi v) {
    int n = int(c.size()), m = int(l.size());
    vector<vector<ii> > Op(n);
    ///o operatie e (poz, val)
    Op[0].push_back({0, INF});
    Op[0].push_back({1, -INF});
    for(int i = 0; i < m; ++i) {
        Op[l[i]].push_back({i + 2, v[i]});
        if(r[i] + 1 < n) 
            Op[r[i] + 1].push_back({i + 2, -v[i]});
    }
    AINT Sol(m + 2);
    vi Re(n, 0);
    ll s = 0;
    for(int i = 0; i < n; ++i) {
        for(auto [p, v] : Op[i]) {
            Sol.update(p, m + 1, v);
            s += v;
        }
        ll mi, ma;
        pl prev;
        Sol.query(c[i], mi, ma, prev);
       // Sol.afis("Sol la " + to_string(i) + " = ");
        if(prev.second > ma) {
            //a murit la mi
            Re[i] = s - mi;
        } else {
            Re[i] = s - (ma - c[i]);
        }
    }
    return Re;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 748 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 342 ms 42844 KB Output is correct
2 Correct 309 ms 42064 KB Output is correct
3 Correct 320 ms 41652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 166 ms 31480 KB Output is correct
3 Correct 51 ms 9816 KB Output is correct
4 Correct 280 ms 43688 KB Output is correct
5 Correct 304 ms 44212 KB Output is correct
6 Correct 337 ms 44624 KB Output is correct
7 Correct 300 ms 43860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 432 KB Output is correct
3 Correct 87 ms 28100 KB Output is correct
4 Correct 48 ms 8788 KB Output is correct
5 Correct 140 ms 35916 KB Output is correct
6 Correct 143 ms 36544 KB Output is correct
7 Correct 154 ms 37076 KB Output is correct
8 Correct 156 ms 35784 KB Output is correct
9 Correct 141 ms 37284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 748 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 342 ms 42844 KB Output is correct
7 Correct 309 ms 42064 KB Output is correct
8 Correct 320 ms 41652 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 166 ms 31480 KB Output is correct
11 Correct 51 ms 9816 KB Output is correct
12 Correct 280 ms 43688 KB Output is correct
13 Correct 304 ms 44212 KB Output is correct
14 Correct 337 ms 44624 KB Output is correct
15 Correct 300 ms 43860 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 432 KB Output is correct
18 Correct 87 ms 28100 KB Output is correct
19 Correct 48 ms 8788 KB Output is correct
20 Correct 140 ms 35916 KB Output is correct
21 Correct 143 ms 36544 KB Output is correct
22 Correct 154 ms 37076 KB Output is correct
23 Correct 156 ms 35784 KB Output is correct
24 Correct 141 ms 37284 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 54 ms 9012 KB Output is correct
27 Correct 179 ms 31056 KB Output is correct
28 Correct 294 ms 42388 KB Output is correct
29 Correct 314 ms 42920 KB Output is correct
30 Correct 318 ms 43096 KB Output is correct
31 Correct 313 ms 43088 KB Output is correct
32 Correct 324 ms 43296 KB Output is correct