#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()
const int maxn=1e5+10;
const int LOG=int(log2(maxn))+1;
vector<pi> events(maxn);
vector<vi> table(maxn,vi(LOG,-1));
vector<vector<pi>> rmq(LOG,vector<pi>(maxn,{1000000010,-1}));
vi pow2(LOG,1);
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin >> n >> q;
vi b;
for (int i = 0; i < n; i++) {
cin >> events[i].fi >> events[i].se;
b.push_back(events[i].se);
}
sort(all(b));
b.erase(unique(all(b)),b.end());
for (int i=0; i<n; i++) {
int r=lower_bound(all(b),events[i].se)-b.begin();
rmq[0][r]=min(rmq[0][r],{events[i].fi,i});
}
for (int i=0; i+1<LOG; i++) {
for (int j=0; j+(1<<(i+1))<=n; j++) {
rmq[i+1][j]=min(rmq[i][j],rmq[i][j+(1<<i)]);
}
}
for (int i=0; i<n; i++) {
int l=lower_bound(all(b),events[i].fi)-b.begin();
int r=lower_bound(all(b),events[i].se)-b.begin();
int h=31-__builtin_clz(r-l+1);
table[i][0]=min(rmq[h][l],rmq[h][r+1-(1<<h)]).se;
}
for (int i=0; i+1<LOG; i++) {
for (int j = 0; j < n; j++) {
table[j][i+1]=table[table[j][i]][i];
}
}
int x,y,l;
for (int i=0; i<q; i++) {
l=1;
cin >> x >> y;
x--;
y--;
if (x==y) {
cout << "0\n";
continue;
}
if (events[y].fi<=events[x].se && events[x].se<=events[y].se) {
cout << "1\n";
continue;
}
for (int i=LOG-1; i>=0; i--) {
if (events[x].se<events[table[y][i]].fi) {
l+=1<<i;
y=table[y][i];
}
}
y=table[y][0];
if (events[x].se<events[y].fi || events[x].se>events[y].se) {
cout << "impossible\n";
}
else {
cout << l+(events[x].se>=events[y].fi) << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
98 ms |
29252 KB |
Output is correct |
3 |
Correct |
115 ms |
29376 KB |
Output is correct |
4 |
Correct |
140 ms |
29600 KB |
Output is correct |
5 |
Correct |
125 ms |
29248 KB |
Output is correct |
6 |
Correct |
123 ms |
29376 KB |
Output is correct |
7 |
Correct |
120 ms |
29252 KB |
Output is correct |
8 |
Correct |
91 ms |
29764 KB |
Output is correct |
9 |
Correct |
90 ms |
29880 KB |
Output is correct |
10 |
Correct |
116 ms |
29640 KB |
Output is correct |
11 |
Correct |
125 ms |
29584 KB |
Output is correct |
12 |
Correct |
63 ms |
28996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
25432 KB |
Output is correct |
2 |
Correct |
15 ms |
25432 KB |
Output is correct |
3 |
Correct |
14 ms |
25432 KB |
Output is correct |
4 |
Correct |
14 ms |
25436 KB |
Output is correct |
5 |
Correct |
15 ms |
25600 KB |
Output is correct |
6 |
Correct |
13 ms |
25436 KB |
Output is correct |
7 |
Correct |
14 ms |
25432 KB |
Output is correct |
8 |
Correct |
14 ms |
25436 KB |
Output is correct |
9 |
Correct |
13 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
25432 KB |
Output is correct |
2 |
Correct |
15 ms |
25432 KB |
Output is correct |
3 |
Correct |
14 ms |
25432 KB |
Output is correct |
4 |
Correct |
14 ms |
25436 KB |
Output is correct |
5 |
Correct |
15 ms |
25600 KB |
Output is correct |
6 |
Correct |
13 ms |
25436 KB |
Output is correct |
7 |
Correct |
14 ms |
25432 KB |
Output is correct |
8 |
Correct |
14 ms |
25436 KB |
Output is correct |
9 |
Correct |
13 ms |
25436 KB |
Output is correct |
10 |
Correct |
15 ms |
25432 KB |
Output is correct |
11 |
Correct |
15 ms |
25436 KB |
Output is correct |
12 |
Correct |
14 ms |
25512 KB |
Output is correct |
13 |
Correct |
14 ms |
25436 KB |
Output is correct |
14 |
Correct |
13 ms |
25436 KB |
Output is correct |
15 |
Correct |
15 ms |
25436 KB |
Output is correct |
16 |
Correct |
14 ms |
25536 KB |
Output is correct |
17 |
Correct |
14 ms |
25436 KB |
Output is correct |
18 |
Correct |
13 ms |
25436 KB |
Output is correct |
19 |
Correct |
38 ms |
26124 KB |
Output is correct |
20 |
Correct |
38 ms |
26180 KB |
Output is correct |
21 |
Correct |
41 ms |
26504 KB |
Output is correct |
22 |
Correct |
35 ms |
26432 KB |
Output is correct |
23 |
Correct |
31 ms |
26436 KB |
Output is correct |
24 |
Correct |
30 ms |
26424 KB |
Output is correct |
25 |
Correct |
34 ms |
25932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
25432 KB |
Output is correct |
2 |
Correct |
15 ms |
25432 KB |
Output is correct |
3 |
Correct |
14 ms |
25432 KB |
Output is correct |
4 |
Correct |
14 ms |
25436 KB |
Output is correct |
5 |
Correct |
15 ms |
25600 KB |
Output is correct |
6 |
Correct |
13 ms |
25436 KB |
Output is correct |
7 |
Correct |
14 ms |
25432 KB |
Output is correct |
8 |
Correct |
14 ms |
25436 KB |
Output is correct |
9 |
Correct |
13 ms |
25436 KB |
Output is correct |
10 |
Correct |
13 ms |
25432 KB |
Output is correct |
11 |
Correct |
13 ms |
25436 KB |
Output is correct |
12 |
Correct |
14 ms |
25436 KB |
Output is correct |
13 |
Correct |
13 ms |
25436 KB |
Output is correct |
14 |
Correct |
13 ms |
25396 KB |
Output is correct |
15 |
Correct |
13 ms |
25432 KB |
Output is correct |
16 |
Correct |
14 ms |
25404 KB |
Output is correct |
17 |
Correct |
14 ms |
25436 KB |
Output is correct |
18 |
Correct |
13 ms |
25412 KB |
Output is correct |
19 |
Correct |
81 ms |
27640 KB |
Output is correct |
20 |
Correct |
72 ms |
26832 KB |
Output is correct |
21 |
Correct |
70 ms |
27536 KB |
Output is correct |
22 |
Correct |
74 ms |
27468 KB |
Output is correct |
23 |
Correct |
62 ms |
27460 KB |
Output is correct |
24 |
Correct |
72 ms |
27596 KB |
Output is correct |
25 |
Correct |
33 ms |
26900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
29084 KB |
Output is correct |
2 |
Correct |
130 ms |
28932 KB |
Output is correct |
3 |
Correct |
136 ms |
28884 KB |
Output is correct |
4 |
Correct |
89 ms |
29504 KB |
Output is correct |
5 |
Correct |
115 ms |
29640 KB |
Output is correct |
6 |
Correct |
113 ms |
29512 KB |
Output is correct |
7 |
Correct |
117 ms |
29388 KB |
Output is correct |
8 |
Correct |
99 ms |
29504 KB |
Output is correct |
9 |
Correct |
66 ms |
27624 KB |
Output is correct |
10 |
Correct |
110 ms |
28960 KB |
Output is correct |
11 |
Correct |
89 ms |
28884 KB |
Output is correct |
12 |
Correct |
113 ms |
28992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
25436 KB |
Output is correct |
2 |
Correct |
98 ms |
29252 KB |
Output is correct |
3 |
Correct |
115 ms |
29376 KB |
Output is correct |
4 |
Correct |
140 ms |
29600 KB |
Output is correct |
5 |
Correct |
125 ms |
29248 KB |
Output is correct |
6 |
Correct |
123 ms |
29376 KB |
Output is correct |
7 |
Correct |
120 ms |
29252 KB |
Output is correct |
8 |
Correct |
91 ms |
29764 KB |
Output is correct |
9 |
Correct |
90 ms |
29880 KB |
Output is correct |
10 |
Correct |
116 ms |
29640 KB |
Output is correct |
11 |
Correct |
125 ms |
29584 KB |
Output is correct |
12 |
Correct |
63 ms |
28996 KB |
Output is correct |
13 |
Correct |
13 ms |
25432 KB |
Output is correct |
14 |
Correct |
15 ms |
25432 KB |
Output is correct |
15 |
Correct |
14 ms |
25432 KB |
Output is correct |
16 |
Correct |
14 ms |
25436 KB |
Output is correct |
17 |
Correct |
15 ms |
25600 KB |
Output is correct |
18 |
Correct |
13 ms |
25436 KB |
Output is correct |
19 |
Correct |
14 ms |
25432 KB |
Output is correct |
20 |
Correct |
14 ms |
25436 KB |
Output is correct |
21 |
Correct |
13 ms |
25436 KB |
Output is correct |
22 |
Correct |
15 ms |
25432 KB |
Output is correct |
23 |
Correct |
15 ms |
25436 KB |
Output is correct |
24 |
Correct |
14 ms |
25512 KB |
Output is correct |
25 |
Correct |
14 ms |
25436 KB |
Output is correct |
26 |
Correct |
13 ms |
25436 KB |
Output is correct |
27 |
Correct |
15 ms |
25436 KB |
Output is correct |
28 |
Correct |
14 ms |
25536 KB |
Output is correct |
29 |
Correct |
14 ms |
25436 KB |
Output is correct |
30 |
Correct |
13 ms |
25436 KB |
Output is correct |
31 |
Correct |
38 ms |
26124 KB |
Output is correct |
32 |
Correct |
38 ms |
26180 KB |
Output is correct |
33 |
Correct |
41 ms |
26504 KB |
Output is correct |
34 |
Correct |
35 ms |
26432 KB |
Output is correct |
35 |
Correct |
31 ms |
26436 KB |
Output is correct |
36 |
Correct |
30 ms |
26424 KB |
Output is correct |
37 |
Correct |
34 ms |
25932 KB |
Output is correct |
38 |
Correct |
13 ms |
25432 KB |
Output is correct |
39 |
Correct |
13 ms |
25436 KB |
Output is correct |
40 |
Correct |
14 ms |
25436 KB |
Output is correct |
41 |
Correct |
13 ms |
25436 KB |
Output is correct |
42 |
Correct |
13 ms |
25396 KB |
Output is correct |
43 |
Correct |
13 ms |
25432 KB |
Output is correct |
44 |
Correct |
14 ms |
25404 KB |
Output is correct |
45 |
Correct |
14 ms |
25436 KB |
Output is correct |
46 |
Correct |
13 ms |
25412 KB |
Output is correct |
47 |
Correct |
81 ms |
27640 KB |
Output is correct |
48 |
Correct |
72 ms |
26832 KB |
Output is correct |
49 |
Correct |
70 ms |
27536 KB |
Output is correct |
50 |
Correct |
74 ms |
27468 KB |
Output is correct |
51 |
Correct |
62 ms |
27460 KB |
Output is correct |
52 |
Correct |
72 ms |
27596 KB |
Output is correct |
53 |
Correct |
33 ms |
26900 KB |
Output is correct |
54 |
Correct |
96 ms |
29084 KB |
Output is correct |
55 |
Correct |
130 ms |
28932 KB |
Output is correct |
56 |
Correct |
136 ms |
28884 KB |
Output is correct |
57 |
Correct |
89 ms |
29504 KB |
Output is correct |
58 |
Correct |
115 ms |
29640 KB |
Output is correct |
59 |
Correct |
113 ms |
29512 KB |
Output is correct |
60 |
Correct |
117 ms |
29388 KB |
Output is correct |
61 |
Correct |
99 ms |
29504 KB |
Output is correct |
62 |
Correct |
66 ms |
27624 KB |
Output is correct |
63 |
Correct |
110 ms |
28960 KB |
Output is correct |
64 |
Correct |
89 ms |
28884 KB |
Output is correct |
65 |
Correct |
113 ms |
28992 KB |
Output is correct |
66 |
Correct |
17 ms |
25944 KB |
Output is correct |
67 |
Correct |
97 ms |
29300 KB |
Output is correct |
68 |
Correct |
112 ms |
29376 KB |
Output is correct |
69 |
Correct |
152 ms |
29376 KB |
Output is correct |
70 |
Correct |
130 ms |
29384 KB |
Output is correct |
71 |
Correct |
113 ms |
29408 KB |
Output is correct |
72 |
Correct |
115 ms |
29424 KB |
Output is correct |
73 |
Correct |
90 ms |
29764 KB |
Output is correct |
74 |
Correct |
88 ms |
29628 KB |
Output is correct |
75 |
Correct |
122 ms |
29604 KB |
Output is correct |
76 |
Correct |
107 ms |
29572 KB |
Output is correct |
77 |
Correct |
61 ms |
28996 KB |
Output is correct |
78 |
Correct |
15 ms |
25428 KB |
Output is correct |
79 |
Correct |
14 ms |
25436 KB |
Output is correct |
80 |
Correct |
13 ms |
25436 KB |
Output is correct |
81 |
Correct |
13 ms |
25436 KB |
Output is correct |
82 |
Correct |
13 ms |
25436 KB |
Output is correct |
83 |
Correct |
13 ms |
25496 KB |
Output is correct |
84 |
Correct |
13 ms |
25436 KB |
Output is correct |
85 |
Correct |
13 ms |
25436 KB |
Output is correct |
86 |
Correct |
39 ms |
26180 KB |
Output is correct |
87 |
Correct |
38 ms |
26180 KB |
Output is correct |
88 |
Correct |
41 ms |
26436 KB |
Output is correct |
89 |
Correct |
32 ms |
26536 KB |
Output is correct |
90 |
Correct |
32 ms |
26436 KB |
Output is correct |
91 |
Correct |
29 ms |
26432 KB |
Output is correct |
92 |
Correct |
34 ms |
25932 KB |
Output is correct |
93 |
Correct |
76 ms |
27640 KB |
Output is correct |
94 |
Correct |
70 ms |
26700 KB |
Output is correct |
95 |
Correct |
76 ms |
27432 KB |
Output is correct |
96 |
Correct |
75 ms |
27644 KB |
Output is correct |
97 |
Correct |
67 ms |
27620 KB |
Output is correct |
98 |
Correct |
72 ms |
27456 KB |
Output is correct |
99 |
Correct |
32 ms |
26944 KB |
Output is correct |
100 |
Correct |
112 ms |
29380 KB |
Output is correct |
101 |
Correct |
132 ms |
29380 KB |
Output is correct |
102 |
Correct |
98 ms |
29640 KB |
Output is correct |
103 |
Correct |
110 ms |
28992 KB |
Output is correct |
104 |
Correct |
90 ms |
28832 KB |
Output is correct |
105 |
Correct |
115 ms |
29124 KB |
Output is correct |
106 |
Correct |
101 ms |
28604 KB |
Output is correct |
107 |
Correct |
120 ms |
28620 KB |
Output is correct |
108 |
Correct |
96 ms |
29248 KB |
Output is correct |
109 |
Correct |
108 ms |
29516 KB |
Output is correct |
110 |
Correct |
90 ms |
29380 KB |
Output is correct |
111 |
Correct |
92 ms |
29252 KB |
Output is correct |