#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const ll Inf = 1e18;
struct Node {
int val, id;
Node(int val, int id) : val(val), id(id) {}
Node() : val((int)2e9), id(-1) {}
Node operator +(const Node &other) const {
if (val <= other.val) return (*this);
else return other;
}
};
const int N = 2e5 + 7;
Node t[4 * N];
void build(int l, int r, int v, vector <int> &a) {
if (l == r) {
t[v] = Node(a[l], l);
} else {
int m = (l + r) >> 1;
build(l, m, 2 * v + 1, a);
build(m + 1, r, 2 * v + 2, a);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
Node get(int ql, int qr, int l, int r, int v) {
if (qr < l || r < ql) return Node();
if (ql <= l && r <= qr) return t[v];
int m = (l + r) >> 1;
return get(ql, qr, l, m, 2 * v + 1) + get(ql, qr, m + 1, r, 2 * v + 2);
}
void kill(int id, int l, int r, int v) {
if (l == r) {
t[v] = Node();
} else {
int m = (l + r) >> 1;
if (id <= m) kill(id, l, m, 2 * v + 1);
else kill(id, m + 1, r, 2 * v + 2);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m;
cin >> n >> m;
vector <int> t(m), l(m), r(m), c(m);
vector <pair <int, int>> cm;
for (int i = 0; i < m; ++i) {
cin >> t[i] >> l[i] >> r[i] >> c[i];
--l[i];
cm.push_back({l[i] + t[i], i});
}
sort(cm.begin(), cm.end());
vector <int> a(m);
vector <int> pos(m);
for (int i = 0; i < m; ++i) {
int id = cm[i].second;
a[i] = l[id] - t[id];
pos[id] = i;
}
build(0, m - 1, 0, a);
// r[i] - t[i] >= l[j] - t[j]
// r[i] + t[i] >= l[j] + t[j]
// i -> j
set <pair <ll, int>> q;
for (int i = 0; i < m; ++i) {
if (l[i] == 0) {
q.insert({c[i], i});
kill(pos[i], 0, m - 1, 0);
}
}
ll ans = Inf;
while (!q.empty()) {
auto p = *q.begin();
q.erase(q.begin());
int u = p.second;
ll cd = p.first;
if (r[u] == n) ans = min(ans, cd);
int cr = upper_bound(cm.begin(), cm.end(), make_pair(r[u] + t[u], n)) - cm.begin() - 1;
while (true) {
auto nd = get(0, cr, 0, m - 1, 0);
if (nd.val <= r[u] - t[u]) {
int v = cm[nd.id].second;
kill(pos[v], 0, m - 1, 0);
q.insert({cd + c[v], v});
} else {
break;
}
}
}
if (ans == Inf) cout << "-1\n";
else cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
9712 KB |
Output is correct |
2 |
Correct |
91 ms |
9712 KB |
Output is correct |
3 |
Correct |
145 ms |
12400 KB |
Output is correct |
4 |
Correct |
137 ms |
11372 KB |
Output is correct |
5 |
Correct |
193 ms |
15800 KB |
Output is correct |
6 |
Correct |
145 ms |
10104 KB |
Output is correct |
7 |
Correct |
126 ms |
9712 KB |
Output is correct |
8 |
Correct |
74 ms |
9840 KB |
Output is correct |
9 |
Correct |
69 ms |
9712 KB |
Output is correct |
10 |
Correct |
74 ms |
9792 KB |
Output is correct |
11 |
Correct |
174 ms |
15984 KB |
Output is correct |
12 |
Correct |
172 ms |
15984 KB |
Output is correct |
13 |
Correct |
198 ms |
14320 KB |
Output is correct |
14 |
Correct |
193 ms |
14192 KB |
Output is correct |
15 |
Correct |
141 ms |
10096 KB |
Output is correct |
16 |
Correct |
144 ms |
9840 KB |
Output is correct |
17 |
Correct |
134 ms |
9712 KB |
Output is correct |
18 |
Correct |
162 ms |
15984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6656 KB |
Output is correct |
2 |
Correct |
8 ms |
6528 KB |
Output is correct |
3 |
Correct |
8 ms |
6656 KB |
Output is correct |
4 |
Correct |
8 ms |
6528 KB |
Output is correct |
5 |
Correct |
8 ms |
6528 KB |
Output is correct |
6 |
Correct |
8 ms |
6656 KB |
Output is correct |
7 |
Correct |
10 ms |
6656 KB |
Output is correct |
8 |
Correct |
8 ms |
6656 KB |
Output is correct |
9 |
Correct |
8 ms |
6528 KB |
Output is correct |
10 |
Correct |
8 ms |
6656 KB |
Output is correct |
11 |
Correct |
8 ms |
6528 KB |
Output is correct |
12 |
Correct |
8 ms |
6528 KB |
Output is correct |
13 |
Correct |
8 ms |
6528 KB |
Output is correct |
14 |
Correct |
8 ms |
6528 KB |
Output is correct |
15 |
Correct |
8 ms |
6528 KB |
Output is correct |
16 |
Correct |
9 ms |
6528 KB |
Output is correct |
17 |
Correct |
8 ms |
6656 KB |
Output is correct |
18 |
Correct |
9 ms |
6528 KB |
Output is correct |
19 |
Correct |
8 ms |
6528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
6656 KB |
Output is correct |
2 |
Correct |
8 ms |
6528 KB |
Output is correct |
3 |
Correct |
8 ms |
6656 KB |
Output is correct |
4 |
Correct |
8 ms |
6528 KB |
Output is correct |
5 |
Correct |
8 ms |
6528 KB |
Output is correct |
6 |
Correct |
8 ms |
6656 KB |
Output is correct |
7 |
Correct |
10 ms |
6656 KB |
Output is correct |
8 |
Correct |
8 ms |
6656 KB |
Output is correct |
9 |
Correct |
8 ms |
6528 KB |
Output is correct |
10 |
Correct |
8 ms |
6656 KB |
Output is correct |
11 |
Correct |
8 ms |
6528 KB |
Output is correct |
12 |
Correct |
8 ms |
6528 KB |
Output is correct |
13 |
Correct |
8 ms |
6528 KB |
Output is correct |
14 |
Correct |
8 ms |
6528 KB |
Output is correct |
15 |
Correct |
8 ms |
6528 KB |
Output is correct |
16 |
Correct |
9 ms |
6528 KB |
Output is correct |
17 |
Correct |
8 ms |
6656 KB |
Output is correct |
18 |
Correct |
9 ms |
6528 KB |
Output is correct |
19 |
Correct |
8 ms |
6528 KB |
Output is correct |
20 |
Correct |
13 ms |
6912 KB |
Output is correct |
21 |
Correct |
13 ms |
6912 KB |
Output is correct |
22 |
Correct |
13 ms |
6784 KB |
Output is correct |
23 |
Correct |
13 ms |
6784 KB |
Output is correct |
24 |
Correct |
15 ms |
7040 KB |
Output is correct |
25 |
Correct |
15 ms |
6784 KB |
Output is correct |
26 |
Correct |
14 ms |
6784 KB |
Output is correct |
27 |
Correct |
14 ms |
6784 KB |
Output is correct |
28 |
Correct |
15 ms |
7040 KB |
Output is correct |
29 |
Correct |
15 ms |
6784 KB |
Output is correct |
30 |
Correct |
11 ms |
6784 KB |
Output is correct |
31 |
Correct |
11 ms |
6780 KB |
Output is correct |
32 |
Correct |
17 ms |
7040 KB |
Output is correct |
33 |
Correct |
15 ms |
7040 KB |
Output is correct |
34 |
Correct |
14 ms |
6784 KB |
Output is correct |
35 |
Correct |
15 ms |
7040 KB |
Output is correct |
36 |
Correct |
14 ms |
7040 KB |
Output is correct |
37 |
Correct |
15 ms |
6784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
9712 KB |
Output is correct |
2 |
Correct |
91 ms |
9712 KB |
Output is correct |
3 |
Correct |
145 ms |
12400 KB |
Output is correct |
4 |
Correct |
137 ms |
11372 KB |
Output is correct |
5 |
Correct |
193 ms |
15800 KB |
Output is correct |
6 |
Correct |
145 ms |
10104 KB |
Output is correct |
7 |
Correct |
126 ms |
9712 KB |
Output is correct |
8 |
Correct |
74 ms |
9840 KB |
Output is correct |
9 |
Correct |
69 ms |
9712 KB |
Output is correct |
10 |
Correct |
74 ms |
9792 KB |
Output is correct |
11 |
Correct |
174 ms |
15984 KB |
Output is correct |
12 |
Correct |
172 ms |
15984 KB |
Output is correct |
13 |
Correct |
198 ms |
14320 KB |
Output is correct |
14 |
Correct |
193 ms |
14192 KB |
Output is correct |
15 |
Correct |
141 ms |
10096 KB |
Output is correct |
16 |
Correct |
144 ms |
9840 KB |
Output is correct |
17 |
Correct |
134 ms |
9712 KB |
Output is correct |
18 |
Correct |
162 ms |
15984 KB |
Output is correct |
19 |
Correct |
8 ms |
6656 KB |
Output is correct |
20 |
Correct |
8 ms |
6528 KB |
Output is correct |
21 |
Correct |
8 ms |
6656 KB |
Output is correct |
22 |
Correct |
8 ms |
6528 KB |
Output is correct |
23 |
Correct |
8 ms |
6528 KB |
Output is correct |
24 |
Correct |
8 ms |
6656 KB |
Output is correct |
25 |
Correct |
10 ms |
6656 KB |
Output is correct |
26 |
Correct |
8 ms |
6656 KB |
Output is correct |
27 |
Correct |
8 ms |
6528 KB |
Output is correct |
28 |
Correct |
8 ms |
6656 KB |
Output is correct |
29 |
Correct |
8 ms |
6528 KB |
Output is correct |
30 |
Correct |
8 ms |
6528 KB |
Output is correct |
31 |
Correct |
8 ms |
6528 KB |
Output is correct |
32 |
Correct |
8 ms |
6528 KB |
Output is correct |
33 |
Correct |
8 ms |
6528 KB |
Output is correct |
34 |
Correct |
9 ms |
6528 KB |
Output is correct |
35 |
Correct |
8 ms |
6656 KB |
Output is correct |
36 |
Correct |
9 ms |
6528 KB |
Output is correct |
37 |
Correct |
8 ms |
6528 KB |
Output is correct |
38 |
Correct |
13 ms |
6912 KB |
Output is correct |
39 |
Correct |
13 ms |
6912 KB |
Output is correct |
40 |
Correct |
13 ms |
6784 KB |
Output is correct |
41 |
Correct |
13 ms |
6784 KB |
Output is correct |
42 |
Correct |
15 ms |
7040 KB |
Output is correct |
43 |
Correct |
15 ms |
6784 KB |
Output is correct |
44 |
Correct |
14 ms |
6784 KB |
Output is correct |
45 |
Correct |
14 ms |
6784 KB |
Output is correct |
46 |
Correct |
15 ms |
7040 KB |
Output is correct |
47 |
Correct |
15 ms |
6784 KB |
Output is correct |
48 |
Correct |
11 ms |
6784 KB |
Output is correct |
49 |
Correct |
11 ms |
6780 KB |
Output is correct |
50 |
Correct |
17 ms |
7040 KB |
Output is correct |
51 |
Correct |
15 ms |
7040 KB |
Output is correct |
52 |
Correct |
14 ms |
6784 KB |
Output is correct |
53 |
Correct |
15 ms |
7040 KB |
Output is correct |
54 |
Correct |
14 ms |
7040 KB |
Output is correct |
55 |
Correct |
15 ms |
6784 KB |
Output is correct |
56 |
Correct |
144 ms |
11888 KB |
Output is correct |
57 |
Correct |
155 ms |
12716 KB |
Output is correct |
58 |
Correct |
131 ms |
9840 KB |
Output is correct |
59 |
Correct |
144 ms |
9840 KB |
Output is correct |
60 |
Correct |
134 ms |
9712 KB |
Output is correct |
61 |
Correct |
133 ms |
11120 KB |
Output is correct |
62 |
Correct |
145 ms |
12140 KB |
Output is correct |
63 |
Correct |
130 ms |
9848 KB |
Output is correct |
64 |
Correct |
125 ms |
9712 KB |
Output is correct |
65 |
Correct |
128 ms |
9712 KB |
Output is correct |
66 |
Correct |
124 ms |
9708 KB |
Output is correct |
67 |
Correct |
184 ms |
10160 KB |
Output is correct |
68 |
Correct |
162 ms |
9712 KB |
Output is correct |
69 |
Correct |
147 ms |
9712 KB |
Output is correct |
70 |
Correct |
184 ms |
11376 KB |
Output is correct |
71 |
Correct |
171 ms |
9712 KB |
Output is correct |
72 |
Correct |
153 ms |
9712 KB |
Output is correct |
73 |
Correct |
181 ms |
9968 KB |
Output is correct |
74 |
Correct |
73 ms |
9712 KB |
Output is correct |
75 |
Correct |
73 ms |
9712 KB |
Output is correct |
76 |
Correct |
186 ms |
15984 KB |
Output is correct |
77 |
Correct |
223 ms |
17904 KB |
Output is correct |
78 |
Correct |
197 ms |
14188 KB |
Output is correct |
79 |
Correct |
176 ms |
9840 KB |
Output is correct |
80 |
Correct |
167 ms |
9824 KB |
Output is correct |
81 |
Correct |
80 ms |
9716 KB |
Output is correct |
82 |
Correct |
161 ms |
10224 KB |
Output is correct |
83 |
Correct |
165 ms |
10096 KB |
Output is correct |
84 |
Correct |
175 ms |
9840 KB |
Output is correct |
85 |
Correct |
119 ms |
9712 KB |
Output is correct |
86 |
Correct |
143 ms |
10604 KB |
Output is correct |
87 |
Correct |
134 ms |
9712 KB |
Output is correct |
88 |
Correct |
147 ms |
9712 KB |
Output is correct |
89 |
Correct |
141 ms |
9712 KB |
Output is correct |
90 |
Correct |
230 ms |
16116 KB |
Output is correct |
91 |
Correct |
163 ms |
12912 KB |
Output is correct |
92 |
Correct |
136 ms |
9748 KB |
Output is correct |
93 |
Correct |
175 ms |
9968 KB |
Output is correct |
94 |
Correct |
197 ms |
10992 KB |
Output is correct |
95 |
Correct |
165 ms |
9840 KB |
Output is correct |
96 |
Correct |
230 ms |
15860 KB |
Output is correct |
97 |
Correct |
220 ms |
15984 KB |
Output is correct |
98 |
Correct |
216 ms |
15984 KB |
Output is correct |
99 |
Correct |
220 ms |
15984 KB |
Output is correct |
100 |
Correct |
194 ms |
16752 KB |
Output is correct |
101 |
Correct |
232 ms |
17008 KB |
Output is correct |
102 |
Correct |
223 ms |
15984 KB |
Output is correct |
103 |
Correct |
135 ms |
10736 KB |
Output is correct |