#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rd);
}
const int N = 2e5 + 7;
const int Mod =1e9 + 7;
const int szBL = 916;
const int INF = 2e9 + 7;
const int BASE = 137;
struct Data {
ll T, L, R, C;
};
int n, m;
Data a[N];
vector<pair<ll,ll>> ke[N];
ll Dist[N];
vector<ll> Val; int nVal;
struct Segment_Tree_Set {
int Range;
set<pair<int, int>> sval[N];
pair<int,int> st[N << 2];
void init (int n ) {
Range = n;
}
void Merge (int id) {
if (st[id << 1].fs <= st[id << 1 | 1].fs) st[id] = st[id << 1];
else st[id] = st[id << 1 | 1];
}
void update (int id, int l, int r, int pos, pair<int, int> cur) {
if (l > pos || r < pos) return;
if (l == r) {
sval[l].insert(cur);
st[id] = *sval[l].begin();
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, pos, cur);
update (id << 1 | 1, mid + 1, r, pos, cur);
Merge (id);
}
void deupdate (int id, int l, int r, int pos, pair<int,int> cur){
if (l > pos || r < pos) return;
if (l == r) {
sval[l].erase(sval[l].find(cur));
if (sval[l].empty()) st[id] = {INF, -1};
else st[id] = *sval[l].begin();
return;
}
int mid = l + r >> 1;
deupdate (id << 1, l, mid, pos, cur);
deupdate (id << 1 | 1, mid + 1, r, pos, cur);
Merge (id);
}
pair<int,int> get (int id, int l, int r, int u, int v) {
if (l > v || r < u) return {INF, -1};
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
pair<int,int> p1 = get (id << 1, l, mid, u, v),
p2 = get (id << 1 | 1, mid + 1, r, u, v);
if (p1.fs <= p2.fs) return p1;
else return p2;
}
void update (int pos, pair<int,int> cur) {
update (1, 1, Range, pos, cur);
}
void deupdate (int pos, pair<int,int> cur) {
deupdate (1, 1, Range, pos, cur);
}
pair<int,int> get (int u, int v) {
return get (1, 1, Range, u, v);
}
}ST1, ST2;
void solution () {
cin >> n >> m;
rep (i, 1, m) {
cin >> a[i].T >> a[i].L >> a[i].R >> a[i].C;
--a[i].L;
}
rep (i, 1, m) {
Val.push_back(a[i].T);
}
sort (ALL(Val));
Val.resize(nVal = unique(ALL(Val)) - Val.begin());
ST1.init(nVal);
ST2.init(nVal);
/**
1: a[i].R + a[i].T >= a[j].R + a[j].T ti < tj
2: a[i].R - a[i].T >= a[j].L - a[j].T ti > tj
*/
auto CPR = [] (int val) {
return lower_bound(ALL(Val), val) - Val.begin() + 1;
};
rep (i, 1, m) {
int pos = CPR(a[i].T);
ST1.update (pos, mp(a[i].L + a[i].T, i));
ST2.update (pos, mp(a[i].L - a[i].T, i));
}
memset(Dist, 0x3f, sizeof Dist);
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
rep (i, 1, m) {
if (a[i].L == 0) {
pq.push({a[i].C, i});
Dist[i] = a[i].C;
}
}
while (!pq.empty()) {
int u = pq.top().se;
if (a[u].R == n) {
cout << Dist[u] <<"\n";
return;
}
pq.pop();
int pos = CPR(a[u].T);
while (1) {
int v = ST1.get(pos, nVal).se;
// cout << u <<" "<<v<<" a \n";
if (v == -1 || a[u].R + a[u].T < a[v].L + a[v].T) break;
Dist[v] = min(Dist[v], Dist[u] + a[v].C);
pq.push({Dist[v], v});
ST1.deupdate (CPR(a[v].T), mp(a[v].L + a[v].T, v));
}
while (1) {
int v = ST2.get(1, pos).se;
// cout << u <<" "<<v<<" b\n";
if (v == -1 || a[u].R - a[u].T < a[v].L - a[v].T) break;
Dist[v] = min(Dist[v], Dist[u] + a[v].C);
pq.push({Dist[v], v});
ST2.deupdate (CPR(a[v].T), mp(a[v].L - a[v].T, v));
}
}
cout << -1 <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// file ("c");
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
1 2
*/
Compilation message
treatment.cpp: In member function 'void Segment_Tree_Set::update(int, int, int, int, std::pair<int, int>)':
treatment.cpp:60:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = l + r >> 1;
| ~~^~~
treatment.cpp: In member function 'void Segment_Tree_Set::deupdate(int, int, int, int, std::pair<int, int>)':
treatment.cpp:73:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int mid = l + r >> 1;
| ~~^~~
treatment.cpp: In member function 'std::pair<int, int> Segment_Tree_Set::get(int, int, int, int, int)':
treatment.cpp:81:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
54472 KB |
Output is correct |
2 |
Correct |
96 ms |
54328 KB |
Output is correct |
3 |
Correct |
122 ms |
55668 KB |
Output is correct |
4 |
Correct |
121 ms |
55628 KB |
Output is correct |
5 |
Correct |
92 ms |
58600 KB |
Output is correct |
6 |
Correct |
98 ms |
54812 KB |
Output is correct |
7 |
Correct |
94 ms |
54220 KB |
Output is correct |
8 |
Correct |
71 ms |
54480 KB |
Output is correct |
9 |
Correct |
67 ms |
54332 KB |
Output is correct |
10 |
Correct |
71 ms |
54224 KB |
Output is correct |
11 |
Correct |
112 ms |
58704 KB |
Output is correct |
12 |
Correct |
121 ms |
58556 KB |
Output is correct |
13 |
Correct |
130 ms |
58596 KB |
Output is correct |
14 |
Correct |
118 ms |
58460 KB |
Output is correct |
15 |
Correct |
124 ms |
54544 KB |
Output is correct |
16 |
Correct |
134 ms |
54472 KB |
Output is correct |
17 |
Correct |
121 ms |
53524 KB |
Output is correct |
18 |
Correct |
109 ms |
57936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
37828 KB |
Output is correct |
2 |
Correct |
22 ms |
37984 KB |
Output is correct |
3 |
Correct |
22 ms |
38048 KB |
Output is correct |
4 |
Correct |
17 ms |
38024 KB |
Output is correct |
5 |
Correct |
17 ms |
37980 KB |
Output is correct |
6 |
Correct |
17 ms |
37980 KB |
Output is correct |
7 |
Correct |
18 ms |
37980 KB |
Output is correct |
8 |
Correct |
16 ms |
37944 KB |
Output is correct |
9 |
Correct |
17 ms |
37980 KB |
Output is correct |
10 |
Correct |
21 ms |
37980 KB |
Output is correct |
11 |
Correct |
16 ms |
37980 KB |
Output is correct |
12 |
Correct |
17 ms |
37976 KB |
Output is correct |
13 |
Correct |
17 ms |
37980 KB |
Output is correct |
14 |
Correct |
17 ms |
37972 KB |
Output is correct |
15 |
Correct |
18 ms |
38232 KB |
Output is correct |
16 |
Correct |
17 ms |
38040 KB |
Output is correct |
17 |
Correct |
16 ms |
37976 KB |
Output is correct |
18 |
Correct |
17 ms |
37980 KB |
Output is correct |
19 |
Correct |
17 ms |
37976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
37828 KB |
Output is correct |
2 |
Correct |
22 ms |
37984 KB |
Output is correct |
3 |
Correct |
22 ms |
38048 KB |
Output is correct |
4 |
Correct |
17 ms |
38024 KB |
Output is correct |
5 |
Correct |
17 ms |
37980 KB |
Output is correct |
6 |
Correct |
17 ms |
37980 KB |
Output is correct |
7 |
Correct |
18 ms |
37980 KB |
Output is correct |
8 |
Correct |
16 ms |
37944 KB |
Output is correct |
9 |
Correct |
17 ms |
37980 KB |
Output is correct |
10 |
Correct |
21 ms |
37980 KB |
Output is correct |
11 |
Correct |
16 ms |
37980 KB |
Output is correct |
12 |
Correct |
17 ms |
37976 KB |
Output is correct |
13 |
Correct |
17 ms |
37980 KB |
Output is correct |
14 |
Correct |
17 ms |
37972 KB |
Output is correct |
15 |
Correct |
18 ms |
38232 KB |
Output is correct |
16 |
Correct |
17 ms |
38040 KB |
Output is correct |
17 |
Correct |
16 ms |
37976 KB |
Output is correct |
18 |
Correct |
17 ms |
37980 KB |
Output is correct |
19 |
Correct |
17 ms |
37976 KB |
Output is correct |
20 |
Correct |
24 ms |
38720 KB |
Output is correct |
21 |
Correct |
24 ms |
38748 KB |
Output is correct |
22 |
Correct |
24 ms |
38744 KB |
Output is correct |
23 |
Correct |
22 ms |
38740 KB |
Output is correct |
24 |
Correct |
24 ms |
39004 KB |
Output is correct |
25 |
Correct |
27 ms |
38836 KB |
Output is correct |
26 |
Correct |
26 ms |
38664 KB |
Output is correct |
27 |
Correct |
27 ms |
38868 KB |
Output is correct |
28 |
Correct |
29 ms |
38964 KB |
Output is correct |
29 |
Correct |
26 ms |
38744 KB |
Output is correct |
30 |
Correct |
21 ms |
38748 KB |
Output is correct |
31 |
Correct |
20 ms |
38748 KB |
Output is correct |
32 |
Correct |
23 ms |
38968 KB |
Output is correct |
33 |
Correct |
23 ms |
38972 KB |
Output is correct |
34 |
Correct |
28 ms |
38748 KB |
Output is correct |
35 |
Correct |
23 ms |
38744 KB |
Output is correct |
36 |
Correct |
23 ms |
38820 KB |
Output is correct |
37 |
Correct |
27 ms |
38744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
54472 KB |
Output is correct |
2 |
Correct |
96 ms |
54328 KB |
Output is correct |
3 |
Correct |
122 ms |
55668 KB |
Output is correct |
4 |
Correct |
121 ms |
55628 KB |
Output is correct |
5 |
Correct |
92 ms |
58600 KB |
Output is correct |
6 |
Correct |
98 ms |
54812 KB |
Output is correct |
7 |
Correct |
94 ms |
54220 KB |
Output is correct |
8 |
Correct |
71 ms |
54480 KB |
Output is correct |
9 |
Correct |
67 ms |
54332 KB |
Output is correct |
10 |
Correct |
71 ms |
54224 KB |
Output is correct |
11 |
Correct |
112 ms |
58704 KB |
Output is correct |
12 |
Correct |
121 ms |
58556 KB |
Output is correct |
13 |
Correct |
130 ms |
58596 KB |
Output is correct |
14 |
Correct |
118 ms |
58460 KB |
Output is correct |
15 |
Correct |
124 ms |
54544 KB |
Output is correct |
16 |
Correct |
134 ms |
54472 KB |
Output is correct |
17 |
Correct |
121 ms |
53524 KB |
Output is correct |
18 |
Correct |
109 ms |
57936 KB |
Output is correct |
19 |
Correct |
18 ms |
37828 KB |
Output is correct |
20 |
Correct |
22 ms |
37984 KB |
Output is correct |
21 |
Correct |
22 ms |
38048 KB |
Output is correct |
22 |
Correct |
17 ms |
38024 KB |
Output is correct |
23 |
Correct |
17 ms |
37980 KB |
Output is correct |
24 |
Correct |
17 ms |
37980 KB |
Output is correct |
25 |
Correct |
18 ms |
37980 KB |
Output is correct |
26 |
Correct |
16 ms |
37944 KB |
Output is correct |
27 |
Correct |
17 ms |
37980 KB |
Output is correct |
28 |
Correct |
21 ms |
37980 KB |
Output is correct |
29 |
Correct |
16 ms |
37980 KB |
Output is correct |
30 |
Correct |
17 ms |
37976 KB |
Output is correct |
31 |
Correct |
17 ms |
37980 KB |
Output is correct |
32 |
Correct |
17 ms |
37972 KB |
Output is correct |
33 |
Correct |
18 ms |
38232 KB |
Output is correct |
34 |
Correct |
17 ms |
38040 KB |
Output is correct |
35 |
Correct |
16 ms |
37976 KB |
Output is correct |
36 |
Correct |
17 ms |
37980 KB |
Output is correct |
37 |
Correct |
17 ms |
37976 KB |
Output is correct |
38 |
Correct |
24 ms |
38720 KB |
Output is correct |
39 |
Correct |
24 ms |
38748 KB |
Output is correct |
40 |
Correct |
24 ms |
38744 KB |
Output is correct |
41 |
Correct |
22 ms |
38740 KB |
Output is correct |
42 |
Correct |
24 ms |
39004 KB |
Output is correct |
43 |
Correct |
27 ms |
38836 KB |
Output is correct |
44 |
Correct |
26 ms |
38664 KB |
Output is correct |
45 |
Correct |
27 ms |
38868 KB |
Output is correct |
46 |
Correct |
29 ms |
38964 KB |
Output is correct |
47 |
Correct |
26 ms |
38744 KB |
Output is correct |
48 |
Correct |
21 ms |
38748 KB |
Output is correct |
49 |
Correct |
20 ms |
38748 KB |
Output is correct |
50 |
Correct |
23 ms |
38968 KB |
Output is correct |
51 |
Correct |
23 ms |
38972 KB |
Output is correct |
52 |
Correct |
28 ms |
38748 KB |
Output is correct |
53 |
Correct |
23 ms |
38744 KB |
Output is correct |
54 |
Correct |
23 ms |
38820 KB |
Output is correct |
55 |
Correct |
27 ms |
38744 KB |
Output is correct |
56 |
Correct |
279 ms |
55244 KB |
Output is correct |
57 |
Correct |
228 ms |
56388 KB |
Output is correct |
58 |
Correct |
224 ms |
53444 KB |
Output is correct |
59 |
Correct |
228 ms |
53968 KB |
Output is correct |
60 |
Correct |
221 ms |
54728 KB |
Output is correct |
61 |
Correct |
216 ms |
53452 KB |
Output is correct |
62 |
Correct |
240 ms |
55180 KB |
Output is correct |
63 |
Correct |
219 ms |
54544 KB |
Output is correct |
64 |
Correct |
226 ms |
54480 KB |
Output is correct |
65 |
Correct |
106 ms |
54448 KB |
Output is correct |
66 |
Correct |
174 ms |
54720 KB |
Output is correct |
67 |
Correct |
312 ms |
55504 KB |
Output is correct |
68 |
Correct |
262 ms |
55164 KB |
Output is correct |
69 |
Correct |
264 ms |
55080 KB |
Output is correct |
70 |
Correct |
309 ms |
55500 KB |
Output is correct |
71 |
Correct |
282 ms |
55224 KB |
Output is correct |
72 |
Correct |
259 ms |
54992 KB |
Output is correct |
73 |
Correct |
319 ms |
55244 KB |
Output is correct |
74 |
Correct |
104 ms |
55048 KB |
Output is correct |
75 |
Correct |
108 ms |
55204 KB |
Output is correct |
76 |
Correct |
234 ms |
56872 KB |
Output is correct |
77 |
Correct |
182 ms |
57396 KB |
Output is correct |
78 |
Correct |
280 ms |
58956 KB |
Output is correct |
79 |
Correct |
350 ms |
55104 KB |
Output is correct |
80 |
Correct |
328 ms |
54728 KB |
Output is correct |
81 |
Correct |
142 ms |
55000 KB |
Output is correct |
82 |
Correct |
366 ms |
54476 KB |
Output is correct |
83 |
Correct |
341 ms |
54660 KB |
Output is correct |
84 |
Correct |
345 ms |
54280 KB |
Output is correct |
85 |
Correct |
257 ms |
55020 KB |
Output is correct |
86 |
Correct |
253 ms |
55104 KB |
Output is correct |
87 |
Correct |
268 ms |
55096 KB |
Output is correct |
88 |
Correct |
330 ms |
55180 KB |
Output is correct |
89 |
Correct |
257 ms |
55136 KB |
Output is correct |
90 |
Correct |
207 ms |
57336 KB |
Output is correct |
91 |
Correct |
148 ms |
55068 KB |
Output is correct |
92 |
Correct |
259 ms |
55232 KB |
Output is correct |
93 |
Correct |
329 ms |
55244 KB |
Output is correct |
94 |
Correct |
319 ms |
55500 KB |
Output is correct |
95 |
Correct |
275 ms |
55176 KB |
Output is correct |
96 |
Correct |
206 ms |
56524 KB |
Output is correct |
97 |
Correct |
228 ms |
59420 KB |
Output is correct |
98 |
Correct |
248 ms |
59252 KB |
Output is correct |
99 |
Correct |
284 ms |
57328 KB |
Output is correct |
100 |
Correct |
199 ms |
59264 KB |
Output is correct |
101 |
Correct |
291 ms |
59296 KB |
Output is correct |
102 |
Correct |
242 ms |
59304 KB |
Output is correct |
103 |
Correct |
204 ms |
55760 KB |
Output is correct |