P1102 A-B 数对

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

const int N=2e5+10;
int n,c;
int a[N];

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

    for(int i=1;i<=n;i++){
        cin>>a[i];
    }

    sort(a+1,a+n+1);
    int l=1,r=1;
    long long ans=0;
    for(int i=1;i<=n;i++){
        int A=a[i]+c;
        while(l<=n&&a[l]<A) l++;
        while(r<=n&&a[r]<=A) r++;
        ans+=r-l;
    }

    cout<<ans<<endl;

    return 0;
}
posted @ 2026-02-10 19:47  AnoSky  阅读(8)  评论(0)    收藏  举报