#include <bits/stdc++.h>
using namespace std;
typedef pair<int64_t, int64_t> Interval;
struct SegmentTree
{
vector<pair<int64_t, unsigned>> t;
size_t l;
SegmentTree(size_t n)
{
l = 1U << (32U - __builtin_clz(n));
t = vector<pair<int64_t, unsigned>>(2 * l, make_pair(INT64_MAX, UINT_MAX));
}
void update(size_t i, pair<int64_t, unsigned> const &x)
{
i += l;
t[i] = min(t[i], x);
while (i >>= 1)
t[i] = min(t[2 * i], t[2 * i + 1]);
}
pair<int64_t, unsigned> range_min(size_t i, size_t j)
{
i += l, j += l;
pair<int64_t, unsigned> x = make_pair(INT64_MAX, UINT_MAX);
while (i <= j)
{
if (i & 1)
x = min(x, t[i++]);
if (!(j & 1))
x = min(x, t[j--]);
i >>= 1;
j >>= 1;
}
return x;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, q;
cin >> n >> q;
vector<Interval> intervals(n);
vector<int64_t> coords;
for (Interval &z : intervals)
{
cin >> z.first >> z.second;
coords.push_back(z.first);
coords.push_back(z.second);
}
sort(coords.begin(), coords.end());
coords.resize(unique(coords.begin(), coords.end()) - coords.begin());
for (Interval &x : intervals)
{
x.first = lower_bound(coords.begin(), coords.end(), x.first) - coords.begin();
x.second = lower_bound(coords.begin(), coords.end(), x.second) - coords.begin();
}
SegmentTree tree(coords.size());
vector<vector<unsigned>> pre(n);
for (size_t i = 0; i < n; i++)
tree.update(intervals[i].second, make_pair(intervals[i].first, i));
for (size_t i = 0; i < n; i++)
{
pre[i].push_back(
tree.range_min(intervals[i].first, intervals[i].second).second);
}
for (size_t j = 1; j <= 32U - __builtin_clz(n); j++)
{
for (size_t i = 0; i < n; i++)
{
pre[i].push_back(pre[pre[i][j - 1]][j - 1]);
}
}
while (q--)
{
unsigned u, v;
cin >> u >> v;
u--, v--;
if (u == v)
{
cout << "0\n";
continue;
}
if (intervals[u].second > intervals[v].second)
{
cout << "impossible\n";
continue;
}
if (intervals[v].first <= intervals[u].second &&
intervals[u].second <= intervals[v].second)
{
cout << "1\n";
continue;
}
unsigned d = 0;
for (unsigned i = pre[v].size() - 1; i < pre[v].size(); i--)
{
if (intervals[pre[v][i]].first > intervals[u].second)
v = pre[v][i], d += 1U << i;
}
if (d <= n)
cout << d + 2 << '\n';
else
cout << "impossible\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
161 ms |
24560 KB |
Output is correct |
3 |
Correct |
187 ms |
27740 KB |
Output is correct |
4 |
Correct |
213 ms |
27672 KB |
Output is correct |
5 |
Correct |
181 ms |
27816 KB |
Output is correct |
6 |
Correct |
185 ms |
27760 KB |
Output is correct |
7 |
Correct |
186 ms |
27836 KB |
Output is correct |
8 |
Correct |
134 ms |
32296 KB |
Output is correct |
9 |
Correct |
136 ms |
32236 KB |
Output is correct |
10 |
Correct |
218 ms |
28128 KB |
Output is correct |
11 |
Correct |
188 ms |
32232 KB |
Output is correct |
12 |
Correct |
102 ms |
27392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
34 ms |
2688 KB |
Output is correct |
20 |
Correct |
33 ms |
2764 KB |
Output is correct |
21 |
Correct |
33 ms |
2932 KB |
Output is correct |
22 |
Correct |
24 ms |
3020 KB |
Output is correct |
23 |
Correct |
28 ms |
3132 KB |
Output is correct |
24 |
Correct |
21 ms |
3152 KB |
Output is correct |
25 |
Correct |
27 ms |
2760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
140 ms |
26064 KB |
Output is correct |
20 |
Correct |
121 ms |
25152 KB |
Output is correct |
21 |
Correct |
125 ms |
25840 KB |
Output is correct |
22 |
Correct |
140 ms |
29992 KB |
Output is correct |
23 |
Correct |
120 ms |
30020 KB |
Output is correct |
24 |
Correct |
129 ms |
30036 KB |
Output is correct |
25 |
Correct |
64 ms |
21180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
24648 KB |
Output is correct |
2 |
Correct |
177 ms |
27764 KB |
Output is correct |
3 |
Correct |
215 ms |
27700 KB |
Output is correct |
4 |
Correct |
132 ms |
32324 KB |
Output is correct |
5 |
Correct |
223 ms |
28012 KB |
Output is correct |
6 |
Correct |
182 ms |
31900 KB |
Output is correct |
7 |
Correct |
201 ms |
31988 KB |
Output is correct |
8 |
Correct |
161 ms |
32148 KB |
Output is correct |
9 |
Correct |
118 ms |
30056 KB |
Output is correct |
10 |
Correct |
158 ms |
31556 KB |
Output is correct |
11 |
Correct |
151 ms |
31376 KB |
Output is correct |
12 |
Correct |
174 ms |
31620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
161 ms |
24560 KB |
Output is correct |
3 |
Correct |
187 ms |
27740 KB |
Output is correct |
4 |
Correct |
213 ms |
27672 KB |
Output is correct |
5 |
Correct |
181 ms |
27816 KB |
Output is correct |
6 |
Correct |
185 ms |
27760 KB |
Output is correct |
7 |
Correct |
186 ms |
27836 KB |
Output is correct |
8 |
Correct |
134 ms |
32296 KB |
Output is correct |
9 |
Correct |
136 ms |
32236 KB |
Output is correct |
10 |
Correct |
218 ms |
28128 KB |
Output is correct |
11 |
Correct |
188 ms |
32232 KB |
Output is correct |
12 |
Correct |
102 ms |
27392 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 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 |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
34 ms |
2688 KB |
Output is correct |
32 |
Correct |
33 ms |
2764 KB |
Output is correct |
33 |
Correct |
33 ms |
2932 KB |
Output is correct |
34 |
Correct |
24 ms |
3020 KB |
Output is correct |
35 |
Correct |
28 ms |
3132 KB |
Output is correct |
36 |
Correct |
21 ms |
3152 KB |
Output is correct |
37 |
Correct |
27 ms |
2760 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
320 KB |
Output is correct |
40 |
Correct |
1 ms |
456 KB |
Output is correct |
41 |
Correct |
1 ms |
508 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
140 ms |
26064 KB |
Output is correct |
48 |
Correct |
121 ms |
25152 KB |
Output is correct |
49 |
Correct |
125 ms |
25840 KB |
Output is correct |
50 |
Correct |
140 ms |
29992 KB |
Output is correct |
51 |
Correct |
120 ms |
30020 KB |
Output is correct |
52 |
Correct |
129 ms |
30036 KB |
Output is correct |
53 |
Correct |
64 ms |
21180 KB |
Output is correct |
54 |
Correct |
168 ms |
24648 KB |
Output is correct |
55 |
Correct |
177 ms |
27764 KB |
Output is correct |
56 |
Correct |
215 ms |
27700 KB |
Output is correct |
57 |
Correct |
132 ms |
32324 KB |
Output is correct |
58 |
Correct |
223 ms |
28012 KB |
Output is correct |
59 |
Correct |
182 ms |
31900 KB |
Output is correct |
60 |
Correct |
201 ms |
31988 KB |
Output is correct |
61 |
Correct |
161 ms |
32148 KB |
Output is correct |
62 |
Correct |
118 ms |
30056 KB |
Output is correct |
63 |
Correct |
158 ms |
31556 KB |
Output is correct |
64 |
Correct |
151 ms |
31376 KB |
Output is correct |
65 |
Correct |
174 ms |
31620 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
177 ms |
27780 KB |
Output is correct |
68 |
Correct |
190 ms |
27724 KB |
Output is correct |
69 |
Correct |
226 ms |
27640 KB |
Output is correct |
70 |
Correct |
191 ms |
27792 KB |
Output is correct |
71 |
Correct |
174 ms |
27756 KB |
Output is correct |
72 |
Correct |
161 ms |
27752 KB |
Output is correct |
73 |
Correct |
139 ms |
32308 KB |
Output is correct |
74 |
Correct |
133 ms |
32284 KB |
Output is correct |
75 |
Correct |
179 ms |
28016 KB |
Output is correct |
76 |
Correct |
176 ms |
32224 KB |
Output is correct |
77 |
Correct |
110 ms |
27460 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
456 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
496 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
32 ms |
2648 KB |
Output is correct |
87 |
Correct |
32 ms |
2780 KB |
Output is correct |
88 |
Correct |
44 ms |
2964 KB |
Output is correct |
89 |
Correct |
24 ms |
3024 KB |
Output is correct |
90 |
Correct |
22 ms |
3144 KB |
Output is correct |
91 |
Correct |
31 ms |
3204 KB |
Output is correct |
92 |
Correct |
27 ms |
2820 KB |
Output is correct |
93 |
Correct |
142 ms |
25968 KB |
Output is correct |
94 |
Correct |
151 ms |
25204 KB |
Output is correct |
95 |
Correct |
119 ms |
25840 KB |
Output is correct |
96 |
Correct |
120 ms |
30108 KB |
Output is correct |
97 |
Correct |
133 ms |
30004 KB |
Output is correct |
98 |
Correct |
143 ms |
29984 KB |
Output is correct |
99 |
Correct |
62 ms |
21192 KB |
Output is correct |
100 |
Correct |
184 ms |
32000 KB |
Output is correct |
101 |
Correct |
198 ms |
31984 KB |
Output is correct |
102 |
Correct |
157 ms |
32040 KB |
Output is correct |
103 |
Correct |
170 ms |
31532 KB |
Output is correct |
104 |
Correct |
161 ms |
31388 KB |
Output is correct |
105 |
Correct |
184 ms |
31556 KB |
Output is correct |
106 |
Correct |
209 ms |
27116 KB |
Output is correct |
107 |
Correct |
241 ms |
27392 KB |
Output is correct |
108 |
Correct |
152 ms |
27856 KB |
Output is correct |
109 |
Correct |
158 ms |
27836 KB |
Output is correct |
110 |
Correct |
160 ms |
31852 KB |
Output is correct |
111 |
Correct |
170 ms |
31856 KB |
Output is correct |