#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 ll inf = 1e18 + 7;
vvp table;
vp events;
vector<pair<int, pii>> endTimes;
int mylog;
int jumpK(int end, int k) {
for (int i = mylog;i >= 0;i--) {
if ((k >> i) & 1) {
end = table[i][end].first;
}
}
return end;
}
bool check(int start, int end, int jumps) {
if (start == end)return true;
if (jumps == 0) {
return start == end;
}
jumps--;
int wall = events[start].second;
int far = jumpK(end, jumps);
if (events[far].first <= wall && wall <= events[far].second)return true;
return false;
}
int binSearch(int l, int r, int start, int end) {
if (!check(start, end, r + 1)) return -1;
while (l < r)
{
int m = l + (r - l) / 2;
if (check(start, end, m)) {
r = m;
}
else {
l = m + 1;
}
}
return l;
}
int query(int start, int end, int n) {
if (events[start].second > events[end].second)return -1;
int ans = binSearch(0, n, start, end);
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, vp(n));
for (int i = 0;i < n;i++) {
cin >> events[i].first >> events[i].second;
endTimes[i] = { events[i].second, {events[i].first, i} };
}
sort(endTimes.begin(), endTimes.end());
for (int i = 0;i < endTimes.size();i++) {
int index = endTimes[i].second.second;
int far = lower_bound(endTimes.begin(), endTimes.end(), (pair<int, pii>){ events[index].first, make_pair(-1,-1)}) - endTimes.begin();
table[0][index] = { endTimes[far].second.second, endTimes[far].first};
}
for (int i = 1;i <= mylog;i++) {
for (int v = 0;v < n;v++) {
int p = table[i - 1][v].first;
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, n);
if (ans == -1) {
cout << "impossible\n";
}
else {
cout << ans << '\n';
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:83: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]
83 | for (int i = 0;i < endTimes.size();i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
97 ms |
17236 KB |
Output is correct |
3 |
Incorrect |
57 ms |
17544 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
17224 KB |
Output is correct |
2 |
Incorrect |
57 ms |
17560 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
97 ms |
17236 KB |
Output is correct |
3 |
Incorrect |
57 ms |
17544 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |