using namespace std;
#include <iostream>
#define MAXN 100005
using namespace std;
struct node {
int l, r;
long long v;
node *c[2];
node(int l, int r) : l{l}, r{r} {
v = 1e17;
c[0] = nullptr;
c[1] = nullptr;
}
void upd(int x,long long v) {
this -> v = min(this -> v,v);
if (l != r) {
int m = l + (r - l) / 2;
if (x <= m) {
if (!c[0]) c[0] = new node(l, m);
c[0] -> upd(x, v);
} else {
if (!c[1]) c[1] = new node(m + 1, r);
c[1] -> upd(x, v);
}
}
}
long long qry(int x, int y) {
if (x <= l && r <= y) return v;
if (y < l || x > r) return 1e17;
long long res = 1e17;
if (c[0]) res = min(res,c[0] -> qry(x, y));
if (c[1]) res = min(res,c[1] -> qry(x, y));
return res;
}
};
int qa[MAXN],qb[MAXN],qc[MAXN],M,N;
long long arr[MAXN];
long long solve() {
node *st1 = new node(1,1000000000);
node *st2 = new node(1,1000000000);
long long ans = 1e17;
st1 -> upd(1,0);
st2 -> upd(N,0);
for(int i = 0;i < M;++i) {
long long nv1 = st1 -> qry(qa[i],qb[i]);
long long nv2 = st2 -> qry(qa[i],qb[i]);
st1 -> upd(qc[i],nv1 + arr[i]);
st2 -> upd(qc[i],nv2 + arr[i]);
ans = min(ans,nv1 + nv2 + arr[i]);
}
return ans;
}
int main() {
cin >> M >> N;
for(int i = 0;i < M;++i) {
cin >> qa[i] >> qb[i] >> qc[i] >> arr[i];
}
long long ans = solve();
if(ans == 1e17) {
cout << -1 << endl;
}else{
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
768 KB |
Output is correct |
13 |
Correct |
2 ms |
768 KB |
Output is correct |
14 |
Correct |
2 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
768 KB |
Output is correct |
13 |
Correct |
2 ms |
768 KB |
Output is correct |
14 |
Correct |
2 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
1664 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
7 ms |
2176 KB |
Output is correct |
20 |
Correct |
7 ms |
1408 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
7 ms |
2176 KB |
Output is correct |
23 |
Correct |
7 ms |
2432 KB |
Output is correct |
24 |
Correct |
7 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
768 KB |
Output is correct |
13 |
Correct |
2 ms |
768 KB |
Output is correct |
14 |
Correct |
2 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
7 ms |
1664 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
7 ms |
2176 KB |
Output is correct |
20 |
Correct |
7 ms |
1408 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
7 ms |
2176 KB |
Output is correct |
23 |
Correct |
7 ms |
2432 KB |
Output is correct |
24 |
Correct |
7 ms |
2304 KB |
Output is correct |
25 |
Correct |
50 ms |
6264 KB |
Output is correct |
26 |
Correct |
165 ms |
24824 KB |
Output is correct |
27 |
Correct |
516 ms |
54776 KB |
Output is correct |
28 |
Correct |
543 ms |
7544 KB |
Output is correct |
29 |
Correct |
398 ms |
54636 KB |
Output is correct |
30 |
Correct |
720 ms |
21496 KB |
Output is correct |
31 |
Correct |
850 ms |
103932 KB |
Output is correct |
32 |
Correct |
835 ms |
85112 KB |
Output is correct |
33 |
Correct |
105 ms |
20088 KB |
Output is correct |
34 |
Correct |
374 ms |
68860 KB |
Output is correct |
35 |
Correct |
590 ms |
141944 KB |
Output is correct |
36 |
Correct |
842 ms |
137720 KB |
Output is correct |
37 |
Correct |
748 ms |
137720 KB |
Output is correct |
38 |
Correct |
712 ms |
137848 KB |
Output is correct |