This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |