#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%lld\n", x);
#define PRINT(x) cerr << #x << '=' << x << endl;
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 100010
lld a[MAXN], b[MAXN], c[MAXN], d[MAXN], segL[12*MAXN], segR[12*MAXN];
lld dpl[MAXN], dpr[MAXN];
set<int> hshs;
map<int, int> hsh;
void update(lld seg[], int node, int l, int r, int idx, lld val) {
if(l == r) {
seg[node] = min(seg[node], val);
return;
}
if(idx <= mid) update(seg, 2*node, l, mid, idx, val);
else update(seg, 2*node+1, mid+1, r, idx, val);
seg[node] = min(seg[2*node], seg[2*node+1]);
}
lld query(lld seg[], int node, int l, int r, int L, int R) {
if(L <= l && r <= R) return seg[node];
lld rez = LINF;
if(L <= mid) rez = min(rez, query(seg, 2*node, l, mid, L, R));
if(R > mid) rez = min(rez, query(seg, 2*node+1, mid+1, r, L, R));
return rez;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int m, n; cin >> m >> n;
hshs.insert(1); hshs.insert(n);
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
hshs.insert(a[i]); hshs.insert(b[i]); hshs.insert(c[i]);
}
int N = 0;
for(auto x : hshs) hsh[x] = ++N;
fill(segL, segL+4*N, LINF);
fill(segR, segR+4*N, LINF);
update(segL, 1, 1, N, 1, 0);
update(segR, 1, 1, N, N, 0);
lld rez = LINF;
for(int i = 1; i <= m; i++) {
a[i] = hsh[a[i]]; b[i] = hsh[b[i]]; c[i] = hsh[c[i]];
dpl[i] = query(segL, 1, 1, N, a[i], b[i]) + d[i];
dpr[i] = query(segR, 1, 1, N, a[i], b[i]) + d[i];
rez = min(rez, dpl[i]+dpr[i]-d[i]);
update(segL, 1, 1, N, c[i], dpl[i]);
update(segR, 1, 1, N, c[i], dpr[i]);
}
cout << (rez == LINF ? -1:rez);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
768 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
896 KB |
Output is correct |
23 |
Correct |
4 ms |
896 KB |
Output is correct |
24 |
Correct |
4 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
768 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
896 KB |
Output is correct |
23 |
Correct |
4 ms |
896 KB |
Output is correct |
24 |
Correct |
4 ms |
896 KB |
Output is correct |
25 |
Correct |
34 ms |
3556 KB |
Output is correct |
26 |
Correct |
141 ms |
10360 KB |
Output is correct |
27 |
Correct |
444 ms |
19488 KB |
Output is correct |
28 |
Correct |
149 ms |
5368 KB |
Output is correct |
29 |
Correct |
303 ms |
17040 KB |
Output is correct |
30 |
Correct |
307 ms |
7820 KB |
Output is correct |
31 |
Correct |
771 ms |
31300 KB |
Output is correct |
32 |
Correct |
647 ms |
27896 KB |
Output is correct |
33 |
Correct |
91 ms |
11256 KB |
Output is correct |
34 |
Correct |
359 ms |
28280 KB |
Output is correct |
35 |
Correct |
443 ms |
55804 KB |
Output is correct |
36 |
Correct |
1000 ms |
55928 KB |
Output is correct |
37 |
Correct |
761 ms |
55948 KB |
Output is correct |
38 |
Correct |
725 ms |
55928 KB |
Output is correct |