#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 = 1e5 + 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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
38060 KB |
Output is correct |
2 |
Correct |
70 ms |
38216 KB |
Output is correct |
3 |
Correct |
90 ms |
39300 KB |
Output is correct |
4 |
Correct |
92 ms |
39112 KB |
Output is correct |
5 |
Correct |
84 ms |
40600 KB |
Output is correct |
6 |
Correct |
84 ms |
38228 KB |
Output is correct |
7 |
Correct |
80 ms |
38016 KB |
Output is correct |
8 |
Correct |
58 ms |
38052 KB |
Output is correct |
9 |
Correct |
59 ms |
38116 KB |
Output is correct |
10 |
Correct |
58 ms |
38228 KB |
Output is correct |
11 |
Correct |
77 ms |
40776 KB |
Output is correct |
12 |
Correct |
80 ms |
40776 KB |
Output is correct |
13 |
Correct |
87 ms |
40772 KB |
Output is correct |
14 |
Correct |
85 ms |
40776 KB |
Output is correct |
15 |
Correct |
86 ms |
38212 KB |
Output is correct |
16 |
Correct |
86 ms |
38220 KB |
Output is correct |
17 |
Correct |
83 ms |
38216 KB |
Output is correct |
18 |
Correct |
76 ms |
40636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8792 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8792 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8796 KB |
Output is correct |
20 |
Correct |
7 ms |
10076 KB |
Output is correct |
21 |
Correct |
7 ms |
10076 KB |
Output is correct |
22 |
Correct |
7 ms |
10332 KB |
Output is correct |
23 |
Correct |
7 ms |
10180 KB |
Output is correct |
24 |
Correct |
7 ms |
10076 KB |
Output is correct |
25 |
Correct |
7 ms |
9888 KB |
Output is correct |
26 |
Correct |
6 ms |
9820 KB |
Output is correct |
27 |
Correct |
6 ms |
9820 KB |
Output is correct |
28 |
Correct |
6 ms |
10076 KB |
Output is correct |
29 |
Correct |
6 ms |
9820 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
4 ms |
9820 KB |
Output is correct |
32 |
Correct |
5 ms |
10076 KB |
Output is correct |
33 |
Correct |
5 ms |
10076 KB |
Output is correct |
34 |
Correct |
9 ms |
10224 KB |
Output is correct |
35 |
Correct |
5 ms |
10080 KB |
Output is correct |
36 |
Correct |
5 ms |
10192 KB |
Output is correct |
37 |
Correct |
9 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
38060 KB |
Output is correct |
2 |
Correct |
70 ms |
38216 KB |
Output is correct |
3 |
Correct |
90 ms |
39300 KB |
Output is correct |
4 |
Correct |
92 ms |
39112 KB |
Output is correct |
5 |
Correct |
84 ms |
40600 KB |
Output is correct |
6 |
Correct |
84 ms |
38228 KB |
Output is correct |
7 |
Correct |
80 ms |
38016 KB |
Output is correct |
8 |
Correct |
58 ms |
38052 KB |
Output is correct |
9 |
Correct |
59 ms |
38116 KB |
Output is correct |
10 |
Correct |
58 ms |
38228 KB |
Output is correct |
11 |
Correct |
77 ms |
40776 KB |
Output is correct |
12 |
Correct |
80 ms |
40776 KB |
Output is correct |
13 |
Correct |
87 ms |
40772 KB |
Output is correct |
14 |
Correct |
85 ms |
40776 KB |
Output is correct |
15 |
Correct |
86 ms |
38212 KB |
Output is correct |
16 |
Correct |
86 ms |
38220 KB |
Output is correct |
17 |
Correct |
83 ms |
38216 KB |
Output is correct |
18 |
Correct |
76 ms |
40636 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8796 KB |
Output is correct |
23 |
Correct |
1 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
1 ms |
8792 KB |
Output is correct |
28 |
Correct |
1 ms |
8792 KB |
Output is correct |
29 |
Correct |
1 ms |
8796 KB |
Output is correct |
30 |
Correct |
1 ms |
8796 KB |
Output is correct |
31 |
Correct |
1 ms |
8796 KB |
Output is correct |
32 |
Correct |
1 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8796 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
35 |
Correct |
1 ms |
8796 KB |
Output is correct |
36 |
Correct |
1 ms |
8796 KB |
Output is correct |
37 |
Correct |
1 ms |
8796 KB |
Output is correct |
38 |
Correct |
7 ms |
10076 KB |
Output is correct |
39 |
Correct |
7 ms |
10076 KB |
Output is correct |
40 |
Correct |
7 ms |
10332 KB |
Output is correct |
41 |
Correct |
7 ms |
10180 KB |
Output is correct |
42 |
Correct |
7 ms |
10076 KB |
Output is correct |
43 |
Correct |
7 ms |
9888 KB |
Output is correct |
44 |
Correct |
6 ms |
9820 KB |
Output is correct |
45 |
Correct |
6 ms |
9820 KB |
Output is correct |
46 |
Correct |
6 ms |
10076 KB |
Output is correct |
47 |
Correct |
6 ms |
9820 KB |
Output is correct |
48 |
Correct |
4 ms |
9820 KB |
Output is correct |
49 |
Correct |
4 ms |
9820 KB |
Output is correct |
50 |
Correct |
5 ms |
10076 KB |
Output is correct |
51 |
Correct |
5 ms |
10076 KB |
Output is correct |
52 |
Correct |
9 ms |
10224 KB |
Output is correct |
53 |
Correct |
5 ms |
10080 KB |
Output is correct |
54 |
Correct |
5 ms |
10192 KB |
Output is correct |
55 |
Correct |
9 ms |
10076 KB |
Output is correct |
56 |
Correct |
214 ms |
42492 KB |
Output is correct |
57 |
Correct |
206 ms |
42444 KB |
Output is correct |
58 |
Correct |
215 ms |
41396 KB |
Output is correct |
59 |
Correct |
213 ms |
41392 KB |
Output is correct |
60 |
Correct |
174 ms |
47912 KB |
Output is correct |
61 |
Correct |
221 ms |
41308 KB |
Output is correct |
62 |
Correct |
266 ms |
42444 KB |
Output is correct |
63 |
Correct |
179 ms |
39092 KB |
Output is correct |
64 |
Correct |
164 ms |
39084 KB |
Output is correct |
65 |
Correct |
95 ms |
41744 KB |
Output is correct |
66 |
Correct |
142 ms |
47544 KB |
Output is correct |
67 |
Correct |
194 ms |
39100 KB |
Output is correct |
68 |
Correct |
176 ms |
39056 KB |
Output is correct |
69 |
Correct |
164 ms |
40112 KB |
Output is correct |
70 |
Correct |
224 ms |
39224 KB |
Output is correct |
71 |
Correct |
193 ms |
39488 KB |
Output is correct |
72 |
Correct |
161 ms |
39032 KB |
Output is correct |
73 |
Correct |
220 ms |
39212 KB |
Output is correct |
74 |
Correct |
78 ms |
39016 KB |
Output is correct |
75 |
Correct |
73 ms |
38952 KB |
Output is correct |
76 |
Correct |
136 ms |
46072 KB |
Output is correct |
77 |
Correct |
129 ms |
40572 KB |
Output is correct |
78 |
Correct |
227 ms |
46792 KB |
Output is correct |
79 |
Correct |
264 ms |
39156 KB |
Output is correct |
80 |
Correct |
275 ms |
44464 KB |
Output is correct |
81 |
Correct |
114 ms |
38960 KB |
Output is correct |
82 |
Correct |
267 ms |
43776 KB |
Output is correct |
83 |
Correct |
257 ms |
39340 KB |
Output is correct |
84 |
Correct |
240 ms |
38968 KB |
Output is correct |
85 |
Correct |
218 ms |
39096 KB |
Output is correct |
86 |
Correct |
191 ms |
38972 KB |
Output is correct |
87 |
Correct |
202 ms |
39096 KB |
Output is correct |
88 |
Correct |
220 ms |
39220 KB |
Output is correct |
89 |
Correct |
228 ms |
39092 KB |
Output is correct |
90 |
Correct |
151 ms |
41128 KB |
Output is correct |
91 |
Correct |
116 ms |
39096 KB |
Output is correct |
92 |
Correct |
214 ms |
39084 KB |
Output is correct |
93 |
Correct |
249 ms |
39096 KB |
Output is correct |
94 |
Correct |
252 ms |
39604 KB |
Output is correct |
95 |
Correct |
225 ms |
39096 KB |
Output is correct |
96 |
Correct |
138 ms |
40636 KB |
Output is correct |
97 |
Correct |
157 ms |
41136 KB |
Output is correct |
98 |
Correct |
164 ms |
41112 KB |
Output is correct |
99 |
Correct |
146 ms |
41200 KB |
Output is correct |
100 |
Correct |
157 ms |
41444 KB |
Output is correct |
101 |
Correct |
206 ms |
41164 KB |
Output is correct |
102 |
Correct |
145 ms |
41192 KB |
Output is correct |
103 |
Correct |
164 ms |
39340 KB |
Output is correct |