답안 #722774

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722774 2023-04-12T20:26:37 Z Mr_Husanboy 사탕 분배 (IOI21_candies) C++17
100 / 100
1432 ms 55316 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, sum = 0; bool marked = false;
        void apply(ll val){
            sum += 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;
        res.mn = min(a.mn, b.mn);
        res.mx = max(a.mx, b.mx);
        res.sum = a.sum + b.sum;
        return res;
    }
    Node neutral;
    vector<Node> t;
    int n;

    //construction

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

    void build(vector<ll> &a, ll x, ll lx, ll rx){
        if(lx == rx){
            t[x].mn = a[lx];
            return;
        }
        ll 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<ll> &a){
        int sz = len(a);
        init(sz);
        build(a, 1, 0, n - 1);
    }

    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);
    }
};



vector<int> distribute_candies(vector<int> c, vector<int> l,
                                    vector<int> r, vector<int> v) {
    #define int long long
    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;
            SegmentTree::Node res = t.get(mid, m);
            if(res.mx - res.mn > c[i]){
                l = mid + 1;
            }else r = mid - 1;
        }
        r ++;
        // cout << r << ": " << ' ';
        // for(int j = 0; j <= m; j ++){
        //     cout << t.get(j, j).sum << ' ';
        // }cout << endl;
        if(r == 0 || t.get(r - 1, r - 1).sum > 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[u]);
        }
    }
    //for(auto u : suf) cout << u << ' ';cout << endl;
    return c;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1333 ms 55216 KB Output is correct
2 Correct 1396 ms 55244 KB Output is correct
3 Correct 1432 ms 55212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 280 ms 41976 KB Output is correct
3 Correct 354 ms 11720 KB Output is correct
4 Correct 1398 ms 55280 KB Output is correct
5 Correct 1414 ms 55232 KB Output is correct
6 Correct 1304 ms 55216 KB Output is correct
7 Correct 1301 ms 55212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 163 ms 39760 KB Output is correct
4 Correct 336 ms 11580 KB Output is correct
5 Correct 1044 ms 50636 KB Output is correct
6 Correct 1022 ms 50636 KB Output is correct
7 Correct 1020 ms 50636 KB Output is correct
8 Correct 1008 ms 50636 KB Output is correct
9 Correct 1044 ms 50636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 2 ms 724 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 6 ms 852 KB Output is correct
6 Correct 1333 ms 55216 KB Output is correct
7 Correct 1396 ms 55244 KB Output is correct
8 Correct 1432 ms 55212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 280 ms 41976 KB Output is correct
11 Correct 354 ms 11720 KB Output is correct
12 Correct 1398 ms 55280 KB Output is correct
13 Correct 1414 ms 55232 KB Output is correct
14 Correct 1304 ms 55216 KB Output is correct
15 Correct 1301 ms 55212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 163 ms 39760 KB Output is correct
19 Correct 336 ms 11580 KB Output is correct
20 Correct 1044 ms 50636 KB Output is correct
21 Correct 1022 ms 50636 KB Output is correct
22 Correct 1020 ms 50636 KB Output is correct
23 Correct 1008 ms 50636 KB Output is correct
24 Correct 1044 ms 50636 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 318 ms 11740 KB Output is correct
27 Correct 289 ms 41996 KB Output is correct
28 Correct 1319 ms 55232 KB Output is correct
29 Correct 1361 ms 55316 KB Output is correct
30 Correct 1359 ms 55216 KB Output is correct
31 Correct 1291 ms 55220 KB Output is correct
32 Correct 1321 ms 55304 KB Output is correct