Submission #722744

# Submission time Handle Problem Language Result Execution time Memory
722744 2023-04-12T18:54:10 Z Mr_Husanboy Distributing Candies (IOI21_candies) C++17
0 / 100
1194 ms 52880 KB
#include "candies.h"
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template<typename T>
int len(T &a){
    return a.size();
}

const ll infl = 1e18;

// struct Segtree{
//     struct node{
//         ll mx = 0, mn = 0, add = 0;
//         void apply(ll x){
//             mn += x; mx += x;
//             add += x;
//         }
//     };

//     vector<node> t;
//     int n;

//     Segtree(int _n){
//         n = _n;
//         t.resize(4 * n);
//     }

//     void push(int x){
//         if(t[x].add){
//             t[x << 1].apply(t[x].add);
//             t[x << 1 | 1].apply(t[x].add);
//             t[x].add = 0;
//         }
//     }

//     node merge(node a, node b){
//         return {max(a.mx, b.mx), min(a.mn, b.mn), 0ll};
//     }

//     void upd(int x, int lx, int rx, int l, int r, ll val){
//         if(l <= lx && rx <= r){
//             t[x].apply(val);
//             return;
//         }
//         if(r < lx || l > rx){
//             return;
//         }
//         int m = (rx + lx) >> 1;
//         push(x);
//         upd(x << 1, lx, m, l, r, val);
//         upd(x << 1 | 1, m + 1, rx, l, r, val);
//         t[x] = merge(t[x << 1], t[x << 1 | 1]);
//     }

//     node get(int x, int lx, int rx, int l, int r){
//         if(l <= lx && rx <= r){
//             return t[x];
//         }
//         if(r < lx || l > rx){
//             return {0,0,0};
//         }
//         push(x);
//         int m = (lx + rx) >> 1;
//         return merge(get(x << 1, lx, m, l, r),
//             get(x << 1 | 1, m + 1, rx, l, r));
//     }

//     void upd(int l, int r, ll val){
//         upd(1, 0, n - 1, l, r, val);
//     }

//     node get(int l, int r){
//         return get(1, 0, n - 1, l, r);
//     }
// };



struct SegmentTree{

    //To change
    struct Node{
        //defaults
        ll mn = 0, mx = 0, add = 0; bool marked = false;
        void apply(int val){
            mn += val;
            mx += val;
            add += val;
            marked = true;
        }
    };

    void push(int x){
        if(t[x].marked){
                t[x << 1].apply(t[x].add);
                t[x << 1 | 1].apply(t[x].add);
                t[x].add = 0; t[x].marked = false;
        }
    }

    Node merge(Node a, Node b){
        Node res;
        if(a.mn < b.mn){
            res.mn = a.mn;
        }else{
            res.mn = b.mn;
        }
        res.mx = max(a.mx, b.mx);
        return res;
    }
    Node neutral;
    vector<Node> t;
    int n;

    //construction

    void init(int _n){
        n = _n;
        t.assign(4 * n, neutral);
    }

    void build(vector<int> &a, int x, int lx, int rx){
        if(lx == rx){
            t[x].mn = a[lx];
            return;
        }
        int mx = (lx + rx) >> 1;
        build(a, x << 1, lx, mx);
        build(a, x << 1 | 1, mx + 1, rx);
        t[x] = merge(t[x << 1], t[x << 1 | 1]);
    }

    void upd(int x, int lx, int rx, int l, int r, int val){
        if(rx < l || r < lx) return;
        if(l <= lx && rx <= r){
            t[x].apply(val); return;
        }
        push(x);
        int mx = (lx + rx) >> 1;
        upd(x << 1, lx, mx, l, r, val);
        upd(x << 1 | 1, mx + 1, rx, l, r, val);
        t[x] = merge(t[x << 1], t[x << 1 | 1]);
    }

    Node get(int x, int lx, int rx, int l, int r){
        if(rx < l || r < lx){
            return neutral;
        }
        if(l <= lx && rx <= r){
            return t[x];
        }
        push(x);
        int mx = (lx + rx) >> 1;
        return merge(get(x << 1, lx, mx, l, r), get(x << 1 | 1, mx + 1, rx, l, r));
    }

    // lessen

    void build(vector<int> &a){
        int sz = len(a);
        init(sz);
        build(a, 1, 0, n - 1);
    }

    void upd(int l, int r, int val){
        upd(1, 0, n - 1, l, r, val);
    }

    Node get(int l, int r){
        return get(1, 0, n - 1, l, r);
    }
};



vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    int n = c.size();
    int m = len(v);
    SegmentTree t;
    t.init(m + 1);
    vector<vector<int>> in(n), out(n);
    for(int i = 0; i < m; i ++){
        in[l[i]].push_back(i);
        out[r[i]].push_back(i);
    }

    for(int i = 0; i < n; i ++){
        for(auto u : in[i]){
            t.upd(0, u, -v[u]);
        }
        int l = 0, r = m;
        while(l <= r){
            int mid = (l + r) >> 1;
            auto res = t.get(mid, m);
            if(res.mx - res.mn >= c[i]){
                l = mid + 1;
            }else r = mid - 1;
        }
        r ++;
        if(r == 0 || v[r - 1] < 0){
            c[i] = -t.get(r, m).mn;
        }else{
            c[i] = c[i] - t.get(r, m).mx;
        }

        for(auto u : out[i]){
            t.upd(0, u, v[i]);
        }

    }
    //for(auto u : suf) cout << u << ' ';cout << endl;
    return c;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1194 ms 52880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 164 ms 34580 KB Output is correct
4 Runtime error 281 ms 24476 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -