#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pll pair<ll,ll>
#define fs first
#define sc second
const int mxn = 6e5+10;
const ll inf = 1e18;
struct SEG{
#define mid ((l+r)>>1)
#define ls now*2+1
#define rs now*2+2
ll seg[mxn*4];
SEG(){
fill(seg,seg+mxn*4,inf);
}
void modify(int now,int l,int r,int p,ll v){
if(l==r){
seg[now] = min(seg[now],v);
return;
}
if(mid>=p)modify(ls,l,mid,p,v);
else modify(rs,mid+1,r,p,v);
seg[now] = min(seg[ls],seg[rs]);
}
ll getval(int now,int l,int r,int s,int e){
if(l>=s&&e>=r)return seg[now];
if(mid>=e)return getval(ls,l,mid,s,e);
else if(mid<s)return getval(rs,mid+1,r,s,e);
else return min(getval(ls,l,mid,s,e),getval(rs,mid+1,r,s,e));
}
#undef ls
#undef rs
#undef mid
};
struct D{
int s,e,cen,cost;
D(){}
D(int ss,int ee,int cc,int co):s(ss),e(ee),cen(cc),cost(co){}
};
SEG lseg,rseg;
ll ans = inf;
ll N,M;
D arr[mxn];
vector<int> all;
ll dist[mxn];
int main(){
cin>>M>>N;
all.push_back(N);
all.push_back(1);
for(int i = 1;i<=M;i++){
cin>>arr[i].s>>arr[i].e>>arr[i].cen>>arr[i].cost;
all.push_back(arr[i].s);
all.push_back(arr[i].cen);
all.push_back(arr[i].e);
}
#define _all(T) T.begin(),T.end()
sort(_all(all));all.resize(unique(_all(all))-all.begin());
for(int i = 1;i<=M;i++){
arr[i].s = lower_bound(_all(all),arr[i].s)-all.begin();
arr[i].e = lower_bound(_all(all),arr[i].e)-all.begin();
arr[i].cen = lower_bound(_all(all),arr[i].cen)-all.begin();
}
N = all.size()-1;
lseg.modify(0,0,N,0,0);
rseg.modify(0,0,N,N,0);
for(int i = 1;i<=M;i++){
ll sum = 0;
ll dist = lseg.getval(0,0,N,arr[i].s,arr[i].e);
sum += dist;
lseg.modify(0,0,N,arr[i].cen,dist+arr[i].cost);
dist = rseg.getval(0,0,N,arr[i].s,arr[i].e);
sum += dist;
rseg.modify(0,0,N,arr[i].cen,dist+arr[i].cost);
ans = min(ans,sum+arr[i].cost);
}
cout<<(ans>=inf?-1:ans)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
12 ms |
39516 KB |
Output is correct |
3 |
Correct |
8 ms |
39580 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
10 ms |
39588 KB |
Output is correct |
7 |
Correct |
10 ms |
39512 KB |
Output is correct |
8 |
Correct |
11 ms |
39512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
12 ms |
39516 KB |
Output is correct |
3 |
Correct |
8 ms |
39580 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
10 ms |
39588 KB |
Output is correct |
7 |
Correct |
10 ms |
39512 KB |
Output is correct |
8 |
Correct |
11 ms |
39512 KB |
Output is correct |
9 |
Correct |
9 ms |
39512 KB |
Output is correct |
10 |
Correct |
10 ms |
39512 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
9 ms |
39516 KB |
Output is correct |
13 |
Correct |
8 ms |
39628 KB |
Output is correct |
14 |
Correct |
9 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
12 ms |
39516 KB |
Output is correct |
3 |
Correct |
8 ms |
39580 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
10 ms |
39588 KB |
Output is correct |
7 |
Correct |
10 ms |
39512 KB |
Output is correct |
8 |
Correct |
11 ms |
39512 KB |
Output is correct |
9 |
Correct |
9 ms |
39512 KB |
Output is correct |
10 |
Correct |
10 ms |
39512 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
9 ms |
39516 KB |
Output is correct |
13 |
Correct |
8 ms |
39628 KB |
Output is correct |
14 |
Correct |
9 ms |
39516 KB |
Output is correct |
15 |
Correct |
9 ms |
39516 KB |
Output is correct |
16 |
Correct |
9 ms |
39584 KB |
Output is correct |
17 |
Correct |
10 ms |
39516 KB |
Output is correct |
18 |
Correct |
10 ms |
39680 KB |
Output is correct |
19 |
Correct |
10 ms |
39516 KB |
Output is correct |
20 |
Correct |
10 ms |
39512 KB |
Output is correct |
21 |
Correct |
10 ms |
39516 KB |
Output is correct |
22 |
Correct |
10 ms |
39516 KB |
Output is correct |
23 |
Correct |
10 ms |
39516 KB |
Output is correct |
24 |
Correct |
11 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
39512 KB |
Output is correct |
2 |
Correct |
12 ms |
39516 KB |
Output is correct |
3 |
Correct |
8 ms |
39580 KB |
Output is correct |
4 |
Correct |
9 ms |
39516 KB |
Output is correct |
5 |
Correct |
9 ms |
39516 KB |
Output is correct |
6 |
Correct |
10 ms |
39588 KB |
Output is correct |
7 |
Correct |
10 ms |
39512 KB |
Output is correct |
8 |
Correct |
11 ms |
39512 KB |
Output is correct |
9 |
Correct |
9 ms |
39512 KB |
Output is correct |
10 |
Correct |
10 ms |
39512 KB |
Output is correct |
11 |
Correct |
9 ms |
39516 KB |
Output is correct |
12 |
Correct |
9 ms |
39516 KB |
Output is correct |
13 |
Correct |
8 ms |
39628 KB |
Output is correct |
14 |
Correct |
9 ms |
39516 KB |
Output is correct |
15 |
Correct |
9 ms |
39516 KB |
Output is correct |
16 |
Correct |
9 ms |
39584 KB |
Output is correct |
17 |
Correct |
10 ms |
39516 KB |
Output is correct |
18 |
Correct |
10 ms |
39680 KB |
Output is correct |
19 |
Correct |
10 ms |
39516 KB |
Output is correct |
20 |
Correct |
10 ms |
39512 KB |
Output is correct |
21 |
Correct |
10 ms |
39516 KB |
Output is correct |
22 |
Correct |
10 ms |
39516 KB |
Output is correct |
23 |
Correct |
10 ms |
39516 KB |
Output is correct |
24 |
Correct |
11 ms |
39516 KB |
Output is correct |
25 |
Correct |
25 ms |
40280 KB |
Output is correct |
26 |
Correct |
63 ms |
41432 KB |
Output is correct |
27 |
Correct |
154 ms |
44036 KB |
Output is correct |
28 |
Correct |
209 ms |
46536 KB |
Output is correct |
29 |
Correct |
110 ms |
43208 KB |
Output is correct |
30 |
Correct |
191 ms |
46564 KB |
Output is correct |
31 |
Correct |
209 ms |
46536 KB |
Output is correct |
32 |
Correct |
233 ms |
46528 KB |
Output is correct |
33 |
Correct |
38 ms |
40916 KB |
Output is correct |
34 |
Correct |
104 ms |
43212 KB |
Output is correct |
35 |
Correct |
158 ms |
46536 KB |
Output is correct |
36 |
Correct |
217 ms |
46532 KB |
Output is correct |
37 |
Correct |
204 ms |
46536 KB |
Output is correct |
38 |
Correct |
190 ms |
46536 KB |
Output is correct |