#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
const int SZ = 20;
vector<vector<int>> tl;
// Segtree operations for maintaining reachable left nodes:
void btl(int n) {
tl = vector<vector<int>>(2 * n, vector<int>(SZ));
for (int i = 0; i < n; ++i) for (int j = 0; j < SZ; ++j) tl[i + n][j] = i;
for (int i = n - 1; i > 0; --i) for (int j = 0; j < SZ; ++j) tl[i][j] = min(tl[2 * i][j], tl[2 * i + 1][j]);
}
void upd(int n, int p, int j, int v) { for (tl[p += n][j] = v; p > 1; p >>= 1) tl[p >> 1][j] = min(tl[p][j], tl[p ^ 1][j]); }
int qry(int n, int l, int r, int j) {
int ans = INT_MAX;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = min(ans, tl[l++][j]);
if (r & 1) ans = min(ans, tl[--r][j]);
}
return ans;
}
int main() {
int n, q; cin >> n >> q;
vector<pii> events(n); for (int i = 0; i < n; ++i) cin >> events[i].first >> events[i].second;
set<int> nums; for (pii event : events) nums.insert(event.first), nums.insert(event.second);
map<int, int> m;
int id = 0;
for (int num : nums) m[num] = id++;
for (int i = 0; i < n; ++i) events[i].first = m[events[i].first], events[i].second = m[events[i].second];
vector<int> mns(id); for (int i = 0; i < id; ++i) mns[i] = i;
for (pii event : events) mns[event.second] = min(mns[event.second], event.first);
vector<pii> qs(q); for (int i = 0; i < q; ++i) cin >> qs[i].first >> qs[i].second;
for (int i = 0; i < q; ++i) --qs[i].first, --qs[i].second;
vector<vector<pair<pii, int>>> fq(id);
for (int i = 0; i < q; ++i) {
if (qs[i].first == qs[i].second) continue;
int e1 = events[qs[i].first].second, s2 = events[qs[i].second].first, e2 = events[qs[i].second].second;
fq[e2].push_back({{s2, e1}, i});
}
btl(id);
vector<vector<int>> jump(id, vector<int>(SZ));
for (int i = 0; i < id; ++i) for (int j = 0; j < SZ; ++j) jump[i][j] = i;
vector<int> ans(q);
for (int i = 0; i < id; ++i) {
// Updates jumps:
upd(id, i, 0, mns[i]);
for (int j = 1; j < SZ; ++j) upd(id, i, j, qry(id, tl[i + id][j - 1], id, j - 1));
// Finds query answers:
for (pair<pii, int> query : fq[i]) {
int start = query.first.first, end = query.first.second, ind = query.second, count = 1;
if (end > i) count = -1;
while (start > end) {
if (qry(id, start, i + 1, SZ - 1) > end) {
count = -1;
break;
}
int j = SZ - 1;
for (; j >= 0; --j) {
if (qry(id, start, i + 1, j) > end) {
count += (1 << j);
start = qry(id, start, i + 1, j);
break;
}
}
if (j < 0) ++count, start = qry(id, start, i + 1, 0);
}
ans[ind] = count;
}
}
for (int num : ans) cout << (num < 0 ? "impossible" : to_string(num)) << '\n';
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
811 ms |
51300 KB |
Output is correct |
3 |
Correct |
744 ms |
51796 KB |
Output is correct |
4 |
Correct |
811 ms |
51788 KB |
Output is correct |
5 |
Correct |
835 ms |
56568 KB |
Output is correct |
6 |
Correct |
831 ms |
61008 KB |
Output is correct |
7 |
Correct |
799 ms |
62412 KB |
Output is correct |
8 |
Correct |
644 ms |
99728 KB |
Output is correct |
9 |
Correct |
618 ms |
99636 KB |
Output is correct |
10 |
Correct |
558 ms |
52684 KB |
Output is correct |
11 |
Correct |
596 ms |
71476 KB |
Output is correct |
12 |
Correct |
329 ms |
51820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
4 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
4 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
4 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
308 ms |
6848 KB |
Output is correct |
20 |
Correct |
288 ms |
6088 KB |
Output is correct |
21 |
Correct |
190 ms |
6468 KB |
Output is correct |
22 |
Correct |
72 ms |
6344 KB |
Output is correct |
23 |
Correct |
85 ms |
8620 KB |
Output is correct |
24 |
Correct |
78 ms |
8620 KB |
Output is correct |
25 |
Correct |
173 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
724 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
4 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
776 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
1236 KB |
Output is correct |
17 |
Correct |
5 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
380 ms |
48516 KB |
Output is correct |
20 |
Correct |
351 ms |
48332 KB |
Output is correct |
21 |
Correct |
329 ms |
48588 KB |
Output is correct |
22 |
Correct |
443 ms |
65740 KB |
Output is correct |
23 |
Correct |
590 ms |
95796 KB |
Output is correct |
24 |
Correct |
636 ms |
95808 KB |
Output is correct |
25 |
Correct |
56 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
799 ms |
51364 KB |
Output is correct |
2 |
Correct |
736 ms |
51788 KB |
Output is correct |
3 |
Correct |
824 ms |
51780 KB |
Output is correct |
4 |
Correct |
611 ms |
99660 KB |
Output is correct |
5 |
Correct |
554 ms |
52656 KB |
Output is correct |
6 |
Correct |
952 ms |
99780 KB |
Output is correct |
7 |
Correct |
917 ms |
99916 KB |
Output is correct |
8 |
Correct |
809 ms |
99908 KB |
Output is correct |
9 |
Correct |
615 ms |
95756 KB |
Output is correct |
10 |
Correct |
964 ms |
99132 KB |
Output is correct |
11 |
Correct |
810 ms |
98880 KB |
Output is correct |
12 |
Correct |
968 ms |
99020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
811 ms |
51300 KB |
Output is correct |
3 |
Correct |
744 ms |
51796 KB |
Output is correct |
4 |
Correct |
811 ms |
51788 KB |
Output is correct |
5 |
Correct |
835 ms |
56568 KB |
Output is correct |
6 |
Correct |
831 ms |
61008 KB |
Output is correct |
7 |
Correct |
799 ms |
62412 KB |
Output is correct |
8 |
Correct |
644 ms |
99728 KB |
Output is correct |
9 |
Correct |
618 ms |
99636 KB |
Output is correct |
10 |
Correct |
558 ms |
52684 KB |
Output is correct |
11 |
Correct |
596 ms |
71476 KB |
Output is correct |
12 |
Correct |
329 ms |
51820 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
4 ms |
1236 KB |
Output is correct |
20 |
Correct |
4 ms |
1236 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
4 ms |
1236 KB |
Output is correct |
29 |
Correct |
4 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
308 ms |
6848 KB |
Output is correct |
32 |
Correct |
288 ms |
6088 KB |
Output is correct |
33 |
Correct |
190 ms |
6468 KB |
Output is correct |
34 |
Correct |
72 ms |
6344 KB |
Output is correct |
35 |
Correct |
85 ms |
8620 KB |
Output is correct |
36 |
Correct |
78 ms |
8620 KB |
Output is correct |
37 |
Correct |
173 ms |
8172 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
3 ms |
776 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
3 ms |
724 KB |
Output is correct |
44 |
Correct |
4 ms |
1236 KB |
Output is correct |
45 |
Correct |
5 ms |
1236 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
380 ms |
48516 KB |
Output is correct |
48 |
Correct |
351 ms |
48332 KB |
Output is correct |
49 |
Correct |
329 ms |
48588 KB |
Output is correct |
50 |
Correct |
443 ms |
65740 KB |
Output is correct |
51 |
Correct |
590 ms |
95796 KB |
Output is correct |
52 |
Correct |
636 ms |
95808 KB |
Output is correct |
53 |
Correct |
56 ms |
980 KB |
Output is correct |
54 |
Correct |
799 ms |
51364 KB |
Output is correct |
55 |
Correct |
736 ms |
51788 KB |
Output is correct |
56 |
Correct |
824 ms |
51780 KB |
Output is correct |
57 |
Correct |
611 ms |
99660 KB |
Output is correct |
58 |
Correct |
554 ms |
52656 KB |
Output is correct |
59 |
Correct |
952 ms |
99780 KB |
Output is correct |
60 |
Correct |
917 ms |
99916 KB |
Output is correct |
61 |
Correct |
809 ms |
99908 KB |
Output is correct |
62 |
Correct |
615 ms |
95756 KB |
Output is correct |
63 |
Correct |
964 ms |
99132 KB |
Output is correct |
64 |
Correct |
810 ms |
98880 KB |
Output is correct |
65 |
Correct |
968 ms |
99020 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
806 ms |
51360 KB |
Output is correct |
68 |
Correct |
752 ms |
51896 KB |
Output is correct |
69 |
Correct |
808 ms |
51692 KB |
Output is correct |
70 |
Correct |
848 ms |
56500 KB |
Output is correct |
71 |
Correct |
806 ms |
61016 KB |
Output is correct |
72 |
Correct |
808 ms |
62528 KB |
Output is correct |
73 |
Correct |
615 ms |
99788 KB |
Output is correct |
74 |
Correct |
653 ms |
99764 KB |
Output is correct |
75 |
Correct |
557 ms |
52656 KB |
Output is correct |
76 |
Correct |
606 ms |
71464 KB |
Output is correct |
77 |
Correct |
326 ms |
51812 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
3 ms |
724 KB |
Output is correct |
80 |
Correct |
3 ms |
724 KB |
Output is correct |
81 |
Correct |
3 ms |
724 KB |
Output is correct |
82 |
Correct |
3 ms |
724 KB |
Output is correct |
83 |
Correct |
4 ms |
1248 KB |
Output is correct |
84 |
Correct |
4 ms |
1236 KB |
Output is correct |
85 |
Correct |
1 ms |
212 KB |
Output is correct |
86 |
Correct |
309 ms |
6736 KB |
Output is correct |
87 |
Correct |
292 ms |
5920 KB |
Output is correct |
88 |
Correct |
193 ms |
6436 KB |
Output is correct |
89 |
Correct |
72 ms |
6352 KB |
Output is correct |
90 |
Correct |
86 ms |
8652 KB |
Output is correct |
91 |
Correct |
85 ms |
8612 KB |
Output is correct |
92 |
Correct |
172 ms |
8104 KB |
Output is correct |
93 |
Correct |
369 ms |
48312 KB |
Output is correct |
94 |
Correct |
371 ms |
48460 KB |
Output is correct |
95 |
Correct |
331 ms |
48460 KB |
Output is correct |
96 |
Correct |
452 ms |
65868 KB |
Output is correct |
97 |
Correct |
601 ms |
95804 KB |
Output is correct |
98 |
Correct |
629 ms |
95796 KB |
Output is correct |
99 |
Correct |
55 ms |
980 KB |
Output is correct |
100 |
Correct |
943 ms |
99892 KB |
Output is correct |
101 |
Correct |
903 ms |
99844 KB |
Output is correct |
102 |
Correct |
807 ms |
100024 KB |
Output is correct |
103 |
Correct |
981 ms |
99024 KB |
Output is correct |
104 |
Correct |
755 ms |
98772 KB |
Output is correct |
105 |
Correct |
961 ms |
99020 KB |
Output is correct |
106 |
Correct |
491 ms |
40444 KB |
Output is correct |
107 |
Correct |
683 ms |
52528 KB |
Output is correct |
108 |
Correct |
413 ms |
52544 KB |
Output is correct |
109 |
Correct |
415 ms |
52532 KB |
Output is correct |
110 |
Correct |
740 ms |
99772 KB |
Output is correct |
111 |
Correct |
730 ms |
99768 KB |
Output is correct |