This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
const int Inf2 = 2'000'000'000;
int n, m;
int T[N], L[N], R[N], C[N], mk[N];
int Tc[N];
ll dis[N];
struct Segment {
pii seg[N << 2];
Segment (){
memset(seg, 0, sizeof seg);
}
pii Get(int id, int l, int r, int L, int R){
if(r <= L || R <= l) return pii(Inf2, -1);
if(l <= L && R <= r) return seg[id];
int mid = (L + R) >> 1;
return min(Get(id << 1, l, r, L, mid), Get(id << 1 | 1, l, r, mid, R));
}
void Set(int id, int idx, int v, int L, int R){
if(R - L == 1){
seg[id] = {v, L};
return ;
}
int mid = (L + R) >> 1;
if(idx < mid)
Set(id << 1, idx, v, L, mid);
else
Set(id << 1 | 1, idx, v, mid, R);
seg[id] = min(seg[id << 1], seg[id << 1 | 1]);
}
};
Segment S1, S2;
bool Edge(int idx, int j){
if(j == -1) return false;
if(mk[j]) return false;
if(T[j] <= T[idx]){
if(L[j] + (T[idx] - T[j]) <= R[idx] + 1)
return true;
}
if(T[j] > T[idx]){
if(R[idx] - (T[j] - T[idx]) >= L[j] - 1)
return true;
}
return false;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
vector<pll> A;
for(int i = 1; i <= m; i++){
cin >> T[i] >> L[i] >> R[i] >> C[i];
A.pb({T[i], i});
}
/*
cerr << "####\n";
for(int i = 1; i <= m; i++) for(int j = 1; j <= m; j++)
if(Edge(i, j))
cerr << i << " -> " << j << '\n';
cerr << "###\n";
*/
sort(all(A));
for(int i = 0; i < m; i++) Tc[A[i].S] = i;
ll ans = Inf;
set<pll> st;
memset(dis, 31, sizeof dis);
for(int i = 1; i <= m; i++){
if(L[i] == 1){
dis[i] = C[i];
st.insert({dis[i], i});
S1.Set(1, Tc[i], Inf2, 0, m);
S2.Set(1, Tc[i], Inf2, 0, m);
} else {
S1.Set(1, Tc[i], L[i] - T[i], 0, m);
S2.Set(1, Tc[i], L[i] + T[i], 0, m);
}
}
while(!st.empty()){
int idx = st.begin()->S;
st.erase(st.begin());
//debug(idx);
/*
ll mn = Inf, idx = -1;
for(int i = 1; i <= m; i++){
if(mk[i]) continue;
if(dis[i] < mn){
mn = dis[i];
idx = i;
}
}
if(idx == -1) break;
*/
int RT = Tc[idx];
//S1.Set(1, RT, Inf, 0, m);
//S2.Set(1, RT, Inf, 0, m);
//mk[idx] = 1;
if(R[idx] == n) ans = min(ans, dis[idx]);
//debug(idx);
while(true){
int al = S1.Get(1, 0, RT, 0, m).S;
if(al == -1) break;
int wh = A[al].S;
//cerr << idx << ' ' << wh << '\n';
if(!Edge(idx, wh)) break;
S1.Set(1, Tc[wh], Inf2, 0, m);
S2.Set(1, Tc[wh], Inf2, 0, m);
mk[wh] = 1;
dis[wh] = dis[idx] + C[wh];
st.insert({dis[wh], wh});
}
while(true){
int al = S2.Get(1, RT, m, 0, m).S;
if(al == -1) break;
int wh = A[al].S;
//int wh = A[S2.Get(1, RT, m, 0, m).S].S;
//cerr << idx << ' ' << wh << '\n';
if(!Edge(idx, wh)) break;
S1.Set(1, Tc[wh], Inf2, 0, m);
S2.Set(1, Tc[wh], Inf2, 0, m);
mk[wh] = 1;
dis[wh] = dis[idx] + C[wh];
st.insert({dis[wh], wh});
}
/*
for(int j = 1; j <= m; j++){
//if(L[j] > R[idx] + 1) continue;
if(mk[j]) continue;
if(T[j] <= T[idx]){
if(L[j] + (T[idx] - T[j]) <= R[idx] + 1)
dis[j] = min(dis[j], dis[idx] + C[j]);
}
if(T[j] > T[idx]){
if(R[idx] - (T[j] - T[idx]) >= L[j] - 1)
dis[j] = min(dis[j], dis[idx] + C[j]);
}
}
*/
}
if(ans >= Inf) return cout << "-1\n", 0;
cout << ans << '\n';
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... |