Submission #1084217

# Submission time Handle Problem Language Result Execution time Memory
1084217 2024-09-05T16:16:27 Z steveonalex Distributing Candies (IOI21_candies) C++17
100 / 100
677 ms 44716 KB
 #include "candies.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

struct SegmentTree{
    struct Node{
        ll l, r, lazy;
        Node(){l = r = lazy = 0;}
    };

    int n;
    vector<Node> a;

    SegmentTree(int _n){
        n = _n;
        a.resize(n * 4 + 8);
    }

    void update_node(int i, ll v){
        a[i].l += v; a[i].r += v; a[i].lazy += v;
    }

    void merge_child(int id){
        a[id].l = min(a[id * 2].l, a[id * 2 + 1].l);
        a[id].r = max(a[id * 2].r, a[id * 2 + 1].r);
    }

    void down(int id){
        update_node(id * 2, a[id].lazy); update_node(id * 2 + 1, a[id].lazy);
        a[id].lazy = 0;
    }

    void update(int u, int v, int val, int l, int r, int id){
        if (u <= l && r <= v){
            update_node(id, val);
            return;
        }
        if (a[id].lazy) down(id);
        int mid = (l + r) >> 1;
        if (u <= mid) update(u, v, val, l, mid, id * 2);
        if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
        merge_child(id);
    }

    void update(int u, int v, int val){
        update(u, v, val, 0, n, 1);
    }

    pair<ll, ll> combine_range(pair<ll, ll> a, pair<ll, ll> b){
        return make_pair(min(a.first, b.first), max(a.second, b.second));
    }

    pair<ll, ll> get(int u, int v, int l, int r, int id){
        if (u <= l && r <= v) return make_pair(a[id].l, a[id].r);
        if (a[id].lazy) down(id);
        int mid = (l + r) >> 1;
        pair<ll, ll> ans = {1e18, -1e18};
        if (u <= mid) ans = combine_range(ans, get(u, v, l, mid, id * 2));
        if (v > mid) ans = combine_range(ans, get(u, v, mid + 1, r, id * 2 + 1));
        return ans;
    }

    pair<ll, ll> get(int u, int v){
        return get(u, v, 0, n, 1);
    }
};

vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    int n = c.size(), q = l.size();
    SegmentTree st(q);
    vector<vector<pair<int, int>>> queries(n+1);
    for(int i = 0; i<q; ++i){
        queries[l[i]].push_back({i + 1, v[i]});
        queries[r[i]+1].push_back({i + 1, -v[i]});
    }

    vector<int> a(n);
    ll sum = 0;
    for(int i = 0; i<n; ++i){
        for(pair<int, int> j: queries[i]){
            st.update(j.first, q, j.second);
            sum += j.second;
        }
        pair<ll, ll> cur = st.get(0, q);
        // if (i == n-1){
        //     cout << cur.first << " " << cur.second << "\n";
        // }
        if (cur.second - cur.first <= c[i]){
            a[i] = sum - cur.first;
        }
        else{
            int l = 1, r = q;
            while(l < r){
                int mid = (l + r) >> 1;
                pair<ll, ll> cur = st.get(mid, q);
                if (cur.second - cur.first <= c[i]) r = mid;
                else l = mid + 1;
            }
            pair<ll, ll> cur = st.get(l, q);
            int x = l - 1;
            if (v[x] > 0){
                a[i] = c[i] - (cur.second - sum);
            }
            else a[i] = sum - cur.first;
        }
    }

    return a;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 3 ms 708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 677 ms 42736 KB Output is correct
2 Correct 652 ms 42068 KB Output is correct
3 Correct 610 ms 41808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 161 ms 31316 KB Output is correct
3 Correct 144 ms 9912 KB Output is correct
4 Correct 570 ms 43856 KB Output is correct
5 Correct 617 ms 44116 KB Output is correct
6 Correct 595 ms 44716 KB Output is correct
7 Correct 616 ms 43964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 79 ms 29888 KB Output is correct
4 Correct 107 ms 8788 KB Output is correct
5 Correct 338 ms 37560 KB Output is correct
6 Correct 360 ms 38068 KB Output is correct
7 Correct 395 ms 38836 KB Output is correct
8 Correct 310 ms 37300 KB Output is correct
9 Correct 416 ms 38840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 3 ms 708 KB Output is correct
6 Correct 677 ms 42736 KB Output is correct
7 Correct 652 ms 42068 KB Output is correct
8 Correct 610 ms 41808 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 161 ms 31316 KB Output is correct
11 Correct 144 ms 9912 KB Output is correct
12 Correct 570 ms 43856 KB Output is correct
13 Correct 617 ms 44116 KB Output is correct
14 Correct 595 ms 44716 KB Output is correct
15 Correct 616 ms 43964 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 79 ms 29888 KB Output is correct
19 Correct 107 ms 8788 KB Output is correct
20 Correct 338 ms 37560 KB Output is correct
21 Correct 360 ms 38068 KB Output is correct
22 Correct 395 ms 38836 KB Output is correct
23 Correct 310 ms 37300 KB Output is correct
24 Correct 416 ms 38840 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 89 ms 9052 KB Output is correct
27 Correct 151 ms 31056 KB Output is correct
28 Correct 471 ms 42320 KB Output is correct
29 Correct 578 ms 42952 KB Output is correct
30 Correct 609 ms 43092 KB Output is correct
31 Correct 641 ms 43088 KB Output is correct
32 Correct 600 ms 43344 KB Output is correct