#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int Mxn = 1e5 + 1;
const ll INF = 1e17;
struct SegTree {
vector<ll> segs;
SegTree() {segs.assign(Mxn<<1, INF);}
void upd(int i, ll x) {
i += Mxn; segs[i] = min(segs[i], x);
for (i >>= 1; i; i >>= 1) {
segs[i] = min(segs[i<<1], segs[i<<1|1]);
}
}
ll query(int l, int r, ll ans = INF) {
for (l += Mxn, r += Mxn + 1; l < r; l >>= 1, r >>= 1) {
if (l&1) ans = min(ans, segs[l++]);
if (r&1) ans = min(ans, segs[--r]);
}
return ans;
}
} St[2];
struct coso {int a, b, c; ll d;};
ll dp[2][Mxn];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int m, n, cnt = 0; cin >> m >> n;
vector<coso> A(m);
map<int, int> cc; cc[1], cc[n];
for (auto &[a, b, c, d] : A) {
cin >> a >> b >> c >> d;
cc[a], cc[b], cc[c];
}
for (auto &[x, y] : cc) y = cnt++;
for (auto &[a, b, c, d] : A) {
a = cc[a], b = cc[b], c = cc[c];
}
n = cc[n];
St[0].upd(0, 0); St[1].upd(n, 0);
for (int i = 0; i < m; i++) {
auto [a, b, c, d] = A[i];
for (int j = 0; j < 2; j++) {
dp[j][i] = St[j].query(a, b);
if (dp[j][i] == INF) continue;
dp[j][i] += d;
St[j].upd(c, dp[j][i]);
}
}
ll ans = INF; bool flag = false;
for (int i = 0; i < m; i++) {
if (dp[0][i] != INF && dp[1][i] != INF) flag = true;
ans = min(dp[0][i] + dp[1][i] - A[i].d, ans);
}
if (flag) cout << ans << '\n';
else cout << -1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3480 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3476 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3476 KB |
Output is correct |
8 |
Correct |
2 ms |
3472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3480 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3476 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3476 KB |
Output is correct |
8 |
Correct |
2 ms |
3472 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3484 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
3 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3480 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3476 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3476 KB |
Output is correct |
8 |
Correct |
2 ms |
3472 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3484 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
3 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3476 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3488 KB |
Output is correct |
19 |
Correct |
4 ms |
3616 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3492 KB |
Output is correct |
22 |
Correct |
5 ms |
3628 KB |
Output is correct |
23 |
Correct |
4 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3480 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3476 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3476 KB |
Output is correct |
8 |
Correct |
2 ms |
3472 KB |
Output is correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3484 KB |
Output is correct |
12 |
Correct |
3 ms |
3412 KB |
Output is correct |
13 |
Correct |
3 ms |
3412 KB |
Output is correct |
14 |
Correct |
2 ms |
3412 KB |
Output is correct |
15 |
Correct |
2 ms |
3476 KB |
Output is correct |
16 |
Correct |
3 ms |
3412 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3488 KB |
Output is correct |
19 |
Correct |
4 ms |
3616 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3492 KB |
Output is correct |
22 |
Correct |
5 ms |
3628 KB |
Output is correct |
23 |
Correct |
4 ms |
3668 KB |
Output is correct |
24 |
Correct |
4 ms |
3668 KB |
Output is correct |
25 |
Correct |
19 ms |
4960 KB |
Output is correct |
26 |
Correct |
54 ms |
7908 KB |
Output is correct |
27 |
Runtime error |
139 ms |
22328 KB |
Execution killed with signal 11 |
28 |
Halted |
0 ms |
0 KB |
- |