Submission #1342248

#TimeUsernameProblemLanguageResultExecution timeMemory
1342248Born_To_LaughPinball (JOI14_pinball)C++17
0 / 100
1 ms344 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 1e18 + 7;
#define int ll

const int maxn = 1e5 + 10;
int m, n;
ll dpl[maxn], dpr[maxn];
ll a[maxn], b[maxn], c[maxn], d[maxn];
class SegTree
{
    private:
        int n;
        vector<ll> tree;
        inline void upd(int id, int l, int r, int pos, ll val){
            if(l == r){
                tree[id] = min(tree[id], val);
                return;
            }
            int mid = (l + r) >> 1;
            if(pos <= mid) upd(id << 1, l, mid, pos, val);
            else upd(id << 1|1, mid + 1, r, pos, val);
            tree[id] = min(tree[id << 1], tree[id << 1|1]);
        }
        inline ll qr(int id, int l, int r, int lo, int hi){
            if(l > hi || r < lo) return INF;
            else if(l >= lo && r <= hi) return tree[id];
            int mid = (l + r) >> 1;
            return min(qr(id << 1, l, mid, lo, hi), qr(id << 1|1, mid + 1, r, lo, hi));
        }
    public:
        inline void init(int len){
            n = len + 1;
            tree.assign(n << 2, INF);
        }
        inline void upd(int pos, int val){
            upd(1, 1, n, pos, val);
        }
        inline ll qr(int lo, int hi){
            return qr(1, 1, n, lo, hi);
        }
};

// dpl[i] = mincost to let evrball fr 1 -> b[i] fall in c[i];
// dpr[i] = mincost to let evrball fr a[i] -> n fall in c[i];

SegTree stl, str;
vector<ll> temp;

void solve(){
    cin >> m >> n;
    temp.push_back(0);
    temp.push_back(1);
    temp.push_back(n);
    for(int i=1; i<=m; ++i){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        temp.push_back(a[i]);
        temp.push_back(b[i]);
        temp.push_back(c[i]);
    }
    sort(alle(temp));
    temp.erase(unique(alle(temp)), temp.end());

    n = lower_bound(alle(temp), n) - temp.begin();
    for(int i=1; i<=m; ++i){
        a[i] = lower_bound(alle(temp), a[i]) - temp.begin();
        b[i] = lower_bound(alle(temp), b[i]) - temp.begin();
        c[i] = lower_bound(alle(temp), c[i]) - temp.begin();
    }

    stl.init(n);
    str.init(n);
    
    ll ans = INF;

    for(int i=1; i<=n; ++i){
        dpl[i] = stl.qr(a[i], b[i]) + d[i];
        dpr[i] = str.qr(a[i], b[i]) + d[i];

        if(a[i] == 1){
            dpl[i] = min(dpl[i], d[i]);
        }
        if(b[i] == n){
            dpr[i] = min(dpr[i], d[i]);
        }

        ans = min(ans, dpl[i] + str.qr(a[i], b[i]));
        ans = min(ans, dpr[i] + stl.qr(a[i], b[i]));
        if(a[i] == 1 && b[i] == n) ans = min(ans, d[i]);
        

        stl.upd(c[i], dpl[i]);
        str.upd(c[i], dpr[i]);
    }

    if(ans >= INF) cout << -1 << '\n';
    else cout << ans << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...