P1102 A-B 数对(lower_bound函数)

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=2e5+10;
LL a[N];
LL c;
int n;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>c;

    for(int i=0;i<n;i++) cin>>a[i];
    
    sort(a,a+n);

    LL ans=0;

    for(int i=0;i<n;i++){
        LL A=a[i];
        LL target=A+c;

        int l=lower_bound(a,a+n,target)-a;
        int r=upper_bound(a,a+n,target)-a;

        ans+=(r-l);
    }

    cout<<ans<<endl;
}
posted @ 2026-01-25 23:15  AnoSky  阅读(2)  评论(0)    收藏  举报