Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- _____ _ _ _ _
- |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
- | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
- | | | | | | __// ___ \| | | \__ \ | | | |_| | |
- |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
- */
- #include<bits/stdc++.h>
- #define ll long long
- #define rep(i,a,b) for(ll int i=a;i<b;i++)
- #define vi vector<ll int>
- #define all(a) (a).begin(),(a).end()
- using namespace std;
- void solve()
- {
- ll n,k;
- cin>>n>>k;
- vi v(n);
- rep(i,0,n)
- {
- cin>>v[i];
- }
- ll ans=0;
- sort(all(v));
- rep(i,0,n)
- {
- ll l=0,r=i-1;
- while(l<=r)
- {
- ll mid=(l+r)/2;
- if(abs(v[mid]-v[i])<=k)
- r=mid-1;
- else
- l=mid+1;
- }
- ans+=i-r-1;
- l=i+1;
- r=n-1;
- while(l<=r)
- {
- ll mid=(l+r)/2;
- if(abs(v[mid]-v[i])<=k)
- l=mid+1;
- else
- r=mid-1;
- }
- ans+=l-i-1;
- }
- cout<<ans/2+n;
- return;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int TESTS=1;
- // cin>>TESTS;
- while(TESTS--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement