每日3

52H1Z發表於2024-11-15

include<bits/stdc++.h>

using namespace std;
int a[2000200];
int main() {
int n,c;
cin>>n>>c;
for(int i = 0;i < n;i ++)cin>>a[i];
sort(a,a + n);
long long cnt = 0;
for(int i = 0;i < n;i ++)
{

      int l = i,r = n;
      while(l < r)
      {
          int mid = (l+r)/2;
          if(a[mid] - a[i] >= c)
          {
              r = mid;
          }else l = mid + 1;
      }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    int l2 = i+1,r2 = n-1;
      while(l2<r2)
      {
          int mid = (l2 + r2 + 1)/2;
          if(a[mid]-a[i] <= c)
          {
              l2 = mid;
          }else r2 = mid - 1;
      }
      if(a[r] - a[i] == c && a[l2] - a[i] == c)
      {
          cnt += r2 - r + 1;
      }
      
  }
cout<<cnt<<endl;
  
return 0;

}

相關文章