天天看點

codeforces 153 A. Points on Line 簡單數學題 簡單推導推導的事情

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100002;
int n,d,a[maxn],q[maxn];
long long int  ans;
int main()
{
    while(~scanf("%d%d",&n,&d))
    {
        int i,j,l=1,r=1;
        ans=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            while(l<r&&a[i]-a[l]>d) l++;
             ans+=(long long)(r-l)*(r-l-1)/2;
           ++r;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
4 3
1 2 3 4

4 2
-3 -2 -1 0
*/