#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e6 + 10;
int pos[N];
int T[N], L[N], R[N], C[N], vis[N], rnk[N];
priority_queue <pair <ll, ll>> f[2][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> m >> n;
priority_queue <pair <ll, ll>, vector <pair <ll, ll>>, greater <pair <ll, ll>>> pq;
for (int i = 1; i <= n; i++) {
cin >> T[i] >> L[i] >> R[i] >> C[i];
pos[i] = T[i];
if (L[i] == 1) {
pq.push({C[i], i});
vis[i] = 1;
}
}
sort(pos + 1, pos + n + 1);
for (int i = 1; i <= n; i++) {
int p = lower_bound(pos + 1, pos + 1 + n, T[i]) - pos;
rnk[i] = p;
for (int k = p; k; k -= k & (-k))
f[0][k].push({-L[i] - T[i] + 1, i});
for (int k = p; k <= n; k += k & (-k))
f[1][k].push({T[i] - L[i] + 1, i});
}
while (sz(pq)) {
auto tt = pq.top();
pq.pop();
int id = tt.s;
int ra = rnk[id];
if (R[id] == m) {
cout << tt.f << '\n';
return 0;
}
for (int i = ra; i <= n; i += i & (-i)) {
while (sz(f[0][i])) {
auto t = f[0][i].top();
if (vis[t.s]) {
f[0][i].pop();
continue;
}
if (t.f >= -T[id] - R[id]) {
f[0][i].pop();
pq.push({tt.f + C[t.s], t.s});
vis[t.s] = 1;
} else {
break;
}
}
}
for (int i = ra; i; i -= i & (-i)) {
while (sz(f[1][i])) {
auto t = f[1][i].top();
if (vis[t.s]) {
f[1][i].pop();
continue;
}
if (t.f >= T[id] - R[id]) {
f[1][i].pop();
pq.push({tt.f + C[t.s], t.s});
vis[t.s] = 1;
} else {
break;
}
}
}
}
cout << -1 << "\n";
return 0;
}
// case 1:
// t[i] > t[j]
// t[i] - t[j] <= r[i] - l[j] + 1
// t[i] - r[i] <= t[j] - l[j] + 1
// case 2:
// t[i] < t[j]
// t[j] - t[i] <= r[i] - l[j] + 1
// -t[i] - r[i] <= -l[j] - t[j] + 1
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
112460 KB |
Output is correct |
2 |
Correct |
82 ms |
112452 KB |
Output is correct |
3 |
Correct |
109 ms |
112692 KB |
Output is correct |
4 |
Correct |
122 ms |
112576 KB |
Output is correct |
5 |
Correct |
95 ms |
114888 KB |
Output is correct |
6 |
Correct |
96 ms |
112452 KB |
Output is correct |
7 |
Correct |
94 ms |
112448 KB |
Output is correct |
8 |
Correct |
71 ms |
112292 KB |
Output is correct |
9 |
Correct |
69 ms |
112336 KB |
Output is correct |
10 |
Correct |
68 ms |
112372 KB |
Output is correct |
11 |
Correct |
89 ms |
115012 KB |
Output is correct |
12 |
Correct |
94 ms |
115004 KB |
Output is correct |
13 |
Correct |
118 ms |
115048 KB |
Output is correct |
14 |
Correct |
101 ms |
115040 KB |
Output is correct |
15 |
Correct |
104 ms |
112460 KB |
Output is correct |
16 |
Correct |
103 ms |
112452 KB |
Output is correct |
17 |
Correct |
105 ms |
111692 KB |
Output is correct |
18 |
Correct |
86 ms |
114480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
76380 KB |
Output is correct |
2 |
Correct |
10 ms |
76380 KB |
Output is correct |
3 |
Correct |
11 ms |
76376 KB |
Output is correct |
4 |
Correct |
11 ms |
76376 KB |
Output is correct |
5 |
Correct |
12 ms |
76376 KB |
Output is correct |
6 |
Correct |
10 ms |
76380 KB |
Output is correct |
7 |
Correct |
10 ms |
76492 KB |
Output is correct |
8 |
Correct |
10 ms |
76380 KB |
Output is correct |
9 |
Correct |
10 ms |
76464 KB |
Output is correct |
10 |
Correct |
14 ms |
76376 KB |
Output is correct |
11 |
Correct |
13 ms |
76536 KB |
Output is correct |
12 |
Correct |
11 ms |
76376 KB |
Output is correct |
13 |
Correct |
11 ms |
76380 KB |
Output is correct |
14 |
Correct |
10 ms |
76560 KB |
Output is correct |
15 |
Correct |
10 ms |
76416 KB |
Output is correct |
16 |
Correct |
10 ms |
76380 KB |
Output is correct |
17 |
Correct |
10 ms |
76564 KB |
Output is correct |
18 |
Correct |
10 ms |
76536 KB |
Output is correct |
19 |
Correct |
11 ms |
76380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
76380 KB |
Output is correct |
2 |
Correct |
10 ms |
76380 KB |
Output is correct |
3 |
Correct |
11 ms |
76376 KB |
Output is correct |
4 |
Correct |
11 ms |
76376 KB |
Output is correct |
5 |
Correct |
12 ms |
76376 KB |
Output is correct |
6 |
Correct |
10 ms |
76380 KB |
Output is correct |
7 |
Correct |
10 ms |
76492 KB |
Output is correct |
8 |
Correct |
10 ms |
76380 KB |
Output is correct |
9 |
Correct |
10 ms |
76464 KB |
Output is correct |
10 |
Correct |
14 ms |
76376 KB |
Output is correct |
11 |
Correct |
13 ms |
76536 KB |
Output is correct |
12 |
Correct |
11 ms |
76376 KB |
Output is correct |
13 |
Correct |
11 ms |
76380 KB |
Output is correct |
14 |
Correct |
10 ms |
76560 KB |
Output is correct |
15 |
Correct |
10 ms |
76416 KB |
Output is correct |
16 |
Correct |
10 ms |
76380 KB |
Output is correct |
17 |
Correct |
10 ms |
76564 KB |
Output is correct |
18 |
Correct |
10 ms |
76536 KB |
Output is correct |
19 |
Correct |
11 ms |
76380 KB |
Output is correct |
20 |
Correct |
17 ms |
77956 KB |
Output is correct |
21 |
Correct |
17 ms |
77912 KB |
Output is correct |
22 |
Correct |
16 ms |
78168 KB |
Output is correct |
23 |
Correct |
16 ms |
78204 KB |
Output is correct |
24 |
Correct |
16 ms |
77924 KB |
Output is correct |
25 |
Correct |
16 ms |
77656 KB |
Output is correct |
26 |
Correct |
19 ms |
77656 KB |
Output is correct |
27 |
Correct |
17 ms |
77656 KB |
Output is correct |
28 |
Correct |
15 ms |
77912 KB |
Output is correct |
29 |
Correct |
16 ms |
77912 KB |
Output is correct |
30 |
Correct |
15 ms |
77916 KB |
Output is correct |
31 |
Correct |
14 ms |
77708 KB |
Output is correct |
32 |
Correct |
15 ms |
77912 KB |
Output is correct |
33 |
Correct |
15 ms |
77916 KB |
Output is correct |
34 |
Correct |
17 ms |
78172 KB |
Output is correct |
35 |
Correct |
15 ms |
77916 KB |
Output is correct |
36 |
Correct |
15 ms |
77912 KB |
Output is correct |
37 |
Correct |
18 ms |
77916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
112460 KB |
Output is correct |
2 |
Correct |
82 ms |
112452 KB |
Output is correct |
3 |
Correct |
109 ms |
112692 KB |
Output is correct |
4 |
Correct |
122 ms |
112576 KB |
Output is correct |
5 |
Correct |
95 ms |
114888 KB |
Output is correct |
6 |
Correct |
96 ms |
112452 KB |
Output is correct |
7 |
Correct |
94 ms |
112448 KB |
Output is correct |
8 |
Correct |
71 ms |
112292 KB |
Output is correct |
9 |
Correct |
69 ms |
112336 KB |
Output is correct |
10 |
Correct |
68 ms |
112372 KB |
Output is correct |
11 |
Correct |
89 ms |
115012 KB |
Output is correct |
12 |
Correct |
94 ms |
115004 KB |
Output is correct |
13 |
Correct |
118 ms |
115048 KB |
Output is correct |
14 |
Correct |
101 ms |
115040 KB |
Output is correct |
15 |
Correct |
104 ms |
112460 KB |
Output is correct |
16 |
Correct |
103 ms |
112452 KB |
Output is correct |
17 |
Correct |
105 ms |
111692 KB |
Output is correct |
18 |
Correct |
86 ms |
114480 KB |
Output is correct |
19 |
Correct |
10 ms |
76380 KB |
Output is correct |
20 |
Correct |
10 ms |
76380 KB |
Output is correct |
21 |
Correct |
11 ms |
76376 KB |
Output is correct |
22 |
Correct |
11 ms |
76376 KB |
Output is correct |
23 |
Correct |
12 ms |
76376 KB |
Output is correct |
24 |
Correct |
10 ms |
76380 KB |
Output is correct |
25 |
Correct |
10 ms |
76492 KB |
Output is correct |
26 |
Correct |
10 ms |
76380 KB |
Output is correct |
27 |
Correct |
10 ms |
76464 KB |
Output is correct |
28 |
Correct |
14 ms |
76376 KB |
Output is correct |
29 |
Correct |
13 ms |
76536 KB |
Output is correct |
30 |
Correct |
11 ms |
76376 KB |
Output is correct |
31 |
Correct |
11 ms |
76380 KB |
Output is correct |
32 |
Correct |
10 ms |
76560 KB |
Output is correct |
33 |
Correct |
10 ms |
76416 KB |
Output is correct |
34 |
Correct |
10 ms |
76380 KB |
Output is correct |
35 |
Correct |
10 ms |
76564 KB |
Output is correct |
36 |
Correct |
10 ms |
76536 KB |
Output is correct |
37 |
Correct |
11 ms |
76380 KB |
Output is correct |
38 |
Correct |
17 ms |
77956 KB |
Output is correct |
39 |
Correct |
17 ms |
77912 KB |
Output is correct |
40 |
Correct |
16 ms |
78168 KB |
Output is correct |
41 |
Correct |
16 ms |
78204 KB |
Output is correct |
42 |
Correct |
16 ms |
77924 KB |
Output is correct |
43 |
Correct |
16 ms |
77656 KB |
Output is correct |
44 |
Correct |
19 ms |
77656 KB |
Output is correct |
45 |
Correct |
17 ms |
77656 KB |
Output is correct |
46 |
Correct |
15 ms |
77912 KB |
Output is correct |
47 |
Correct |
16 ms |
77912 KB |
Output is correct |
48 |
Correct |
15 ms |
77916 KB |
Output is correct |
49 |
Correct |
14 ms |
77708 KB |
Output is correct |
50 |
Correct |
15 ms |
77912 KB |
Output is correct |
51 |
Correct |
15 ms |
77916 KB |
Output is correct |
52 |
Correct |
17 ms |
78172 KB |
Output is correct |
53 |
Correct |
15 ms |
77916 KB |
Output is correct |
54 |
Correct |
15 ms |
77912 KB |
Output is correct |
55 |
Correct |
18 ms |
77916 KB |
Output is correct |
56 |
Correct |
245 ms |
116168 KB |
Output is correct |
57 |
Correct |
217 ms |
116496 KB |
Output is correct |
58 |
Correct |
233 ms |
114940 KB |
Output is correct |
59 |
Correct |
222 ms |
115376 KB |
Output is correct |
60 |
Correct |
196 ms |
122796 KB |
Output is correct |
61 |
Correct |
237 ms |
114936 KB |
Output is correct |
62 |
Correct |
230 ms |
116104 KB |
Output is correct |
63 |
Correct |
178 ms |
113328 KB |
Output is correct |
64 |
Correct |
179 ms |
113332 KB |
Output is correct |
65 |
Correct |
104 ms |
116196 KB |
Output is correct |
66 |
Correct |
161 ms |
122164 KB |
Output is correct |
67 |
Correct |
217 ms |
114148 KB |
Output is correct |
68 |
Correct |
211 ms |
114276 KB |
Output is correct |
69 |
Correct |
190 ms |
115124 KB |
Output is correct |
70 |
Correct |
238 ms |
114228 KB |
Output is correct |
71 |
Correct |
224 ms |
114488 KB |
Output is correct |
72 |
Correct |
209 ms |
113980 KB |
Output is correct |
73 |
Correct |
229 ms |
114220 KB |
Output is correct |
74 |
Correct |
97 ms |
114132 KB |
Output is correct |
75 |
Correct |
90 ms |
114112 KB |
Output is correct |
76 |
Correct |
149 ms |
121000 KB |
Output is correct |
77 |
Correct |
152 ms |
115632 KB |
Output is correct |
78 |
Correct |
243 ms |
121464 KB |
Output is correct |
79 |
Correct |
273 ms |
114096 KB |
Output is correct |
80 |
Correct |
289 ms |
119052 KB |
Output is correct |
81 |
Correct |
140 ms |
114224 KB |
Output is correct |
82 |
Correct |
278 ms |
117800 KB |
Output is correct |
83 |
Correct |
283 ms |
113332 KB |
Output is correct |
84 |
Correct |
259 ms |
113464 KB |
Output is correct |
85 |
Correct |
215 ms |
114104 KB |
Output is correct |
86 |
Correct |
197 ms |
114100 KB |
Output is correct |
87 |
Correct |
227 ms |
114104 KB |
Output is correct |
88 |
Correct |
233 ms |
114224 KB |
Output is correct |
89 |
Correct |
244 ms |
114092 KB |
Output is correct |
90 |
Correct |
169 ms |
116320 KB |
Output is correct |
91 |
Correct |
130 ms |
114044 KB |
Output is correct |
92 |
Correct |
220 ms |
114100 KB |
Output is correct |
93 |
Correct |
257 ms |
114092 KB |
Output is correct |
94 |
Correct |
250 ms |
114232 KB |
Output is correct |
95 |
Correct |
244 ms |
114104 KB |
Output is correct |
96 |
Correct |
141 ms |
115548 KB |
Output is correct |
97 |
Correct |
169 ms |
116396 KB |
Output is correct |
98 |
Correct |
164 ms |
116332 KB |
Output is correct |
99 |
Correct |
175 ms |
116148 KB |
Output is correct |
100 |
Correct |
197 ms |
116664 KB |
Output is correct |
101 |
Correct |
201 ms |
116280 KB |
Output is correct |
102 |
Correct |
161 ms |
116400 KB |
Output is correct |
103 |
Correct |
177 ms |
114480 KB |
Output is correct |