Submission #1041068

#TimeUsernameProblemLanguageResultExecution timeMemory
1041068LOLOLOTreatment Project (JOI20_treatment)C++17
100 / 100
289 ms122796 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 1e6 + 10;
int pos[N];
int T[N], L[N], R[N], C[N], vis[N], rnk[N];
priority_queue <pair <ll, ll>> f[2][N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> m >> n;

    priority_queue <pair <ll, ll>, vector <pair <ll, ll>>, greater <pair <ll, ll>>> pq;

    for (int i = 1; i <= n; i++) {
        cin >> T[i] >> L[i] >> R[i] >> C[i];
        pos[i] = T[i];
        if (L[i] == 1) {
            pq.push({C[i], i});
            vis[i] = 1;
        }
    }

    sort(pos + 1, pos + n + 1);

    for (int i = 1; i <= n; i++) {
        int p = lower_bound(pos + 1, pos + 1 + n, T[i]) - pos;
        rnk[i] = p;

        for (int k = p; k; k -= k & (-k)) 
            f[0][k].push({-L[i] - T[i] + 1, i});

        for (int k = p; k <= n; k += k & (-k))
            f[1][k].push({T[i] - L[i] + 1, i});
    }

    while (sz(pq)) {
        auto tt = pq.top();
        pq.pop();
        int id = tt.s;
        int ra = rnk[id];

        if (R[id] == m) {
            cout << tt.f << '\n';
            return 0;
        }

        for (int i = ra; i <= n; i += i & (-i)) {
            while (sz(f[0][i])) {
                auto t = f[0][i].top();
                if (vis[t.s]) {
                    f[0][i].pop();
                    continue; 
                }

                if (t.f >= -T[id] - R[id]) {
                    f[0][i].pop();
                    pq.push({tt.f + C[t.s], t.s});
                    vis[t.s] = 1;
                } else {
                    break;
                }
            }
        }

        for (int i = ra; i; i -= i & (-i)) {
            while (sz(f[1][i])) {
                auto t = f[1][i].top();
                if (vis[t.s]) {
                    f[1][i].pop();
                    continue;
                }

                if (t.f >= T[id] - R[id]) {
                    f[1][i].pop();
                    pq.push({tt.f + C[t.s], t.s});
                    vis[t.s] = 1;
                } else {
                    break;
                }
            }
        }
    }

    cout << -1 << "\n";
    return 0;
}

// case 1:
// t[i] > t[j]
// t[i] - t[j] <= r[i] - l[j] + 1
// t[i] - r[i] <= t[j] - l[j] + 1

// case 2: 
// t[i] < t[j] 
// t[j] - t[i] <= r[i] - l[j] + 1
// -t[i] - r[i] <= -l[j] - t[j] + 1
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...