#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 3e5+5;
const ll inf = 1e18;
vector<int> pos;
ll st[2][4*maxn];
int pp(int x){
return lower_bound(pos.begin(), pos.end(), x)-pos.begin();
}
int A[maxn], B[maxn], C[maxn], D[maxn];
int n, m, sz;
void upd(int ty, int l, int r, int k, ll val, int id){
if(l==r)st[ty][id] = min(st[ty][id], val);
else{
int mid = (l+r)>>1;
if(k <= mid)upd(ty, l, mid, k, val, id<<1);
else upd(ty, mid+1, r, k, val, id<<1|1);
st[ty][id] = min(st[ty][id<<1], st[ty][id<<1|1]);
}
}
ll query(int ty, int l, int r, int ql, int qr, int id){
if(l > qr || ql > r)return inf;
if(ql <= l && qr >= r)return st[ty][id];
else{
int mid = (l+r)>>1;
return min(query(ty, l, mid, ql, qr, id<<1), query(ty, mid+1, r, ql, qr, id<<1|1));
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
ll ans = inf;
pos = {-1, 1, m};
for(int i = 0; i < n; i++){
cin >> A[i] >> B[i] >> C[i] >> D[i];
pos.push_back(A[i]);
pos.push_back(B[i]);
pos.push_back(C[i]);
}
sort(pos.begin(), pos.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
sz = pos.size()-1;
for(int i = 0; i < 4*maxn; i++)st[0][i]=st[1][i]=inf;
upd(0, 1, sz, 1, 0, 1);
upd(1, 1, sz, sz, 0, 1);
for(int i = 0; i < n; i++){
A[i] = pp(A[i]);
B[i] = pp(B[i]);
C[i] = pp(C[i]);
ans = min(ans, D[i] + query(0, 1, sz, A[i], B[i], 1) + query(1, 1, sz, A[i], B[i], 1));
upd(0, 1, sz, C[i], query(0, 1, sz, A[i], C[i], 1)+D[i], 1);
upd(1, 1, sz, C[i], query(1, 1, sz, C[i], B[i], 1)+D[i], 1);
}
if(ans >= inf)cout << "-1";
else cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19100 KB |
Output is correct |
5 |
Correct |
8 ms |
19084 KB |
Output is correct |
6 |
Correct |
8 ms |
19028 KB |
Output is correct |
7 |
Correct |
8 ms |
19028 KB |
Output is correct |
8 |
Correct |
9 ms |
19056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19100 KB |
Output is correct |
5 |
Correct |
8 ms |
19084 KB |
Output is correct |
6 |
Correct |
8 ms |
19028 KB |
Output is correct |
7 |
Correct |
8 ms |
19028 KB |
Output is correct |
8 |
Correct |
9 ms |
19056 KB |
Output is correct |
9 |
Correct |
8 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
10 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19084 KB |
Output is correct |
14 |
Correct |
9 ms |
19028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19100 KB |
Output is correct |
5 |
Correct |
8 ms |
19084 KB |
Output is correct |
6 |
Correct |
8 ms |
19028 KB |
Output is correct |
7 |
Correct |
8 ms |
19028 KB |
Output is correct |
8 |
Correct |
9 ms |
19056 KB |
Output is correct |
9 |
Correct |
8 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
10 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19084 KB |
Output is correct |
14 |
Correct |
9 ms |
19028 KB |
Output is correct |
15 |
Correct |
8 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19084 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
9 ms |
19164 KB |
Output is correct |
19 |
Correct |
11 ms |
19156 KB |
Output is correct |
20 |
Correct |
11 ms |
19156 KB |
Output is correct |
21 |
Correct |
9 ms |
19028 KB |
Output is correct |
22 |
Correct |
10 ms |
19156 KB |
Output is correct |
23 |
Correct |
10 ms |
19088 KB |
Output is correct |
24 |
Correct |
12 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
8 ms |
19028 KB |
Output is correct |
3 |
Correct |
8 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19100 KB |
Output is correct |
5 |
Correct |
8 ms |
19084 KB |
Output is correct |
6 |
Correct |
8 ms |
19028 KB |
Output is correct |
7 |
Correct |
8 ms |
19028 KB |
Output is correct |
8 |
Correct |
9 ms |
19056 KB |
Output is correct |
9 |
Correct |
8 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19028 KB |
Output is correct |
11 |
Correct |
10 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19084 KB |
Output is correct |
14 |
Correct |
9 ms |
19028 KB |
Output is correct |
15 |
Correct |
8 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19084 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
9 ms |
19164 KB |
Output is correct |
19 |
Correct |
11 ms |
19156 KB |
Output is correct |
20 |
Correct |
11 ms |
19156 KB |
Output is correct |
21 |
Correct |
9 ms |
19028 KB |
Output is correct |
22 |
Correct |
10 ms |
19156 KB |
Output is correct |
23 |
Correct |
10 ms |
19088 KB |
Output is correct |
24 |
Correct |
12 ms |
19156 KB |
Output is correct |
25 |
Correct |
37 ms |
19476 KB |
Output is correct |
26 |
Correct |
73 ms |
19920 KB |
Output is correct |
27 |
Correct |
200 ms |
21104 KB |
Output is correct |
28 |
Correct |
182 ms |
21880 KB |
Output is correct |
29 |
Correct |
134 ms |
20592 KB |
Output is correct |
30 |
Correct |
219 ms |
21884 KB |
Output is correct |
31 |
Correct |
324 ms |
21928 KB |
Output is correct |
32 |
Correct |
267 ms |
21932 KB |
Output is correct |
33 |
Correct |
42 ms |
19792 KB |
Output is correct |
34 |
Correct |
145 ms |
20584 KB |
Output is correct |
35 |
Correct |
181 ms |
21952 KB |
Output is correct |
36 |
Correct |
355 ms |
21888 KB |
Output is correct |
37 |
Correct |
260 ms |
21928 KB |
Output is correct |
38 |
Correct |
258 ms |
21888 KB |
Output is correct |