#include<stdio.h> int main() { int i,j,n,k,p; __int64 t=0,t1=0,a[100001]={1}; scanf("%d %d",&n,&p); scanf("%I64d",&a[1]); for(i=2;i<=n;i++) { scanf("%I64d",&a[i]); while((a[i]-a[a[0]])>p)a[0]++; t+=((i-a[0])*(i-a[0]-1))/2; } printf("%I64d",t); return 0; }
看代码长度 像一道水题。。