Submission #897137

# Submission time Handle Problem Language Result Execution time Memory
897137 2024-01-02T15:21:40 Z TahirAliyev Distributing Candies (IOI21_candies) C++17
100 / 100
991 ms 42944 KB
#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define oo 1e16

using namespace std;

int n, q;
const int MAX = 2e5 + 5;
pii tree[4 * MAX];
ll lazy[4 * MAX];
int z = 1;

void relax(int node, int l, int r){
    if(!lazy[node]) return;
    tree[node].first += lazy[node];
    tree[node].second += lazy[node];
    if(l == r){
        lazy[node] = 0;
        return;
    }
    lazy[2 * node] += lazy[node];
    lazy[2 * node + 1] += lazy[node];
    lazy[node] = 0;
}

pii comb(pii a, pii b){
    return {min(a.first, b.first), max(a.second, b.second)};
}

void update(int node, int l, int r, int ul, int ur, int v){
    relax(node, l, r);
    if(ur < l || r < ul){
        return;
    }
    if(ul <= l && r <= ur){
        lazy[node] += v;
        relax(node, l, r);
        return;
    }
    int mid = (l + r) / 2;
    update(2 * node, l, mid, ul, ur, v);
    update(2 * node + 1, mid + 1, r, ul, ur, v);
    tree[node] = comb(tree[2 * node], tree[2 * node + 1]);
}

pii ask(int node, int l, int r, int ql, int qr){
    if(qr < l || r < ql) return {oo, -oo};
    relax(node, l, r);
    if(ql <= l && r <= qr) return tree[node];
    int mid = (l + r) / 2;
    return comb(ask(2 * node, l, mid, ql, qr), ask(2 * node + 1, mid + 1, r, ql, qr));
}

vector<int> E[MAX];

vector<int> distribute_candies(vector<int> c, vector<int> l,
                               vector<int> r, vector<int> v){                         
    n = c.size(), q = l.size();
    l.insert(l.begin(), 0);
    r.insert(r.begin(), 0);
    v.insert(v.begin(), 0);
    c.insert(c.begin(), 0);

    for(int i = 1; i <= n; i++){
        E[i].clear();
    }
    for(int i = 0; i <= 4 * q; i++){
        tree[i] = {0, 0};
        lazy[i] = 0;
    }
    for(int i = 1; i <= q; i++){
        l[i]++;
        r[i]++;
        E[l[i]].push_back(i);
        E[r[i] + 1].push_back(i);
    }
    vector<int> res;
    for(int i = 1; i <= n; i++){
        for(int t : E[i]){
            if(i == l[t]){
                update(1, 0, q, t, q, v[t]);
            }
            else{
                update(1, 0, q, t, q, -v[t]);
            }
        }
        int L = 0, R = q;
        int ans = -1;
        while(L <= R){
            int mid = (L + R) / 2;
            pii a = ask(1, 0, q, mid, q);
            if(a.second - a.first > c[i]){
                L = mid + 1;
                ans = mid;
            }
            else{
                R = mid - 1;
            }
        }
        ll lst = ask(1, 0, q, q, q).first;
        if(ans == -1){
            ll mn = ask(1, 0, q, 0, q).first;
            res.push_back(lst - mn);
            continue;
        }
        pii a = ask(1, 0, q, ans, q);
        ll frs = ask(1, 0, q, ans, ans).first;
        ll bottom =  0;
        if(a.first == frs){
            bottom = a.second - c[i];
        }
        else{
            bottom = a.first;
        }
        res.push_back(lst - bottom);
    }
    z++;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 3 ms 8936 KB Output is correct
4 Correct 3 ms 8804 KB Output is correct
5 Correct 6 ms 9004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 918 ms 37532 KB Output is correct
2 Correct 945 ms 40692 KB Output is correct
3 Correct 991 ms 40808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 218 ms 30900 KB Output is correct
3 Correct 284 ms 15448 KB Output is correct
4 Correct 949 ms 42648 KB Output is correct
5 Correct 941 ms 42488 KB Output is correct
6 Correct 911 ms 42944 KB Output is correct
7 Correct 938 ms 42708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 3 ms 8796 KB Output is correct
3 Correct 100 ms 31148 KB Output is correct
4 Correct 245 ms 12364 KB Output is correct
5 Correct 701 ms 33920 KB Output is correct
6 Correct 662 ms 33916 KB Output is correct
7 Correct 691 ms 33956 KB Output is correct
8 Correct 658 ms 33916 KB Output is correct
9 Correct 712 ms 33916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 2 ms 8796 KB Output is correct
3 Correct 3 ms 8936 KB Output is correct
4 Correct 3 ms 8804 KB Output is correct
5 Correct 6 ms 9004 KB Output is correct
6 Correct 918 ms 37532 KB Output is correct
7 Correct 945 ms 40692 KB Output is correct
8 Correct 991 ms 40808 KB Output is correct
9 Correct 2 ms 8796 KB Output is correct
10 Correct 218 ms 30900 KB Output is correct
11 Correct 284 ms 15448 KB Output is correct
12 Correct 949 ms 42648 KB Output is correct
13 Correct 941 ms 42488 KB Output is correct
14 Correct 911 ms 42944 KB Output is correct
15 Correct 938 ms 42708 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 100 ms 31148 KB Output is correct
19 Correct 245 ms 12364 KB Output is correct
20 Correct 701 ms 33920 KB Output is correct
21 Correct 662 ms 33916 KB Output is correct
22 Correct 691 ms 33956 KB Output is correct
23 Correct 658 ms 33916 KB Output is correct
24 Correct 712 ms 33916 KB Output is correct
25 Correct 2 ms 8792 KB Output is correct
26 Correct 227 ms 13384 KB Output is correct
27 Correct 193 ms 32972 KB Output is correct
28 Correct 890 ms 41312 KB Output is correct
29 Correct 956 ms 41744 KB Output is correct
30 Correct 947 ms 41784 KB Output is correct
31 Correct 987 ms 41924 KB Output is correct
32 Correct 963 ms 42176 KB Output is correct