题意:给你n个区间,m个询问,对于每个询问求最少可以用多少个区间完成覆盖,区间可以重叠。
思路:倍增法,f[i][j]表示i点连续向后扩展2^j个区间后可以达到的最后位置,预处理O(nlogn),注意一下细节。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6+10;
int n,m,ans,a[N],mx,f[N][21],x,y;
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
{
cin>>x>>y;
a[x]=max(a[x],y);///注意有公共起点
mx=max(mx,y);
}
for(int i=1; i<=mx; i++) a[i]=max(a[i-1],a[i]);
for(int i=0; i<=mx; i++) f[i][0]=a[i];
///倍增
for(int j=1; j<=20; j++)
for(int i=0; i<=mx; i++)
f[i][j]=f[f[i][j-1]][j-1];
while(m--)
{
ans=0;
cin>>x>>y;
for(int i=20; i>=0; i--) if(f[x][i]<y) ans+=1<<i,x=f[x][i];
if(a[x]<y) cout<<-1<<endl;
else cout<<ans+1<<endl;
}
return 0;
}