bool home = 0;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;
struct T {
int l;
int r;
int init;
};
bool operator < (T a, T b) {
if (a.r != b.r) {
return a.r < b.r;
} else {
return a.l < b.l;
}
}
T v[N];
int dp[N];
int nxt[N];
int ant[N];
int inv[N];
int low[N];
int high[N];
int cnt_rs_under(int limit) {
int low = 1, high = n, cnt = 0;
while (low <= high) {
int mid = (low + high) / 2;
if (v[mid].r <= limit) {
cnt = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return cnt;
}
pair<int, int> join(pair<int, int> a, pair<int, int> b) {
return {min(a.first, b.first), max(a.second, b.second)};
}
const int K = 20;
const int INF = (int) 1e9 + 7;
pair<int, int> after[K][N];
pair<int, int> tree[K][4 * N];
void build(int v, int tl, int tr, int k) {
if (tl == tr) {
tree[k][v] = after[k][tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, k);
build(2 * v + 1, tm + 1, tr, k);
tree[k][v] = join(tree[k][2 * v], tree[k][2 * v + 1]);
}
}
pair<int, int> get(int v, int tl, int tr, int l, int r, int k) {
if (tr < l || r < tl) {
return make_pair(INF, -INF);
}
if (l <= tl && tr <= r) {
return tree[k][v];
}
int tm = (tl + tr) / 2;
return join(get(2 * v, tl, tm, l, r, k), get(2 * v + 1, tm + 1, tr, l, r, k));
}
pair<int, int> get(int l, int r, int k) {
return get(1, 1, n, l, r, k);
}
void build(int k) {
build(1, 1, n, k);
}
pair<int, int> do_2_pw_k_steps(pair<int, int> cur, int k) {
int L = cur.first, R = cur.second;
assert(1 <= L && L <= R && R <= n);
return get(L, R, k);
}
int main() {
if (!home) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
} else {
freopen("input.txt", "r", stdin);
}
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> v[i].l >> v[i].r;
v[i].init = i;
}
sort(v + 1, v + n + 1);
for (int i = 1; i <= n; i++) {
int x = cnt_rs_under(v[i].l - 1) + 1;
int y = cnt_rs_under(v[i].r);
low[i] = x;
high[i] = y;
after[0][i] = {low[i], high[i]};
}
build(0);
for (int k = 1; k < K; k++) {
for (int i = 1; i <= n; i++) {
after[k][i] = do_2_pw_k_steps(after[k - 1][i], k - 1);
}
build(k);
}
for (int i = 1; i <= n; i++) {
inv[v[i].init] = i;
}
for (int iq = 1; iq <= q; iq++) {
int ff, ss;
cin >> ff >> ss;
ff = inv[ff];
ss = inv[ss];
int cost = 1;
pair<int, int> cur = {ss, ss};
{
pair<int, int> nxt_cur = do_2_pw_k_steps(cur, K - 1);
if (!(nxt_cur.first <= ff && ff <= nxt_cur.second)) {
cout << "impossible\n";
continue;
}
}
if (ff == ss) {
cout << "0\n";
continue;
}
for (int k = K - 1; k >= 0; k--) {
pair<int, int> nxt_cur = do_2_pw_k_steps(cur, k);
if (!(nxt_cur.first <= ff && ff <= nxt_cur.second)) {
cost += (1 << k);
cur = nxt_cur;
}
}
cout << cost << "\n";
}
return 0;
}
/**
**/
Compilation message
events.cpp: In function 'int main()':
events.cpp:96:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
648 ms |
60936 KB |
Output is correct |
3 |
Correct |
833 ms |
63180 KB |
Output is correct |
4 |
Correct |
916 ms |
63116 KB |
Output is correct |
5 |
Correct |
801 ms |
63140 KB |
Output is correct |
6 |
Correct |
794 ms |
63204 KB |
Output is correct |
7 |
Correct |
761 ms |
63424 KB |
Output is correct |
8 |
Correct |
338 ms |
63564 KB |
Output is correct |
9 |
Correct |
347 ms |
63552 KB |
Output is correct |
10 |
Correct |
658 ms |
63484 KB |
Output is correct |
11 |
Correct |
544 ms |
63648 KB |
Output is correct |
12 |
Correct |
338 ms |
62880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
1108 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
1076 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
1108 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
1076 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
3 ms |
1108 KB |
Output is correct |
14 |
Correct |
3 ms |
980 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
287 ms |
5372 KB |
Output is correct |
20 |
Correct |
316 ms |
5488 KB |
Output is correct |
21 |
Correct |
200 ms |
5728 KB |
Output is correct |
22 |
Correct |
110 ms |
5836 KB |
Output is correct |
23 |
Correct |
132 ms |
5632 KB |
Output is correct |
24 |
Correct |
133 ms |
5676 KB |
Output is correct |
25 |
Correct |
253 ms |
5448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
1108 KB |
Output is correct |
4 |
Correct |
3 ms |
980 KB |
Output is correct |
5 |
Correct |
3 ms |
1076 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
0 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
3 ms |
980 KB |
Output is correct |
14 |
Correct |
3 ms |
1108 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
3 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
390 ms |
59436 KB |
Output is correct |
20 |
Correct |
383 ms |
60656 KB |
Output is correct |
21 |
Correct |
337 ms |
61288 KB |
Output is correct |
22 |
Correct |
380 ms |
61436 KB |
Output is correct |
23 |
Correct |
307 ms |
61356 KB |
Output is correct |
24 |
Correct |
370 ms |
61476 KB |
Output is correct |
25 |
Correct |
73 ms |
60700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
639 ms |
61000 KB |
Output is correct |
2 |
Correct |
846 ms |
63188 KB |
Output is correct |
3 |
Correct |
903 ms |
63184 KB |
Output is correct |
4 |
Correct |
340 ms |
63668 KB |
Output is correct |
5 |
Correct |
610 ms |
63556 KB |
Output is correct |
6 |
Correct |
770 ms |
63308 KB |
Output is correct |
7 |
Correct |
769 ms |
63396 KB |
Output is correct |
8 |
Correct |
665 ms |
63436 KB |
Output is correct |
9 |
Correct |
323 ms |
61384 KB |
Output is correct |
10 |
Correct |
707 ms |
62856 KB |
Output is correct |
11 |
Correct |
564 ms |
62656 KB |
Output is correct |
12 |
Correct |
727 ms |
62924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
648 ms |
60936 KB |
Output is correct |
3 |
Correct |
833 ms |
63180 KB |
Output is correct |
4 |
Correct |
916 ms |
63116 KB |
Output is correct |
5 |
Correct |
801 ms |
63140 KB |
Output is correct |
6 |
Correct |
794 ms |
63204 KB |
Output is correct |
7 |
Correct |
761 ms |
63424 KB |
Output is correct |
8 |
Correct |
338 ms |
63564 KB |
Output is correct |
9 |
Correct |
347 ms |
63552 KB |
Output is correct |
10 |
Correct |
658 ms |
63484 KB |
Output is correct |
11 |
Correct |
544 ms |
63648 KB |
Output is correct |
12 |
Correct |
338 ms |
62880 KB |
Output is correct |
13 |
Correct |
0 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
1108 KB |
Output is correct |
16 |
Correct |
3 ms |
980 KB |
Output is correct |
17 |
Correct |
3 ms |
1076 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1108 KB |
Output is correct |
20 |
Correct |
3 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
0 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
980 KB |
Output is correct |
25 |
Correct |
3 ms |
1108 KB |
Output is correct |
26 |
Correct |
3 ms |
980 KB |
Output is correct |
27 |
Correct |
3 ms |
1108 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
1108 KB |
Output is correct |
31 |
Correct |
287 ms |
5372 KB |
Output is correct |
32 |
Correct |
316 ms |
5488 KB |
Output is correct |
33 |
Correct |
200 ms |
5728 KB |
Output is correct |
34 |
Correct |
110 ms |
5836 KB |
Output is correct |
35 |
Correct |
132 ms |
5632 KB |
Output is correct |
36 |
Correct |
133 ms |
5676 KB |
Output is correct |
37 |
Correct |
253 ms |
5448 KB |
Output is correct |
38 |
Correct |
0 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
3 ms |
980 KB |
Output is correct |
41 |
Correct |
3 ms |
980 KB |
Output is correct |
42 |
Correct |
3 ms |
1108 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
3 ms |
1108 KB |
Output is correct |
45 |
Correct |
3 ms |
1108 KB |
Output is correct |
46 |
Correct |
1 ms |
980 KB |
Output is correct |
47 |
Correct |
390 ms |
59436 KB |
Output is correct |
48 |
Correct |
383 ms |
60656 KB |
Output is correct |
49 |
Correct |
337 ms |
61288 KB |
Output is correct |
50 |
Correct |
380 ms |
61436 KB |
Output is correct |
51 |
Correct |
307 ms |
61356 KB |
Output is correct |
52 |
Correct |
370 ms |
61476 KB |
Output is correct |
53 |
Correct |
73 ms |
60700 KB |
Output is correct |
54 |
Correct |
639 ms |
61000 KB |
Output is correct |
55 |
Correct |
846 ms |
63188 KB |
Output is correct |
56 |
Correct |
903 ms |
63184 KB |
Output is correct |
57 |
Correct |
340 ms |
63668 KB |
Output is correct |
58 |
Correct |
610 ms |
63556 KB |
Output is correct |
59 |
Correct |
770 ms |
63308 KB |
Output is correct |
60 |
Correct |
769 ms |
63396 KB |
Output is correct |
61 |
Correct |
665 ms |
63436 KB |
Output is correct |
62 |
Correct |
323 ms |
61384 KB |
Output is correct |
63 |
Correct |
707 ms |
62856 KB |
Output is correct |
64 |
Correct |
564 ms |
62656 KB |
Output is correct |
65 |
Correct |
727 ms |
62924 KB |
Output is correct |
66 |
Correct |
1 ms |
596 KB |
Output is correct |
67 |
Correct |
700 ms |
63244 KB |
Output is correct |
68 |
Correct |
864 ms |
63352 KB |
Output is correct |
69 |
Correct |
904 ms |
63180 KB |
Output is correct |
70 |
Correct |
805 ms |
63220 KB |
Output is correct |
71 |
Correct |
790 ms |
63340 KB |
Output is correct |
72 |
Correct |
771 ms |
63244 KB |
Output is correct |
73 |
Correct |
337 ms |
63764 KB |
Output is correct |
74 |
Correct |
342 ms |
63600 KB |
Output is correct |
75 |
Correct |
604 ms |
63504 KB |
Output is correct |
76 |
Correct |
536 ms |
63524 KB |
Output is correct |
77 |
Correct |
337 ms |
62816 KB |
Output is correct |
78 |
Correct |
1 ms |
596 KB |
Output is correct |
79 |
Correct |
4 ms |
1108 KB |
Output is correct |
80 |
Correct |
4 ms |
1108 KB |
Output is correct |
81 |
Correct |
3 ms |
1112 KB |
Output is correct |
82 |
Correct |
3 ms |
1108 KB |
Output is correct |
83 |
Correct |
4 ms |
1108 KB |
Output is correct |
84 |
Correct |
3 ms |
1108 KB |
Output is correct |
85 |
Correct |
2 ms |
1108 KB |
Output is correct |
86 |
Correct |
298 ms |
5440 KB |
Output is correct |
87 |
Correct |
339 ms |
5468 KB |
Output is correct |
88 |
Correct |
208 ms |
5700 KB |
Output is correct |
89 |
Correct |
102 ms |
5860 KB |
Output is correct |
90 |
Correct |
131 ms |
5708 KB |
Output is correct |
91 |
Correct |
144 ms |
5736 KB |
Output is correct |
92 |
Correct |
256 ms |
5364 KB |
Output is correct |
93 |
Correct |
391 ms |
61404 KB |
Output is correct |
94 |
Correct |
384 ms |
60584 KB |
Output is correct |
95 |
Correct |
341 ms |
61328 KB |
Output is correct |
96 |
Correct |
373 ms |
61332 KB |
Output is correct |
97 |
Correct |
308 ms |
61308 KB |
Output is correct |
98 |
Correct |
381 ms |
61356 KB |
Output is correct |
99 |
Correct |
70 ms |
60740 KB |
Output is correct |
100 |
Correct |
757 ms |
63420 KB |
Output is correct |
101 |
Correct |
737 ms |
63228 KB |
Output is correct |
102 |
Correct |
652 ms |
63436 KB |
Output is correct |
103 |
Correct |
733 ms |
62900 KB |
Output is correct |
104 |
Correct |
567 ms |
62752 KB |
Output is correct |
105 |
Correct |
705 ms |
62984 KB |
Output is correct |
106 |
Correct |
673 ms |
62528 KB |
Output is correct |
107 |
Correct |
825 ms |
62620 KB |
Output is correct |
108 |
Correct |
576 ms |
63292 KB |
Output is correct |
109 |
Correct |
551 ms |
63404 KB |
Output is correct |
110 |
Correct |
611 ms |
63228 KB |
Output is correct |
111 |
Correct |
581 ms |
63180 KB |
Output is correct |