#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mxn=1e5+5,sz=(1<<20);
ll n,m,g[mxn][4];
vector<int>v;
struct segment{
ll mn[sz];
void up(int id,int L,int R,int l,ll x,bool w){
if(L+1==R){
if(w==1 || mn[id]>x){
mn[id]=x;
}
return;
}
int mid=(L+R)/2;
if(l<mid)
up(id*2,L,mid,l,x,w);
else
up(id*2+1,mid,R,l,x,w);
if(mn[id*2]<mn[id*2+1])
mn[id]=mn[id*2];
else
mn[id]=mn[id*2+1];
}
ll get(int id,int L,int R,int l,int r){
if(L==l && R==r){
return mn[id];
}
ll x1=1e16,x2=1e16;
int mid=(L+R)/2;
if(l<mid)
x1=get(id*2,L,mid,l,min(r,mid));
if(r>mid)
x2=get(id*2+1,mid,R,max(l,mid),r);
return min(x1,x2);
}
}seg[3];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>m>>n;
for(int i=1;i<=m;i++){
for(int j=0;j<3;j++){
cin>>g[i][j];
v.push_back(g[i][j]);
}
cin>>g[i][3];
}
if(n==1){
cout<<0;
return 0;
}
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
for(int i=1;i<=m;i++){
for(int j=0;j<3;j++){
int x=lower_bound(v.begin(),v.end(),g[i][j])-v.begin();
g[i][j]=x;
}
}
if(v.back()!=n || v[0]!=1){
cout<<-1;
return 0;
}
n=v.size()-1;
for(int i=0;i<=n;i++){
for(int j=0;j<3;j++){
seg[j].up(1,0,n+1,i,1e16,1);
}
}
seg[0].up(1,0,n+1,0,0,1);
seg[1].up(1,0,n+1,n,0,1);
for(int i=1;i<=m;i++){
ll x=seg[0].get(1,0,n+1,g[i][0],g[i][1]+1);
ll y=seg[1].get(1,0,n+1,g[i][0],g[i][1]+1);
//cout<<x<<" "<<y<<'\n';
seg[2].up(1,0,n+1,g[i][2],x+y+g[i][3],0);
seg[0].up(1,0,n+1,g[i][2],x+g[i][3],0);
seg[1].up(1,0,n+1,g[i][2],y+g[i][3],0);
}
ll ans=seg[2].get(1,0,n+1,0,n+1);
if(ans==1e16)
ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6664 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6744 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6744 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6664 KB |
Output is correct |
22 |
Correct |
4 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
3 ms |
6628 KB |
Output is correct |
25 |
Correct |
19 ms |
15448 KB |
Output is correct |
26 |
Correct |
57 ms |
16384 KB |
Output is correct |
27 |
Correct |
140 ms |
18420 KB |
Output is correct |
28 |
Correct |
118 ms |
14696 KB |
Output is correct |
29 |
Correct |
102 ms |
17616 KB |
Output is correct |
30 |
Correct |
161 ms |
20936 KB |
Output is correct |
31 |
Correct |
221 ms |
27080 KB |
Output is correct |
32 |
Correct |
211 ms |
21252 KB |
Output is correct |
33 |
Correct |
34 ms |
15840 KB |
Output is correct |
34 |
Correct |
117 ms |
23756 KB |
Output is correct |
35 |
Correct |
202 ms |
33372 KB |
Output is correct |
36 |
Correct |
268 ms |
33316 KB |
Output is correct |
37 |
Correct |
251 ms |
33188 KB |
Output is correct |
38 |
Correct |
228 ms |
33476 KB |
Output is correct |