#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1e5 + 69;
const int K = 18;
int st[8 * N];
int n, q, l[N], r[N], pos[N];
int cl[N], cr[N];
int up[N][K];
void modify(int v, int tl, int tr, int p, int x) {
if (tl == tr) {
if (l[x] < l[st[v]])
st[v] = x;
}
else {
int mid = (tl + tr) / 2;
if (p <= mid)
modify(v * 2, tl, mid, p, x);
else
modify(v * 2 + 1, mid + 1, tr, p, x);
if (l[st[v * 2]] < l[st[v * 2 + 1]])
st[v] = st[v * 2];
else
st[v] = st[v * 2 + 1];
}
}
int get(int v, int tl, int tr, int x, int y) {
if (tl > y || tr < x)
return 0;
else if (tl >= x && tr <= y)
return st[v];
else {
int mid = (tl + tr) / 2;
int ll = get(v * 2, tl, mid, x, y);
int rr = get(v * 2 + 1, mid + 1, tr, x, y);
if (l[ll] < l[rr])
return ll;
return rr;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n >> q;
vector<int> xs;
for (int i = 1;i <= n;i++) {
cin >> l[i] >> r[i];
xs.push_back(l[i]);
xs.push_back(r[i]);
}
l[0] = 1e9 + 10;
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
vector<pair<int, int>> ord;
for (int i = 1;i <= n;i++) {
cl[i] = lower_bound(xs.begin(), xs.end(), l[i]) - xs.begin() + 1;
cr[i] = lower_bound(xs.begin(), xs.end(), r[i]) - xs.begin() + 1;
l[i] = cl[i];
r[i] = cr[i];
ord.push_back({l[i], i});
}
sort(ord.begin(), ord.end());
const int m = 2 * n;
for (int j = 0;j < n;j++) {
int i = ord[j].s;
pos[i] = j + 1;
int g = get(1, 1, m, l[i], r[i]);
if (l[i] < l[g])
up[i][0] = i;
else
up[i][0] = g;
modify(1, 1, m, r[i], i);
}
for (int j = 1;j < K;j++) {
for (int i = n;i >= 1;i--) {
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
l[0] = 0;
while (q--) {
int x, y;
cin >> x >> y;
if (x == y) {
cout << "0\n";
continue;
}
if (r[x] <= r[y] && l[up[y][K - 1]] <= r[x]) {
int ans = 0;
if (l[y] <= r[x]) {
cout << "1\n";
continue;
}
for (int i = K - 1;i >= 0;i--) {
if (l[up[y][i]] > r[x]) {
ans += (1 << i);
y = up[y][i];
}
}
assert(l[up[y][0]] <= r[x]);
if (up[y][0] == x)
ans += 1;
else
ans += 2;
cout << ans << '\n';
}
else
cout << "impossible\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
114 ms |
13220 KB |
Output is correct |
3 |
Correct |
106 ms |
13164 KB |
Output is correct |
4 |
Correct |
110 ms |
13104 KB |
Output is correct |
5 |
Correct |
121 ms |
13384 KB |
Output is correct |
6 |
Correct |
107 ms |
13432 KB |
Output is correct |
7 |
Correct |
108 ms |
13372 KB |
Output is correct |
8 |
Correct |
101 ms |
14416 KB |
Output is correct |
9 |
Correct |
99 ms |
14148 KB |
Output is correct |
10 |
Correct |
111 ms |
13612 KB |
Output is correct |
11 |
Correct |
115 ms |
16884 KB |
Output is correct |
12 |
Correct |
64 ms |
16104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4640 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4640 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4584 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
26 ms |
7260 KB |
Output is correct |
20 |
Correct |
24 ms |
7516 KB |
Output is correct |
21 |
Correct |
27 ms |
8540 KB |
Output is correct |
22 |
Correct |
20 ms |
8576 KB |
Output is correct |
23 |
Correct |
20 ms |
8540 KB |
Output is correct |
24 |
Correct |
22 ms |
8540 KB |
Output is correct |
25 |
Correct |
22 ms |
8244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4640 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4584 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
4444 KB |
Output is correct |
16 |
Correct |
2 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
87 ms |
12624 KB |
Output is correct |
20 |
Correct |
79 ms |
12620 KB |
Output is correct |
21 |
Correct |
104 ms |
12536 KB |
Output is correct |
22 |
Correct |
84 ms |
12664 KB |
Output is correct |
23 |
Correct |
97 ms |
12884 KB |
Output is correct |
24 |
Correct |
94 ms |
12976 KB |
Output is correct |
25 |
Correct |
46 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
13136 KB |
Output is correct |
2 |
Correct |
107 ms |
13104 KB |
Output is correct |
3 |
Correct |
130 ms |
13104 KB |
Output is correct |
4 |
Correct |
98 ms |
14284 KB |
Output is correct |
5 |
Correct |
105 ms |
13432 KB |
Output is correct |
6 |
Correct |
135 ms |
13900 KB |
Output is correct |
7 |
Correct |
121 ms |
17024 KB |
Output is correct |
8 |
Correct |
113 ms |
16976 KB |
Output is correct |
9 |
Correct |
99 ms |
14856 KB |
Output is correct |
10 |
Correct |
116 ms |
16568 KB |
Output is correct |
11 |
Correct |
121 ms |
16464 KB |
Output is correct |
12 |
Correct |
124 ms |
16544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
114 ms |
13220 KB |
Output is correct |
3 |
Correct |
106 ms |
13164 KB |
Output is correct |
4 |
Correct |
110 ms |
13104 KB |
Output is correct |
5 |
Correct |
121 ms |
13384 KB |
Output is correct |
6 |
Correct |
107 ms |
13432 KB |
Output is correct |
7 |
Correct |
108 ms |
13372 KB |
Output is correct |
8 |
Correct |
101 ms |
14416 KB |
Output is correct |
9 |
Correct |
99 ms |
14148 KB |
Output is correct |
10 |
Correct |
111 ms |
13612 KB |
Output is correct |
11 |
Correct |
115 ms |
16884 KB |
Output is correct |
12 |
Correct |
64 ms |
16104 KB |
Output is correct |
13 |
Correct |
1 ms |
4640 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4584 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
26 ms |
7260 KB |
Output is correct |
32 |
Correct |
24 ms |
7516 KB |
Output is correct |
33 |
Correct |
27 ms |
8540 KB |
Output is correct |
34 |
Correct |
20 ms |
8576 KB |
Output is correct |
35 |
Correct |
20 ms |
8540 KB |
Output is correct |
36 |
Correct |
22 ms |
8540 KB |
Output is correct |
37 |
Correct |
22 ms |
8244 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
2 ms |
4444 KB |
Output is correct |
44 |
Correct |
2 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
87 ms |
12624 KB |
Output is correct |
48 |
Correct |
79 ms |
12620 KB |
Output is correct |
49 |
Correct |
104 ms |
12536 KB |
Output is correct |
50 |
Correct |
84 ms |
12664 KB |
Output is correct |
51 |
Correct |
97 ms |
12884 KB |
Output is correct |
52 |
Correct |
94 ms |
12976 KB |
Output is correct |
53 |
Correct |
46 ms |
12380 KB |
Output is correct |
54 |
Correct |
100 ms |
13136 KB |
Output is correct |
55 |
Correct |
107 ms |
13104 KB |
Output is correct |
56 |
Correct |
130 ms |
13104 KB |
Output is correct |
57 |
Correct |
98 ms |
14284 KB |
Output is correct |
58 |
Correct |
105 ms |
13432 KB |
Output is correct |
59 |
Correct |
135 ms |
13900 KB |
Output is correct |
60 |
Correct |
121 ms |
17024 KB |
Output is correct |
61 |
Correct |
113 ms |
16976 KB |
Output is correct |
62 |
Correct |
99 ms |
14856 KB |
Output is correct |
63 |
Correct |
116 ms |
16568 KB |
Output is correct |
64 |
Correct |
121 ms |
16464 KB |
Output is correct |
65 |
Correct |
124 ms |
16544 KB |
Output is correct |
66 |
Correct |
1 ms |
4440 KB |
Output is correct |
67 |
Correct |
105 ms |
16304 KB |
Output is correct |
68 |
Correct |
105 ms |
16208 KB |
Output is correct |
69 |
Correct |
116 ms |
16252 KB |
Output is correct |
70 |
Correct |
110 ms |
16464 KB |
Output is correct |
71 |
Correct |
119 ms |
16672 KB |
Output is correct |
72 |
Correct |
106 ms |
16460 KB |
Output is correct |
73 |
Correct |
97 ms |
17240 KB |
Output is correct |
74 |
Correct |
98 ms |
17196 KB |
Output is correct |
75 |
Correct |
113 ms |
16828 KB |
Output is correct |
76 |
Correct |
109 ms |
16980 KB |
Output is correct |
77 |
Correct |
63 ms |
16044 KB |
Output is correct |
78 |
Correct |
1 ms |
4440 KB |
Output is correct |
79 |
Correct |
1 ms |
4444 KB |
Output is correct |
80 |
Correct |
1 ms |
4444 KB |
Output is correct |
81 |
Correct |
1 ms |
4444 KB |
Output is correct |
82 |
Correct |
2 ms |
4700 KB |
Output is correct |
83 |
Correct |
2 ms |
4444 KB |
Output is correct |
84 |
Correct |
1 ms |
4444 KB |
Output is correct |
85 |
Correct |
1 ms |
4576 KB |
Output is correct |
86 |
Correct |
27 ms |
8284 KB |
Output is correct |
87 |
Correct |
23 ms |
8284 KB |
Output is correct |
88 |
Correct |
25 ms |
8536 KB |
Output is correct |
89 |
Correct |
20 ms |
8540 KB |
Output is correct |
90 |
Correct |
19 ms |
8536 KB |
Output is correct |
91 |
Correct |
18 ms |
8540 KB |
Output is correct |
92 |
Correct |
23 ms |
8244 KB |
Output is correct |
93 |
Correct |
88 ms |
14476 KB |
Output is correct |
94 |
Correct |
83 ms |
13744 KB |
Output is correct |
95 |
Correct |
94 ms |
14560 KB |
Output is correct |
96 |
Correct |
85 ms |
14872 KB |
Output is correct |
97 |
Correct |
102 ms |
14932 KB |
Output is correct |
98 |
Correct |
99 ms |
14924 KB |
Output is correct |
99 |
Correct |
42 ms |
13648 KB |
Output is correct |
100 |
Correct |
137 ms |
16972 KB |
Output is correct |
101 |
Correct |
127 ms |
17052 KB |
Output is correct |
102 |
Correct |
114 ms |
17104 KB |
Output is correct |
103 |
Correct |
119 ms |
16616 KB |
Output is correct |
104 |
Correct |
121 ms |
16288 KB |
Output is correct |
105 |
Correct |
118 ms |
16468 KB |
Output is correct |
106 |
Correct |
103 ms |
15628 KB |
Output is correct |
107 |
Correct |
114 ms |
15680 KB |
Output is correct |
108 |
Correct |
114 ms |
16536 KB |
Output is correct |
109 |
Correct |
111 ms |
16364 KB |
Output is correct |
110 |
Correct |
138 ms |
16916 KB |
Output is correct |
111 |
Correct |
147 ms |
17176 KB |
Output is correct |