Submission #1357864

#TimeUsernameProblemLanguageResultExecution timeMemory
1357864iamhereforfunPinball (JOI14_pinball)C++20
11 / 100
11 ms14156 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))
#define int long long

const int N = 2e5 + 5;
const int M = 2e6 + 5;
const int O = 1e6;
const long long K = 2;
const int LG = 60;
const long long INF = 1e18 + 5;
const int C = 26;
const int B = 1000;
const int MOD = 1e9 + 7;

struct SegmentTree
{
    vector<int> st;
    int n;
    void update(int v, int val, int pos, int l, int r)
    {
        if (l == r)
        {
            st[v] = min(st[v], val);
        }
        else
        {
            int m = (l + r) / 2;
            if (pos <= m)
            {
                update(2 * v, val, pos, l, m);
            }
            else
            {
                update(2 * v + 1, val, pos, m + 1, r);
            }
            st[v] = min(st[2 * v], st[2 * v + 1]);
        }
    }
    int get(int v, int l, int r, int tl, int tr)
    {
        if (tl > tr)
            return INF;
        if (tl <= l && r <= tr)
        {
            return st[v];
        }
        else
        {
            int m = (l + r) / 2;
            return min(get(2 * v, l, m, tl, min(tr, m)), get(2 * v + 1, m + 1, r, max(tl, m + 1), tr));
        }
    }
    SegmentTree(int len)
    {
        n = len;
        st.assign(4 * n, INF);
    }
    void update(int val, int pos)
    {
        update(1, val, pos, 0, n - 1);
    }
    int get(int l, int r)
    {
        return get(1, 0, n - 1, l, r);
    }
};

int m, n, ans;
array<int, 4> line[N];
vector<int> v;

inline void solve()
{
    cin >> m >> n;
    for (int x = 0; x < n; x++)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        line[x] = {a, b, c, d};
        v.push_back(a);
        v.push_back(c);
        v.push_back(b);
    }
    v.push_back(1);
    v.push_back(n);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    SegmentTree mn(v.size()), mx(v.size());
    mn.update(0, 0);
    mx.update(0, v.size() - 1);
    ans = INF;
    for (int x = 0; x < n; x++)
    {
        auto [a, b, c, d] = line[x];
        a = lower_bound(v.begin(), v.end(), a) - v.begin();
        b = lower_bound(v.begin(), v.end(), b) - v.begin();
        c = lower_bound(v.begin(), v.end(), c) - v.begin();
        int i = mn.get(a, b), j = mx.get(a, b);
        if (i != INF && j != INF)
        {
            ans = min(ans, i + j + d);
        }
        if (i != INF)
        {
            mn.update(i + d, c);
        }
        if (j != INF)
        {
            mx.update(j + d, c);
        }
    }
    if(ans == INF)
    {
        cout << -1 << "\n";
    }
    else
    {
        cout << ans << "\n";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...