题目


AC代码
cpp
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a[200005];
void solve()
{
ll n,q;
cin>>n>>q;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
while(q--)
{
ll l,r;
cin>>l>>r;
if(l>r)
{
cout<<0<<endl;
continue;
}
ll ans;
ll lpos=0,rpos=n+1,mid;
while(lpos+1!=rpos)
{
mid=lpos+(rpos-lpos>>1);
if(a[mid]<l)lpos=mid;
else rpos=mid;
}
ans=rpos; // 左起点下标
rpos=n+1;
while(lpos+1!=rpos)
{
mid=lpos+(rpos-lpos>>1);
if(a[mid]<=r)lpos=mid;
else rpos=mid;
}
ans=lpos-ans+1;
cout<<ans<<endl;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
个人见解
今天的题相对于昨天还是比较简单的,主要就是考察二分的使用,模板题。