-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1102.cpp
55 lines (55 loc) · 1016 Bytes
/
P1102.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
long num[200000];
long n, c, sum;
int binSearch(int x, int l, int r)
{
while (l < r)
{
int mid = (l + r) >> 1;
if (num[mid] >= x)
{
r = mid;
}
else
l = mid + 1;
}
if (x == num[r])
return l;
return -1;
}
int binSearch2(int x, int l, int r)
{
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (num[mid] <= x)
{
l = mid;
}
else
r = mid - 1;
}
if (x == num[r])
return r;
return -1;
}
int main()
{
cin >> n >> c;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
sort(num, num + n);
for (int i = 0; i < n - 1; i++)
{
long A = c + num[i];
int locat1 = binSearch(A, i + 1, n - 1);
int locat2 = binSearch2(A, i + 1, n - 1);
if (locat1 == -1)
continue;
sum += locat2 - locat1 + 1;
}
cout << sum << endl;
}