#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ = 1e5 + 10, inf = (ll)2e9 * (ll)1e9 + 7, mod = 998244353;
ll n, m, tc = 1, a, b, c, d, sum, x, y, z, base, ans, k;
ll S[n_], E[n_], par[n_][20], rev[n_], rev2[n_];
bool cmp(ll x, ll y) {
if (E[x] ^ E[y]) return E[x] < E[y];
return S[x] < S[y];
}
bool cmp2(ll x, ll y) { return S[x] < S[y]; }
class seg {
public:
vector<ll> tree;
ll base;
seg(int n) {
for (base = 1; base <= n; base *= 2);
// 0~n쓰겠다
tree.resize(n * 4 + 4);
for (int i = 0; i < tree.size(); i++) tree[i] = inf;
}
ll f(ll l, ll r) {
l += base, r += base;
ll ret = inf;
while (l <= r) {
if (l % 2) ret = min(ret, tree[l++]);
if (!(r % 2)) ret = min(ret, tree[r--]);
l /= 2, r /= 2;
}
return ret;
}
void update(ll i, ll v) {
i += base;
tree[i] = min(tree[i], v);
i /= 2;
while (i) {
tree[i] = min(tree[i * 2], tree[i * 2 + 1]);
i /= 2;
}
}
};
void solve() {
ll Q;
cin >> n >> Q;
vector<ll> T, idx, idx2;
for (int i = 1; i <= n; i++) {
cin >> S[i] >> E[i];
T.push_back(S[i]), T.push_back(E[i]);
idx.push_back(i);
}
sort(all(idx), cmp2), sort(all(T)), T.erase(unique(all(T)), T.end());
seg ST(2 * n + 1);
for (int i = 0; i < n; i++) {
rev[idx[i]] = i;
ll a = lower_bound(all(T), E[idx[i]]) - T.begin();
ST.update(a, i);
}
for (int i = 0; i < n; i++) {
ll l = lower_bound(all(T), S[idx[i]]) - T.begin(), r = lower_bound(all(T), E[idx[i]]) - T.begin();
par[i][0] = ST.f(l, r);
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
par[i][j] = min(par[i][j - 1], par[par[i][j - 1]][j - 1]);
}
}
while (Q--) {
cin >> x >> y;
if (x == y) {
cout << "0" << '\n';
continue;
}
x = rev[x], y = rev[y];
ll l = 0, r = n;
ans = inf;
while (l <= r) {
ll mid = (l + r) / 2, now = y, cost = mid;
for (int i = 19; i >= 0; i--) {
if (cost >= (1ll << i)) {
cost -= (1ll << i);
now = par[now][i];
}
}
if (S[idx[now]] <= E[idx[x]]) {
ans = min(ans, mid);
r = mid - 1;
} else
l = mid + 1;
}
ll now = y, cost = ans;
for (int i = 19; i >= 0; i--) {
if (cost >= (1ll << i)) {
cost -= (1ll << i);
now = par[now][i];
}
}
assert(now != x);
if (E[idx[x]] < S[idx[now]] || E[idx[x]] > E[idx[now]])
cout << "impossible" << '\n';
else
cout << ans + 1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// cin >> tc;
while (tc--) solve();
};
Compilation message
events.cpp: In constructor 'seg::seg(int)':
events.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < tree.size(); i++) tree[i] = inf;
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
128 ms |
31800 KB |
Output is correct |
3 |
Correct |
160 ms |
31808 KB |
Output is correct |
4 |
Correct |
170 ms |
31556 KB |
Output is correct |
5 |
Correct |
149 ms |
31900 KB |
Output is correct |
6 |
Correct |
141 ms |
31808 KB |
Output is correct |
7 |
Correct |
146 ms |
31768 KB |
Output is correct |
8 |
Correct |
122 ms |
32064 KB |
Output is correct |
9 |
Correct |
127 ms |
32068 KB |
Output is correct |
10 |
Correct |
197 ms |
32012 KB |
Output is correct |
11 |
Correct |
172 ms |
32148 KB |
Output is correct |
12 |
Correct |
96 ms |
31452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
728 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
728 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
41 ms |
3152 KB |
Output is correct |
20 |
Correct |
42 ms |
3408 KB |
Output is correct |
21 |
Correct |
72 ms |
3552 KB |
Output is correct |
22 |
Correct |
57 ms |
3668 KB |
Output is correct |
23 |
Correct |
40 ms |
3420 KB |
Output is correct |
24 |
Correct |
38 ms |
3576 KB |
Output is correct |
25 |
Correct |
44 ms |
3188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
728 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
73 ms |
29912 KB |
Output is correct |
20 |
Correct |
69 ms |
29204 KB |
Output is correct |
21 |
Correct |
78 ms |
29768 KB |
Output is correct |
22 |
Correct |
72 ms |
30020 KB |
Output is correct |
23 |
Correct |
77 ms |
29888 KB |
Output is correct |
24 |
Correct |
80 ms |
30004 KB |
Output is correct |
25 |
Correct |
49 ms |
29148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
31812 KB |
Output is correct |
2 |
Correct |
159 ms |
31808 KB |
Output is correct |
3 |
Correct |
162 ms |
31556 KB |
Output is correct |
4 |
Correct |
119 ms |
32052 KB |
Output is correct |
5 |
Correct |
204 ms |
32064 KB |
Output is correct |
6 |
Correct |
185 ms |
31808 KB |
Output is correct |
7 |
Correct |
182 ms |
31768 KB |
Output is correct |
8 |
Correct |
151 ms |
31880 KB |
Output is correct |
9 |
Correct |
78 ms |
29876 KB |
Output is correct |
10 |
Correct |
131 ms |
31384 KB |
Output is correct |
11 |
Correct |
123 ms |
31300 KB |
Output is correct |
12 |
Correct |
130 ms |
31552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
128 ms |
31800 KB |
Output is correct |
3 |
Correct |
160 ms |
31808 KB |
Output is correct |
4 |
Correct |
170 ms |
31556 KB |
Output is correct |
5 |
Correct |
149 ms |
31900 KB |
Output is correct |
6 |
Correct |
141 ms |
31808 KB |
Output is correct |
7 |
Correct |
146 ms |
31768 KB |
Output is correct |
8 |
Correct |
122 ms |
32064 KB |
Output is correct |
9 |
Correct |
127 ms |
32068 KB |
Output is correct |
10 |
Correct |
197 ms |
32012 KB |
Output is correct |
11 |
Correct |
172 ms |
32148 KB |
Output is correct |
12 |
Correct |
96 ms |
31452 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
600 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
41 ms |
3152 KB |
Output is correct |
32 |
Correct |
42 ms |
3408 KB |
Output is correct |
33 |
Correct |
72 ms |
3552 KB |
Output is correct |
34 |
Correct |
57 ms |
3668 KB |
Output is correct |
35 |
Correct |
40 ms |
3420 KB |
Output is correct |
36 |
Correct |
38 ms |
3576 KB |
Output is correct |
37 |
Correct |
44 ms |
3188 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
73 ms |
29912 KB |
Output is correct |
48 |
Correct |
69 ms |
29204 KB |
Output is correct |
49 |
Correct |
78 ms |
29768 KB |
Output is correct |
50 |
Correct |
72 ms |
30020 KB |
Output is correct |
51 |
Correct |
77 ms |
29888 KB |
Output is correct |
52 |
Correct |
80 ms |
30004 KB |
Output is correct |
53 |
Correct |
49 ms |
29148 KB |
Output is correct |
54 |
Correct |
127 ms |
31812 KB |
Output is correct |
55 |
Correct |
159 ms |
31808 KB |
Output is correct |
56 |
Correct |
162 ms |
31556 KB |
Output is correct |
57 |
Correct |
119 ms |
32052 KB |
Output is correct |
58 |
Correct |
204 ms |
32064 KB |
Output is correct |
59 |
Correct |
185 ms |
31808 KB |
Output is correct |
60 |
Correct |
182 ms |
31768 KB |
Output is correct |
61 |
Correct |
151 ms |
31880 KB |
Output is correct |
62 |
Correct |
78 ms |
29876 KB |
Output is correct |
63 |
Correct |
131 ms |
31384 KB |
Output is correct |
64 |
Correct |
123 ms |
31300 KB |
Output is correct |
65 |
Correct |
130 ms |
31552 KB |
Output is correct |
66 |
Correct |
1 ms |
344 KB |
Output is correct |
67 |
Correct |
123 ms |
31672 KB |
Output is correct |
68 |
Correct |
176 ms |
31652 KB |
Output is correct |
69 |
Correct |
168 ms |
31596 KB |
Output is correct |
70 |
Correct |
152 ms |
31816 KB |
Output is correct |
71 |
Correct |
151 ms |
31812 KB |
Output is correct |
72 |
Correct |
161 ms |
31812 KB |
Output is correct |
73 |
Correct |
117 ms |
32088 KB |
Output is correct |
74 |
Correct |
122 ms |
32068 KB |
Output is correct |
75 |
Correct |
189 ms |
32148 KB |
Output is correct |
76 |
Correct |
186 ms |
32200 KB |
Output is correct |
77 |
Correct |
96 ms |
31556 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
604 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
1 ms |
732 KB |
Output is correct |
83 |
Correct |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
44 ms |
3280 KB |
Output is correct |
87 |
Correct |
44 ms |
3368 KB |
Output is correct |
88 |
Correct |
71 ms |
3664 KB |
Output is correct |
89 |
Correct |
45 ms |
3668 KB |
Output is correct |
90 |
Correct |
41 ms |
3408 KB |
Output is correct |
91 |
Correct |
48 ms |
3420 KB |
Output is correct |
92 |
Correct |
41 ms |
3152 KB |
Output is correct |
93 |
Correct |
71 ms |
30016 KB |
Output is correct |
94 |
Correct |
68 ms |
29248 KB |
Output is correct |
95 |
Correct |
80 ms |
29764 KB |
Output is correct |
96 |
Correct |
72 ms |
30068 KB |
Output is correct |
97 |
Correct |
80 ms |
29864 KB |
Output is correct |
98 |
Correct |
80 ms |
30020 KB |
Output is correct |
99 |
Correct |
43 ms |
29252 KB |
Output is correct |
100 |
Correct |
195 ms |
31904 KB |
Output is correct |
101 |
Correct |
196 ms |
31772 KB |
Output is correct |
102 |
Correct |
173 ms |
32068 KB |
Output is correct |
103 |
Correct |
139 ms |
31556 KB |
Output is correct |
104 |
Correct |
124 ms |
31196 KB |
Output is correct |
105 |
Correct |
149 ms |
31552 KB |
Output is correct |
106 |
Correct |
207 ms |
31044 KB |
Output is correct |
107 |
Correct |
243 ms |
31296 KB |
Output is correct |
108 |
Correct |
142 ms |
31768 KB |
Output is correct |
109 |
Correct |
141 ms |
31820 KB |
Output is correct |
110 |
Correct |
161 ms |
31808 KB |
Output is correct |
111 |
Correct |
166 ms |
31784 KB |
Output is correct |