#include<bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
int n, q;
int a[20][MAX], S[MAX], E[MAX], v[MAX], id[MAX], _prev[20][MAX];
int lg(int i){
return i ? __builtin_clz(1) - __builtin_clz(i) : -1;
}
void init(){
for(int i = 1; i < 20; i++){
for(int j = 1; j + (1 << i) - 1 < MAX; j++){
if(v[a[i - 1][j]] < v[a[i - 1][j + (1 << (i - 1))]]){
a[i][j] = a[i - 1][j];
}else{
a[i][j] = a[i - 1][j + (1 << (i - 1))];
}
}
}
}
int query(int l, int r){
int k = lg(r - l + 1);
if(v[a[k][l]] <= v[a[k][r - (1 << k) + 1]]) return id[a[k][l]];
return id[a[k][r - (1 << k) + 1]];
}
void solve(){
cin >> n >> q;
map<int, int> m;
for(int i = 1; i <= n; i++) cin >> S[i] >> E[i], m[S[i]], m[E[i]];
int idx = 0;
for(auto& i : m) i.second = ++idx;
for(int i = 1; i <= n; i++) S[i] = m[S[i]], E[i] = m[E[i]];
fill(v + 1, v + 1 + m.size(), MAX);
for (int i = 1; i <= n; i++)
{
if(v[E[i]] > S[i]){
v[E[i]] = S[i];
id[E[i]] = i;
}
}
for (int i = 1; i < MAX; i++) a[0][i] = i;
init();
for (int i = 1; i <= n; i++){
_prev[0][i] = query(S[i], E[i]);
if(_prev[0][i] == i) _prev[0][i] = 0;
}
for (int i = 1; i < 20; i++)
{
for(int j = 1; j <= n; j++){
_prev[i][j] = _prev[i - 1][_prev[i - 1][j]];
}
}
while (q--)
{
int s, e;
cin >> s >> e;
if(s == e){
cout << 0 << endl;
continue;
}
if(E[s] > E[e]){
cout << "impossible" << endl;
continue;
}
int ans = 0;
for(int i = 19; i >= 0; i--){
if(S[_prev[i][e]] > E[s]){
e = _prev[i][e];
ans += (1 << i);
}
}
e = _prev[0][e];
ans++;
if(e == 0) cout << "impossible" << endl;
else cout << ans + 1 << endl;
}
}
int main(){
int t = 1;
//cin>>t;
for(int i = 1; i <= t; i++){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20724 KB |
Output is correct |
2 |
Correct |
359 ms |
38584 KB |
Output is correct |
3 |
Correct |
371 ms |
38428 KB |
Output is correct |
4 |
Incorrect |
412 ms |
38468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20692 KB |
Output is correct |
2 |
Correct |
14 ms |
20648 KB |
Output is correct |
3 |
Correct |
15 ms |
20840 KB |
Output is correct |
4 |
Incorrect |
17 ms |
20832 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20692 KB |
Output is correct |
2 |
Correct |
14 ms |
20648 KB |
Output is correct |
3 |
Correct |
15 ms |
20840 KB |
Output is correct |
4 |
Incorrect |
17 ms |
20832 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20692 KB |
Output is correct |
2 |
Correct |
14 ms |
20648 KB |
Output is correct |
3 |
Correct |
15 ms |
20840 KB |
Output is correct |
4 |
Incorrect |
17 ms |
20832 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
362 ms |
38488 KB |
Output is correct |
2 |
Correct |
414 ms |
38468 KB |
Output is correct |
3 |
Incorrect |
428 ms |
38520 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20724 KB |
Output is correct |
2 |
Correct |
359 ms |
38584 KB |
Output is correct |
3 |
Correct |
371 ms |
38428 KB |
Output is correct |
4 |
Incorrect |
412 ms |
38468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |