#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
typedef long double ld;
using namespace std;
const int maxn = 2e5 + 5, logn = 18, inf = 1e9 + 5;
//const int maxn = 16, logn = 18, inf = 1e9 + 5;
vector<pair<int, int> > st(maxn * 2, { inf, inf });
int mini(int l, int r)
{
pair<int, int> p = { inf, inf };
for (l += maxn, r += maxn + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) p = min(st[l++], p);
if (r & 1) p = min(st[--r], p);
}
return p.second;
}
vector<vector<int> > l(logn, vector<int>(maxn, -1));
struct usek
{
int l, r;
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<usek> u(n);
vector<int> s;
for (int i = 0; i < n; i++)
{
cin >> u[i].l >> u[i].r;
s.push_back(u[i].l);
s.push_back(u[i].r);
}
sort(s.begin(), s.end());
for (int i = 0; i < n; i++)
{
u[i].l = lower_bound(s.begin(), s.end(), u[i].l) - s.begin();
u[i].r = lower_bound(s.begin(), s.end(), u[i].r) - s.begin();
st[maxn + u[i].r] = min(st[maxn + u[i].r], { u[i].l, i });
}
for (int i = maxn - 1; i > 0; i--) st[i] = min(st[i << 1], st[(i << 1) | 1]);
for (int i = 0; i < n; i++)
{
int mi = mini(u[i].l, u[i].r);
l[0][i] = mi;
}
for (int j = 1; j < logn; j++)
{
for (int i = 0; i < n; i++) l[j][i] = l[j - 1][l[j - 1][i]];
}
while (q--)
{
int s, e;
cin >> s >> e;
s--, e--;
//cout << " ";
if (s == e)
{
cout << "0\n";
continue;
}
if (u[s].r > u[e].r)
{
cout << "impossible\n";
continue;
}
if (u[e].l <= u[s].r)
{
cout << "1\n";
continue;
}
int ans = 1;
for (int j = logn - 1; j >= 0; j--)
{
if (u[l[j][e]].l > u[s].r) ans += (1 << j), e = l[j][e];
}
if (u[l[0][e]].l <= u[s].r) cout << ans + 1 << "\n";
else cout << "impossible\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Correct |
92 ms |
20780 KB |
Output is correct |
3 |
Correct |
110 ms |
23880 KB |
Output is correct |
4 |
Correct |
133 ms |
23876 KB |
Output is correct |
5 |
Correct |
115 ms |
23888 KB |
Output is correct |
6 |
Correct |
110 ms |
23876 KB |
Output is correct |
7 |
Correct |
120 ms |
23956 KB |
Output is correct |
8 |
Correct |
115 ms |
24392 KB |
Output is correct |
9 |
Correct |
120 ms |
24256 KB |
Output is correct |
10 |
Correct |
119 ms |
24236 KB |
Output is correct |
11 |
Correct |
134 ms |
24224 KB |
Output is correct |
12 |
Correct |
71 ms |
23564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18360 KB |
Output is correct |
2 |
Correct |
9 ms |
18316 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
9 ms |
18260 KB |
Output is correct |
5 |
Correct |
11 ms |
18340 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18260 KB |
Output is correct |
8 |
Correct |
9 ms |
18260 KB |
Output is correct |
9 |
Correct |
8 ms |
18260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18360 KB |
Output is correct |
2 |
Correct |
9 ms |
18316 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
9 ms |
18260 KB |
Output is correct |
5 |
Correct |
11 ms |
18340 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18260 KB |
Output is correct |
8 |
Correct |
9 ms |
18260 KB |
Output is correct |
9 |
Correct |
8 ms |
18260 KB |
Output is correct |
10 |
Correct |
8 ms |
18260 KB |
Output is correct |
11 |
Correct |
8 ms |
18324 KB |
Output is correct |
12 |
Correct |
8 ms |
18288 KB |
Output is correct |
13 |
Correct |
9 ms |
18260 KB |
Output is correct |
14 |
Correct |
8 ms |
18260 KB |
Output is correct |
15 |
Correct |
11 ms |
18288 KB |
Output is correct |
16 |
Correct |
9 ms |
18260 KB |
Output is correct |
17 |
Correct |
9 ms |
18260 KB |
Output is correct |
18 |
Correct |
9 ms |
18312 KB |
Output is correct |
19 |
Correct |
40 ms |
18260 KB |
Output is correct |
20 |
Correct |
37 ms |
18648 KB |
Output is correct |
21 |
Correct |
39 ms |
19388 KB |
Output is correct |
22 |
Correct |
34 ms |
19560 KB |
Output is correct |
23 |
Correct |
37 ms |
19364 KB |
Output is correct |
24 |
Correct |
30 ms |
19396 KB |
Output is correct |
25 |
Correct |
34 ms |
19060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18360 KB |
Output is correct |
2 |
Correct |
9 ms |
18316 KB |
Output is correct |
3 |
Correct |
9 ms |
18260 KB |
Output is correct |
4 |
Correct |
9 ms |
18260 KB |
Output is correct |
5 |
Correct |
11 ms |
18340 KB |
Output is correct |
6 |
Correct |
9 ms |
18260 KB |
Output is correct |
7 |
Correct |
11 ms |
18260 KB |
Output is correct |
8 |
Correct |
9 ms |
18260 KB |
Output is correct |
9 |
Correct |
8 ms |
18260 KB |
Output is correct |
10 |
Correct |
8 ms |
18260 KB |
Output is correct |
11 |
Correct |
8 ms |
18260 KB |
Output is correct |
12 |
Correct |
9 ms |
18260 KB |
Output is correct |
13 |
Correct |
9 ms |
18260 KB |
Output is correct |
14 |
Correct |
12 ms |
18292 KB |
Output is correct |
15 |
Correct |
9 ms |
18256 KB |
Output is correct |
16 |
Correct |
9 ms |
18260 KB |
Output is correct |
17 |
Correct |
9 ms |
18260 KB |
Output is correct |
18 |
Correct |
9 ms |
18260 KB |
Output is correct |
19 |
Correct |
67 ms |
20164 KB |
Output is correct |
20 |
Correct |
67 ms |
20180 KB |
Output is correct |
21 |
Correct |
79 ms |
20176 KB |
Output is correct |
22 |
Correct |
67 ms |
20124 KB |
Output is correct |
23 |
Correct |
79 ms |
20180 KB |
Output is correct |
24 |
Correct |
76 ms |
20180 KB |
Output is correct |
25 |
Correct |
36 ms |
20180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
20796 KB |
Output is correct |
2 |
Correct |
107 ms |
23864 KB |
Output is correct |
3 |
Correct |
162 ms |
23752 KB |
Output is correct |
4 |
Correct |
107 ms |
24260 KB |
Output is correct |
5 |
Correct |
115 ms |
24224 KB |
Output is correct |
6 |
Correct |
119 ms |
23904 KB |
Output is correct |
7 |
Correct |
134 ms |
24012 KB |
Output is correct |
8 |
Correct |
126 ms |
24136 KB |
Output is correct |
9 |
Correct |
82 ms |
22032 KB |
Output is correct |
10 |
Correct |
115 ms |
23628 KB |
Output is correct |
11 |
Correct |
119 ms |
23364 KB |
Output is correct |
12 |
Correct |
113 ms |
23612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18260 KB |
Output is correct |
2 |
Correct |
92 ms |
20780 KB |
Output is correct |
3 |
Correct |
110 ms |
23880 KB |
Output is correct |
4 |
Correct |
133 ms |
23876 KB |
Output is correct |
5 |
Correct |
115 ms |
23888 KB |
Output is correct |
6 |
Correct |
110 ms |
23876 KB |
Output is correct |
7 |
Correct |
120 ms |
23956 KB |
Output is correct |
8 |
Correct |
115 ms |
24392 KB |
Output is correct |
9 |
Correct |
120 ms |
24256 KB |
Output is correct |
10 |
Correct |
119 ms |
24236 KB |
Output is correct |
11 |
Correct |
134 ms |
24224 KB |
Output is correct |
12 |
Correct |
71 ms |
23564 KB |
Output is correct |
13 |
Correct |
8 ms |
18360 KB |
Output is correct |
14 |
Correct |
9 ms |
18316 KB |
Output is correct |
15 |
Correct |
9 ms |
18260 KB |
Output is correct |
16 |
Correct |
9 ms |
18260 KB |
Output is correct |
17 |
Correct |
11 ms |
18340 KB |
Output is correct |
18 |
Correct |
9 ms |
18260 KB |
Output is correct |
19 |
Correct |
11 ms |
18260 KB |
Output is correct |
20 |
Correct |
9 ms |
18260 KB |
Output is correct |
21 |
Correct |
8 ms |
18260 KB |
Output is correct |
22 |
Correct |
8 ms |
18260 KB |
Output is correct |
23 |
Correct |
8 ms |
18324 KB |
Output is correct |
24 |
Correct |
8 ms |
18288 KB |
Output is correct |
25 |
Correct |
9 ms |
18260 KB |
Output is correct |
26 |
Correct |
8 ms |
18260 KB |
Output is correct |
27 |
Correct |
11 ms |
18288 KB |
Output is correct |
28 |
Correct |
9 ms |
18260 KB |
Output is correct |
29 |
Correct |
9 ms |
18260 KB |
Output is correct |
30 |
Correct |
9 ms |
18312 KB |
Output is correct |
31 |
Correct |
40 ms |
18260 KB |
Output is correct |
32 |
Correct |
37 ms |
18648 KB |
Output is correct |
33 |
Correct |
39 ms |
19388 KB |
Output is correct |
34 |
Correct |
34 ms |
19560 KB |
Output is correct |
35 |
Correct |
37 ms |
19364 KB |
Output is correct |
36 |
Correct |
30 ms |
19396 KB |
Output is correct |
37 |
Correct |
34 ms |
19060 KB |
Output is correct |
38 |
Correct |
8 ms |
18260 KB |
Output is correct |
39 |
Correct |
8 ms |
18260 KB |
Output is correct |
40 |
Correct |
9 ms |
18260 KB |
Output is correct |
41 |
Correct |
9 ms |
18260 KB |
Output is correct |
42 |
Correct |
12 ms |
18292 KB |
Output is correct |
43 |
Correct |
9 ms |
18256 KB |
Output is correct |
44 |
Correct |
9 ms |
18260 KB |
Output is correct |
45 |
Correct |
9 ms |
18260 KB |
Output is correct |
46 |
Correct |
9 ms |
18260 KB |
Output is correct |
47 |
Correct |
67 ms |
20164 KB |
Output is correct |
48 |
Correct |
67 ms |
20180 KB |
Output is correct |
49 |
Correct |
79 ms |
20176 KB |
Output is correct |
50 |
Correct |
67 ms |
20124 KB |
Output is correct |
51 |
Correct |
79 ms |
20180 KB |
Output is correct |
52 |
Correct |
76 ms |
20180 KB |
Output is correct |
53 |
Correct |
36 ms |
20180 KB |
Output is correct |
54 |
Correct |
91 ms |
20796 KB |
Output is correct |
55 |
Correct |
107 ms |
23864 KB |
Output is correct |
56 |
Correct |
162 ms |
23752 KB |
Output is correct |
57 |
Correct |
107 ms |
24260 KB |
Output is correct |
58 |
Correct |
115 ms |
24224 KB |
Output is correct |
59 |
Correct |
119 ms |
23904 KB |
Output is correct |
60 |
Correct |
134 ms |
24012 KB |
Output is correct |
61 |
Correct |
126 ms |
24136 KB |
Output is correct |
62 |
Correct |
82 ms |
22032 KB |
Output is correct |
63 |
Correct |
115 ms |
23628 KB |
Output is correct |
64 |
Correct |
119 ms |
23364 KB |
Output is correct |
65 |
Correct |
113 ms |
23612 KB |
Output is correct |
66 |
Correct |
9 ms |
18260 KB |
Output is correct |
67 |
Correct |
93 ms |
23872 KB |
Output is correct |
68 |
Correct |
110 ms |
23872 KB |
Output is correct |
69 |
Correct |
168 ms |
23872 KB |
Output is correct |
70 |
Correct |
113 ms |
23868 KB |
Output is correct |
71 |
Correct |
112 ms |
23964 KB |
Output is correct |
72 |
Correct |
116 ms |
23980 KB |
Output is correct |
73 |
Correct |
135 ms |
24348 KB |
Output is correct |
74 |
Correct |
119 ms |
24268 KB |
Output is correct |
75 |
Correct |
128 ms |
24144 KB |
Output is correct |
76 |
Correct |
122 ms |
24176 KB |
Output is correct |
77 |
Correct |
65 ms |
23540 KB |
Output is correct |
78 |
Correct |
8 ms |
18260 KB |
Output is correct |
79 |
Correct |
9 ms |
18260 KB |
Output is correct |
80 |
Correct |
9 ms |
18260 KB |
Output is correct |
81 |
Correct |
9 ms |
18260 KB |
Output is correct |
82 |
Correct |
9 ms |
18260 KB |
Output is correct |
83 |
Correct |
9 ms |
18232 KB |
Output is correct |
84 |
Correct |
10 ms |
18328 KB |
Output is correct |
85 |
Correct |
10 ms |
18320 KB |
Output is correct |
86 |
Correct |
40 ms |
19148 KB |
Output is correct |
87 |
Correct |
41 ms |
19268 KB |
Output is correct |
88 |
Correct |
40 ms |
19460 KB |
Output is correct |
89 |
Correct |
34 ms |
19516 KB |
Output is correct |
90 |
Correct |
32 ms |
19424 KB |
Output is correct |
91 |
Correct |
28 ms |
19368 KB |
Output is correct |
92 |
Correct |
36 ms |
19024 KB |
Output is correct |
93 |
Correct |
69 ms |
22076 KB |
Output is correct |
94 |
Correct |
61 ms |
21304 KB |
Output is correct |
95 |
Correct |
82 ms |
21960 KB |
Output is correct |
96 |
Correct |
73 ms |
22076 KB |
Output is correct |
97 |
Correct |
80 ms |
22068 KB |
Output is correct |
98 |
Correct |
75 ms |
22088 KB |
Output is correct |
99 |
Correct |
37 ms |
21456 KB |
Output is correct |
100 |
Correct |
124 ms |
23996 KB |
Output is correct |
101 |
Correct |
119 ms |
23968 KB |
Output is correct |
102 |
Correct |
146 ms |
24116 KB |
Output is correct |
103 |
Correct |
111 ms |
23592 KB |
Output is correct |
104 |
Correct |
114 ms |
23452 KB |
Output is correct |
105 |
Correct |
111 ms |
23612 KB |
Output is correct |
106 |
Correct |
103 ms |
23224 KB |
Output is correct |
107 |
Correct |
128 ms |
23268 KB |
Output is correct |
108 |
Correct |
122 ms |
23920 KB |
Output is correct |
109 |
Correct |
115 ms |
24000 KB |
Output is correct |
110 |
Correct |
117 ms |
23876 KB |
Output is correct |
111 |
Correct |
120 ms |
23924 KB |
Output is correct |