#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int Mxn = 3e5 + 3;
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 |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9728 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9728 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9728 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9692 KB |
Output is correct |
17 |
Correct |
6 ms |
9832 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9812 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9728 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9672 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9692 KB |
Output is correct |
17 |
Correct |
6 ms |
9832 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
5 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9812 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
20 ms |
10892 KB |
Output is correct |
26 |
Correct |
58 ms |
13260 KB |
Output is correct |
27 |
Correct |
155 ms |
17192 KB |
Output is correct |
28 |
Correct |
112 ms |
17488 KB |
Output is correct |
29 |
Correct |
125 ms |
17868 KB |
Output is correct |
30 |
Correct |
150 ms |
18228 KB |
Output is correct |
31 |
Correct |
352 ms |
25360 KB |
Output is correct |
32 |
Correct |
239 ms |
23176 KB |
Output is correct |
33 |
Correct |
40 ms |
13944 KB |
Output is correct |
34 |
Correct |
122 ms |
20592 KB |
Output is correct |
35 |
Correct |
160 ms |
31628 KB |
Output is correct |
36 |
Correct |
385 ms |
31540 KB |
Output is correct |
37 |
Correct |
311 ms |
31592 KB |
Output is correct |
38 |
Correct |
256 ms |
31532 KB |
Output is correct |