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;
const int N = 100005;
int cost[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> l(n + 1), r(n + 1);
vector<array<int, 3>> pts(n * 2); //L / R, 0 / 1, idx
for (int i = 1; i <= n; ++i) {
cin >> l[i] >> r[i];
pts[(i - 1) * 2] = {l[i], 0, i};
pts[(i - 1) * 2 + 1] = {r[i], 1, i};
}
sort(pts.begin(), pts.end(), [&](array<int, 3> x, array<int, 3> y) {
return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0];
});
auto Find = [&](int u, int v) {
memset(cost, -1, sizeof cost);
cost[u] = 0;
set<pair<int, int>> se; // contains all Rs which have smaller L than my R and which are still not visited yet
for (int i = 0; i < n * 2; ++i) {
if (pts[i][1] == 0) {
se.emplace(r[pts[i][2]], pts[i][2]);
} else {
se.erase(make_pair(pts[i][0], pts[i][2]));
if (cost[pts[i][2]] == -1 || se.empty()) {
continue;
}
vector<pair<int, int>> to_erase;
auto it = se.begin();
while (it != se.end()) {
to_erase.push_back(*it);
if (it->first < pts[i][0]) {
to_erase.push_back(*it);
} else {
if (cost[it->second] == -1) {
cost[it->second] = cost[pts[i][2]] + 1;
} else {
cost[it->second] = min(cost[it->second], cost[pts[i][2]] + 1);
}
}
it++;
}
for (auto it : to_erase) {
se.erase(it);
}
}
}
return cost[v];
};
auto intersect = [&]( int y, int i, int j) -> bool {
return y >= i && y <= j;
};
while (q--) {
int u, v;
cin >> u >> v;
int x = Find(u, v);
if (x == -1) {
cout << "impossible" << '\n';
} else {
cout << x << '\n';
}
}
return 0;
}
Compilation message (stderr)
events.cpp: In function 'int main()':
events.cpp:57:8: warning: variable 'intersect' set but not used [-Wunused-but-set-variable]
57 | auto intersect = [&]( int y, int i, int j) -> bool {
| ^~~~~~~~~
# | 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... |