#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]);
ll lt = query(0, 1, sz, A[i], B[i], 1), rt = query(1, 1, sz, A[i], B[i], 1);
ans = min(ans, D[i] + lt + rt);
upd(0, 1, sz, C[i], lt+D[i], 1);
upd(1, 1, sz, C[i], rt+D[i], 1);
}
if(ans >= inf)cout << "-1";
else cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19104 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
10 ms |
19024 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
8 ms |
19092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19104 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
10 ms |
19024 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
8 ms |
19092 KB |
Output is correct |
9 |
Correct |
9 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19108 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19072 KB |
Output is correct |
14 |
Correct |
9 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19104 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
10 ms |
19024 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
8 ms |
19092 KB |
Output is correct |
9 |
Correct |
9 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19108 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19072 KB |
Output is correct |
14 |
Correct |
9 ms |
19052 KB |
Output is correct |
15 |
Correct |
9 ms |
19116 KB |
Output is correct |
16 |
Correct |
9 ms |
19156 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
13 ms |
19132 KB |
Output is correct |
19 |
Correct |
10 ms |
19076 KB |
Output is correct |
20 |
Correct |
12 ms |
19156 KB |
Output is correct |
21 |
Correct |
12 ms |
19028 KB |
Output is correct |
22 |
Correct |
10 ms |
19156 KB |
Output is correct |
23 |
Correct |
10 ms |
19156 KB |
Output is correct |
24 |
Correct |
10 ms |
19156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19104 KB |
Output is correct |
3 |
Correct |
9 ms |
19028 KB |
Output is correct |
4 |
Correct |
9 ms |
19028 KB |
Output is correct |
5 |
Correct |
10 ms |
19032 KB |
Output is correct |
6 |
Correct |
10 ms |
19024 KB |
Output is correct |
7 |
Correct |
11 ms |
19028 KB |
Output is correct |
8 |
Correct |
8 ms |
19092 KB |
Output is correct |
9 |
Correct |
9 ms |
19028 KB |
Output is correct |
10 |
Correct |
9 ms |
19108 KB |
Output is correct |
11 |
Correct |
9 ms |
19028 KB |
Output is correct |
12 |
Correct |
9 ms |
19028 KB |
Output is correct |
13 |
Correct |
9 ms |
19072 KB |
Output is correct |
14 |
Correct |
9 ms |
19052 KB |
Output is correct |
15 |
Correct |
9 ms |
19116 KB |
Output is correct |
16 |
Correct |
9 ms |
19156 KB |
Output is correct |
17 |
Correct |
10 ms |
19156 KB |
Output is correct |
18 |
Correct |
13 ms |
19132 KB |
Output is correct |
19 |
Correct |
10 ms |
19076 KB |
Output is correct |
20 |
Correct |
12 ms |
19156 KB |
Output is correct |
21 |
Correct |
12 ms |
19028 KB |
Output is correct |
22 |
Correct |
10 ms |
19156 KB |
Output is correct |
23 |
Correct |
10 ms |
19156 KB |
Output is correct |
24 |
Correct |
10 ms |
19156 KB |
Output is correct |
25 |
Correct |
25 ms |
19476 KB |
Output is correct |
26 |
Correct |
55 ms |
19920 KB |
Output is correct |
27 |
Correct |
186 ms |
21096 KB |
Output is correct |
28 |
Correct |
169 ms |
21892 KB |
Output is correct |
29 |
Correct |
117 ms |
20548 KB |
Output is correct |
30 |
Correct |
173 ms |
21888 KB |
Output is correct |
31 |
Correct |
245 ms |
21988 KB |
Output is correct |
32 |
Correct |
224 ms |
21892 KB |
Output is correct |
33 |
Correct |
37 ms |
19792 KB |
Output is correct |
34 |
Correct |
107 ms |
20544 KB |
Output is correct |
35 |
Correct |
157 ms |
21872 KB |
Output is correct |
36 |
Correct |
259 ms |
21888 KB |
Output is correct |
37 |
Correct |
211 ms |
21896 KB |
Output is correct |
38 |
Correct |
218 ms |
21876 KB |
Output is correct |