#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 10;
const ll inf = 1e18;
int a[maxn], b[maxn], c[maxn], d[maxn];
vector<int> comp;
ll dp[maxn], pd[maxn];
ll seg[2][4 * maxn];
ll get(int w, int id, int L, int R, int l, int r){
if (r <= L or R <= l)
return inf;
if (l <= L and R <= r)
return seg[w][id];
int mid = (L + R) >> 1;
return min(get(w, 2*id+0, L, mid, l, r), get(w, 2*id+1, mid, R, l, r));
}
void add(int w, int id, int L, int R, int idx, ll val){
if (idx < L or R <= idx)
return;
if (L + 1 == R){
seg[w][id] = val;
return;
}
int mid = (L + R) >> 1;
add(w, 2 * id + 0, L, mid, idx, val);
add(w, 2 * id + 1, mid, R, idx, val);
seg[w][id] = min(seg[w][2 * id + 0], seg[w][2 * id + 1]);
}
int cmp(int x){
return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
}
void add(int x){
comp.push_back(x);
}
int main(){
ios_base::sync_with_stdio (false);
cin.tie(0), cout.tie(0);
int m, n;
cin >> m >> n;
for (int i = 1; i <= m; i++){
cin >> a[i] >> c[i] >> b[i] >> d[i];
add(a[i]), add(b[i]), add(c[i]);
}
comp.push_back(1);
comp.push_back(n);
sort(comp.begin(), comp.end());
comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
for (int i = 1; i <= m; i++)
a[i] = cmp(a[i]), b[i] = cmp(b[i]), c[i] = cmp(c[i]);
n = comp.size();
for (int i = 0; i < n; i++){
dp[i] = pd[i] = inf;
add(0, 1, 0, n, i, inf);
add(1, 1, 0, n, i, inf);
}
dp[0] = pd[n - 1] = 0;
add(0, 1, 0, n, 0, 0);
add(1, 1, 0, n, n - 1, 0);
ll answer = inf;
for (int i = 1; i <= m; i++){
ll s = get(0, 1, 0, n, a[i], c[i] + 1);
ll t = get(1, 1, 0, n, a[i], c[i] + 1);
answer = min(answer, s + t + d[i]);
if (s + d[i] < dp[b[i]]){
dp[b[i]] = s + d[i];
add(0, 1, 0, n, b[i], dp[b[i]]);
}
if (t + d[i] < pd[b[i]]){
pd[b[i]] = t + d[i];
add(1, 1, 0, n, b[i], pd[b[i]]);
}
}
if (answer == inf)
answer = -1;
cout << answer << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
25 |
Correct |
30 ms |
2548 KB |
Output is correct |
26 |
Correct |
87 ms |
4996 KB |
Output is correct |
27 |
Correct |
229 ms |
10600 KB |
Output is correct |
28 |
Correct |
148 ms |
7532 KB |
Output is correct |
29 |
Correct |
171 ms |
9352 KB |
Output is correct |
30 |
Correct |
196 ms |
8508 KB |
Output is correct |
31 |
Correct |
386 ms |
18028 KB |
Output is correct |
32 |
Correct |
327 ms |
13116 KB |
Output is correct |
33 |
Correct |
60 ms |
4728 KB |
Output is correct |
34 |
Correct |
187 ms |
14416 KB |
Output is correct |
35 |
Correct |
344 ms |
28240 KB |
Output is correct |
36 |
Correct |
463 ms |
28176 KB |
Output is correct |
37 |
Correct |
396 ms |
28300 KB |
Output is correct |
38 |
Correct |
382 ms |
28240 KB |
Output is correct |