Submission #898601

#TimeUsernameProblemLanguageResultExecution timeMemory
898601boxTreatment Project (JOI20_treatment)C++17
100 / 100
257 ms65248 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif

#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;

vector<vector<pi>> P, Q;

struct segt {
    int l, r;
    segt *lc = NULL, *rc = NULL;
    vector<pi> one, two;
    segt(int l, int r) : l(l), r(r) {
        if (l == r) {
            one = move(P[l]), two = move(Q[l]);
            sort(all(one), greater<>()), sort(all(two), greater<>());
        } else {
            int m = (l + r) / 2;
            lc = new segt(l, m), rc = new segt(m + 1, r);
            one.resize(sz(lc->one) + sz(rc->one));
            std::merge(all(lc->one), all(rc->one), begin(one), greater<>());
            two.resize(sz(lc->two) + sz(rc->two));
            std::merge(all(lc->two), all(rc->two), begin(two), greater<>());
        }
    }
    void qry_one(int ql, int qr, int x, vi& v) {
        if (ql <= l && qr >= r) {
            while (sz(one) && one.back().first <= x) {
                v.push_back(one.back().second);
                one.pop_back();
            }
            return;
        }
        if (ql <= lc->r) lc->qry_one(ql, qr, x, v);
        if (qr >= rc->l) rc->qry_one(ql, qr, x, v);
    }
    void qry_two(int ql, int qr, int x, vi& v) {
        if (ql <= l && qr >= r) {
            while (sz(two) && two.back().first <= x) {
                v.push_back(two.back().second);
                two.pop_back();
            }
            return;
        }
        if (ql <= lc->r) lc->qry_two(ql, qr, x, v);
        if (qr >= rc->l) rc->qry_two(ql, qr, x, v);
    }
};

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int N, M;
    cin >> N >> M;
    vi T(M), L(M), R(M), C(M), index(M);
    for (int i = 0; i < M; i++) cin >> T[i] >> L[i] >> R[i] >> C[i], L[i]--;
    vi t = T;
    sort(all(t)), t.erase(unique(all(t)), end(t));
    P.resize(sz(t)), Q.resize(sz(t));
    for (int i = 0; i < M; i++) {
        index[i] = lower_bound(all(t), T[i]) - begin(t);
        P[index[i]].push_back({L[i] + T[i], i});
        Q[index[i]].push_back({L[i] - T[i], i});
    }
    segt *tree = new segt(0, sz(t) - 1);
    vector<i64> dist(M, -1);
    typedef pair<i64, int> U;
    priority_queue<U, vector<U>, greater<U>> pq;
    for (int i = 0; i < M; i++) if (L[i] == 0) pq.push({dist[i] = C[i], i});
    while (sz(pq)) {
        auto [d, i] = pq.top(); pq.pop();
        vi v;
        tree->qry_one(index[i], sz(t) - 1, R[i] + T[i], v);
        tree->qry_two(0, index[i], R[i] - T[i], v);
        for (int j : v) if (dist[j] == -1) pq.push({dist[j] = dist[i] + C[j], j});
    }
    i64 ans = LLONG_MAX;
    for (int i = 0; i < M; i++) if (~dist[i] && R[i] == N)
        ans = min(ans, dist[i]);
    cout << (ans == LLONG_MAX ? -1 : ans) << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...