Submission #1347248

#TimeUsernameProblemLanguageResultExecution timeMemory
1347248nguyenkhangninh99Treatment Project (JOI20_treatment)C++20
100 / 100
103 ms37504 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 1e5 + 5;
int dist[maxn], ptr[4 * maxn];     
vector<int> st[4 * maxn];

struct Project {
    int t, l, r, c, x, y;
};

vector<Project> prro;

void build(int id, int l, int r){
    if(l == r){
        st[id].push_back(l);
        return;
    }
    
    int mid = (l + r) / 2, i = 0, j = 0;
    build(2 * id, l, mid);
    build(2 * id + 1, mid + 1, r);
    
    while(i < st[2 * id].size() && j < st[2 * id + 1].size()){
        if(prro[st[2 * id][i]].y < prro[st[2 * id + 1][j]].y) st[id].push_back(st[2 * id][i++]);
        else st[id].push_back(st[2 * id + 1][j++]);
    }
    while(i < st[2 * id].size()) st[id].push_back(st[2 * id][i++]);
    while(j < st[2 * id + 1].size()) st[id].push_back(st[2 * id + 1][j++]);
}

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

void update(int id, int l, int r, int boundx, int boundy, int cost){
    if(prro[l].x > boundx) return;
    if(prro[r].x <= boundx){
        while(ptr[id] < st[id].size()){
            int v = st[id][ptr[id]];
            if(prro[v].y <= boundy){
                if(dist[v] > cost + prro[v].c){
                    dist[v] = cost + prro[v].c;
                    pq.push({dist[v], v});
                }
                ptr[id]++; 
            }else break; 
        }
        return;
    }
    
    int mid = (l + r) / 2;
    update(id * 2, l, mid, boundx, boundy, cost);
    update(id * 2 + 1, mid + 1, r, boundx, boundy, cost);
}

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

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

    prro.resize(m);
    for(int i = 0; i < m; i++){
        cin >> prro[i].t >> prro[i].l >> prro[i].r >> prro[i].c;
        prro[i].r += 1; 
        prro[i].x = prro[i].l - prro[i].t;
        prro[i].y = prro[i].l + prro[i].t;
    }

    sort(prro.begin(), prro.end(), [](Project a, Project b){
        return a.x < b.x;
    });
    build(1, 0, m - 1);
    for(int i = 0; i < m; i++){
        if(prro[i].l == 1){
            dist[i] = prro[i].c;
            pq.push({dist[i], i});
        }else dist[i] = 1e18;
    }

    while(pq.size()){
        auto [cur, u] = pq.top();
        pq.pop();

        if(cur > dist[u]) continue;
        if(prro[u].r >= n + 1){
            cout << cur;
            return 0;
        }
        update(1, 0, m - 1, prro[u].r - prro[u].t, prro[u].r + prro[u].t, cur);
    }
    cout << -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...