#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll inf=1e18;
struct device{
ll i, a, b, c, d;
ll dp[2]={inf, inf};
bool operator<(const device B)const{
return b<B.b;
}
};
struct segtree{
segtree *left, *right;
int l, r, mid;
ll mn=inf;
segtree(int x, int y){
l=x;
r=y;
mid=(l+r)/2;
if(l==r) return;
left = new segtree(l,mid);
right = new segtree(mid+1,r);
}
void update(int x, ll dp, bool force=false){
if(x<l || r<x) return;
if(x<=l && r<=x){
mn=min(dp,mn);
if(force) mn=dp;
return;
}
left->update(x,dp,force);
right->update(x,dp,force);
mn=min(left->mn,right->mn);
}
ll query(int x, int y){
if(y<l || r<x) return 1e18;
if(x<=l && r<=y) return mn;
return min(left->query(x,y),right->query(x,y));
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
vector<device> devs(n);
ll ans=1e18;
map<int, int> ccmp;
set<int> nums;
for(int i=0; i<n; i++){
cin>>devs[i].a>>devs[i].b>>devs[i].c>>devs[i].d;
devs[i].i=i;
nums.insert(devs[i].a);
nums.insert(devs[i].b);
nums.insert(devs[i].c);
}
nums.insert(1);
nums.insert(m);
int x=0;
for(auto e:nums) ccmp[e]=x++;
segtree ST(0,x), ST2(0,x);
ST.update(0,0);
for(int i=0; i<n; i++){
devs[i].a=ccmp[devs[i].a];
devs[i].b=ccmp[devs[i].b];
devs[i].c=ccmp[devs[i].c];
}
for(int i=0; i<n; i++){
devs[i].dp[0]=ST.query(devs[i].a,devs[i].b)+devs[i].d;
ST.update(devs[i].c,devs[i].dp[0]);
}
ST2.update(x-1,0);
for(int i=0; i<n; i++){
devs[i].dp[1]=ST2.query(devs[i].a,devs[i].b)+devs[i].d;
ST2.update(devs[i].c,devs[i].dp[1]);
ans=min(ans,devs[i].dp[1]+devs[i].dp[0]-devs[i].d);
//cout<<devs[i].a<<" "<<devs[i].b<<" "<<devs[i].c<<" "<<devs[i].dp[0]<<" "<<devs[i].dp[1]<<endl;
}
if(ans==inf) cout<<-1<<endl;
else cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
672 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
672 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
24 ms |
5980 KB |
Output is correct |
26 |
Correct |
97 ms |
18404 KB |
Output is correct |
27 |
Correct |
258 ms |
35156 KB |
Output is correct |
28 |
Correct |
118 ms |
10128 KB |
Output is correct |
29 |
Correct |
231 ms |
31048 KB |
Output is correct |
30 |
Correct |
190 ms |
14672 KB |
Output is correct |
31 |
Correct |
539 ms |
57036 KB |
Output is correct |
32 |
Correct |
340 ms |
43776 KB |
Output is correct |
33 |
Correct |
55 ms |
18768 KB |
Output is correct |
34 |
Correct |
218 ms |
47352 KB |
Output is correct |
35 |
Correct |
348 ms |
93896 KB |
Output is correct |
36 |
Correct |
715 ms |
94280 KB |
Output is correct |
37 |
Correct |
607 ms |
94272 KB |
Output is correct |
38 |
Correct |
505 ms |
94248 KB |
Output is correct |