#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
int N, Q; int S[100001], E[100001];
vector<int> adj[100001];
vector<pair<ii, int>> arr;
int lb(int v) {
int lo = 0, hi = N - 1, res = N;
while(lo <= hi) {
int md = (lo + hi) / 2;
if(arr[md].ff.ff <= v)
res = md, lo = md + 1;
else hi = md - 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> Q;
for(int l = 1; l <= N; l++) {
cin >> S[l] >> E[l];
arr.pb({{S[l], E[l]}, l});
}
sort(all(arr));
for(int l = 1; l <= N; l++) {
int i = lb(E[l]);
for(; i < N; i++) {
int s = arr[i].ff.ff, e = arr[i].ff.ss, d = arr[i].ss;
if(E[l] >= s && E[l] <= e)
adj[l].pb(i);
else if(E[l] < s) break;
}
}
vector<ii> query[N + 1]; int ans[Q];
for(int l = 0; l < Q; l++) {
int U, V; cin >> U >> V;
query[U].pb({V, l});
}
for(int U = 1; U <= N; U++) {
if(!query[U].size()) continue;
vector<int> D(N + 1, -1);
queue<int> q; q.push(U); D[U] = 0;
while(!q.empty()) {
int A = q.front(); q.pop();
for(auto u : adj[A]) {
if(D[u] != -1) continue;
D[u] = D[A] + 1; q.push(u);
}
}
for(auto V : query[U])
ans[V.ss] = D[V.ff];
}
for(int l = 0; l < Q; l++) {
if(ans[l] == -1) cout << "impossible\n";
else cout << ans[l] << "\n";
}
}
Compilation message
events.cpp: In function 'int32_t main()':
events.cpp:45:61: warning: unused variable 'd' [-Wunused-variable]
45 | int s = arr[i].ff.ff, e = arr[i].ff.ss, d = arr[i].ss;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
78 ms |
13324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |