#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<ll> vll;
typedef vector<vll> vvll;
const int inf = 1e9 + 7;
struct segmentTree
{
vp st;
int size;
segmentTree() {};
void init(int n) {
for (size = 1;size < n;size *= 2) {}
st.resize(size * 2, { inf, -1 });
}
void update(int i, pii val) {
i += size;
if (st[i].first <= val.first)return;
st[i] = val;
for (i /= 2;i;i /= 2) {
if (st[i * 2].first <= st[i * 2 + 1].first) {
st[i] = st[i * 2];
}
else st[i] = st[i * 2 + 1];
}
}
pii query(int l, int r) {
pii ans = { inf,0 };
for (l += size, r += size;l <= r;l /= 2, r /= 2) {
if (l % 2) {
if (ans.first > st[l].first)ans = st[l];
l++;
}
if (r % 2 == 0) {
if (ans.first > st[r].first)ans = st[r];
r--;
}
}
return ans;
}
};
vi times;
vvi table;
vp events;
vector<pair<int, pii>> endTimes;
int mylog;
int query(int start, int end) {
if (start == end)return 0;
if (events[start].second > events[end].second)return -1;
int wall = events[start].second;
int jumps = 1;
if (events[end].first <= wall && wall <= events[end].second) return jumps;
for (int i = mylog;i >= 0;i--) {
if (events[table[i][end]].first > wall) {
jumps += 1 << i;
end = table[i][end];
}
}
jumps++;
end = table[0][end];
if (events[end].first <= wall && wall <= events[end].second)return jumps;
return -1;
}
int id(int val) {
int ans = lower_bound(times.begin(), times.end(), val) - times.begin();
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
mylog = ceil(log2(n + 1));
events.resize(n);
endTimes.resize(n);
table.resize(mylog + 1, vi(n));
for (int i = 0;i < n;i++) {
cin >> events[i].first >> events[i].second;
times.push_back(events[i].first);
times.push_back(events[i].second);
endTimes[i] = { events[i].second, {events[i].first, i} };
}
times.push_back(inf);
sort(times.begin(), times.end());
sort(endTimes.begin(), endTimes.end());
segmentTree st;
st.init(times.size());
for (int i = 0;i < endTimes.size();i++) {
int index = endTimes[i].second.second;
st.update(id(events[index].second), { events[index].first, index });
int far = st.query(id(events[index].first), id(events[index].second)).second;
table[0][index] = far;
}
for (int i = 1;i <= mylog;i++) {
for (int v = 0;v < n;v++) {
int p = table[i - 1][v];
table[i][v] = table[i - 1][p];
}
}
int s, e;
for (int i = 0;i < q;i++) {
cin >> s >> e;
s--; e--;
int ans = query(s, e);
if (ans == -1) {
cout << "impossible\n";
}
else {
cout << ans << '\n';
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:110:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 0;i < endTimes.size();i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
91 ms |
18404 KB |
Output is correct |
3 |
Correct |
110 ms |
18448 KB |
Output is correct |
4 |
Correct |
129 ms |
18340 KB |
Output is correct |
5 |
Correct |
104 ms |
18508 KB |
Output is correct |
6 |
Correct |
100 ms |
18452 KB |
Output is correct |
7 |
Correct |
116 ms |
18568 KB |
Output is correct |
8 |
Correct |
108 ms |
18880 KB |
Output is correct |
9 |
Correct |
159 ms |
18852 KB |
Output is correct |
10 |
Correct |
96 ms |
18816 KB |
Output is correct |
11 |
Correct |
93 ms |
18768 KB |
Output is correct |
12 |
Correct |
60 ms |
18188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
36 ms |
2508 KB |
Output is correct |
20 |
Correct |
24 ms |
2496 KB |
Output is correct |
21 |
Correct |
26 ms |
2736 KB |
Output is correct |
22 |
Correct |
21 ms |
2900 KB |
Output is correct |
23 |
Correct |
21 ms |
2660 KB |
Output is correct |
24 |
Correct |
19 ms |
2636 KB |
Output is correct |
25 |
Correct |
22 ms |
2288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
61 ms |
16704 KB |
Output is correct |
20 |
Correct |
57 ms |
15860 KB |
Output is correct |
21 |
Correct |
66 ms |
16576 KB |
Output is correct |
22 |
Correct |
66 ms |
16604 KB |
Output is correct |
23 |
Correct |
66 ms |
16688 KB |
Output is correct |
24 |
Correct |
67 ms |
16688 KB |
Output is correct |
25 |
Correct |
33 ms |
15932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
18368 KB |
Output is correct |
2 |
Correct |
95 ms |
18380 KB |
Output is correct |
3 |
Correct |
102 ms |
18448 KB |
Output is correct |
4 |
Correct |
104 ms |
18880 KB |
Output is correct |
5 |
Correct |
93 ms |
18696 KB |
Output is correct |
6 |
Correct |
107 ms |
18568 KB |
Output is correct |
7 |
Correct |
102 ms |
18536 KB |
Output is correct |
8 |
Correct |
97 ms |
18636 KB |
Output is correct |
9 |
Correct |
68 ms |
16584 KB |
Output is correct |
10 |
Correct |
98 ms |
18196 KB |
Output is correct |
11 |
Correct |
93 ms |
17968 KB |
Output is correct |
12 |
Correct |
98 ms |
18124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
91 ms |
18404 KB |
Output is correct |
3 |
Correct |
110 ms |
18448 KB |
Output is correct |
4 |
Correct |
129 ms |
18340 KB |
Output is correct |
5 |
Correct |
104 ms |
18508 KB |
Output is correct |
6 |
Correct |
100 ms |
18452 KB |
Output is correct |
7 |
Correct |
116 ms |
18568 KB |
Output is correct |
8 |
Correct |
108 ms |
18880 KB |
Output is correct |
9 |
Correct |
159 ms |
18852 KB |
Output is correct |
10 |
Correct |
96 ms |
18816 KB |
Output is correct |
11 |
Correct |
93 ms |
18768 KB |
Output is correct |
12 |
Correct |
60 ms |
18188 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
36 ms |
2508 KB |
Output is correct |
32 |
Correct |
24 ms |
2496 KB |
Output is correct |
33 |
Correct |
26 ms |
2736 KB |
Output is correct |
34 |
Correct |
21 ms |
2900 KB |
Output is correct |
35 |
Correct |
21 ms |
2660 KB |
Output is correct |
36 |
Correct |
19 ms |
2636 KB |
Output is correct |
37 |
Correct |
22 ms |
2288 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
316 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
61 ms |
16704 KB |
Output is correct |
48 |
Correct |
57 ms |
15860 KB |
Output is correct |
49 |
Correct |
66 ms |
16576 KB |
Output is correct |
50 |
Correct |
66 ms |
16604 KB |
Output is correct |
51 |
Correct |
66 ms |
16688 KB |
Output is correct |
52 |
Correct |
67 ms |
16688 KB |
Output is correct |
53 |
Correct |
33 ms |
15932 KB |
Output is correct |
54 |
Correct |
81 ms |
18368 KB |
Output is correct |
55 |
Correct |
95 ms |
18380 KB |
Output is correct |
56 |
Correct |
102 ms |
18448 KB |
Output is correct |
57 |
Correct |
104 ms |
18880 KB |
Output is correct |
58 |
Correct |
93 ms |
18696 KB |
Output is correct |
59 |
Correct |
107 ms |
18568 KB |
Output is correct |
60 |
Correct |
102 ms |
18536 KB |
Output is correct |
61 |
Correct |
97 ms |
18636 KB |
Output is correct |
62 |
Correct |
68 ms |
16584 KB |
Output is correct |
63 |
Correct |
98 ms |
18196 KB |
Output is correct |
64 |
Correct |
93 ms |
17968 KB |
Output is correct |
65 |
Correct |
98 ms |
18124 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
82 ms |
18396 KB |
Output is correct |
68 |
Correct |
92 ms |
18428 KB |
Output is correct |
69 |
Correct |
101 ms |
18372 KB |
Output is correct |
70 |
Correct |
106 ms |
18436 KB |
Output is correct |
71 |
Correct |
97 ms |
18440 KB |
Output is correct |
72 |
Correct |
117 ms |
18580 KB |
Output is correct |
73 |
Correct |
99 ms |
18936 KB |
Output is correct |
74 |
Correct |
106 ms |
19048 KB |
Output is correct |
75 |
Correct |
96 ms |
18804 KB |
Output is correct |
76 |
Correct |
97 ms |
18768 KB |
Output is correct |
77 |
Correct |
60 ms |
18112 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
464 KB |
Output is correct |
81 |
Correct |
1 ms |
340 KB |
Output is correct |
82 |
Correct |
1 ms |
328 KB |
Output is correct |
83 |
Correct |
2 ms |
328 KB |
Output is correct |
84 |
Correct |
1 ms |
340 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
26 ms |
2504 KB |
Output is correct |
87 |
Correct |
24 ms |
2556 KB |
Output is correct |
88 |
Correct |
26 ms |
2736 KB |
Output is correct |
89 |
Correct |
22 ms |
2884 KB |
Output is correct |
90 |
Correct |
20 ms |
2700 KB |
Output is correct |
91 |
Correct |
22 ms |
2704 KB |
Output is correct |
92 |
Correct |
23 ms |
2388 KB |
Output is correct |
93 |
Correct |
60 ms |
16712 KB |
Output is correct |
94 |
Correct |
56 ms |
15816 KB |
Output is correct |
95 |
Correct |
66 ms |
16576 KB |
Output is correct |
96 |
Correct |
62 ms |
16672 KB |
Output is correct |
97 |
Correct |
67 ms |
16676 KB |
Output is correct |
98 |
Correct |
66 ms |
16692 KB |
Output is correct |
99 |
Correct |
32 ms |
16032 KB |
Output is correct |
100 |
Correct |
116 ms |
18576 KB |
Output is correct |
101 |
Correct |
98 ms |
18504 KB |
Output is correct |
102 |
Correct |
104 ms |
18700 KB |
Output is correct |
103 |
Correct |
98 ms |
18180 KB |
Output is correct |
104 |
Correct |
93 ms |
17924 KB |
Output is correct |
105 |
Correct |
97 ms |
18252 KB |
Output is correct |
106 |
Correct |
94 ms |
17856 KB |
Output is correct |
107 |
Correct |
95 ms |
17796 KB |
Output is correct |
108 |
Correct |
89 ms |
18556 KB |
Output is correct |
109 |
Correct |
96 ms |
18496 KB |
Output is correct |
110 |
Correct |
106 ms |
18432 KB |
Output is correct |
111 |
Correct |
109 ms |
18448 KB |
Output is correct |