#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, q, x, y, l[ 100001 ], r[ 200001 ];
int mp[ 100001 ];
int t[ 800001 ];
struct Node {
int mn, i;
Node operator + ( Node a ) {
Node c;
if ( mn < a.mn )
c.mn = mn, c.i = i;
else
c = a;
return c;
}
} tt[ 800001 ], an;
vector <pair<pair<int, int> , int> > vv;
vector <int> v[ 100001 ];
int pr[ 20 ][ 100001 ];
int tin[ 100001 ], tout[ 100001 ], tmr;
int used[ 100001 ];
vector <int> g;
map <int, int> mpp;
set <pair<int, int> > st;
void upd1( int v, int tl, int tr, int pos ) {
if ( tl > pos || tr < pos )
return;
if ( tl == tr ) {
if ( tt[ v ].mn > an.mn )
tt[ v ] = an;
return;
}
int mid = ( tl + tr ) / 2;
upd1( v + v, tl, mid, pos );
upd1( v + v + 1, mid + 1, tr, pos );
tt[ v ] = tt[ v + v ] + tt[ v + v + 1 ];
}
void gett1( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
an = an + tt[ v ];
return;
}
int mid = ( tl + tr ) / 2;
gett1( v + v, tl, mid, l, r ) ;
gett1( v + v + 1, mid + 1, tr, l, r );
}
void build( int v, int tl, int tr ) {
t[ v ] = oo;
tt[ v ].mn = oo;
if ( tl == tr )
return;
int mid = ( tl + tr ) / 2;
build( v + v, tl, mid );
build( v + v + 1, mid + 1, tr );
}
void upd( int v, int tl, int tr, int pos, int x ) {
if ( tl > pos || tr < pos )
return;
if ( tl == tr ) {
t[ v ] = min( t[ v ], x );
return;
}
int mid = ( tl + tr ) / 2;
upd( v + v, tl, mid, pos, x );
upd( v + v + 1, mid + 1, tr, pos, x );
t[ v ] = min( t[ v + v ], t[ v + v + 1 ] );
}
void gett( int v, int tl, int tr, int l, int r ) {
if ( tl > r || tr < l )
return;
if ( tl >= l && tr <= r ) {
num = min( num, t[ v ] );
return;
}
int mid = ( tl + tr ) / 2;
gett( v + v, tl, mid, l, r );
gett( v + v + 1, mid + 1, tr, l, r );
}
void dfs( int u, int pr ) {
::pr[ 0 ][ u ] = pr;
used[ u] = 1;
tin[ u ] = ++tmr;
for ( int i = 1; i < 20; i ++ )
::pr[ i ][ u ] = ::pr[ i - 1 ][ ::pr[ i - 1 ][ u ] ];
for ( auto to : v[ u ] ) {
if ( to != pr )
dfs( to, u );
}
tout[ u ] = tmr;
}
bool ok( int a, int b ) {
return ( tin[ a ] <= tin[ b ] && tout[ a ] >= tout[ b ] );
}
int bin( int a, int b ) {
for ( int i = 19; i >= 0; i -- ) {
if ( r[ b ] < l[ pr[ i ][ a ] ] ) {
a = pr[ i ][ a ];
ans += ( 1 << i );
}
}
return a;
}
main () {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
build( 1, 1, n + n );
for ( int i = 1; i <= n; i ++ ) {
cin >> l[ i ] >> r[ i ];
g.push_back( l[ i ] );
g.push_back( r[ i ] );
}
sort( g.begin(), g.end() );
for ( auto i : g ) {
if ( !mpp[ i ] ) {
mpp[ i ] = ++num;
}
}
for ( int i = 1; i <= n; i ++ ) {
l[ i ] = mpp[ l[ i ] ];
r[ i ] = mpp[ r[ i ] ];
upd( 1, 1, n + n, r[ i ], l[ i ] );
}
for ( int i = 1; i <= n; i ++ ) {
vv.push_back( { make_pair( r[ i ], l[ i ] ), i } );
}
sort( vv.begin(), vv.end() );
for ( int i = 0; i < n; i ++ ) {
mp[ vv[ i ].second ] = i + 1;
l[ i + 1 ] = vv[ i ].first.second;
r[ i + 1 ] = vv[ i ].first.first;
}
for ( int i = 1; i <= n; i ++ ) {
an.mn = oo;
gett1( 1, 1, n + n, l[ i ], r[ i ] );
if ( an.mn < l[ i ] ) {
v[ an.i ].push_back( i );
}
an = { l[ i ], i };
upd1( 1, 1, n + n, r[ i ] );
}
for ( int i = 1; i <= n; i ++ ) {
if ( !used[ i ] ) {
dfs( i, i );
}
}
int x, L, R;
while ( q -- ) {
cin >> x >> y;
x = mp[ x ];
y = mp[ y ];
if ( x == y ) {
cout << "0\n";
continue;
}
if ( l[ y ] <= r[ x ] && r[ x ] <= r[ y ] ) {
cout << "1\n";
continue;
}
if ( y < x ) {
cout << "impossible\n";
continue;
}
ans = 2;
y = bin( y, x );
num = oo;
gett( 1, 1, n + n, l[ y ], r[ y ] );
if ( num <= r[ x ] )
cout << ans << "\n";
else
cout << "impossible\n";
}
}
Compilation message
events.cpp:161:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
161 | main () {
| ^~~~
events.cpp: In function 'int main()':
events.cpp:205:9: warning: unused variable 'L' [-Wunused-variable]
205 | int x, L, R;
| ^
events.cpp:205:12: warning: unused variable 'R' [-Wunused-variable]
205 | int x, L, R;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
267 ms |
53952 KB |
Output is correct |
3 |
Correct |
246 ms |
53836 KB |
Output is correct |
4 |
Correct |
253 ms |
53900 KB |
Output is correct |
5 |
Correct |
286 ms |
53824 KB |
Output is correct |
6 |
Correct |
256 ms |
56876 KB |
Output is correct |
7 |
Correct |
255 ms |
56828 KB |
Output is correct |
8 |
Correct |
288 ms |
57444 KB |
Output is correct |
9 |
Correct |
312 ms |
57572 KB |
Output is correct |
10 |
Correct |
278 ms |
55056 KB |
Output is correct |
11 |
Correct |
323 ms |
55776 KB |
Output is correct |
12 |
Correct |
166 ms |
50460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
3 ms |
3284 KB |
Output is correct |
4 |
Correct |
5 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3284 KB |
Output is correct |
6 |
Correct |
3 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3284 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
3 ms |
3284 KB |
Output is correct |
4 |
Correct |
5 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3284 KB |
Output is correct |
6 |
Correct |
3 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3284 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3212 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
3 ms |
3224 KB |
Output is correct |
13 |
Correct |
3 ms |
3284 KB |
Output is correct |
14 |
Correct |
2 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3156 KB |
Output is correct |
16 |
Correct |
4 ms |
3284 KB |
Output is correct |
17 |
Correct |
4 ms |
3332 KB |
Output is correct |
18 |
Correct |
2 ms |
3156 KB |
Output is correct |
19 |
Correct |
45 ms |
6924 KB |
Output is correct |
20 |
Correct |
51 ms |
6976 KB |
Output is correct |
21 |
Correct |
43 ms |
7188 KB |
Output is correct |
22 |
Correct |
48 ms |
7012 KB |
Output is correct |
23 |
Correct |
37 ms |
7308 KB |
Output is correct |
24 |
Correct |
30 ms |
7204 KB |
Output is correct |
25 |
Correct |
48 ms |
6844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
3 ms |
3284 KB |
Output is correct |
4 |
Correct |
5 ms |
3156 KB |
Output is correct |
5 |
Correct |
3 ms |
3284 KB |
Output is correct |
6 |
Correct |
3 ms |
3156 KB |
Output is correct |
7 |
Correct |
4 ms |
3284 KB |
Output is correct |
8 |
Correct |
3 ms |
3284 KB |
Output is correct |
9 |
Correct |
3 ms |
3212 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
4 ms |
3220 KB |
Output is correct |
13 |
Correct |
3 ms |
3284 KB |
Output is correct |
14 |
Correct |
3 ms |
3284 KB |
Output is correct |
15 |
Correct |
3 ms |
3156 KB |
Output is correct |
16 |
Correct |
4 ms |
3284 KB |
Output is correct |
17 |
Correct |
3 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
3156 KB |
Output is correct |
19 |
Correct |
221 ms |
55224 KB |
Output is correct |
20 |
Correct |
219 ms |
54384 KB |
Output is correct |
21 |
Correct |
232 ms |
48904 KB |
Output is correct |
22 |
Correct |
244 ms |
53368 KB |
Output is correct |
23 |
Correct |
295 ms |
55524 KB |
Output is correct |
24 |
Correct |
299 ms |
56780 KB |
Output is correct |
25 |
Correct |
77 ms |
42000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
53948 KB |
Output is correct |
2 |
Correct |
258 ms |
53840 KB |
Output is correct |
3 |
Correct |
281 ms |
53972 KB |
Output is correct |
4 |
Correct |
282 ms |
54460 KB |
Output is correct |
5 |
Correct |
295 ms |
51948 KB |
Output is correct |
6 |
Correct |
412 ms |
55824 KB |
Output is correct |
7 |
Correct |
407 ms |
58680 KB |
Output is correct |
8 |
Correct |
367 ms |
57636 KB |
Output is correct |
9 |
Correct |
305 ms |
55480 KB |
Output is correct |
10 |
Correct |
366 ms |
58420 KB |
Output is correct |
11 |
Correct |
344 ms |
57684 KB |
Output is correct |
12 |
Correct |
362 ms |
58356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2772 KB |
Output is correct |
2 |
Correct |
267 ms |
53952 KB |
Output is correct |
3 |
Correct |
246 ms |
53836 KB |
Output is correct |
4 |
Correct |
253 ms |
53900 KB |
Output is correct |
5 |
Correct |
286 ms |
53824 KB |
Output is correct |
6 |
Correct |
256 ms |
56876 KB |
Output is correct |
7 |
Correct |
255 ms |
56828 KB |
Output is correct |
8 |
Correct |
288 ms |
57444 KB |
Output is correct |
9 |
Correct |
312 ms |
57572 KB |
Output is correct |
10 |
Correct |
278 ms |
55056 KB |
Output is correct |
11 |
Correct |
323 ms |
55776 KB |
Output is correct |
12 |
Correct |
166 ms |
50460 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
3284 KB |
Output is correct |
16 |
Correct |
5 ms |
3156 KB |
Output is correct |
17 |
Correct |
3 ms |
3284 KB |
Output is correct |
18 |
Correct |
3 ms |
3156 KB |
Output is correct |
19 |
Correct |
4 ms |
3284 KB |
Output is correct |
20 |
Correct |
3 ms |
3284 KB |
Output is correct |
21 |
Correct |
3 ms |
3212 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
3224 KB |
Output is correct |
25 |
Correct |
3 ms |
3284 KB |
Output is correct |
26 |
Correct |
2 ms |
3284 KB |
Output is correct |
27 |
Correct |
3 ms |
3156 KB |
Output is correct |
28 |
Correct |
4 ms |
3284 KB |
Output is correct |
29 |
Correct |
4 ms |
3332 KB |
Output is correct |
30 |
Correct |
2 ms |
3156 KB |
Output is correct |
31 |
Correct |
45 ms |
6924 KB |
Output is correct |
32 |
Correct |
51 ms |
6976 KB |
Output is correct |
33 |
Correct |
43 ms |
7188 KB |
Output is correct |
34 |
Correct |
48 ms |
7012 KB |
Output is correct |
35 |
Correct |
37 ms |
7308 KB |
Output is correct |
36 |
Correct |
30 ms |
7204 KB |
Output is correct |
37 |
Correct |
48 ms |
6844 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2772 KB |
Output is correct |
40 |
Correct |
4 ms |
3220 KB |
Output is correct |
41 |
Correct |
3 ms |
3284 KB |
Output is correct |
42 |
Correct |
3 ms |
3284 KB |
Output is correct |
43 |
Correct |
3 ms |
3156 KB |
Output is correct |
44 |
Correct |
4 ms |
3284 KB |
Output is correct |
45 |
Correct |
3 ms |
3284 KB |
Output is correct |
46 |
Correct |
2 ms |
3156 KB |
Output is correct |
47 |
Correct |
221 ms |
55224 KB |
Output is correct |
48 |
Correct |
219 ms |
54384 KB |
Output is correct |
49 |
Correct |
232 ms |
48904 KB |
Output is correct |
50 |
Correct |
244 ms |
53368 KB |
Output is correct |
51 |
Correct |
295 ms |
55524 KB |
Output is correct |
52 |
Correct |
299 ms |
56780 KB |
Output is correct |
53 |
Correct |
77 ms |
42000 KB |
Output is correct |
54 |
Correct |
242 ms |
53948 KB |
Output is correct |
55 |
Correct |
258 ms |
53840 KB |
Output is correct |
56 |
Correct |
281 ms |
53972 KB |
Output is correct |
57 |
Correct |
282 ms |
54460 KB |
Output is correct |
58 |
Correct |
295 ms |
51948 KB |
Output is correct |
59 |
Correct |
412 ms |
55824 KB |
Output is correct |
60 |
Correct |
407 ms |
58680 KB |
Output is correct |
61 |
Correct |
367 ms |
57636 KB |
Output is correct |
62 |
Correct |
305 ms |
55480 KB |
Output is correct |
63 |
Correct |
366 ms |
58420 KB |
Output is correct |
64 |
Correct |
344 ms |
57684 KB |
Output is correct |
65 |
Correct |
362 ms |
58356 KB |
Output is correct |
66 |
Correct |
2 ms |
2772 KB |
Output is correct |
67 |
Correct |
253 ms |
57024 KB |
Output is correct |
68 |
Correct |
279 ms |
57040 KB |
Output is correct |
69 |
Correct |
263 ms |
57096 KB |
Output is correct |
70 |
Correct |
278 ms |
56972 KB |
Output is correct |
71 |
Correct |
264 ms |
56768 KB |
Output is correct |
72 |
Correct |
276 ms |
56884 KB |
Output is correct |
73 |
Correct |
288 ms |
57512 KB |
Output is correct |
74 |
Correct |
310 ms |
57468 KB |
Output is correct |
75 |
Correct |
285 ms |
55068 KB |
Output is correct |
76 |
Correct |
351 ms |
55696 KB |
Output is correct |
77 |
Correct |
163 ms |
50508 KB |
Output is correct |
78 |
Correct |
2 ms |
2864 KB |
Output is correct |
79 |
Correct |
4 ms |
3284 KB |
Output is correct |
80 |
Correct |
3 ms |
3284 KB |
Output is correct |
81 |
Correct |
3 ms |
3284 KB |
Output is correct |
82 |
Correct |
3 ms |
3156 KB |
Output is correct |
83 |
Correct |
3 ms |
3336 KB |
Output is correct |
84 |
Correct |
3 ms |
3284 KB |
Output is correct |
85 |
Correct |
3 ms |
3156 KB |
Output is correct |
86 |
Correct |
48 ms |
6888 KB |
Output is correct |
87 |
Correct |
42 ms |
6980 KB |
Output is correct |
88 |
Correct |
45 ms |
7180 KB |
Output is correct |
89 |
Correct |
42 ms |
7140 KB |
Output is correct |
90 |
Correct |
38 ms |
7152 KB |
Output is correct |
91 |
Correct |
30 ms |
7180 KB |
Output is correct |
92 |
Correct |
48 ms |
6868 KB |
Output is correct |
93 |
Correct |
217 ms |
55224 KB |
Output is correct |
94 |
Correct |
245 ms |
54476 KB |
Output is correct |
95 |
Correct |
228 ms |
48864 KB |
Output is correct |
96 |
Correct |
235 ms |
53504 KB |
Output is correct |
97 |
Correct |
322 ms |
55456 KB |
Output is correct |
98 |
Correct |
307 ms |
56732 KB |
Output is correct |
99 |
Correct |
79 ms |
42012 KB |
Output is correct |
100 |
Correct |
385 ms |
58844 KB |
Output is correct |
101 |
Correct |
397 ms |
58744 KB |
Output is correct |
102 |
Correct |
420 ms |
57596 KB |
Output is correct |
103 |
Correct |
370 ms |
58368 KB |
Output is correct |
104 |
Correct |
402 ms |
57644 KB |
Output is correct |
105 |
Correct |
359 ms |
58392 KB |
Output is correct |
106 |
Correct |
279 ms |
53336 KB |
Output is correct |
107 |
Correct |
327 ms |
56444 KB |
Output is correct |
108 |
Correct |
288 ms |
50884 KB |
Output is correct |
109 |
Correct |
277 ms |
50916 KB |
Output is correct |
110 |
Correct |
389 ms |
57024 KB |
Output is correct |
111 |
Correct |
422 ms |
57144 KB |
Output is correct |