#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, q, sp[100100][20], mn[100100][20], R[100100], rev[100100];
array<int, 3> a[100100];
int find(int a, int b){
int k = 31 - __builtin_clz(b - a);
return min(mn[a][k], mn[b-(1<<k)+1][k]);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for(int i=1;i<=n;i++) cin >> a[i][1] >> a[i][0], a[i][2] = i;
sort(a+1, a+1+n);
for(int i=n;i>=1;i--){
auto [e, s, id] = a[i];
R[i] = a[i+1][0] == e ? R[i+1] : i;
rev[id] = i;
mn[i][0] = lower_bound(a+1, a+1+n, array<int, 3>({s, 0, 0})) - a;
}
for(int j=1;j<20;j++) for(int i=1;i<=n;i++) if(i+(1<<j)-1 <= n) mn[i][j] = min(mn[i][j-1], mn[i+(1<<j-1)][j-1]);
for(int i=1;i<=n;i++) sp[i][0] = find(i, R[i]);
for(int j=1;j<20;j++) for(int i=1;i<=n;i++) sp[i][j] = min(sp[i][j-1], sp[sp[i][j-1]][j-1]);
while(q--){
int a, b; cin >> a >> b;
a = rev[a], b = rev[b];
if(R[b] < a) {
cout << "impossible\n";
} else if(a == b) {
cout << "0\n";
} else {
int ans = 0;
for(int i=19;i>=0;i--) if(sp[b][i] > a) b = sp[b][i], ans += 1 << i;
ans++, b = sp[b][0];
if(a < b) cout << "impossible\n";
else cout << ans << "\n";
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:27:106: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
27 | for(int j=1;j<20;j++) for(int i=1;i<=n;i++) if(i+(1<<j)-1 <= n) mn[i][j] = min(mn[i][j-1], mn[i+(1<<j-1)][j-1]);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
79 ms |
18632 KB |
Output is correct |
3 |
Correct |
80 ms |
18648 KB |
Output is correct |
4 |
Correct |
93 ms |
18640 KB |
Output is correct |
5 |
Correct |
89 ms |
18656 KB |
Output is correct |
6 |
Correct |
80 ms |
18632 KB |
Output is correct |
7 |
Correct |
90 ms |
18768 KB |
Output is correct |
8 |
Correct |
100 ms |
19072 KB |
Output is correct |
9 |
Correct |
85 ms |
19156 KB |
Output is correct |
10 |
Correct |
97 ms |
19008 KB |
Output is correct |
11 |
Correct |
118 ms |
19012 KB |
Output is correct |
12 |
Correct |
73 ms |
19124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
484 KB |
Output is correct |
5 |
Correct |
1 ms |
528 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
18628 KB |
Output is correct |
2 |
Correct |
98 ms |
18592 KB |
Output is correct |
3 |
Correct |
86 ms |
18636 KB |
Output is correct |
4 |
Correct |
79 ms |
19148 KB |
Output is correct |
5 |
Correct |
118 ms |
18976 KB |
Output is correct |
6 |
Correct |
98 ms |
18736 KB |
Output is correct |
7 |
Correct |
99 ms |
18760 KB |
Output is correct |
8 |
Correct |
86 ms |
18860 KB |
Output is correct |
9 |
Correct |
54 ms |
17976 KB |
Output is correct |
10 |
Correct |
105 ms |
18448 KB |
Output is correct |
11 |
Correct |
87 ms |
18184 KB |
Output is correct |
12 |
Correct |
78 ms |
18420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
79 ms |
18632 KB |
Output is correct |
3 |
Correct |
80 ms |
18648 KB |
Output is correct |
4 |
Correct |
93 ms |
18640 KB |
Output is correct |
5 |
Correct |
89 ms |
18656 KB |
Output is correct |
6 |
Correct |
80 ms |
18632 KB |
Output is correct |
7 |
Correct |
90 ms |
18768 KB |
Output is correct |
8 |
Correct |
100 ms |
19072 KB |
Output is correct |
9 |
Correct |
85 ms |
19156 KB |
Output is correct |
10 |
Correct |
97 ms |
19008 KB |
Output is correct |
11 |
Correct |
118 ms |
19012 KB |
Output is correct |
12 |
Correct |
73 ms |
19124 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
484 KB |
Output is correct |
17 |
Correct |
1 ms |
528 KB |
Output is correct |
18 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |