// In the Name of Allah
// Ya Ali
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all( x ) x.begin() , x.end()
#define pb push_back
#define mp make_pair
const int maxn = 1e5 + 10 ;
const int INF = 1e18 ;
int n , m , ind[ maxn ] , a[ maxn ] , b[ maxn ] , c[ maxn ] , d[ maxn ] , l1[ maxn ] , r1[ maxn ] , ans = INF ;
int mn1[ 4*maxn ] , mn2[ 4*maxn ] ;
vector< pair< int , int > > vv ;
void build()
{
for(int i = 0 ; i <= 4*m ; i ++)
mn1[ i ] = mn2[ i ] = INF;
}
int ask1( int x , int y , int l = 0 , int r = m-1 , int node = 1 )
{
if( l > y || r < x )
return INF ;
if( l >= x && r <= y )
return mn1[ node ];
int mid = (r+l)/2 ;
return min( ask1( x , y , l , mid , 2*node ) , ask1( x , y , mid+1 , r , (2*node)+1 ) ) ;
}
int ask2( int x , int y , int l = 0 , int r = m-1 , int node = 1 )
{
if( l > y || r < x )
return INF ;
if( l >= x && r <= y )
return mn2[ node ];
int mid = (r+l)/2 ;
return min( ask2( x , y , l , mid , 2*node ) , ask2( x , y , mid+1 , r , (2*node)+1 ) ) ;
}
void up1( int x , int y , int va , int l = 0 , int r = m-1 , int node = 1 )
{
if( l > y || r < x )
return;
if( l >= x && r <= y ){
mn1[ node ] = va ;
return ;
}
int mid = (r+l)/2;
up1( x , y , va , l , mid , 2*node ) ;
up1( x , y , va , mid+1 , r , (2*node)+1 ) ;
mn1[ node ] = min( mn1[ 2*node ] , mn1[ (2*node)+1 ] ) ;
}
void up2( int x , int y , int va , int l = 0 , int r = m-1 , int node = 1 )
{
if( l > y || r < x )
return;
if( l >= x && r <= y ){
mn2[ node ] = va ;
return ;
}
int mid = (r+l)/2;
up2( x , y , va , l , mid , 2*node ) ;
up2( x , y , va , mid+1 , r , (2*node)+1 ) ;
mn2[ node ] = min( mn2[ 2*node ] , mn2[ (2*node)+1 ] ) ;
}
signed main()
{
ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
cin >> m >> n ;
for(int i = 1 ; i <= m ; i ++){
cin >> a[ i ] >> b[ i ] >> c[ i ] >> d[ i ];
vv.pb({ c[ i ] , i }) ;
}
sort( all( vv ) ) ;
for(int i = 0 ; i < m ; i ++)
ind[ vv[ i ].S ] = i ;
for(int i = 1 ; i <= m ; i ++){
l1[ i ] = lower_bound( all( vv ) , mp( a[ i ] , 0ll ) ) - vv.begin();
r1[ i ] = upper_bound( all( vv ) , mp( b[ i ] , INF ) ) - vv.begin();
r1[ i ] -- ;
}
build();
for(int i = 1 ; i <= m ; i ++){
//for dp1
int x = INF ;
if( a[ i ] == 1 )x = d[ i ] ;
x = min( x , ask1( l1[ i ] , r1[ i ] ) + d[ i ] ) ;
up1( ind[ i ] , ind[ i ] , x ) ;
// for dp2
x = INF ;
if( b[ i ] == n )x = d[ i ] ;
x = min( x , ask2( l1[ i ] , r1[ i ] ) + d[ i ] ) ;
up2( ind[ i ] , ind[ i ] , x );
if( a[ i ] > 1 && b[ i ] < n )ans = min( ans , ask1( l1[ i ] , r1[ i ] ) + ask2( l1[ i ] , r1[ i ] ) + d[ i ] ) ;
if( a[ i ] == 1 && b[ i ] < n )ans = min( ans , ask2( l1[ i ] , r1[ i ] ) + d[ i ] ) ;
if( a[ i ] > 1 && b[ i ] == n )ans = min( ans , ask1( l1[ i ] , r1[ i ] ) + d[ i ] ) ;
if( a[ i ] == 1 && b[ i ] == n )ans = min( ans , d[ i ] ) ;
}
if( ans == INF )cout << -1 ;
else cout << ans ;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
7 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
7 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
27 ms |
2176 KB |
Output is correct |
26 |
Correct |
65 ms |
4732 KB |
Output is correct |
27 |
Correct |
195 ms |
12344 KB |
Output is correct |
28 |
Correct |
280 ms |
17628 KB |
Output is correct |
29 |
Correct |
139 ms |
9072 KB |
Output is correct |
30 |
Correct |
309 ms |
17700 KB |
Output is correct |
31 |
Correct |
295 ms |
17640 KB |
Output is correct |
32 |
Correct |
294 ms |
17640 KB |
Output is correct |
33 |
Correct |
39 ms |
3836 KB |
Output is correct |
34 |
Correct |
123 ms |
9072 KB |
Output is correct |
35 |
Correct |
183 ms |
17644 KB |
Output is correct |
36 |
Correct |
282 ms |
17640 KB |
Output is correct |
37 |
Correct |
240 ms |
17948 KB |
Output is correct |
38 |
Correct |
234 ms |
17640 KB |
Output is correct |