#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;}
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#define int ll
const int N = 2e6 + 5;
const ll oo = 1e18;
int a[N];
int b[N];
int c[N];
int d[N];
ll t1[N];
ll t2[N];
void build(int l,int r,int node,ll t[]) {
t[node] = oo;
if (l == r)
return ;
int m = (l + r) / 2;
build(l,m,node * 2,t);
build(m+1,r,node * 2 + 1,t);
}
void update(int l,int r,int pos,ll v,int node,ll t[]) {
if (l == r)
return void(t[node] = min(t[node],v));
int m = (l + r) / 2;
if (pos <= m)
update(l,m,pos,v,node * 2,t);
else
update(m+1,r,pos,v,node * 2 + 1,t);
t[node] = min(t[node * 2],t[node * 2 + 1]);
}
ll query(int l,int r,int l1,int r1,int node,ll t[]) {
if (l1 <= l && r <= r1)
return t[node];
ll ans = oo;
int m = (l + r) / 2;
if (l1 <= m)
smin(ans,query(l,m,l1,r1,node * 2,t));
if (m+1<=r1)
smin(ans,query(m+1,r,l1,r1,node * 2 + 1,t));
return ans;
}
int32_t main(void) {
int n,m;
cin>>n>>m;
vi w;
w.pb(1);
w.pb(m);
for (int i = 1;i <= n;++i) {
cin>>a[i]>>b[i]>>c[i]>>d[i];
w.pb(a[i]);
w.pb(b[i]);
w.pb(c[i]);
}
sort(all(w));
w.resize(unique(all(w)) - w.begin());
m = w.size();
build(1,m,1,t1);
build(1,m,1,t2);
update(1,m,1,0,1,t1);
update(1,m,m,0,1,t2);
ll ans = oo;
for (int i = 1;i <= n;++i) {
a[i] = lower_bound(all(w),a[i]) - w.begin() + 1;
b[i] = lower_bound(all(w),b[i]) - w.begin() + 1;
c[i] = lower_bound(all(w),c[i]) - w.begin() + 1;
}
for (int i = 1;i <= n;++i) {
ll A = query(1,m,a[i],c[i],1,t1);
ll B = query(1,m,c[i],b[i],1,t2);
smin(ans,d[i] + A + B);
update(1,m,c[i],A + d[i],1,t1);
update(1,m,c[i],B + d[i],1,t2);
}
if (ans == oo)
ans = -1;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |