Submission #1092211

#TimeUsernameProblemLanguageResultExecution timeMemory
1092211anhthiTreatment Project (JOI20_treatment)C++11
100 / 100
319 ms54472 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)

template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }

template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }

template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }

int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}

const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;

const int mxn = 1e5 + 5;

void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }

int n, m;

struct Treat {
    ll t, l, r, c;
    void input() {
        cin >> t >> l >> r >> c;
    }
} treat[mxn];

ll pos[mxn];
ll f[mxn];

bool visited[mxn];
priority_queue<pair<ll, ll>> mn1[mxn], mn2[mxn];

int main(void) {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define TASK "task"
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    cin >> n >> m;
    rep(i, m) treat[i].input();

    vector<ll> cps;
    rep(i, m) cps.pb(treat[i].t);

    compress(cps);
    int len = sz(cps);

    rep(i, m) {
        pos[i] = lower_bound(all(cps), treat[i].t) - cps.begin() + 1;
        for (int p = pos[i]; p <= len; p += p & -p) {
            mn1[p].push(mp(-(treat[i].l - treat[i].t), i));
        }
        for (int p = pos[i]; p > 0; p -= p & -p) {
            mn2[p].push(mp(-(treat[i].l + treat[i].t), i));
        }
    }

    memset(f, 0x3f, sizeof f);
    priority_queue<pair<ll, ll>> pq;
    forn(i, 1, m) if (treat[i].l == 1) {
        pq.push({-treat[i].c, i});
    }

    while (!pq.empty()) {
        pair<ll, ll> top = pq.top(); pq.pop();
        int u = top.se; ll dist = -top.fi;

        if (treat[u].r == n) {
            cout << dist << '\n';
            exit(0);
        }

        for (int p = pos[u]; p > 0; p -= p & -p) {
            while (!mn1[p].empty()) {
                pair<ll, ll> top = mn1[p].top();
                if (visited[top.se]) {
                    mn1[p].pop();
                    continue;
                }
                if (-top.fi > treat[u].r - treat[u].t + 1) break;
                mn1[p].pop();
                visited[top.se] = true;
                pq.push(mp(-(dist + treat[top.se].c), top.se));

            }
        }
        for (int p = pos[u]; p <= len; p += p & -p) {
            while (!mn2[p].empty()) {
                pair<ll, ll> top = mn2[p].top();
                if (visited[top.se]) {
                    mn2[p].pop();
                    continue;
                }
                if (-top.fi > treat[u].r + treat[u].t + 1) break;
                mn2[p].pop();
                visited[top.se] = true;
                pq.push(mp(-(dist + treat[top.se].c), top.se));

            }
        }
    }

    cout << -1 << '\n';

    return 0;
}

Compilation message (stderr)

treatment.cpp: In function 'int main()':
treatment.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...