This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
#define sqrt sqrtl
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
freopen ( "sum.in", "r", stdin )
*/
int n, m, q, k, a[ 200001 ], b[ 200001 ], s[ 50001 ], f[ 50001 ];
struct Node {
int mx, mn;
void clear( ) {
mx = 0;
mn = n + 1;
}
} t[ 20 ][ 400001 ], p[ 20 ][ 400001 ], an;
vector <int> v[ 2001 ];
void build( int v, int tl, int tr ) {
p[ pos ][ v ].mn = n + 1;
if ( tl == tr ) {
t[ pos ][ v ].mx = tl;
t[ pos ][ v ].mn = tl;
return;
}
int mid = ( tl + tr ) / 2;
build( v + v, tl, mid );
build( v + v + 1, mid + 1, tr );
t[ pos ][ v ].mx = max( t[ pos ][ v + v ].mx, t[ pos ][ v + v + 1 ].mx );
t[ pos ][ v ].mn = min( t[ pos ][ v + v ].mn, t[ pos ][ v + v + 1 ].mn );
}
void pus( int v ) {
if ( p[ pos ][ v ].mx ) {
p[ pos ][ v + v ].mx = max( p[ pos ][ v + v ].mx, p[ pos ][ v ].mx );
p[ pos ][ v + v + 1 ].mx = max( p[ pos ][ v + v + 1 ].mx, p[ pos ][ v ].mx );
t[ pos ][ v + v ].mx = max( t[ pos ][ v + v ].mx, p[ pos ][ v ].mx );
t[ pos ][ v + v + 1 ].mx = max( t[ pos ][ v + v + 1 ].mx, p[ pos ][ v ].mx );
p[ pos ][ v ].mx = 0;
}
if ( p[ pos ][ v ].mn != n + 1 ) {
p[ pos ][ v + v ].mn = min( p[ pos ][ v + v ].mn, p[ pos ][ v ].mn );
p[ pos ][ v + v + 1 ].mn= min( p[ pos ][ v + v + 1 ].mn, p[ pos ][ v ].mn );
t[ pos ][ v + v ].mn = min( t[ pos ][ v + v ].mn, p[ pos ][ v ].mn );
t[ pos ][ v + v + 1 ].mn = min( t[ pos ][ v + v + 1 ].mn, p[ pos ][ v ].mn );
p[ pos ][ v ].mn = n + 1;
}
}
void upd( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
// cout << tl << " " << tr << " x- " << num << "\n";
t[ pos ][ v ].mx = max( t[ pos ][ v ].mx, num );
p[ pos ][ v ].mx = max( p[ pos ][ v ].mx, num );
return;
}
pus( v );
int mid = ( tl + tr ) / 2;
upd( v + v, tl, mid, l, r );
upd( v + v + 1, mid + 1, tr, l, r );
t[ pos ][ v ].mx = max( t[ pos ][ v + v ].mx, t[ pos ][ v + v + 1 ].mx );
}
void updd( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
// cout << tl << " " << tr << " n- " << num << "\n";
t[ pos ][ v ].mn = min( t[ pos ][ v ].mn, num );
p[ pos ][ v ].mn = min( p[ pos ][ v ].mn, num );
return;
}
pus( v );
int mid = ( tl + tr ) / 2;
updd( v + v, tl, mid, l, r );
updd( v + v + 1, mid + 1, tr, l, r );
t[ pos ][ v ].mn = min( t[ pos ][ v + v ].mn, t[ pos ][ v + v + 1 ].mn );
}
void gett( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
an.mx = max( an.mx, t[ pos ][ v ].mx );
an.mn = min( an.mn, t[ pos ][ v ].mn );
return;
}
pus( v );
int mid = ( tl + tr ) / 2;
gett( v + v, tl, mid, l, r );
gett( v + v + 1, mid + 1, tr, l, r );
}
void lca( int l, int r, int b ) {
ans = 0;
num = 0;
for ( int i = 19; i >= 0; i -- ) {
an.clear();
pos = i;
gett( 1, 1, n, l, r );
if ( an.mx < b || an.mn > b ) {
l = min( an.mn, l );
r = max( an.mx, r );
// cout << i << ", ";
ans += ( 1ll << i );
}
}
ans ++;
return;
}
main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
cin >> m;
for ( int i = 1; i <= m; i ++ ) {
cin >> a[ i ] >> b[ i ];
}
cin >> q;
for ( int i = 0; i < 20; i ++ )
pos = i, build( 1, 1, n );
for ( int i = 1; i <= q; i ++ ) {
cin >> s[ i ] >> f[ i ];
}
// if ( n <= 2000 && m <= 2000 && q <= 2000 ) {
//
// }
for ( int i = 1; i <= m; i ++ ) {
if ( a[ i ] > b[ i ] ) {
num = b[ i ];
pos = 0;
updd( 1, 1, n, a[ i ] - k + 1, a[ i ] );
}
else {
num = b[ i ];
pos = 0;
upd( 1, 1, n, a[ i ], a[ i ] + k - 1 );
}
}
int l = n + 1, r = 0;
for ( int i = 1; i < 20; i ++ ) {
for ( int j = 1; j <= n; j ++ ) {
an.clear();
pos = i - 1;
gett( 1, 1, n, j, j );
l = an.mn;
r = an.mx;
if ( !r )
r = j;
if ( l == n + 1 )
l = j;
an.clear();
gett( 1, 1, n, l, r );
num = an.mx;
pos = i;
upd( 1, 1, n, j, j );
num = an.mn;
updd( 1, 1, n, j, j );
}
}
// for ( int i = 0; i < 20; i ++ ) {
// for ( int j = 1; j <= n; j ++ ) {
// pos = i;
// an.clear();
// gett( 1, 1, n, j, j );
// cout << j << " " << i << " - " << an.mn << " " << an.mx << endl;
// }
// }
for ( int i = 1; i <= q; i ++ ) {
if ( s[ i ] == f[ i ] ) {
cout << "0\n";
continue;
}
lca( s[ i ], s[ i ], f[ i ] );
if ( ans == ( 1 << 20 ) )
cout << "-1\n";
else
cout << ans << "\n";
}
}
Compilation message (stderr)
Main.cpp:154:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
154 | main () {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |