#include <bits/stdc++.h>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define bc back()
#define ar array
#define vll vector<ll>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class _T>
bool chmin(_T &x, const _T &y){
if(x>y){
x=y;
return true;
}
return false;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag=false;
if (x<y){
x=y;flag|=true;
}
return flag;
}
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=2e18+7;
const ll mod=1e9+7;
const ll N=3e2+7;
const ld eps=1e-9;
vll t(N*4,inf),t2(N*4,inf);
void upd(ll id,ll v,ll tl,ll tr,ll pos,ll val){
if(tl>pos || tr<pos)return;
if(tl==tr){
if(id==1)chmin(t[v],val);
else chmin(t2[v],val);
return;
}
ll tm=(tl+tr)/2;
upd(id,v*2,tl,tm,pos,val);
upd(id,v*2+1,tm+1,tr,pos,val);
t[v]=min(t[v*2],t[v*2+1]);
t2[v]=min(t2[v*2],t2[v*2+1]);
}
ll get(ll id,ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l)return inf;
if(l<=tl && tr<=r)return (id==1 ? t[v] : t2[v]);
ll tm=(tl+tr)/2;
return min(get(id,v*2,tl,tm,l,r),get(id,v*2+1,tm+1,tr,l,r));
}
void solve(){
ll i,j;
ll n,m;
cin>>n>>m;
set<ll> s;
s.ins(1);
s.ins(m);
vector<ar<ll,4>> v(1);
ar<ll,4> x;
for(i=1;i<=n;i++){
for(j=0;j<4;j++){
cin>>x[j];
if(j<3)s.ins(x[j]);
}
v.pb(x);
}
map<ll,ll> pos;
ll c=1;
for(auto i : s){
pos[i]=c;
c++;
}
m=s.sz;
for(i=1;i<=n;i++){
for(j=0;j<3;j++){
v[i][j]=pos[v[i][j]];
}
}
upd(1,1,1,s.sz,1,0);
upd(2,1,1,s.sz,s.sz,0);
ll ans=inf;
for(i=1;i<=n;i++){
ll x=get(1,1,1,s.sz,v[i][0],v[i][1]),y=get(2,1,1,s.sz,v[i][0],v[i][1]);
upd(1,1,1,s.sz,v[i][2],x+v[i][3]);
upd(2,1,1,s.sz,v[i][2],y+v[i][3]);
ans=min(ans,x+y+v[i][3]);
}
for(i=1;i<=(ll)s.sz;i++){
ans=min(ans,get(1,1,1,s.sz,i,i)+get(2,1,1,s.sz,i,i));
}
if(ans==inf)cout<<-1<<endl;
else cout<<ans<<endl;
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
1
6
5 4 3 2 1 0
*/
Compilation message
pinball.cpp: In function 'void fre(std::string)':
pinball.cpp:42:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:42:64: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |