Submission #854550

#TimeUsernameProblemLanguageResultExecution timeMemory
854550fanwenTreatment Project (JOI20_treatment)C++17
100 / 100
188 ms30564 KiB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(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)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

const int MAXN = 1e5 + 5;

struct Data {
    int T, L, R, C;
} a[MAXN];

int N, M, dd[MAXN];
vector <int> cp;
long long dp[MAXN];

priority_queue <pair <long long, int>, vector <pair <long long, int>>, greater <pair <long long, int>>> pq, q[MAXN];
vector <int> ver;

void update(int u, int val, int id) {
    for (int v = u; v <= (int) cp.size(); v += v & -v) {
        q[v].emplace(val, id);
    }
}

void get(int u, int t) {
    for (; u > 0; u -= u & -u) {
        while(!q[u].empty() && q[u].top().first <= t) {
            ver.emplace_back(q[u].top().second); q[u].pop();
        }
    }
}

void you_make_it(void) {
    cin >> N >> M;
    FOR(i, 1, M) {
        cin >> a[i].T >> a[i].L >> a[i].R >> a[i].C;
        cp.emplace_back(a[i].L - a[i].T);
    }
    sort(cp.begin(), cp.end()); cp.erase(unique(cp.begin(), cp.end()), cp.end());
    memset(dp, 0x3f, sizeof dp);
    FOR(i, 1, M) {
        update(lower_bound(cp.begin(), cp.end(), a[i].L - a[i].T) - cp.begin() + 1, a[i].L + a[i].T, i);
    }
    FOR(i, 1, M) {
        if(a[i].L == 1) {
            dp[i] = a[i].C;
            dd[i] = 1;
            pq.emplace(dp[i], i);
        }
    }
    long long ans = 1E18;
    while(!pq.empty()) {
        auto [du, u] = pq.top(); pq.pop();
        if(du != dp[u]) continue;
        if(a[u].R == N) {
            minimize(ans, du);
            continue;
        }
        ver.clear();
        get(upper_bound(cp.begin(), cp.end(), a[u].R - a[u].T + 1) - cp.begin(), a[u].R + a[u].T + 1);

        for (auto i : ver) {
            if(!dd[i] && minimize(dp[i], dp[u] + a[i].C)) {
                pq.emplace(dp[i], i);
                dd[i] = 1;
            }
        }
    }
    // FOR(i, 1, M) cout << dp[i] << " \n"[i == M];
    cout << (ans >= 1E18 ? -1 : ans);
}
signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("treatment");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message (stderr)

treatment.cpp: In function 'int main()':
treatment.cpp:13:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:90:5: note: in expansion of macro 'file'
   90 |     file("treatment");
      |     ^~~~
treatment.cpp:13:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
treatment.cpp:90:5: note: in expansion of macro 'file'
   90 |     file("treatment");
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...