#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
const long long inf=1e14+9;
const int maxn=1e5+9;
struct IT{
vector<long long>st;
void build(int id,int l,int r){
st[id]=inf;
if (l==r)return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void resz(int n){
st.resize(4*n+9);
build(1,1,n);
}
void update(int id,int l,int r,int u,long long val){
if (l>u||r<u)return;
if (l==r){
st[id]=min(st[id],val);
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,val);
update(id*2+1,mid+1,r,u,val);
st[id]=min(st[id*2],st[id*2+1]);
}
long long get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return inf;
if (u<=l&&r<=v)return st[id];
int mid=(l+r)/2;
return min(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
};
struct pin{
int x,y,z;
long long t;
}a[maxn];
int m,n;
void nen(){
vector<int>t;
t.pb(1),t.pb(n),t.pb(0);
for1(i,1,m){
t.pb(a[i].x);
t.pb(a[i].y);
t.pb(a[i].z);
}
sort(all(t));
map<int,int>d;
for1(i,1,sz(t)-1){
if (t[i]==t[i-1])continue;
d[t[i]]=d[t[i-1]]+1;
n=d[t[i]];
}
for1(i,1,m){
a[i].x=d[a[i].x],a[i].y=d[a[i].y],a[i].z=d[a[i].z];
}
}
void solve(){
IT st1,st2;
st1.resz(n),st2.resz(n);
st1.update(1,1,n,1,0);
st2.update(1,1,n,n,0);
long long ans=inf;
for1(i,1,m){
ans=min(ans,a[i].t+st1.get(1,1,n,a[i].x,a[i].y)+st2.get(1,1,n,a[i].x,a[i].y));
long long v1=a[i].t+st1.get(1,1,n,a[i].x,a[i].y);
long long v2=a[i].t+st2.get(1,1,n,a[i].x,a[i].y);
st1.update(1,1,n,a[i].z,v1),st2.update(1,1,n,a[i].z,v2);
}
if (ans>=inf)ans=-1;
cout<<ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>m>>n;
for1(i,1,m)cin>>a[i].x>>a[i].y>>a[i].z>>a[i].t;
nen();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
624 KB |
Output is correct |
25 |
Correct |
26 ms |
2260 KB |
Output is correct |
26 |
Correct |
69 ms |
6080 KB |
Output is correct |
27 |
Correct |
195 ms |
11572 KB |
Output is correct |
28 |
Correct |
137 ms |
6472 KB |
Output is correct |
29 |
Correct |
141 ms |
10224 KB |
Output is correct |
30 |
Correct |
190 ms |
6484 KB |
Output is correct |
31 |
Correct |
297 ms |
17456 KB |
Output is correct |
32 |
Correct |
285 ms |
13836 KB |
Output is correct |
33 |
Correct |
43 ms |
6040 KB |
Output is correct |
34 |
Correct |
159 ms |
14896 KB |
Output is correct |
35 |
Correct |
253 ms |
27656 KB |
Output is correct |
36 |
Correct |
374 ms |
27756 KB |
Output is correct |
37 |
Correct |
376 ms |
27728 KB |
Output is correct |
38 |
Correct |
312 ms |
27732 KB |
Output is correct |