#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <climits>
using namespace std;
#define pii pair<int, int>
vector<vector<int>> tl;
// Segtree operations for maintaining reachable left nodes:
void btl(int n) {
tl = vector<vector<int>>(2 * n, vector<int>(30));
for (int i = 0; i < n; ++i) for (int j = 0; j < 30; ++j) tl[i + n][j] = i;
for (int i = n - 1; i > 0; --i) for (int j = 0; j < 30; ++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>(30));
for (int i = 0; i < id; ++i) for (int j = 0; j < 30; ++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 < 30; ++j) upd(id, i, j, qry(id, tl[i + id][j - 1], id, j - 1));
//cout << "JUMP " << tl[i + id][0] << '\n';
// 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;
//if (start > end) cout << "DOING " << start << ' ' << end << '\n';
while (start > end) {
//cout << start << ' ' << '\n';
if (qry(id, start, i + 1, 29) > end) {
count = -1;
break;
}
int j = 29;
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 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1175 ms |
63828 KB |
Output is correct |
3 |
Correct |
1041 ms |
64292 KB |
Output is correct |
4 |
Correct |
1101 ms |
64156 KB |
Output is correct |
5 |
Correct |
1208 ms |
69936 KB |
Output is correct |
6 |
Correct |
1123 ms |
75316 KB |
Output is correct |
7 |
Correct |
1192 ms |
77008 KB |
Output is correct |
8 |
Correct |
796 ms |
120620 KB |
Output is correct |
9 |
Correct |
778 ms |
118444 KB |
Output is correct |
10 |
Correct |
690 ms |
62076 KB |
Output is correct |
11 |
Correct |
799 ms |
84668 KB |
Output is correct |
12 |
Correct |
402 ms |
61208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
5 ms |
824 KB |
Output is correct |
7 |
Correct |
8 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
1468 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
5 ms |
824 KB |
Output is correct |
7 |
Correct |
8 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
1468 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
4 ms |
824 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
824 KB |
Output is correct |
16 |
Correct |
5 ms |
1468 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
451 ms |
8332 KB |
Output is correct |
20 |
Correct |
406 ms |
7424 KB |
Output is correct |
21 |
Correct |
249 ms |
7988 KB |
Output is correct |
22 |
Correct |
77 ms |
7876 KB |
Output is correct |
23 |
Correct |
92 ms |
10708 KB |
Output is correct |
24 |
Correct |
102 ms |
10680 KB |
Output is correct |
25 |
Correct |
225 ms |
10068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
852 KB |
Output is correct |
6 |
Correct |
5 ms |
824 KB |
Output is correct |
7 |
Correct |
8 ms |
1364 KB |
Output is correct |
8 |
Correct |
6 ms |
1468 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 |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
4 ms |
852 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
3 ms |
836 KB |
Output is correct |
16 |
Correct |
6 ms |
1464 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
501 ms |
59788 KB |
Output is correct |
20 |
Correct |
447 ms |
58924 KB |
Output is correct |
21 |
Correct |
478 ms |
59680 KB |
Output is correct |
22 |
Correct |
558 ms |
80668 KB |
Output is correct |
23 |
Correct |
824 ms |
114612 KB |
Output is correct |
24 |
Correct |
783 ms |
114572 KB |
Output is correct |
25 |
Correct |
57 ms |
1068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1130 ms |
63820 KB |
Output is correct |
2 |
Correct |
1012 ms |
64292 KB |
Output is correct |
3 |
Correct |
1146 ms |
64184 KB |
Output is correct |
4 |
Correct |
790 ms |
120664 KB |
Output is correct |
5 |
Correct |
699 ms |
62068 KB |
Output is correct |
6 |
Correct |
1237 ms |
118572 KB |
Output is correct |
7 |
Correct |
1180 ms |
118568 KB |
Output is correct |
8 |
Correct |
1045 ms |
118700 KB |
Output is correct |
9 |
Correct |
767 ms |
114580 KB |
Output is correct |
10 |
Correct |
1339 ms |
117808 KB |
Output is correct |
11 |
Correct |
1038 ms |
117548 KB |
Output is correct |
12 |
Correct |
1278 ms |
117804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1175 ms |
63828 KB |
Output is correct |
3 |
Correct |
1041 ms |
64292 KB |
Output is correct |
4 |
Correct |
1101 ms |
64156 KB |
Output is correct |
5 |
Correct |
1208 ms |
69936 KB |
Output is correct |
6 |
Correct |
1123 ms |
75316 KB |
Output is correct |
7 |
Correct |
1192 ms |
77008 KB |
Output is correct |
8 |
Correct |
796 ms |
120620 KB |
Output is correct |
9 |
Correct |
778 ms |
118444 KB |
Output is correct |
10 |
Correct |
690 ms |
62076 KB |
Output is correct |
11 |
Correct |
799 ms |
84668 KB |
Output is correct |
12 |
Correct |
402 ms |
61208 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
852 KB |
Output is correct |
16 |
Correct |
3 ms |
852 KB |
Output is correct |
17 |
Correct |
4 ms |
852 KB |
Output is correct |
18 |
Correct |
5 ms |
824 KB |
Output is correct |
19 |
Correct |
8 ms |
1364 KB |
Output is correct |
20 |
Correct |
6 ms |
1468 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
852 KB |
Output is correct |
25 |
Correct |
4 ms |
824 KB |
Output is correct |
26 |
Correct |
3 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
824 KB |
Output is correct |
28 |
Correct |
5 ms |
1468 KB |
Output is correct |
29 |
Correct |
6 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
451 ms |
8332 KB |
Output is correct |
32 |
Correct |
406 ms |
7424 KB |
Output is correct |
33 |
Correct |
249 ms |
7988 KB |
Output is correct |
34 |
Correct |
77 ms |
7876 KB |
Output is correct |
35 |
Correct |
92 ms |
10708 KB |
Output is correct |
36 |
Correct |
102 ms |
10680 KB |
Output is correct |
37 |
Correct |
225 ms |
10068 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
4 ms |
852 KB |
Output is correct |
41 |
Correct |
4 ms |
852 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
3 ms |
836 KB |
Output is correct |
44 |
Correct |
6 ms |
1464 KB |
Output is correct |
45 |
Correct |
6 ms |
1364 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
501 ms |
59788 KB |
Output is correct |
48 |
Correct |
447 ms |
58924 KB |
Output is correct |
49 |
Correct |
478 ms |
59680 KB |
Output is correct |
50 |
Correct |
558 ms |
80668 KB |
Output is correct |
51 |
Correct |
824 ms |
114612 KB |
Output is correct |
52 |
Correct |
783 ms |
114572 KB |
Output is correct |
53 |
Correct |
57 ms |
1068 KB |
Output is correct |
54 |
Correct |
1130 ms |
63820 KB |
Output is correct |
55 |
Correct |
1012 ms |
64292 KB |
Output is correct |
56 |
Correct |
1146 ms |
64184 KB |
Output is correct |
57 |
Correct |
790 ms |
120664 KB |
Output is correct |
58 |
Correct |
699 ms |
62068 KB |
Output is correct |
59 |
Correct |
1237 ms |
118572 KB |
Output is correct |
60 |
Correct |
1180 ms |
118568 KB |
Output is correct |
61 |
Correct |
1045 ms |
118700 KB |
Output is correct |
62 |
Correct |
767 ms |
114580 KB |
Output is correct |
63 |
Correct |
1339 ms |
117808 KB |
Output is correct |
64 |
Correct |
1038 ms |
117548 KB |
Output is correct |
65 |
Correct |
1278 ms |
117804 KB |
Output is correct |
66 |
Correct |
0 ms |
212 KB |
Output is correct |
67 |
Correct |
1145 ms |
60696 KB |
Output is correct |
68 |
Correct |
1014 ms |
61236 KB |
Output is correct |
69 |
Correct |
1118 ms |
61088 KB |
Output is correct |
70 |
Correct |
1215 ms |
66888 KB |
Output is correct |
71 |
Correct |
1186 ms |
72132 KB |
Output is correct |
72 |
Correct |
1131 ms |
73892 KB |
Output is correct |
73 |
Correct |
829 ms |
118460 KB |
Output is correct |
74 |
Correct |
804 ms |
118448 KB |
Output is correct |
75 |
Correct |
722 ms |
62072 KB |
Output is correct |
76 |
Correct |
754 ms |
84572 KB |
Output is correct |
77 |
Correct |
418 ms |
61280 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
876 KB |
Output is correct |
80 |
Correct |
3 ms |
852 KB |
Output is correct |
81 |
Correct |
3 ms |
852 KB |
Output is correct |
82 |
Correct |
3 ms |
852 KB |
Output is correct |
83 |
Correct |
6 ms |
1364 KB |
Output is correct |
84 |
Correct |
6 ms |
1440 KB |
Output is correct |
85 |
Correct |
2 ms |
212 KB |
Output is correct |
86 |
Correct |
465 ms |
7352 KB |
Output is correct |
87 |
Correct |
417 ms |
6396 KB |
Output is correct |
88 |
Correct |
247 ms |
6956 KB |
Output is correct |
89 |
Correct |
78 ms |
6848 KB |
Output is correct |
90 |
Correct |
102 ms |
9636 KB |
Output is correct |
91 |
Correct |
93 ms |
9600 KB |
Output is correct |
92 |
Correct |
222 ms |
9108 KB |
Output is correct |
93 |
Correct |
503 ms |
57912 KB |
Output is correct |
94 |
Correct |
492 ms |
57728 KB |
Output is correct |
95 |
Correct |
411 ms |
57896 KB |
Output is correct |
96 |
Correct |
581 ms |
78680 KB |
Output is correct |
97 |
Correct |
854 ms |
114488 KB |
Output is correct |
98 |
Correct |
815 ms |
114672 KB |
Output is correct |
99 |
Correct |
54 ms |
980 KB |
Output is correct |
100 |
Correct |
1224 ms |
118572 KB |
Output is correct |
101 |
Correct |
1185 ms |
118640 KB |
Output is correct |
102 |
Correct |
1025 ms |
118700 KB |
Output is correct |
103 |
Correct |
1307 ms |
117904 KB |
Output is correct |
104 |
Correct |
1010 ms |
117580 KB |
Output is correct |
105 |
Correct |
1413 ms |
117928 KB |
Output is correct |
106 |
Correct |
609 ms |
47384 KB |
Output is correct |
107 |
Correct |
892 ms |
61788 KB |
Output is correct |
108 |
Correct |
532 ms |
61824 KB |
Output is correct |
109 |
Correct |
527 ms |
62028 KB |
Output is correct |
110 |
Correct |
917 ms |
118556 KB |
Output is correct |
111 |
Correct |
893 ms |
118564 KB |
Output is correct |