#include<bits/stdc++.h>
using namespace std;
const long long MX=3e5+10;
const long long INF=1e18;
long long mas[2][4*MX];
void build(long long v,long long tl,long long tr)
{
mas[0][v]=INF;
mas[1][v]=INF;
if(tl==tr)
{
return;
}
long long mid=(tl+tr)/2;
build(v*2,tl,mid);
build(v*2+1,mid+1,tr);
return;
}
long long get_mn(long long in,long long v,long long tl,long long tr,long long l,long long r)
{
if(l<=tl && tr<=r)
{
return mas[in][v];
}
if(tr<l || r<tl)
{
return INF;
}
long long mid=(tl+tr)/2;
return min(get_mn(in,v*2,tl,mid,l,r),get_mn(in,v*2+1,mid+1,tr,l,r));
}
void upd(long long in,long long v,long long tl,long long tr,long long pos,long long zn)
{
if(tl==tr)
{
mas[in][v]=min(mas[in][v],zn);
return;
}
long long mid=(tl+tr)/2;
if(pos<=mid)
{
upd(in,v*2,tl,mid,pos,zn);
}
else
{
upd(in,v*2+1,mid+1,tr,pos,zn);
}
mas[in][v]=min(mas[in][v*2],mas[in][v*2+1]);
return;
}
long long m,n;
pair < pair < long long , long long > , pair < long long , long long > > zap[MX];
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
map < long long , long long > mp;
cin>>m>>n;
for(long long i=1;i<=m;i++)
{
cin>>zap[i].first.first>>zap[i].first.second>>zap[i].second.first>>zap[i].second.second;
mp[zap[i].first.first]=1;
mp[zap[i].first.second]=1;
mp[zap[i].second.first]=1;
}
mp[1]=1;
mp[n]=1;
long long uk=0;
for(auto & u:mp)
{
uk++;
u.second=uk;
}
n=uk;
build(1,1,n);
upd(0,1,1,n,1,0);
upd(1,1,1,n,n,0);
long long res=INF;
for(long long i=1;i<=m;i++)
{
zap[i].first.first=mp[zap[i].first.first];
zap[i].first.second=mp[zap[i].first.second];
zap[i].second.first=mp[zap[i].second.first];
res=min(res,get_mn(0,1,1,n,zap[i].first.first,zap[i].first.second)+get_mn(1,1,1,n,zap[i].first.first,zap[i].first.second)+zap[i].second.second);
upd(0,1,1,n,zap[i].second.first,get_mn(0,1,1,n,zap[i].first.first,zap[i].first.second)+zap[i].second.second);
upd(1,1,1,n,zap[i].second.first,get_mn(1,1,1,n,zap[i].first.first,zap[i].first.second)+zap[i].second.second);
}
if(res==INF)
{
res=-1;
}
cout<<res<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
2 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4600 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4440 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4704 KB |
Output is correct |
25 |
Correct |
23 ms |
8028 KB |
Output is correct |
26 |
Correct |
65 ms |
11148 KB |
Output is correct |
27 |
Correct |
208 ms |
19884 KB |
Output is correct |
28 |
Correct |
129 ms |
12624 KB |
Output is correct |
29 |
Correct |
136 ms |
18284 KB |
Output is correct |
30 |
Correct |
200 ms |
13832 KB |
Output is correct |
31 |
Correct |
383 ms |
31320 KB |
Output is correct |
32 |
Correct |
299 ms |
24404 KB |
Output is correct |
33 |
Correct |
36 ms |
11100 KB |
Output is correct |
34 |
Correct |
152 ms |
26192 KB |
Output is correct |
35 |
Correct |
206 ms |
46164 KB |
Output is correct |
36 |
Correct |
489 ms |
45976 KB |
Output is correct |
37 |
Correct |
374 ms |
46164 KB |
Output is correct |
38 |
Correct |
357 ms |
45828 KB |
Output is correct |