利用倍增思想,f[i][j]表示i点向右跳2^j次所能到达的最远位置
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int f[maxn][21];
int main()
{
int n, m, x, y, ans;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++){
scanf("%d%d",&x,&y);
x++; y++;
f[x][0] = max(f[x][0],y);
}
for(int i = 1; i < maxn; i++) f[i][0] = max(f[i][0],max(f[i-1][0],i));
for(int j = 1; j <= 20; j++){
for(int i = 1; i < maxn; i++){
f[i][j] = f[f[i][j-1]][j-1];
}
}
while(m--){
scanf("%d%d",&x,&y);
x++; y++;
if(f[x][20] < y){
printf("-1\n");
continue;
}
ans = 0;
for(int i = 20; i >= 0; i--){
if(f[x][i] < y){
x = f[x][i];
ans += 1<<i;
}
}
printf("%d\n",ans+1);
}
return 0;
}