#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int maxn = 1e5, bits = 17, siz = (1 << bits);
pii segtree[siz * 2];
void update(int val, int p, int pos)
{
while (pos != 0)
{
if (val < segtree[pos].first) segtree[pos] = pii(val, p);
pos /= 2;
}
}
pii query(int pos, int l, int r, int gl, int gr)
{
if (l > gr || gl > r) return pii(1e9, 0);
if (gl <= l && r <= gr) return segtree[pos];
int mid = (l + r) >> 1;
return min(query(pos * 2, l, mid, gl, gr), query(pos * 2 + 1, mid + 1, r, gl, gr));
}
struct ev {
int s, e;
} ;
int dist[bits][maxn] = { 0 };
vector <ev> events;
int find_dist(int goal, int p, int last)
{
if (p == last) return -2e9;
for (int i = bits - 1; i >= 0; i--)
{
if (events[dist[i][p]].s <= events[goal].e) continue;
return find_dist(goal, dist[i][p], p) + (1 << i);
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
events.assign(n, ev{0, 0});
int next_num = 0;
map <int, int> num_map;
for (ev &e : events) {
cin >> e.s >> e.e;
num_map[e.e] = 0;
}
for (auto &p : num_map) p.second = next_num++;
for (ev &e : events) {
e.e = num_map[e.e];
e.s = (*num_map.lower_bound(e.s)).second;
// cout << e.s << " " << e.e << "\n";
}
for (int i = 1; i <= next_num + siz; i++) segtree[i].first = 1e9;
// for (int i = 1; i < siz + n; i++)
// {
// if (__builtin_popcount(i) == 1) cout << "\n";
// cout << segtree[i].first << " ";
// }
for (int i = 0; i < n; i++)
{
update(events[i].s, i, events[i].e + siz);
//segtree[events[i].e] = min(segtree[events[i].e], pii(events[i].s, i));
}
// for (int i = 1; i < siz + n; i++)
// {
// if (__builtin_popcount(i) == 1) cout << "\n";
// cout << segtree[i].first << " " << segtree[i].second << "\t";
// }
// cout << "\n";
for (int i = 0; i < n; i++)
{
dist[0][i] = query(1, 0, siz - 1, events[i].s, events[i].e).second;
// cout << dist[0][i] << "\n";
}
// cerr << "output\n";
for (int y = 1; y < bits; y++)
{
for (int i = 0; i < n; i++) {
dist[y][i] = dist[y - 1][dist[y - 1][i]];
}
}
int a, b, val;
while (q--)
{
cin >> a >> b;
a--, b--;
if (a == b)
{
cout << "0\n";
continue;
}
if (events[a].e > events[b].e) {
cout << "impossible\n";
continue;
}
if (events[a].e >= events[b].s) {
cerr << "fast ";
cout << "1\n";
continue;
}
val = find_dist(a, b, -1);
if (val < 0) cout << "impossible\n";
else {
cout << val + 2 << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
153 ms |
17968 KB |
Output is correct |
3 |
Correct |
192 ms |
17960 KB |
Output is correct |
4 |
Correct |
205 ms |
17900 KB |
Output is correct |
5 |
Correct |
170 ms |
17948 KB |
Output is correct |
6 |
Correct |
161 ms |
17996 KB |
Output is correct |
7 |
Correct |
173 ms |
18048 KB |
Output is correct |
8 |
Correct |
154 ms |
18356 KB |
Output is correct |
9 |
Correct |
131 ms |
18380 KB |
Output is correct |
10 |
Correct |
167 ms |
18224 KB |
Output is correct |
11 |
Correct |
146 ms |
18352 KB |
Output is correct |
12 |
Correct |
85 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
3 |
Correct |
2 ms |
1492 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1480 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
3 |
Correct |
2 ms |
1492 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1480 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1508 KB |
Output is correct |
13 |
Correct |
2 ms |
1488 KB |
Output is correct |
14 |
Correct |
2 ms |
1488 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
2 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1488 KB |
Output is correct |
19 |
Correct |
50 ms |
3444 KB |
Output is correct |
20 |
Correct |
38 ms |
3564 KB |
Output is correct |
21 |
Correct |
35 ms |
3716 KB |
Output is correct |
22 |
Correct |
57 ms |
3788 KB |
Output is correct |
23 |
Correct |
101 ms |
3932 KB |
Output is correct |
24 |
Correct |
150 ms |
3988 KB |
Output is correct |
25 |
Correct |
33 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1356 KB |
Output is correct |
3 |
Correct |
2 ms |
1492 KB |
Output is correct |
4 |
Correct |
2 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1480 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
2 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1364 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
2 ms |
1492 KB |
Output is correct |
14 |
Correct |
2 ms |
1492 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
3 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1492 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
127 ms |
16184 KB |
Output is correct |
20 |
Correct |
116 ms |
15400 KB |
Output is correct |
21 |
Correct |
112 ms |
13288 KB |
Output is correct |
22 |
Correct |
150 ms |
16104 KB |
Output is correct |
23 |
Correct |
118 ms |
16328 KB |
Output is correct |
24 |
Correct |
134 ms |
16084 KB |
Output is correct |
25 |
Correct |
36 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
18000 KB |
Output is correct |
2 |
Correct |
164 ms |
17932 KB |
Output is correct |
3 |
Correct |
247 ms |
17900 KB |
Output is correct |
4 |
Correct |
128 ms |
18388 KB |
Output is correct |
5 |
Correct |
197 ms |
18328 KB |
Output is correct |
6 |
Correct |
242 ms |
18084 KB |
Output is correct |
7 |
Correct |
206 ms |
18048 KB |
Output is correct |
8 |
Correct |
167 ms |
18172 KB |
Output is correct |
9 |
Correct |
149 ms |
16140 KB |
Output is correct |
10 |
Correct |
184 ms |
17616 KB |
Output is correct |
11 |
Correct |
185 ms |
17436 KB |
Output is correct |
12 |
Correct |
241 ms |
17720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
153 ms |
17968 KB |
Output is correct |
3 |
Correct |
192 ms |
17960 KB |
Output is correct |
4 |
Correct |
205 ms |
17900 KB |
Output is correct |
5 |
Correct |
170 ms |
17948 KB |
Output is correct |
6 |
Correct |
161 ms |
17996 KB |
Output is correct |
7 |
Correct |
173 ms |
18048 KB |
Output is correct |
8 |
Correct |
154 ms |
18356 KB |
Output is correct |
9 |
Correct |
131 ms |
18380 KB |
Output is correct |
10 |
Correct |
167 ms |
18224 KB |
Output is correct |
11 |
Correct |
146 ms |
18352 KB |
Output is correct |
12 |
Correct |
85 ms |
14956 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1492 KB |
Output is correct |
16 |
Correct |
2 ms |
1492 KB |
Output is correct |
17 |
Correct |
2 ms |
1480 KB |
Output is correct |
18 |
Correct |
2 ms |
1492 KB |
Output is correct |
19 |
Correct |
2 ms |
1492 KB |
Output is correct |
20 |
Correct |
2 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1348 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
2 ms |
1508 KB |
Output is correct |
25 |
Correct |
2 ms |
1488 KB |
Output is correct |
26 |
Correct |
2 ms |
1488 KB |
Output is correct |
27 |
Correct |
2 ms |
1492 KB |
Output is correct |
28 |
Correct |
2 ms |
1492 KB |
Output is correct |
29 |
Correct |
2 ms |
1492 KB |
Output is correct |
30 |
Correct |
1 ms |
1488 KB |
Output is correct |
31 |
Correct |
50 ms |
3444 KB |
Output is correct |
32 |
Correct |
38 ms |
3564 KB |
Output is correct |
33 |
Correct |
35 ms |
3716 KB |
Output is correct |
34 |
Correct |
57 ms |
3788 KB |
Output is correct |
35 |
Correct |
101 ms |
3932 KB |
Output is correct |
36 |
Correct |
150 ms |
3988 KB |
Output is correct |
37 |
Correct |
33 ms |
3404 KB |
Output is correct |
38 |
Correct |
1 ms |
1364 KB |
Output is correct |
39 |
Correct |
1 ms |
1364 KB |
Output is correct |
40 |
Correct |
2 ms |
1492 KB |
Output is correct |
41 |
Correct |
2 ms |
1492 KB |
Output is correct |
42 |
Correct |
2 ms |
1492 KB |
Output is correct |
43 |
Correct |
2 ms |
1492 KB |
Output is correct |
44 |
Correct |
3 ms |
1492 KB |
Output is correct |
45 |
Correct |
2 ms |
1492 KB |
Output is correct |
46 |
Correct |
1 ms |
1492 KB |
Output is correct |
47 |
Correct |
127 ms |
16184 KB |
Output is correct |
48 |
Correct |
116 ms |
15400 KB |
Output is correct |
49 |
Correct |
112 ms |
13288 KB |
Output is correct |
50 |
Correct |
150 ms |
16104 KB |
Output is correct |
51 |
Correct |
118 ms |
16328 KB |
Output is correct |
52 |
Correct |
134 ms |
16084 KB |
Output is correct |
53 |
Correct |
36 ms |
9940 KB |
Output is correct |
54 |
Correct |
164 ms |
18000 KB |
Output is correct |
55 |
Correct |
164 ms |
17932 KB |
Output is correct |
56 |
Correct |
247 ms |
17900 KB |
Output is correct |
57 |
Correct |
128 ms |
18388 KB |
Output is correct |
58 |
Correct |
197 ms |
18328 KB |
Output is correct |
59 |
Correct |
242 ms |
18084 KB |
Output is correct |
60 |
Correct |
206 ms |
18048 KB |
Output is correct |
61 |
Correct |
167 ms |
18172 KB |
Output is correct |
62 |
Correct |
149 ms |
16140 KB |
Output is correct |
63 |
Correct |
184 ms |
17616 KB |
Output is correct |
64 |
Correct |
185 ms |
17436 KB |
Output is correct |
65 |
Correct |
241 ms |
17720 KB |
Output is correct |
66 |
Correct |
1 ms |
1364 KB |
Output is correct |
67 |
Correct |
144 ms |
17944 KB |
Output is correct |
68 |
Correct |
180 ms |
17952 KB |
Output is correct |
69 |
Correct |
194 ms |
17880 KB |
Output is correct |
70 |
Correct |
187 ms |
17928 KB |
Output is correct |
71 |
Correct |
175 ms |
18080 KB |
Output is correct |
72 |
Correct |
160 ms |
18124 KB |
Output is correct |
73 |
Correct |
131 ms |
18460 KB |
Output is correct |
74 |
Correct |
145 ms |
18396 KB |
Output is correct |
75 |
Correct |
160 ms |
18364 KB |
Output is correct |
76 |
Correct |
164 ms |
18384 KB |
Output is correct |
77 |
Correct |
80 ms |
14980 KB |
Output is correct |
78 |
Correct |
1 ms |
1364 KB |
Output is correct |
79 |
Correct |
2 ms |
1492 KB |
Output is correct |
80 |
Correct |
2 ms |
1496 KB |
Output is correct |
81 |
Correct |
2 ms |
1492 KB |
Output is correct |
82 |
Correct |
2 ms |
1492 KB |
Output is correct |
83 |
Correct |
2 ms |
1492 KB |
Output is correct |
84 |
Correct |
2 ms |
1528 KB |
Output is correct |
85 |
Correct |
2 ms |
1492 KB |
Output is correct |
86 |
Correct |
37 ms |
3552 KB |
Output is correct |
87 |
Correct |
36 ms |
3532 KB |
Output is correct |
88 |
Correct |
35 ms |
3776 KB |
Output is correct |
89 |
Correct |
61 ms |
3796 KB |
Output is correct |
90 |
Correct |
101 ms |
3796 KB |
Output is correct |
91 |
Correct |
136 ms |
3952 KB |
Output is correct |
92 |
Correct |
31 ms |
3404 KB |
Output is correct |
93 |
Correct |
110 ms |
16188 KB |
Output is correct |
94 |
Correct |
108 ms |
15424 KB |
Output is correct |
95 |
Correct |
116 ms |
13292 KB |
Output is correct |
96 |
Correct |
142 ms |
16124 KB |
Output is correct |
97 |
Correct |
106 ms |
16204 KB |
Output is correct |
98 |
Correct |
136 ms |
16192 KB |
Output is correct |
99 |
Correct |
39 ms |
10008 KB |
Output is correct |
100 |
Correct |
207 ms |
18060 KB |
Output is correct |
101 |
Correct |
175 ms |
18072 KB |
Output is correct |
102 |
Correct |
186 ms |
18116 KB |
Output is correct |
103 |
Correct |
188 ms |
17656 KB |
Output is correct |
104 |
Correct |
163 ms |
17452 KB |
Output is correct |
105 |
Correct |
174 ms |
17712 KB |
Output is correct |
106 |
Correct |
211 ms |
16044 KB |
Output is correct |
107 |
Correct |
163 ms |
17356 KB |
Output is correct |
108 |
Correct |
169 ms |
15388 KB |
Output is correct |
109 |
Correct |
171 ms |
15408 KB |
Output is correct |
110 |
Correct |
243 ms |
18152 KB |
Output is correct |
111 |
Correct |
299 ms |
18164 KB |
Output is correct |