#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;
        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 m, n_orig; // Use m for M and n_orig for original N
    cin >> m >> n_orig;
    int ans = inf;
    map<int, int> coordsToIdx;
    int cnt = 0;
    vector<vector<int>> input(m, vector<int>(4, 0));
    set<int> s;
    // *** THE FIX: Start by inserting the true boundaries 1 and N ***
    s.insert(1);
    s.insert(n_orig);
    for (int i = 0; i < m; i++){
        cin >> input[i][0] >> input[i][1] >> input[i][2] >> input[i][3];
        // Insert A_i, B_i, C_i into the set
        s.insert(input[i][0]);
        s.insert(input[i][1]);
        s.insert(input[i][2]);
    }
    for (int num: s){
        coordsToIdx[num] = ++cnt;
    }
    int n_compressed = cnt; // Use a new variable for the compressed size
    
    // Initialize DP base cases on the compressed coordinates for 1 and N
    l.update(1, 1, n_compressed, coordsToIdx[1], 0);
    r.update(1, 1, n_compressed, coordsToIdx[n_orig], 0);
    for (int i = 0; i < m; i++){
        int ul = coordsToIdx[input[i][0]];
        int ur = coordsToIdx[input[i][1]];
        int k = coordsToIdx[input[i][2]];
        int val = input[i][3];
        // The core DP logic remains the same, but now operates on a correct problem space
        int cost_left = l.get(1, 1, n_compressed, ul, k);
        int cost_right = r.get(1, 1, n_compressed, k, ur);
        if (cost_left != inf && cost_right != inf) {
            ans = min(ans, cost_left + cost_right + val);
        }
        
        int newL = min(l.get(1, 1, n_compressed, ul, k) + val, l.get(1, 1, n_compressed, k, k));
        if (cost_left != inf) {
             l.update(1, 1, n_compressed, k, newL);
        }
       
        int newR = min(r.get(1, 1, n_compressed, k, ur) + val, r.get(1, 1, n_compressed, k, k));
        if (cost_right != inf) {
            r.update(1, 1, n_compressed, k, newR);
        }
    }
    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... |