二分板子
代码
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const ll N=1e6+5;
ll p[N];
ll o[N];
int main()
{
ll n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>p[i];
sort(p+1,p+1+n);
for(int i=1;i<=n;i++)
{
o[i]=o[i-1]+p[i];
}
while(q--)
{
ll a,b;
cin>>a>>b;
int l=1,r=n;
while(l<=r)
{
int mid=(l+r)/2;
if(p[mid]>b)r=mid-1;
else l=mid+1;
}
if(l-1>a)
{
cout<<o[l-1]-o[l-1-a]<<endl;
}
else
{
cout<<o[l-1]<<endl;
}
}
return 0;
}