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. MySQL更改默认的root账户密码

    编辑mysql的配置文件:my.ini(在MySql安装目录下). 打开配置文件,在文件最后一行添加:skip-grant-tables,然后保存退出 意思为就是在启mysql时不启动grant-ta ...

  2. 开发中,GA、Beta、GA、Trial到底是什么含义

    前言 用过maven的都应该知道,创建maven项目时,其版本号默认会以SNAPSHOT结尾,如下: 通过英文很容易就可以知道这是一个快照版本.但是,在开发中,或者使用别的软件的时候,我们常常会见到各 ...

  3. js中的原生Ajax和JQuery中的Ajax

    AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). js中的Ajax: 参数介绍: open(String method,Str ...

  4. 进程fork

    fork用于父进程创建一个子进程 返回两次 返回-1表示错误 父进程中返回创建子进程的ID,大于0 返回0是表示进入子进程 创建的子进程会继承父进程的属性,比如打开的文件描述符.工作目录.根目录等等. ...

  5. Spark入门:第4节 Spark程序:1 - 9

    五. Spark角色介绍 Spark是基于内存计算的大数据并行计算框架.因为其基于内存计算,比Hadoop中MapReduce计算框架具有更高的实时性,同时保证了高效容错性和可伸缩性.从2009年诞生 ...

  6. Navigation源码(一) move_base最全解析

    一.概述 目测是全网最全的解析,花了几个小时通读并整理的,供大家参考学习. 概况的话可以看下古月居 https://www.guyuehome.com/270,其实它是翻译官方的,英语ok的可以去ro ...

  7. list的三个子类的特点

  8. lnmp一键安装的缺点???

    自己尝试几次在新的服务器上安装lnmp环境,每次都是一键安装,然后在/home/wwwroot/default目录下放对应的PHP文件就Ok了,很方便.其实也尝试过用命令行,就是很多命令的那种安装,中 ...

  9. 吴裕雄--天生自然HADOOP操作实验学习笔记:pvuv统计案例理论

    实验目的 复习pv.uv的概念和原理 了解pv.uv的实际意义和获取方法 实验原理 前面我们已经基于mapreduce进行数据的etl处理,实验有很多不足之处,这次实验我们是基于url数据进行pv和u ...

  10. 【剑指Offer面试编程题】题目1388:跳台阶--九度OJ

    题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1<=n< ...