#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, q, sp[100100][20], mn[100100][20], R[100100], rev[100100], p[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];
if(a[i][0] == a[i+1][0] && a[i][1] == a[i+1][1]) p[i] = p[i+1] ? p[i+1] : i+1;
R[i] = a[i+1][0] == e ? R[i+1] : i;
rev[id] = i;
sp[i][0] = 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]);
while(q--){
int a, b; cin >> a >> b;
a = rev[a], b = rev[b];
int e = R[b];
if(R[b] < a) {
cout << "impossible\n";
} else if(a == b) {
cout << "0\n";
} else if(p[a] && p[b] && p[a] == p[b]) {
cout << "1\n";
} else {
int ans = 0;
while(a < b && ++ans){
int l = b;
b = find(b, e);
if(b == l){
ans = -1;
break;
}
}
if(ans < 0) cout << "impossible\n";
else cout << ans << "\n";
}
}
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:29:106: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
29 | 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]);
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1584 ms |
17936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
476 KB |
Output is correct |
14 |
Correct |
1 ms |
536 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1230 ms |
1840 KB |
Output is correct |
20 |
Execution timed out |
1550 ms |
1484 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 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 |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
136 ms |
17996 KB |
Output is correct |
20 |
Correct |
59 ms |
18076 KB |
Output is correct |
21 |
Correct |
53 ms |
18420 KB |
Output is correct |
22 |
Incorrect |
51 ms |
18060 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1586 ms |
17936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
1584 ms |
17936 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |