#include <bits/stdc++.h>
//#pragma GCC target("sse,sse2,avx2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e5+20,mod = 1e9+7,inf = 1e9+10,sq = 500;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k >>= 1;
}
return z;
}
int s[N],e[N],sp[N][20],seg[N*4];
vector<pll> ve;
bool cmp(pll i,pll j){
if (e[i.Y] == e[j.Y]) return (s[i.Y] < s[j.Y]);
return (e[i.Y] < e[j.Y]);
}
void build(int l,int r,int v = 1){
if (r-l == 1){
seg[v] = ve[l].Y;
return;
}
int u = (v << 1),m = (l+r) >> 1;
build(l,m,u);
build(m,r,u|1);
if (s[seg[u]] < s[seg[u|1]]) seg[v] = seg[u];
else seg[v] = seg[u|1];
}
int que(int l,int r,int S,int E,int v = 1){
if (S == l && r == E) return seg[v];
int m = (l+r) >> 1,u = (v << 1);
if (E <= m) return que(l,m,S,E,u);
if (S >= m) return que(m,r,S,E,u|1);
int x = que(l,m,S,m,u),y = que(m,r,m,E,u|1);
if (s[x] < s[y]) return x;
return y;
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n,q;
cin >> n >> q;
rep(i,1,n+1){
cin >> s[i] >> e[i];
ve.pb({e[i],i});
}
sort(all(ve),cmp);
build(0,n,1);
rep(i,0,n){
int j = ve[i].Y;
int ind = lower_bound(all(ve),make_pair(s[j],0))-ve.begin();
sp[j][0] = que(0,n,ind,i+1,1);
}
rep(j,1,20){
rep(i,1,n+1){
sp[i][j] = sp[sp[i][j-1]][j-1];
}
}
while (q--){
int a,b;
cin >> a >> b;
if (a == b){
cout << 0 << endl;
continue;
}
if (e[a] > e[b] || s[sp[b][19]] > s[a]){
cout << "impossible" << endl;
continue;
}
if (e[a] == e[b]){
cout << 1 << endl;
continue;
}
int x = 0;
repr(i,19,0){
if (s[sp[b][i]] > e[a]){
x += (1 << i);
b = sp[b][i];
}
}
x++;
if (e[a] < s[b]) x++;
cout << x << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
90 ms |
14544 KB |
Output is correct |
3 |
Correct |
95 ms |
14552 KB |
Output is correct |
4 |
Correct |
118 ms |
14404 KB |
Output is correct |
5 |
Correct |
109 ms |
14572 KB |
Output is correct |
6 |
Correct |
98 ms |
14548 KB |
Output is correct |
7 |
Correct |
94 ms |
14664 KB |
Output is correct |
8 |
Correct |
79 ms |
15056 KB |
Output is correct |
9 |
Correct |
78 ms |
14876 KB |
Output is correct |
10 |
Correct |
102 ms |
14792 KB |
Output is correct |
11 |
Correct |
89 ms |
14912 KB |
Output is correct |
12 |
Correct |
66 ms |
14152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
368 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
368 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
356 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
30 ms |
2364 KB |
Output is correct |
20 |
Correct |
32 ms |
2380 KB |
Output is correct |
21 |
Correct |
32 ms |
2676 KB |
Output is correct |
22 |
Correct |
27 ms |
2636 KB |
Output is correct |
23 |
Correct |
26 ms |
2508 KB |
Output is correct |
24 |
Correct |
25 ms |
2516 KB |
Output is correct |
25 |
Correct |
26 ms |
2152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
368 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
368 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
59 ms |
12740 KB |
Output is correct |
20 |
Correct |
55 ms |
12008 KB |
Output is correct |
21 |
Correct |
71 ms |
12548 KB |
Output is correct |
22 |
Correct |
66 ms |
12712 KB |
Output is correct |
23 |
Correct |
56 ms |
12600 KB |
Output is correct |
24 |
Correct |
63 ms |
12752 KB |
Output is correct |
25 |
Correct |
45 ms |
12080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
14496 KB |
Output is correct |
2 |
Correct |
101 ms |
14536 KB |
Output is correct |
3 |
Correct |
119 ms |
14316 KB |
Output is correct |
4 |
Correct |
75 ms |
14768 KB |
Output is correct |
5 |
Correct |
109 ms |
14752 KB |
Output is correct |
6 |
Correct |
106 ms |
14616 KB |
Output is correct |
7 |
Correct |
109 ms |
14552 KB |
Output is correct |
8 |
Correct |
94 ms |
14752 KB |
Output is correct |
9 |
Correct |
59 ms |
12656 KB |
Output is correct |
10 |
Correct |
97 ms |
14220 KB |
Output is correct |
11 |
Correct |
96 ms |
14112 KB |
Output is correct |
12 |
Correct |
102 ms |
14152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
90 ms |
14544 KB |
Output is correct |
3 |
Correct |
95 ms |
14552 KB |
Output is correct |
4 |
Correct |
118 ms |
14404 KB |
Output is correct |
5 |
Correct |
109 ms |
14572 KB |
Output is correct |
6 |
Correct |
98 ms |
14548 KB |
Output is correct |
7 |
Correct |
94 ms |
14664 KB |
Output is correct |
8 |
Correct |
79 ms |
15056 KB |
Output is correct |
9 |
Correct |
78 ms |
14876 KB |
Output is correct |
10 |
Correct |
102 ms |
14792 KB |
Output is correct |
11 |
Correct |
89 ms |
14912 KB |
Output is correct |
12 |
Correct |
66 ms |
14152 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
368 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
356 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
30 ms |
2364 KB |
Output is correct |
32 |
Correct |
32 ms |
2380 KB |
Output is correct |
33 |
Correct |
32 ms |
2676 KB |
Output is correct |
34 |
Correct |
27 ms |
2636 KB |
Output is correct |
35 |
Correct |
26 ms |
2508 KB |
Output is correct |
36 |
Correct |
25 ms |
2516 KB |
Output is correct |
37 |
Correct |
26 ms |
2152 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
368 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
59 ms |
12740 KB |
Output is correct |
48 |
Correct |
55 ms |
12008 KB |
Output is correct |
49 |
Correct |
71 ms |
12548 KB |
Output is correct |
50 |
Correct |
66 ms |
12712 KB |
Output is correct |
51 |
Correct |
56 ms |
12600 KB |
Output is correct |
52 |
Correct |
63 ms |
12752 KB |
Output is correct |
53 |
Correct |
45 ms |
12080 KB |
Output is correct |
54 |
Correct |
83 ms |
14496 KB |
Output is correct |
55 |
Correct |
101 ms |
14536 KB |
Output is correct |
56 |
Correct |
119 ms |
14316 KB |
Output is correct |
57 |
Correct |
75 ms |
14768 KB |
Output is correct |
58 |
Correct |
109 ms |
14752 KB |
Output is correct |
59 |
Correct |
106 ms |
14616 KB |
Output is correct |
60 |
Correct |
109 ms |
14552 KB |
Output is correct |
61 |
Correct |
94 ms |
14752 KB |
Output is correct |
62 |
Correct |
59 ms |
12656 KB |
Output is correct |
63 |
Correct |
97 ms |
14220 KB |
Output is correct |
64 |
Correct |
96 ms |
14112 KB |
Output is correct |
65 |
Correct |
102 ms |
14152 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
82 ms |
14472 KB |
Output is correct |
68 |
Correct |
92 ms |
14484 KB |
Output is correct |
69 |
Correct |
143 ms |
14480 KB |
Output is correct |
70 |
Correct |
96 ms |
14552 KB |
Output is correct |
71 |
Correct |
100 ms |
14580 KB |
Output is correct |
72 |
Correct |
100 ms |
14616 KB |
Output is correct |
73 |
Correct |
75 ms |
14948 KB |
Output is correct |
74 |
Correct |
80 ms |
15024 KB |
Output is correct |
75 |
Correct |
103 ms |
14836 KB |
Output is correct |
76 |
Correct |
99 ms |
14920 KB |
Output is correct |
77 |
Correct |
65 ms |
14172 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
1 ms |
340 KB |
Output is correct |
83 |
Correct |
1 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
364 KB |
Output is correct |
85 |
Correct |
1 ms |
368 KB |
Output is correct |
86 |
Correct |
32 ms |
2368 KB |
Output is correct |
87 |
Correct |
29 ms |
2380 KB |
Output is correct |
88 |
Correct |
31 ms |
2652 KB |
Output is correct |
89 |
Correct |
28 ms |
2720 KB |
Output is correct |
90 |
Correct |
26 ms |
2540 KB |
Output is correct |
91 |
Correct |
24 ms |
2536 KB |
Output is correct |
92 |
Correct |
26 ms |
2212 KB |
Output is correct |
93 |
Correct |
63 ms |
12744 KB |
Output is correct |
94 |
Correct |
54 ms |
11880 KB |
Output is correct |
95 |
Correct |
64 ms |
12652 KB |
Output is correct |
96 |
Correct |
56 ms |
12756 KB |
Output is correct |
97 |
Correct |
59 ms |
12740 KB |
Output is correct |
98 |
Correct |
63 ms |
12812 KB |
Output is correct |
99 |
Correct |
48 ms |
12020 KB |
Output is correct |
100 |
Correct |
142 ms |
14536 KB |
Output is correct |
101 |
Correct |
113 ms |
14628 KB |
Output is correct |
102 |
Correct |
92 ms |
14708 KB |
Output is correct |
103 |
Correct |
96 ms |
14252 KB |
Output is correct |
104 |
Correct |
112 ms |
13844 KB |
Output is correct |
105 |
Correct |
102 ms |
14260 KB |
Output is correct |
106 |
Correct |
106 ms |
13964 KB |
Output is correct |
107 |
Correct |
117 ms |
14116 KB |
Output is correct |
108 |
Correct |
104 ms |
14536 KB |
Output is correct |
109 |
Correct |
90 ms |
14480 KB |
Output is correct |
110 |
Correct |
109 ms |
14548 KB |
Output is correct |
111 |
Correct |
106 ms |
14572 KB |
Output is correct |