Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.

Note that the order of the points inside the group of three chosen points doesn't matter.

Input

The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got.

It is guaranteed that the coordinates of the points in the input strictly increase.

Output

Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1

Note

In the first sample any group of three points meets our conditions.

In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.

In the third sample only one group does: {1, 10, 20}.

思路:维护双指针,每次右指针移一位,左指针到达最远距离,因为是递增序,两指针不减,统计答案时,每次定右指针,次数就是C(区间长度-1)(2),组合数

typedef long long LL;

const int maxm = 1e5+;

int buf[maxm];

int main() {
ios::sync_with_stdio(false), cin.tie();
int n, d;
cin >> n >> d;
for(int i = ; i < n; ++i)
cin >> buf[i];
LL ans = ;
int l = ;
for(int i = ; i < n; ++i) {
while(buf[i] - buf[l] > d) l++;
ans += (LL)(i-l) * (i-l-) / ;
}
cout << ans << "\n";
return ;
}

Day8 - A - Points on Line CodeForces - 251A的更多相关文章

  1. 【转】Points To Line

    原文地址 Python+Arcpy操作Points(.shp)转换至Polyline(.shp),仔细研读Points To Line (Data Management)说明,参数说明如下: Inpu ...

  2. Psychos in a Line CodeForces - 319B (单调栈的应用)

    Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...

  3. A. Points on Line

    A. Points on Line time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. codeforces 251A Points on Line(二分or单调队列)

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  5. 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line

    Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...

  6. codeforces251A. Points on Line

    Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. N ...

  7. 二分三元组 CodeForces - 251A

    题目链接: https://vjudge.net/problem/35188/origin 题目大意: 要求你找到一个 i < j < k时有 a[k]-a[i] <= d的组的个数 ...

  8. A - Points and Segments CodeForces - 429E

    题解: 方法非常巧妙的一道题 首先考虑要求全部为0怎么做 发现是个欧拉回路的问题(很巧妙) 直接dfs一遍就可以了 而这道题 要求是-1,1,0 我们可以先离散化 完了之后判断每个点被奇数还是偶数条边 ...

  9. codeforces 576c// Points on Plane// Codeforces Round #319(Div. 1)

    题意:有n个点,找到一个顺序走遍这n个点,并且曼哈顿距离不超过25e8. 由于给的点坐标都在0-1e6之间.将x轴分成1000*1000,即1000长度为1块.将落在同一块的按y排序,编号为奇的块和偶 ...

随机推荐

  1. 融资拿钱----HHR计划---第四课

    第一节:开始学习 1,预热思考题: (1)投资人愿意投你?你想过你的投资逻辑是什么吗?(赚钱=行业天花板*成功的概率=细分行业天花板*集中度*whynow*whyme) (2)融资就是为了拿钱吗?你了 ...

  2. @implementer,抽象类,接口

    @implementer,抽象类,接口 1.      implementer 在看twisted源码时,经常出现@implementer(IReactorFDSet) 它来自zope.interfa ...

  3. android原始sqlite中query的复杂用法

    android直接执行sql是execSQL(String sql). 这个方法可以执行任意sql语句.但是改变这个不够灵活. query这个方法可以很好的解决这个问题. 执行query查询指定的数据 ...

  4. CSS垂直居中的8种方法

    CSS垂直居中的8种方法 1.通过verticle-align:middle实现CSS垂直居中. 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注 ...

  5. vue基础语法摘要

    1. 2. 3. 4. 5. 6. 7.“动态路由”和“编程式路由”参数的接收方式:路由的参数-----页面之间跳转的参数

  6. Python 基础之集合相关操作与函数和字典相关函数

    一:集合相关操作与相关函数 1.集合相关操作(交叉并补) (1)intersection() 交集 set1 = {"one","two","thre ...

  7. JAVA版StarDict星际译王简单实现

    由胡正开发的星际译王是Linux平台上很强大的一个开源的翻译软件(也有Windows版本的)支持多种词库.多种语言版本.尤其词库设计比较合理.之前看到一篇博文<星际译王词库应用-自制英汉词典&g ...

  8. iOS10打电话、发短信、发邮件等小功能

    注意:iOS10.0以后,使用openURL会有延迟,需要使用 openURL: options: completionHandler: 一.概要 本文中主要就是介绍在iOS中实现打电话.发短信.发邮 ...

  9. request DELETE 请求

    Django对于PUT/DELETE请求并没有像POST/GET那样有一个字典结构.我们需要手动处理request.body获取参数: 第一种方式: ujson.loads(request.body. ...

  10. 初始化加载和导航时脚本执行的函数(初始化脚本执行环境)page.evaluateOnNewDocument

    /** * Copyright 2017 Google Inc., PhantomJS Authors All rights reserved. * * Licensed under the Apac ...