This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 1e5 + 5;
int N, M;
int T[MAX], L[MAX], R[MAX], C[MAX], rnk[MAX];
bool vis[MAX];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> adj1[MAX];
priority_queue<pair<int, int>> adj2[MAX];
void solve(){
cin >> N >> M;
vector<int> v;
for(int i=1; i<=M; i++){
cin >> T[i] >> L[i] >> R[i] >> C[i];
v.push_back(T[i]);
}
cps(v);
for(int i=1; i<=M; i++){
int p = lower_bound(ALL(v), T[i]) - v.begin() + 1;
rnk[i] = p;
for(int k=p; k>0; k-=k&(-k)){
adj1[k].push(make_pair(L[i] + T[i], i));
}
for(int k=p; k<=sz(v); k+=k&(-k)){
adj2[k].push(make_pair(T[i] - L[i], i));
}
}
// //Dijkstra
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
for(int i=1; i<=M; i++){
if(L[i] == 1){
pq.push(make_pair(C[i], i));
vis[i] = true;
}
}
while(sz(pq)){
int u = pq.top().se;
ll c = pq.top().fi;
if(R[u] == N){
cout << c << '\n';
return;
}
pq.pop();
int k = rnk[u];
for(int p=k; p<=sz(v); p+=p&(-p)){
while(sz(adj1[p])){
int v = adj1[p].top().se;
int tmp = adj1[p].top().fi;
if(vis[v]){
adj1[p].pop();
continue;
}
if(tmp > R[u] + T[u] + 1) break;
adj1[p].pop();
vis[v] = true;
pq.push(make_pair(c + C[v], v));
}
}
for(int p=k; p>0; p-=p&(-p)){
while(sz(adj2[p])){
int v = adj2[p].top().se;
int tmp = adj2[p].top().fi;
if(vis[v]){
adj2[p].pop();
continue;
}
if(tmp < T[u] - R[u] - 1) break;
adj2[p].pop();
vis[v] = true;
pq.push(make_pair(c + C[v], v));
}
}
}
cout << -1 << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("deggraph.inp", "r", stdin);
// freopen("deggraph.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
}
# | 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... |