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;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
vector<int> lmao[100005];
int S[100005], E[100005], dist[100005];
int n, q;
int par[20][100005];
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++)cin >> S[i] >> E[i];
set <pi> st;
for(int i=1;i<=n;i++)st.insert({E[i], i});
for(int i=1;i<=n;i++){
set<pi> :: iterator it = st.lower_bound({S[i], 0});
while(it != st.end()){
pi tmp = *it;
if(tmp.fi > E[i])break;
par[0][tmp.se] = i;
it++;
}
}
for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)par[i][j] = par[i-1][par[i-1][j]];
while(q--){
int x, y; cin >> x >> y;
int ans = 0, cur = x;
for(int i=19;i>=0;i--){
if(par[i][cur] != 0 && par[i][cur] <= y)cur = par[i][cur], ans += (1 << i);
}
if(cur == y)cout << ans << '\n';
else cout << "impossible\n";
}
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
events.cpp:45:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
45 | main(){
| ^~~~
# | 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... |