#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
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int par[20][200005], n, q, S[200005], E[200005];
pi A[200005], B[200005];
void solve(){
cin >> n >> q;
for(int i=1;i<=n;i++){
cin >> S[i] >> E[i];
A[i] = {S[i], i};
B[i] = {E[i], i};
}
sort(A+1, A+n+1);
sort(B+1, B+n+1);
int in = 1, mx = 0, idx = -1;
for(int i=1;i<=n;i++){
while(in <= n && A[in].fi <= B[i].fi){
if(mx < E[A[in].se])mx = E[A[in].se], idx = A[in].se;
in++;
}
if(mx > B[i].fi)par[0][B[i].se] = idx;
}
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 s, e; cin >> s >> e;
if(E[s] > E[e])cout << "impossible\n";
else if(s == e)cout << 0 << '\n';
else if(S[e] <= E[s] && E[s] <= E[e])cout << 1 << '\n';
else{
int ans = 0, cur = s;
for(int i=19;i>=0;i--){
int x = par[i][cur];
if(x == 0)continue;
if(E[x] < S[e])cur = par[i][cur], ans += (1 << i);
}
if(par[0][cur] == 0){
cout << "impossible\n";
continue;
}
cur = par[0][cur];
ans+=2;
if(S[e] <= E[cur] && E[cur] <= E[e])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
events.cpp:59:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
59 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
61 ms |
24464 KB |
Output is correct |
3 |
Correct |
83 ms |
24404 KB |
Output is correct |
4 |
Correct |
140 ms |
24476 KB |
Output is correct |
5 |
Correct |
89 ms |
24484 KB |
Output is correct |
6 |
Correct |
80 ms |
24560 KB |
Output is correct |
7 |
Correct |
81 ms |
24596 KB |
Output is correct |
8 |
Correct |
70 ms |
24908 KB |
Output is correct |
9 |
Correct |
65 ms |
24148 KB |
Output is correct |
10 |
Correct |
89 ms |
24856 KB |
Output is correct |
11 |
Correct |
93 ms |
24856 KB |
Output is correct |
12 |
Correct |
42 ms |
23388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
692 KB |
Output is correct |
5 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
692 KB |
Output is correct |
5 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
692 KB |
Output is correct |
5 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
24452 KB |
Output is correct |
2 |
Correct |
91 ms |
24436 KB |
Output is correct |
3 |
Correct |
122 ms |
24400 KB |
Output is correct |
4 |
Correct |
64 ms |
24144 KB |
Output is correct |
5 |
Correct |
98 ms |
24848 KB |
Output is correct |
6 |
Correct |
83 ms |
24648 KB |
Output is correct |
7 |
Correct |
80 ms |
24608 KB |
Output is correct |
8 |
Correct |
73 ms |
24748 KB |
Output is correct |
9 |
Correct |
37 ms |
22716 KB |
Output is correct |
10 |
Correct |
85 ms |
24164 KB |
Output is correct |
11 |
Correct |
69 ms |
23996 KB |
Output is correct |
12 |
Correct |
77 ms |
24204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
61 ms |
24464 KB |
Output is correct |
3 |
Correct |
83 ms |
24404 KB |
Output is correct |
4 |
Correct |
140 ms |
24476 KB |
Output is correct |
5 |
Correct |
89 ms |
24484 KB |
Output is correct |
6 |
Correct |
80 ms |
24560 KB |
Output is correct |
7 |
Correct |
81 ms |
24596 KB |
Output is correct |
8 |
Correct |
70 ms |
24908 KB |
Output is correct |
9 |
Correct |
65 ms |
24148 KB |
Output is correct |
10 |
Correct |
89 ms |
24856 KB |
Output is correct |
11 |
Correct |
93 ms |
24856 KB |
Output is correct |
12 |
Correct |
42 ms |
23388 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
692 KB |
Output is correct |
17 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |