P1102 A-B 数对 - 洛谷 | 计算机科学教育新生态
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 2e5 + 5;
int a[N];
int main() {
__int64 n, t, res = 0;
cin >> n >> t;
for (int i = 1; i <= n; i++) { cin >> a[i]; }
sort(a + 1, a + 1 + n);
for (int i = 1, j = 1, k = 1; i <= n; i++) {
while (j <= n && a[j] - a[i] < t) { j++; }
while (k <= n && a[k] - a[i] <= t) { k++; }
if (a[j] - a[i] == t && a[k - 1] - a[i] == t && k - 1 >= 1) {
res += k - j;
}
}
cout << res;
return 0;
}