#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
template < class T, class T2> inline void chkmax( T & x, const T2 & y) { if ( x < y) x = y; }
template < class T, class T2> inline void chkmin( T & x, const T2 & y) { if ( x > y) x = y; }
const int MAXN = ( 1 << 20 ) ;
struct node
{
int64_t mn;
node( ) { mn = ( int64_t ) 2e17 ; }
node( int64_t val)
{
mn = val;
}
} ;
node temp, broken;
node merge( node l, node r)
{
temp.mn = min( l.mn , r.mn ) ;
return temp;
}
struct segment_tree
{
node tr[ 4 * MAXN] ;
void init( int l, int r, int idx)
{
if ( l == r)
{
tr[ idx] = node( ) ;
return ;
}
int mid = ( l + r) >> 1 ;
init( l, mid, 2 * idx + 1 ) ;
init( mid + 1 , r, 2 * idx + 2 ) ;
tr[ idx] = merge( tr[ 2 * idx + 1 ] , tr[ 2 * idx + 2 ] ) ;
}
void update( int pos, int64_t val, int l, int r, int idx)
{
if ( l > pos || r < pos)
return ;
if ( l == r && l == pos)
{
chkmin( tr[ idx] .mn , val) ;
return ;
}
int mid = ( l + r) >> 1 ;
update( pos, val, l, mid, 2 * idx + 1 ) ;
update( pos, val, mid + 1 , r, 2 * idx + 2 ) ;
tr[ idx] = merge( tr[ 2 * idx + 1 ] , tr[ 2 * idx + 2 ] ) ;
}
node query( int qL, int qR, int l, int r, int idx)
{
if ( l > qR || r < qL)
return broken;
if ( qL <= l && r <= qR)
return tr[ idx] ;
int mid = ( l + r) >> 1 ;
return merge( query( qL, qR, l, mid, 2 * idx + 1 ) , query( qL, qR, mid + 1 , r, 2 * idx + 2 ) ) ;
}
} ;
int n, m;
vector< int64_t > sorted;
int L[ MAXN] , R[ MAXN] , T[ MAXN] ;
int perm[ MAXN] ;
pair< int , int > que[ MAXN] ;
void read( )
{
cin >> n >> m;
for ( int i = 0 ; i < n; i++ )
{
cin >> L[ i] >> R[ i] >> T[ i] ;
//if(L[i] > R[i]) swap(L[i], R[i]);
sorted.push_back ( L[ i] ) ;
sorted.push_back ( R[ i] ) ;
}
for ( int i = 0 ; i < m; i++ )
{
cin >> que[ i] .first >> que[ i] .second ;
//if(que[i].first > que[i].second) swap(que[i].first, que[i].second);
sorted.push_back ( que[ i] .first ) ;
sorted.push_back ( que[ i] .second ) ;
}
}
int64_t answer[ MAXN] ;
int get( int x) { return lower_bound( sorted.begin ( ) , sorted.end ( ) , x) - sorted.begin ( ) ; }
bool cmp_inside( pair< int , int > a, pair< int , int > b)
{
int r1, r2;
if ( a.second ) r1 = que[ a.first ] .second ;
else r1 = R[ a.first ] ;
if ( b.second ) r2 = que[ b.first ] .second ;
else r2 = R[ b.first ] ;
if ( r1 ! = r2) return r1 < r2;
return a.second < b.second ;
}
bool cmp_outside( pair< int , int > a, pair< int , int > b)
{
int r1, r2;
if ( a.second ) r1 = que[ a.first ] .first ;
else r1 = L[ a.first ] ;
if ( b.second ) r2 = que[ b.first ] .first ;
else r2 = L[ b.first ] ;
if ( r1 ! = r2) return r1 < r2;
return a.second < b.second ;
}
segment_tree t;
void solve_inside( )
{
vector< pair< int , int > > li;
for ( int i = 0 ; i < n; i++ )
if ( L[ i] <= R[ i] )
li.push_back ( { i, 0 } ) ;
for ( int i = 0 ; i < m; i++ )
if ( que[ i] .first <= que[ i] .second )
li.push_back ( { i, 1 } ) ;
sort( li.begin ( ) , li.end ( ) , cmp_inside) ;
t.init ( 0 , sorted.size ( ) , 0 ) ;
for ( auto it: li)
{
int type = it.second ;
if ( type == 0 )
t.update ( L[ it.first ] , T[ it.first ] + sorted[ L[ it.first ] ] - sorted[ R[ it.first ] ] , 0 , sorted.size ( ) , 0 ) ;
else
chkmin( answer[ it.first ] , sorted[ que[ it.first ] .second ] - sorted[ que[ it.first ] .first ] + t.query ( que[ it.first ] .first , sorted.size ( ) , 0 , sorted.size ( ) , 0 ) .mn ) ;
}
}
void solve_left( )
{
vector< pair< int , int > > li;
for ( int i = 0 ; i < n; i++ )
if ( L[ i] <= R[ i] )
li.push_back ( { i, 0 } ) ;
for ( int i = 0 ; i < m; i++ )
if ( que[ i] .first <= que[ i] .second )
li.push_back ( { i, 1 } ) ;
sort( li.begin ( ) , li.end ( ) , cmp_outside) ;
t.init ( 0 , sorted.size ( ) , 0 ) ;
for ( auto it: li)
{
int type = it.second ;
if ( type == 0 )
t.update ( R[ it.first ] , T[ it.first ] - sorted[ L[ it.first ] ] - sorted[ R[ it.first ] ] , 0 , sorted.size ( ) , 0 ) ;
else
chkmin( answer[ it.first ] , sorted[ que[ it.first ] .second ] + sorted[ que[ it.first ] .first ] + t.query ( que[ it.first ] .first , que[ it.first ] .second , 0 , sorted.size ( ) , 0 ) .mn ) ;
}
}
void solve_outside( )
{
vector< pair< int , int > > li;
for ( int i = 0 ; i < n; i++ )
if ( L[ i] <= R[ i] )
li.push_back ( { i, 0 } ) ;
for ( int i = 0 ; i < m; i++ )
if ( que[ i] .first <= que[ i] .second )
li.push_back ( { i, 1 } ) ;
sort( li.begin ( ) , li.end ( ) , cmp_outside) ;
t.init ( 0 , sorted.size ( ) , 0 ) ;
for ( auto it: li)
{
int type = it.second ;
if ( type == 0 )
t.update ( R[ it.first ] , T[ it.first ] - sorted[ L[ it.first ] ] + sorted[ R[ it.first ] ] , 0 , sorted.size ( ) , 0 ) ;
else
chkmin( answer[ it.first ] , - sorted[ que[ it.first ] .second ] + sorted[ que[ it.first ] .first ] + t.query ( que[ it.first ] .second , sorted.size ( ) , 0 , sorted.size ( ) , 0 ) .mn ) ;
}
}
void solve_right( )
{
vector< pair< int , int > > li;
for ( int i = 0 ; i < n; i++ )
if ( L[ i] <= R[ i] )
li.push_back ( { i, 0 } ) ;
for ( int i = 0 ; i < m; i++ )
if ( que[ i] .first <= que[ i] .second )
li.push_back ( { i, 1 } ) ;
sort( li.begin ( ) , li.end ( ) , cmp_inside) ;
reverse( li.begin ( ) , li.end ( ) ) ;
t.init ( 0 , sorted.size ( ) , 0 ) ;
for ( auto it: li)
{
int type = it.second ;
if ( type == 0 )
t.update ( L[ it.first ] , T[ it.first ] + sorted[ L[ it.first ] ] + sorted[ R[ it.first ] ] , 0 , sorted.size ( ) , 0 ) ;
else
chkmin( answer[ it.first ] , - sorted[ que[ it.first ] .second ] - sorted[ que[ it.first ] .first ] + t.query ( que[ it.first ] .first , que[ it.first ] .second , 0 , sorted.size ( ) , 0 ) .mn ) ;
}
}
void solve( )
{
for ( int i = 0 ; i < m; i++ )
answer[ i] = abs ( que[ i] .first - que[ i] .second ) ;
sort( sorted.begin ( ) , sorted.end ( ) ) ;
sorted.erase ( unique( sorted.begin ( ) , sorted.end ( ) ) , sorted.end ( ) ) ;
for ( int i = 0 ; i < m; i++ )
{
que[ i] .first = get( que[ i] .first ) ;
que[ i] .second = get( que[ i] .second ) ;
}
for ( int i = 0 ; i < n; i++ )
L[ i] = get( L[ i] ) , R[ i] = get( R[ i] ) ;
solve_inside( ) ;
solve_left( ) ;
solve_outside( ) ;
solve_right( ) ;
for ( int i = 0 ; i < m; i++ )
{
que[ i] .first = - sorted[ que[ i] .first ] ;
que[ i] .second = - sorted[ que[ i] .second ] ;
}
for ( int i = 0 ; i < n; i++ )
L[ i] = - sorted[ L[ i] ] , R[ i] = - sorted[ R[ i] ] ;
for ( auto & it: sorted) it * = - 1 ;
sort( sorted.begin ( ) , sorted.end ( ) ) ;
for ( int i = 0 ; i < m; i++ )
{
que[ i] .first = get( que[ i] .first ) ;
que[ i] .second = get( que[ i] .second ) ;
}
for ( int i = 0 ; i < n; i++ )
L[ i] = get( L[ i] ) , R[ i] = get( R[ i] ) ;
solve_inside( ) ;
solve_left( ) ;
solve_outside( ) ;
solve_right( ) ;
for ( int i = 0 ; i < m; i++ )
cout << answer[ i] << endl;
}
int main( )
{
freopen ( "slingshot.in" , "r" , stdin ) ;
freopen ( "slingshot.out" , "w" , stdout ) ;
ios_base:: sync_with_stdio ( false ) ;
cin .tie ( NULL ) ;
read( ) ;
solve( ) ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZW5kbCAnXG4nCgp1c2luZyBuYW1lc3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