#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int maxn = 3e5 + 5, inf = 1e15 + 1;
struct segment{
    int st[4*maxn+5];
    segment(){
        fill_n(st, 4 * maxn + 5, inf);
    }
    void update(int id, int l, int r, int k, int val){
        if (l > r) return;
        if (l == r){
            st[id] = min(st[id], val);
            return;
        }
        int mid = (l + r) / 2; // Avoid potential overflow
        if (k <= mid) {
            update(id*2, l, mid, k, val);
        } else {
            update(id*2+1, mid+1, r, k, val);
        }
        st[id] = min(st[id*2], st[id*2+1]);
    }
    int get(int id, int l, int r, int ul, int ur){
        if (l > ur || r < ul) return inf;
        if (ul <= l && r <= ur) return st[id];
        
        int mid = (l + r) / 2;
        return min(get(id*2, l, mid, ul, ur), get(id*2+1, mid+1, r, ul, ur));
    }
};
 
segment l, r;
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n, q;
    cin >> q >> n;
    int ans = inf;
    map<int, int> coordsToIdx;
    int cnt = 0;
    vector<vector<int>> input(q+1, vector<int>(4, 0));
    set<int> s;
    s.insert(1);
    s.insert(n);
    for (int i = 0; i < q; i++){
        cin >> input[i][0] >> input[i][1] >> input[i][2] >> input[i][3];
        for (int j = 0; j < 3; j++) s.insert(input[i][j]);
    }
    for (int num: s){
        coordsToIdx[num] = ++cnt;
    }
    
    int compressed_size = cnt;
    
    l.update(1, 1, compressed_size, coordsToIdx[1], 0);
    r.update(1, 1, compressed_size, coordsToIdx[n], 0);
    
    for (int i = 0; i < q; i++){
        int ul = coordsToIdx[input[i][0]];
        int ur = coordsToIdx[input[i][1]];
        int k = coordsToIdx[input[i][2]];
        int val = input[i][3];
        
        // Calculate a potential final answer if this device bridges a left and right merge.
        long long cost_left_range = l.get(1, 1, compressed_size, ul, k);
        long long cost_right_range = r.get(1, 1, compressed_size, k, ur);
        if (cost_left_range < inf && cost_right_range < inf) {
             ans = min(ans, cost_left_range + cost_right_range + val);
        }
        // Update the left-side DP state (L).
        // Find the minimum cost to merge everything up to a point within the device's range,
        // then add the device's cost.
        if (cost_left_range < inf) {
             long long current_L_at_k = l.get(1, 1, compressed_size, k, k);
             l.update(1, 1, compressed_size, k, min(current_L_at_k, cost_left_range + val));
        }
        // Update the right-side DP state (R).
        if (cost_right_range < inf) {
             long long current_R_at_k = r.get(1, 1, compressed_size, k, k);
             r.update(1, 1, compressed_size, k, min(current_R_at_k, cost_right_range + val));
        }
    }
    
    cout << (ans >= inf ? -1 : ans);
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |