#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
int n , q , S[MAXN] , E[MAXN] , nxt[LOG][MAXN];
vector<pair<pii , int>> stk;
vector<int> vec;
int cmp(int x , int y){
if(E[x] != E[y]) return E[x] < E[y];
return S[x] < S[y];
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> q;
for(int i = 1 ; i <= n ; i++){
cin >> S[i] >> E[i];
vec.push_back(i);
}
sort(all(vec) , cmp);
for(auto& i : vec){
while(SZ(stk) && S[i] <= stk.back().X.Y){
stk.pop_back();
}
stk.push_back({{E[i] , S[i]} , i});
nxt[0][i] = lower_bound(all(stk) , pair<pii , int>({{S[i] , -MOD} , -MOD})) -> Y;
//cout << i << sep << nxt[0][i] << endl;
}
for(int i = 1 ; i < LOG ; i++){
for(int j = 1 ; j <= n ; j++){
nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
}
}
while(q--){
int v , u;
cin >> v >> u;
if(E[u] < E[v]){
cout << "impossible" << endl;
continue;
}
if(v == u){
cout << 0 << endl;
continue;
}
if(S[u] <= E[v]){
cout << 1 << endl;
continue;
}
int ans = 0;
for(int i = LOG - 1 ; i >= 0 ; i--){
if(S[nxt[i][u]] > E[v]){
u = nxt[i][u];
ans += (1 << i);
}
}
if(E[v] < S[nxt[0][u]]){
cout << "impossible" << endl;
continue;
}
cout << ans + 2 << endl;
}
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
68 ms |
12600 KB |
Output is correct |
3 |
Correct |
89 ms |
12912 KB |
Output is correct |
4 |
Correct |
123 ms |
13044 KB |
Output is correct |
5 |
Correct |
94 ms |
12808 KB |
Output is correct |
6 |
Correct |
102 ms |
12852 KB |
Output is correct |
7 |
Correct |
92 ms |
12628 KB |
Output is correct |
8 |
Correct |
121 ms |
13568 KB |
Output is correct |
9 |
Correct |
112 ms |
13616 KB |
Output is correct |
10 |
Correct |
97 ms |
13496 KB |
Output is correct |
11 |
Correct |
99 ms |
13040 KB |
Output is correct |
12 |
Correct |
61 ms |
12596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
612 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 |
33 ms |
1796 KB |
Output is correct |
20 |
Correct |
34 ms |
1684 KB |
Output is correct |
21 |
Correct |
33 ms |
1908 KB |
Output is correct |
22 |
Correct |
29 ms |
2060 KB |
Output is correct |
23 |
Correct |
23 ms |
1744 KB |
Output is correct |
24 |
Correct |
22 ms |
1936 KB |
Output is correct |
25 |
Correct |
26 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
596 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 |
596 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 |
612 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 |
43 ms |
12612 KB |
Output is correct |
20 |
Correct |
37 ms |
12184 KB |
Output is correct |
21 |
Correct |
43 ms |
10740 KB |
Output is correct |
22 |
Correct |
41 ms |
11620 KB |
Output is correct |
23 |
Correct |
44 ms |
12280 KB |
Output is correct |
24 |
Correct |
40 ms |
12548 KB |
Output is correct |
25 |
Correct |
26 ms |
10500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
12768 KB |
Output is correct |
2 |
Correct |
90 ms |
12684 KB |
Output is correct |
3 |
Correct |
141 ms |
12776 KB |
Output is correct |
4 |
Correct |
116 ms |
13076 KB |
Output is correct |
5 |
Correct |
102 ms |
13028 KB |
Output is correct |
6 |
Correct |
97 ms |
13072 KB |
Output is correct |
7 |
Correct |
91 ms |
12868 KB |
Output is correct |
8 |
Correct |
89 ms |
13040 KB |
Output is correct |
9 |
Correct |
47 ms |
12140 KB |
Output is correct |
10 |
Correct |
92 ms |
12900 KB |
Output is correct |
11 |
Correct |
78 ms |
12612 KB |
Output is correct |
12 |
Correct |
83 ms |
12484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
68 ms |
12600 KB |
Output is correct |
3 |
Correct |
89 ms |
12912 KB |
Output is correct |
4 |
Correct |
123 ms |
13044 KB |
Output is correct |
5 |
Correct |
94 ms |
12808 KB |
Output is correct |
6 |
Correct |
102 ms |
12852 KB |
Output is correct |
7 |
Correct |
92 ms |
12628 KB |
Output is correct |
8 |
Correct |
121 ms |
13568 KB |
Output is correct |
9 |
Correct |
112 ms |
13616 KB |
Output is correct |
10 |
Correct |
97 ms |
13496 KB |
Output is correct |
11 |
Correct |
99 ms |
13040 KB |
Output is correct |
12 |
Correct |
61 ms |
12596 KB |
Output is correct |
13 |
Correct |
0 ms |
468 KB |
Output is correct |
14 |
Correct |
0 ms |
468 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 |
1 ms |
596 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 |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
612 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 |
33 ms |
1796 KB |
Output is correct |
32 |
Correct |
34 ms |
1684 KB |
Output is correct |
33 |
Correct |
33 ms |
1908 KB |
Output is correct |
34 |
Correct |
29 ms |
2060 KB |
Output is correct |
35 |
Correct |
23 ms |
1744 KB |
Output is correct |
36 |
Correct |
22 ms |
1936 KB |
Output is correct |
37 |
Correct |
26 ms |
1484 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 |
612 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
43 ms |
12612 KB |
Output is correct |
48 |
Correct |
37 ms |
12184 KB |
Output is correct |
49 |
Correct |
43 ms |
10740 KB |
Output is correct |
50 |
Correct |
41 ms |
11620 KB |
Output is correct |
51 |
Correct |
44 ms |
12280 KB |
Output is correct |
52 |
Correct |
40 ms |
12548 KB |
Output is correct |
53 |
Correct |
26 ms |
10500 KB |
Output is correct |
54 |
Correct |
67 ms |
12768 KB |
Output is correct |
55 |
Correct |
90 ms |
12684 KB |
Output is correct |
56 |
Correct |
141 ms |
12776 KB |
Output is correct |
57 |
Correct |
116 ms |
13076 KB |
Output is correct |
58 |
Correct |
102 ms |
13028 KB |
Output is correct |
59 |
Correct |
97 ms |
13072 KB |
Output is correct |
60 |
Correct |
91 ms |
12868 KB |
Output is correct |
61 |
Correct |
89 ms |
13040 KB |
Output is correct |
62 |
Correct |
47 ms |
12140 KB |
Output is correct |
63 |
Correct |
92 ms |
12900 KB |
Output is correct |
64 |
Correct |
78 ms |
12612 KB |
Output is correct |
65 |
Correct |
83 ms |
12484 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
80 ms |
12772 KB |
Output is correct |
68 |
Correct |
104 ms |
13008 KB |
Output is correct |
69 |
Correct |
120 ms |
12656 KB |
Output is correct |
70 |
Correct |
95 ms |
13092 KB |
Output is correct |
71 |
Correct |
114 ms |
12708 KB |
Output is correct |
72 |
Correct |
90 ms |
13108 KB |
Output is correct |
73 |
Correct |
110 ms |
13200 KB |
Output is correct |
74 |
Correct |
124 ms |
13272 KB |
Output is correct |
75 |
Correct |
105 ms |
13192 KB |
Output is correct |
76 |
Correct |
93 ms |
12488 KB |
Output is correct |
77 |
Correct |
55 ms |
12456 KB |
Output is correct |
78 |
Correct |
0 ms |
468 KB |
Output is correct |
79 |
Correct |
1 ms |
596 KB |
Output is correct |
80 |
Correct |
2 ms |
596 KB |
Output is correct |
81 |
Correct |
1 ms |
596 KB |
Output is correct |
82 |
Correct |
1 ms |
596 KB |
Output is correct |
83 |
Correct |
1 ms |
616 KB |
Output is correct |
84 |
Correct |
1 ms |
620 KB |
Output is correct |
85 |
Correct |
1 ms |
596 KB |
Output is correct |
86 |
Correct |
32 ms |
1620 KB |
Output is correct |
87 |
Correct |
32 ms |
1612 KB |
Output is correct |
88 |
Correct |
34 ms |
1964 KB |
Output is correct |
89 |
Correct |
36 ms |
1996 KB |
Output is correct |
90 |
Correct |
25 ms |
1720 KB |
Output is correct |
91 |
Correct |
23 ms |
1828 KB |
Output is correct |
92 |
Correct |
27 ms |
1400 KB |
Output is correct |
93 |
Correct |
46 ms |
11936 KB |
Output is correct |
94 |
Correct |
40 ms |
11852 KB |
Output is correct |
95 |
Correct |
46 ms |
10548 KB |
Output is correct |
96 |
Correct |
41 ms |
11524 KB |
Output is correct |
97 |
Correct |
42 ms |
12220 KB |
Output is correct |
98 |
Correct |
40 ms |
11980 KB |
Output is correct |
99 |
Correct |
26 ms |
10572 KB |
Output is correct |
100 |
Correct |
112 ms |
12956 KB |
Output is correct |
101 |
Correct |
90 ms |
13316 KB |
Output is correct |
102 |
Correct |
94 ms |
13440 KB |
Output is correct |
103 |
Correct |
89 ms |
12940 KB |
Output is correct |
104 |
Correct |
85 ms |
12944 KB |
Output is correct |
105 |
Correct |
80 ms |
12932 KB |
Output is correct |
106 |
Correct |
90 ms |
12924 KB |
Output is correct |
107 |
Correct |
106 ms |
13340 KB |
Output is correct |
108 |
Correct |
100 ms |
11864 KB |
Output is correct |
109 |
Correct |
89 ms |
11884 KB |
Output is correct |
110 |
Correct |
68 ms |
11768 KB |
Output is correct |
111 |
Correct |
64 ms |
11356 KB |
Output is correct |