#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=1e6+5,MAXA=5e6+5,INF=1e9,LINF=1e18;
#define fi first
#define se second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mid (l+r)/2
#define sz(a) int((a).size())
#define all(a) a.begin(),a.end()
#define mod 1000000007LL
#define pb push_back
#define endl "\n"
#define PRINT(x) cerr<<#x<<'-'<<x<<endl<<flush;
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pb push_back
#define pf push_front
#define ppf pop_front
#define ppb pop_back
#define PRINTvec(x) { cerr<<#x<<"-"; for(int i=0;i<sz(x);i++) cerr<<x[i]<<" "; cerr<<endl; }
ll n,m,k,q,x,y,z,res=LINF,l,r;
string s,t;
ifstream input;
ofstream output;
#ifdef ONLINE_JUDGE
#define input cin
#define output cout
#endif
vector<ll> segL(4*MAXN,LINF),segR(4*MAXN,LINF);
vector<ll> dpL(MAXN),dpR(MAXN);
void update(vector<ll>& seg,int node,int l,int r,int idx,ll 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]);
}
ll query(vector<ll>& seg,int node,int l,int r,int L,int R){
if(L<=l && r<=R){
return seg[node];
}
ll ret=LINF;
if(L<=mid) ret=min(ret,query(seg,2*node,l,mid,L,R));
if(R>mid) ret=min(ret,query(seg,2*node+1,mid+1,r,L,R));
return ret;
}
struct dev{
int a,b,c,d;
void input(){
cin>>a>>b>>c>>d;
}
void output(){
cout<<"a="<<a<<" b="<<b<<" c="<<c<<" d="<<d<<endl;
}
};
vector<dev> a;
void solve(){
cin>>n>>m;
a.resize(n+1);
vector<int> svi;
svi.pb(1); svi.pb(m);
for(int i=1;i<=n;i++){
a[i].input();
svi.pb(a[i].a); svi.pb(a[i].b); svi.pb(a[i].c);
}
getunique(svi);
map<int,int> val;
for(int i=0;i<sz(svi);i++){
val[svi[i]]=i+1;
}
for(int i=1;i<=n;i++){
a[i].a=val[a[i].a];
a[i].b=val[a[i].b];
a[i].c=val[a[i].c];
}
for(int i=1;i<=n;i++){
if(a[i].a==1){
dpL[i]=a[i].d;
}
else{
dpL[i]=query(segL,1,1,sz(svi),a[i].a,a[i].b)+a[i].d;
}
if(a[i].b==sz(svi)){
dpR[i]=a[i].d;
}
else{
dpR[i]=query(segR,1,1,sz(svi),a[i].a,a[i].b)+a[i].d;
}
update(segL,1,1,sz(svi),a[i].c,dpL[i]);
update(segR,1,1,sz(svi),a[i].c,dpR[i]);
}
for(int i=1;i<=n;i++){
///cout<<"dpL["<<i<<"]="<<dpL[i]<<endl;
///cout<<"dpR["<<i<<"]="<<dpR[i]<<endl;
if(dpL[i]>=LINF || dpR[i]>=LINF) continue;
res=min(res,dpL[i]+dpR[i]-a[i].d);
}
cout<<(res!=LINF ? res : -1)<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
input.open("D:\\UROS\\Programiranje\\input.in",ios::in);
output.open("D:\\UROS\\Programiranje\\output.out",ios::out|ios::trunc);
#endif
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int t;
//cin>>t;
t=1;
while(t--){
solve();
}
return 0;
}
/*
5 6
2 4 3 5
1 2 2 8
3 6 5 2
4 6 4 7
2 4 3 10
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
78528 KB |
Output is correct |
2 |
Correct |
30 ms |
78540 KB |
Output is correct |
3 |
Correct |
30 ms |
78540 KB |
Output is correct |
4 |
Correct |
31 ms |
78492 KB |
Output is correct |
5 |
Correct |
36 ms |
78600 KB |
Output is correct |
6 |
Correct |
35 ms |
78536 KB |
Output is correct |
7 |
Correct |
31 ms |
78564 KB |
Output is correct |
8 |
Correct |
31 ms |
78568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
78528 KB |
Output is correct |
2 |
Correct |
30 ms |
78540 KB |
Output is correct |
3 |
Correct |
30 ms |
78540 KB |
Output is correct |
4 |
Correct |
31 ms |
78492 KB |
Output is correct |
5 |
Correct |
36 ms |
78600 KB |
Output is correct |
6 |
Correct |
35 ms |
78536 KB |
Output is correct |
7 |
Correct |
31 ms |
78564 KB |
Output is correct |
8 |
Correct |
31 ms |
78568 KB |
Output is correct |
9 |
Correct |
29 ms |
78604 KB |
Output is correct |
10 |
Correct |
29 ms |
78604 KB |
Output is correct |
11 |
Correct |
29 ms |
78624 KB |
Output is correct |
12 |
Correct |
30 ms |
78556 KB |
Output is correct |
13 |
Correct |
30 ms |
78556 KB |
Output is correct |
14 |
Correct |
36 ms |
78540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
78528 KB |
Output is correct |
2 |
Correct |
30 ms |
78540 KB |
Output is correct |
3 |
Correct |
30 ms |
78540 KB |
Output is correct |
4 |
Correct |
31 ms |
78492 KB |
Output is correct |
5 |
Correct |
36 ms |
78600 KB |
Output is correct |
6 |
Correct |
35 ms |
78536 KB |
Output is correct |
7 |
Correct |
31 ms |
78564 KB |
Output is correct |
8 |
Correct |
31 ms |
78568 KB |
Output is correct |
9 |
Correct |
29 ms |
78604 KB |
Output is correct |
10 |
Correct |
29 ms |
78604 KB |
Output is correct |
11 |
Correct |
29 ms |
78624 KB |
Output is correct |
12 |
Correct |
30 ms |
78556 KB |
Output is correct |
13 |
Correct |
30 ms |
78556 KB |
Output is correct |
14 |
Correct |
36 ms |
78540 KB |
Output is correct |
15 |
Correct |
37 ms |
78508 KB |
Output is correct |
16 |
Correct |
29 ms |
78584 KB |
Output is correct |
17 |
Correct |
31 ms |
78732 KB |
Output is correct |
18 |
Correct |
34 ms |
78636 KB |
Output is correct |
19 |
Correct |
31 ms |
78692 KB |
Output is correct |
20 |
Correct |
31 ms |
78648 KB |
Output is correct |
21 |
Correct |
31 ms |
78656 KB |
Output is correct |
22 |
Correct |
31 ms |
78668 KB |
Output is correct |
23 |
Correct |
30 ms |
78740 KB |
Output is correct |
24 |
Correct |
31 ms |
78688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
78528 KB |
Output is correct |
2 |
Correct |
30 ms |
78540 KB |
Output is correct |
3 |
Correct |
30 ms |
78540 KB |
Output is correct |
4 |
Correct |
31 ms |
78492 KB |
Output is correct |
5 |
Correct |
36 ms |
78600 KB |
Output is correct |
6 |
Correct |
35 ms |
78536 KB |
Output is correct |
7 |
Correct |
31 ms |
78564 KB |
Output is correct |
8 |
Correct |
31 ms |
78568 KB |
Output is correct |
9 |
Correct |
29 ms |
78604 KB |
Output is correct |
10 |
Correct |
29 ms |
78604 KB |
Output is correct |
11 |
Correct |
29 ms |
78624 KB |
Output is correct |
12 |
Correct |
30 ms |
78556 KB |
Output is correct |
13 |
Correct |
30 ms |
78556 KB |
Output is correct |
14 |
Correct |
36 ms |
78540 KB |
Output is correct |
15 |
Correct |
37 ms |
78508 KB |
Output is correct |
16 |
Correct |
29 ms |
78584 KB |
Output is correct |
17 |
Correct |
31 ms |
78732 KB |
Output is correct |
18 |
Correct |
34 ms |
78636 KB |
Output is correct |
19 |
Correct |
31 ms |
78692 KB |
Output is correct |
20 |
Correct |
31 ms |
78648 KB |
Output is correct |
21 |
Correct |
31 ms |
78656 KB |
Output is correct |
22 |
Correct |
31 ms |
78668 KB |
Output is correct |
23 |
Correct |
30 ms |
78740 KB |
Output is correct |
24 |
Correct |
31 ms |
78688 KB |
Output is correct |
25 |
Correct |
49 ms |
79684 KB |
Output is correct |
26 |
Correct |
89 ms |
81804 KB |
Output is correct |
27 |
Correct |
245 ms |
85224 KB |
Output is correct |
28 |
Correct |
152 ms |
82244 KB |
Output is correct |
29 |
Correct |
155 ms |
84228 KB |
Output is correct |
30 |
Correct |
214 ms |
82292 KB |
Output is correct |
31 |
Correct |
326 ms |
89168 KB |
Output is correct |
32 |
Correct |
307 ms |
86956 KB |
Output is correct |
33 |
Correct |
86 ms |
81848 KB |
Output is correct |
34 |
Correct |
187 ms |
86900 KB |
Output is correct |
35 |
Correct |
246 ms |
95352 KB |
Output is correct |
36 |
Correct |
446 ms |
95332 KB |
Output is correct |
37 |
Correct |
366 ms |
95404 KB |
Output is correct |
38 |
Correct |
349 ms |
95368 KB |
Output is correct |