#include<bits/stdc++.h>
using namespace std;
struct Lisan: vector<int> {
void done() { sort(begin(), end()); erase(unique(begin(), end()), end()); }
int operator () (const int& v) const { return std::lower_bound(begin(), end(), v) - begin(); }
int upper_bound (const int& v) const { return std::upper_bound(begin(), end(), v) - begin(); }
};
struct Treatment {
int T, L, R, C, x, y;
Treatment (const int& _T, const int& _L, const int& _R, const int& _C):
T(_T), L(_L), R(_R), C(_C), x(L + T), y(L - T) {}
};
struct SegmentTree {
int n; vector<vector<pair<int, int>>> val;
SegmentTree (const int& _n): n(_n), val(4 << __lg(n)) {}
void insert(int i, int l, int r, int p, pair<int, int> v) {
if (l + 1 == r) {
val[i].push_back(v);
return;
}
int m = (l + r) / 2;
if (p < m) insert(i * 2, l, m, p, v);
else insert(i * 2 + 1, m, r, p, v);
}
void insert(int x, int y, int i) { insert(1, 0, n, x, pair<int, int>(y, i)); }
void build(int i, int l, int r) {
if (l + 1 == r) {
sort(begin(val[i]), end(val[i]), greater<pair<int, int>>());
return;
}
int m = (l + r) / 2;
build(i * 2, l, m);
build(i*2+1, m, r);
val[i].reserve(val[i * 2].size() + val[i*2+1].size());
merge(begin(val[i * 2]), end(val[i * 2]), begin(val[i*2+1]), end(val[i*2+1]), back_inserter(val[i]), greater<pair<int, int>>());
}
void build() { build(1, 0, n); }
void collect(int i, int l, int r, int x, int y, vector<int>& ids) {
if (x <= l) return;
if (r <= x) {
while (!val[i].empty()) {
auto [yi, id] = val[i].back();
if (yi <= y) {
ids.emplace_back(id);
val[i].pop_back();
} else break;
}
return;
}
int m = (l + r) / 2;
collect(i * 2, l, m, x, y, ids);
if (m < x) collect(i * 2 + 1, m, r, x, y, ids);
}
void collect(int x, int y, vector<int>& ids) { collect(1, 0, n, x, y, ids); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N, M;
cin >> N >> M;
vector<Treatment> ts;
Lisan lisan;
ts.reserve(M);
lisan.reserve(M);
for (int T, L, R, C, i = 0; i < M; ++i) {
cin >> T >> L >> R >> C;
ts.emplace_back(T, L, R, C);
lisan.emplace_back(ts.back().x);
}
lisan.done();
SegmentTree sgt(lisan.size());
for (int i = 0; i < M; ++i) {
const auto& [T, L, R, C, x, y] = ts[i];
sgt.insert(lisan(x), y, i);
}
sgt.build();
vector<long long> dis(M, LLONG_MAX);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
for (int i = 0; i < M; ++i) {
if (ts[i].L == 1) {
dis[i] = ts[i].C;
pq.emplace(dis[i], i);
}
}
vector<int> vs;
vs.reserve(M);
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
int x = ts[u].R + ts[u].T + 1;
int y = ts[u].R - ts[u].T + 1;
sgt.collect(lisan.upper_bound(x), y, vs);
for (const int& v: vs) {
if (d + ts[v].C < dis[v]) {
dis[v] = d + ts[v].C;
pq.emplace(dis[v], v);
}
}
vs.clear();
}
long long ans = LLONG_MAX;
for (int i = 0; i < M; ++i) {
if (ts[i].R == N) {
ans = min(ans, dis[i]);
}
}
if (ans == LLONG_MAX) ans = -1;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
27836 KB |
Output is correct |
2 |
Correct |
110 ms |
27536 KB |
Output is correct |
3 |
Correct |
101 ms |
23052 KB |
Output is correct |
4 |
Correct |
102 ms |
22688 KB |
Output is correct |
5 |
Correct |
74 ms |
12296 KB |
Output is correct |
6 |
Correct |
72 ms |
13272 KB |
Output is correct |
7 |
Correct |
78 ms |
16936 KB |
Output is correct |
8 |
Correct |
44 ms |
9896 KB |
Output is correct |
9 |
Correct |
49 ms |
12668 KB |
Output is correct |
10 |
Correct |
59 ms |
16624 KB |
Output is correct |
11 |
Correct |
152 ms |
29856 KB |
Output is correct |
12 |
Correct |
153 ms |
30032 KB |
Output is correct |
13 |
Correct |
164 ms |
29744 KB |
Output is correct |
14 |
Correct |
159 ms |
29792 KB |
Output is correct |
15 |
Correct |
140 ms |
27828 KB |
Output is correct |
16 |
Correct |
140 ms |
27880 KB |
Output is correct |
17 |
Correct |
130 ms |
27744 KB |
Output is correct |
18 |
Correct |
146 ms |
29880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
5 ms |
1620 KB |
Output is correct |
21 |
Correct |
5 ms |
1620 KB |
Output is correct |
22 |
Correct |
6 ms |
1492 KB |
Output is correct |
23 |
Correct |
7 ms |
1492 KB |
Output is correct |
24 |
Correct |
8 ms |
1728 KB |
Output is correct |
25 |
Correct |
7 ms |
1492 KB |
Output is correct |
26 |
Correct |
6 ms |
1576 KB |
Output is correct |
27 |
Correct |
5 ms |
1492 KB |
Output is correct |
28 |
Correct |
7 ms |
1620 KB |
Output is correct |
29 |
Correct |
6 ms |
1492 KB |
Output is correct |
30 |
Correct |
5 ms |
1492 KB |
Output is correct |
31 |
Correct |
4 ms |
1472 KB |
Output is correct |
32 |
Correct |
7 ms |
1748 KB |
Output is correct |
33 |
Correct |
7 ms |
1748 KB |
Output is correct |
34 |
Correct |
6 ms |
1620 KB |
Output is correct |
35 |
Correct |
7 ms |
1748 KB |
Output is correct |
36 |
Correct |
6 ms |
1748 KB |
Output is correct |
37 |
Correct |
6 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
27836 KB |
Output is correct |
2 |
Correct |
110 ms |
27536 KB |
Output is correct |
3 |
Correct |
101 ms |
23052 KB |
Output is correct |
4 |
Correct |
102 ms |
22688 KB |
Output is correct |
5 |
Correct |
74 ms |
12296 KB |
Output is correct |
6 |
Correct |
72 ms |
13272 KB |
Output is correct |
7 |
Correct |
78 ms |
16936 KB |
Output is correct |
8 |
Correct |
44 ms |
9896 KB |
Output is correct |
9 |
Correct |
49 ms |
12668 KB |
Output is correct |
10 |
Correct |
59 ms |
16624 KB |
Output is correct |
11 |
Correct |
152 ms |
29856 KB |
Output is correct |
12 |
Correct |
153 ms |
30032 KB |
Output is correct |
13 |
Correct |
164 ms |
29744 KB |
Output is correct |
14 |
Correct |
159 ms |
29792 KB |
Output is correct |
15 |
Correct |
140 ms |
27828 KB |
Output is correct |
16 |
Correct |
140 ms |
27880 KB |
Output is correct |
17 |
Correct |
130 ms |
27744 KB |
Output is correct |
18 |
Correct |
146 ms |
29880 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
5 ms |
1620 KB |
Output is correct |
39 |
Correct |
5 ms |
1620 KB |
Output is correct |
40 |
Correct |
6 ms |
1492 KB |
Output is correct |
41 |
Correct |
7 ms |
1492 KB |
Output is correct |
42 |
Correct |
8 ms |
1728 KB |
Output is correct |
43 |
Correct |
7 ms |
1492 KB |
Output is correct |
44 |
Correct |
6 ms |
1576 KB |
Output is correct |
45 |
Correct |
5 ms |
1492 KB |
Output is correct |
46 |
Correct |
7 ms |
1620 KB |
Output is correct |
47 |
Correct |
6 ms |
1492 KB |
Output is correct |
48 |
Correct |
5 ms |
1492 KB |
Output is correct |
49 |
Correct |
4 ms |
1472 KB |
Output is correct |
50 |
Correct |
7 ms |
1748 KB |
Output is correct |
51 |
Correct |
7 ms |
1748 KB |
Output is correct |
52 |
Correct |
6 ms |
1620 KB |
Output is correct |
53 |
Correct |
7 ms |
1748 KB |
Output is correct |
54 |
Correct |
6 ms |
1748 KB |
Output is correct |
55 |
Correct |
6 ms |
1620 KB |
Output is correct |
56 |
Correct |
136 ms |
28968 KB |
Output is correct |
57 |
Correct |
137 ms |
27364 KB |
Output is correct |
58 |
Correct |
128 ms |
27636 KB |
Output is correct |
59 |
Correct |
137 ms |
27672 KB |
Output is correct |
60 |
Correct |
148 ms |
27852 KB |
Output is correct |
61 |
Correct |
149 ms |
27568 KB |
Output is correct |
62 |
Correct |
150 ms |
28868 KB |
Output is correct |
63 |
Correct |
128 ms |
27964 KB |
Output is correct |
64 |
Correct |
123 ms |
27792 KB |
Output is correct |
65 |
Correct |
132 ms |
27740 KB |
Output is correct |
66 |
Correct |
135 ms |
27468 KB |
Output is correct |
67 |
Correct |
167 ms |
27760 KB |
Output is correct |
68 |
Correct |
148 ms |
27540 KB |
Output is correct |
69 |
Correct |
126 ms |
27596 KB |
Output is correct |
70 |
Correct |
171 ms |
27764 KB |
Output is correct |
71 |
Correct |
155 ms |
27608 KB |
Output is correct |
72 |
Correct |
122 ms |
27660 KB |
Output is correct |
73 |
Correct |
166 ms |
27740 KB |
Output is correct |
74 |
Correct |
93 ms |
27468 KB |
Output is correct |
75 |
Correct |
81 ms |
27368 KB |
Output is correct |
76 |
Correct |
161 ms |
29888 KB |
Output is correct |
77 |
Correct |
185 ms |
29760 KB |
Output is correct |
78 |
Correct |
178 ms |
29784 KB |
Output is correct |
79 |
Correct |
179 ms |
27596 KB |
Output is correct |
80 |
Correct |
144 ms |
27888 KB |
Output is correct |
81 |
Correct |
107 ms |
27468 KB |
Output is correct |
82 |
Correct |
152 ms |
28000 KB |
Output is correct |
83 |
Correct |
159 ms |
28012 KB |
Output is correct |
84 |
Correct |
174 ms |
27616 KB |
Output is correct |
85 |
Correct |
128 ms |
27536 KB |
Output is correct |
86 |
Correct |
145 ms |
27504 KB |
Output is correct |
87 |
Correct |
136 ms |
27724 KB |
Output is correct |
88 |
Correct |
140 ms |
26460 KB |
Output is correct |
89 |
Correct |
148 ms |
27528 KB |
Output is correct |
90 |
Correct |
189 ms |
29824 KB |
Output is correct |
91 |
Correct |
158 ms |
28776 KB |
Output is correct |
92 |
Correct |
150 ms |
27384 KB |
Output is correct |
93 |
Correct |
169 ms |
27496 KB |
Output is correct |
94 |
Correct |
136 ms |
22540 KB |
Output is correct |
95 |
Correct |
156 ms |
27536 KB |
Output is correct |
96 |
Correct |
189 ms |
29708 KB |
Output is correct |
97 |
Correct |
181 ms |
29740 KB |
Output is correct |
98 |
Correct |
181 ms |
29612 KB |
Output is correct |
99 |
Correct |
178 ms |
29728 KB |
Output is correct |
100 |
Correct |
141 ms |
24512 KB |
Output is correct |
101 |
Correct |
186 ms |
29796 KB |
Output is correct |
102 |
Correct |
170 ms |
30016 KB |
Output is correct |
103 |
Correct |
139 ms |
27952 KB |
Output is correct |