/* Ignore Others Only Focus On Yourself! */
/* Im the Best! */
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define Mp make_pair
#define point complex
#define endl '\n'
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file_io freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
#define mashtali return cout << "Hello, World!", 0;
const int N = 1e6 + 10;
const int LOG = 20;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const double pi = acos(-1);
const ld eps = 1e-18;
const ld one = 1.;
ll pw(ll a, ll b, ll M, ll ret = 1) { if(a == 0) return 0; a %= M; while(b) { ret = (b & 1? ret * a % M : ret), a = a * a % M, b >>= 1; } return ret % M; }
mt19937 rng(time(nullptr));
int n, q, S[N], E[N], id[N], nxt[LOG][N];
bool cmp(int i, int j)
{
return (E[i] != E[j]? E[i] < E[j] : S[i] < S[j]);
}
int main()
{
fast_io;
cin >> n >> q;
for(int i = 1; i <= n; i ++)
{
cin >> S[i] >> E[i];
id[i] = i;
}
sort(id + 1, id + n + 1, cmp);
vector < pii > vec;
for(int i2 = 1; i2 <= n; i2 ++)
{
int i = id[i2];
///printf("i = %d\n", i);
while(SZ(vec) && S[i] <= S[vec.back().S])
{
vec.pop_back();
}
vec.push_back(Mp(E[i], i));
nxt[0][i] = vec[lower_bound(all(vec), Mp(S[i], -1)) - vec.begin()].S;
///printf("nxt0 = %d\n", nxt[0][i]);
}
for(int T = 1; T < LOG; T ++)
{
for(int i = 1; i <= n; i ++)
{
nxt[T][i] = nxt[T - 1][nxt[T - 1][i]];
}
}
while(q --)
{
int a, b;
cin >> a >> b;
if(E[a] > E[b])
{
cout << "impossible\n";
continue;
}
if(a == b)
{
cout << 0 << endl;
continue;
}
if(S[b] <= E[a])
{
cout << 1 << endl;
continue;
}
int tot = 0;
for(int T = LOG - 1; ~T; T --)
{
if(E[a] < S[nxt[T][b]])
{
tot ^= 1 << T;
b = nxt[T][b];
}
}
if(S[nxt[0][b]] > E[a])
{
cout << "impossible\n";
continue;
}
cout << tot + 2 << endl;
}
return 0;
}
/* check corner case(n = 1?), watch for negetive index or overflow */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
64 ms |
11272 KB |
Output is correct |
3 |
Correct |
85 ms |
11552 KB |
Output is correct |
4 |
Correct |
123 ms |
11476 KB |
Output is correct |
5 |
Correct |
90 ms |
11256 KB |
Output is correct |
6 |
Correct |
90 ms |
11464 KB |
Output is correct |
7 |
Correct |
95 ms |
11256 KB |
Output is correct |
8 |
Correct |
95 ms |
12044 KB |
Output is correct |
9 |
Correct |
106 ms |
12192 KB |
Output is correct |
10 |
Correct |
113 ms |
11868 KB |
Output is correct |
11 |
Correct |
102 ms |
11544 KB |
Output is correct |
12 |
Correct |
53 ms |
11368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
30 ms |
1660 KB |
Output is correct |
20 |
Correct |
32 ms |
1620 KB |
Output is correct |
21 |
Correct |
32 ms |
1804 KB |
Output is correct |
22 |
Correct |
27 ms |
1952 KB |
Output is correct |
23 |
Correct |
23 ms |
1652 KB |
Output is correct |
24 |
Correct |
23 ms |
1996 KB |
Output is correct |
25 |
Correct |
26 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
496 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
628 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
508 KB |
Output is correct |
17 |
Correct |
1 ms |
496 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
39 ms |
11388 KB |
Output is correct |
20 |
Correct |
34 ms |
10696 KB |
Output is correct |
21 |
Correct |
40 ms |
9812 KB |
Output is correct |
22 |
Correct |
43 ms |
10384 KB |
Output is correct |
23 |
Correct |
41 ms |
10944 KB |
Output is correct |
24 |
Correct |
39 ms |
10948 KB |
Output is correct |
25 |
Correct |
26 ms |
9508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
11280 KB |
Output is correct |
2 |
Correct |
85 ms |
11232 KB |
Output is correct |
3 |
Correct |
134 ms |
11188 KB |
Output is correct |
4 |
Correct |
94 ms |
11556 KB |
Output is correct |
5 |
Correct |
91 ms |
11552 KB |
Output is correct |
6 |
Correct |
95 ms |
11428 KB |
Output is correct |
7 |
Correct |
90 ms |
11420 KB |
Output is correct |
8 |
Correct |
79 ms |
11648 KB |
Output is correct |
9 |
Correct |
49 ms |
10640 KB |
Output is correct |
10 |
Correct |
87 ms |
11336 KB |
Output is correct |
11 |
Correct |
74 ms |
11124 KB |
Output is correct |
12 |
Correct |
78 ms |
10948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
64 ms |
11272 KB |
Output is correct |
3 |
Correct |
85 ms |
11552 KB |
Output is correct |
4 |
Correct |
123 ms |
11476 KB |
Output is correct |
5 |
Correct |
90 ms |
11256 KB |
Output is correct |
6 |
Correct |
90 ms |
11464 KB |
Output is correct |
7 |
Correct |
95 ms |
11256 KB |
Output is correct |
8 |
Correct |
95 ms |
12044 KB |
Output is correct |
9 |
Correct |
106 ms |
12192 KB |
Output is correct |
10 |
Correct |
113 ms |
11868 KB |
Output is correct |
11 |
Correct |
102 ms |
11544 KB |
Output is correct |
12 |
Correct |
53 ms |
11368 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
30 ms |
1660 KB |
Output is correct |
32 |
Correct |
32 ms |
1620 KB |
Output is correct |
33 |
Correct |
32 ms |
1804 KB |
Output is correct |
34 |
Correct |
27 ms |
1952 KB |
Output is correct |
35 |
Correct |
23 ms |
1652 KB |
Output is correct |
36 |
Correct |
23 ms |
1996 KB |
Output is correct |
37 |
Correct |
26 ms |
1356 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
496 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
628 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
508 KB |
Output is correct |
45 |
Correct |
1 ms |
496 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
39 ms |
11388 KB |
Output is correct |
48 |
Correct |
34 ms |
10696 KB |
Output is correct |
49 |
Correct |
40 ms |
9812 KB |
Output is correct |
50 |
Correct |
43 ms |
10384 KB |
Output is correct |
51 |
Correct |
41 ms |
10944 KB |
Output is correct |
52 |
Correct |
39 ms |
10948 KB |
Output is correct |
53 |
Correct |
26 ms |
9508 KB |
Output is correct |
54 |
Correct |
68 ms |
11280 KB |
Output is correct |
55 |
Correct |
85 ms |
11232 KB |
Output is correct |
56 |
Correct |
134 ms |
11188 KB |
Output is correct |
57 |
Correct |
94 ms |
11556 KB |
Output is correct |
58 |
Correct |
91 ms |
11552 KB |
Output is correct |
59 |
Correct |
95 ms |
11428 KB |
Output is correct |
60 |
Correct |
90 ms |
11420 KB |
Output is correct |
61 |
Correct |
79 ms |
11648 KB |
Output is correct |
62 |
Correct |
49 ms |
10640 KB |
Output is correct |
63 |
Correct |
87 ms |
11336 KB |
Output is correct |
64 |
Correct |
74 ms |
11124 KB |
Output is correct |
65 |
Correct |
78 ms |
10948 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
69 ms |
11272 KB |
Output is correct |
68 |
Correct |
86 ms |
11532 KB |
Output is correct |
69 |
Correct |
112 ms |
11296 KB |
Output is correct |
70 |
Correct |
106 ms |
11536 KB |
Output is correct |
71 |
Correct |
106 ms |
11280 KB |
Output is correct |
72 |
Correct |
88 ms |
11588 KB |
Output is correct |
73 |
Correct |
97 ms |
11844 KB |
Output is correct |
74 |
Correct |
103 ms |
11880 KB |
Output is correct |
75 |
Correct |
85 ms |
11728 KB |
Output is correct |
76 |
Correct |
92 ms |
11196 KB |
Output is correct |
77 |
Correct |
55 ms |
11208 KB |
Output is correct |
78 |
Correct |
0 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
596 KB |
Output is correct |
80 |
Correct |
1 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
620 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
1 ms |
596 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
31 ms |
1560 KB |
Output is correct |
87 |
Correct |
30 ms |
1516 KB |
Output is correct |
88 |
Correct |
31 ms |
1868 KB |
Output is correct |
89 |
Correct |
28 ms |
1980 KB |
Output is correct |
90 |
Correct |
25 ms |
1656 KB |
Output is correct |
91 |
Correct |
23 ms |
1700 KB |
Output is correct |
92 |
Correct |
40 ms |
1356 KB |
Output is correct |
93 |
Correct |
40 ms |
10564 KB |
Output is correct |
94 |
Correct |
37 ms |
10432 KB |
Output is correct |
95 |
Correct |
41 ms |
9664 KB |
Output is correct |
96 |
Correct |
43 ms |
10216 KB |
Output is correct |
97 |
Correct |
37 ms |
10860 KB |
Output is correct |
98 |
Correct |
38 ms |
10580 KB |
Output is correct |
99 |
Correct |
30 ms |
9660 KB |
Output is correct |
100 |
Correct |
106 ms |
11492 KB |
Output is correct |
101 |
Correct |
95 ms |
11576 KB |
Output is correct |
102 |
Correct |
81 ms |
12300 KB |
Output is correct |
103 |
Correct |
89 ms |
11828 KB |
Output is correct |
104 |
Correct |
81 ms |
11588 KB |
Output is correct |
105 |
Correct |
77 ms |
11856 KB |
Output is correct |
106 |
Correct |
111 ms |
11936 KB |
Output is correct |
107 |
Correct |
127 ms |
12220 KB |
Output is correct |
108 |
Correct |
90 ms |
11496 KB |
Output is correct |
109 |
Correct |
85 ms |
11276 KB |
Output is correct |
110 |
Correct |
64 ms |
10604 KB |
Output is correct |
111 |
Correct |
65 ms |
10512 KB |
Output is correct |