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 %I64dspecifier.

Sample test(s)
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 second

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define inf 2000000000
#define PI acos(-1.0)
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll; int a[100100],n,d;
int minx[100100][30];
int maxx[100100][30];
void init_RMQ(int n)
{
rep(i,1,n)maxx[i][0]=minx[i][0]=a[i];
for(int j = 1; j < 20; ++j)
for(int i = 1; i <= n; ++i)
if(i + (1 << j) - 1 <= n)
{
maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << (j - 1))][j - 1]);
minx[i][j] = min(minx[i][j - 1], minx[i + (1 << (j - 1))][j - 1]);
}
}
int getmax(int x,int y){
if(x>y)swap(x,y);
int k=log((y-x+1)*1.0)/log(2.0);
return max(maxx[x][k],maxx[y-(1<<k)+1][k]);
}
int getmin(int x,int y){
if(x>y)swap(x,y);
int k=log((y-x+1)*1.0)/log(2.0);
return min(minx[x][k],minx[y-(1<<k)+1][k]);
} int main(){
scanf("%d%d",&n,&d);
rep(i,1,n)scanf("%d",&a[i]);
init_RMQ(n);
int l=1;
int r=3;
long long ans=0;
rep(i,3,n){
r=i;
while(getmax(l,r)-getmin(l,r)>d)l++;
if(r-l+1>=3){
//printf("%d %d\n",l,r);
ans+=(r-l)*1LL*(r-l-1)/2LL;
}
}
printf("%I64d\n",ans);
}

s 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}.

这题也可以用rmq做,然后依次枚举l,r

codeforces251A. Points on Line的更多相关文章

  1. 【转】Points To Line

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

  2. A. Points on Line

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

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

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

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

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

  5. Day8 - A - Points on Line CodeForces - 251A

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

  6. 【Henu ACM Round#19 D】 Points on Line

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); ...

  7. POJ 3805 Separate Points (判断凸包相交)

    题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...

  8. Matlab_Graphics(1)_2D

    1.Add title ,axis Lables, and Legend to Graph: x=linspace(-*pi,2pi,); y1=sin(x); y2=cos(x); figure p ...

  9. (转)The Neural Network Zoo

    转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...

随机推荐

  1. 【Flutter】可滚动组件之CustomScrollView

    前言 CustomScrollView是可以使用Sliver来自定义滚动模型(效果)的组件.它可以包含多种滚动模型,举个例子,假设有一个页面,顶部需要一个GridView,底部需要一个ListView ...

  2. HDU6375双端队列

    要点分析: 1.本题可以使用C++STL中的deque双端队列来方便解决(底层是一个双向的链表) 2.值得注意的是N的上限为150000,所以直接开这么大的空间会超内存,可以配合map一起使用 关于双 ...

  3. ip访问本机vs调试项目

    环境:win10 vs2019 webapi F5启动调试. 问题:localhost可以访问,127.0.0.1和本机ip访问不了.比如想让别人浏览一下看效果,或者测试人员测试功能,每次修改都有重新 ...

  4. 【函数分享】每日PHP函数分享(2021-1-19)

    substr 函数返回字符串的一部分.注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0. string substr (string $string ...

  5. 【Oracle】regexp_substr()函数详解

    环境:Oracle10.2.0.5 在SQL中尝试使用正则 可以试下regexp_substr()来进行分割 首先创建一个实验视图: SQL> create or replace view te ...

  6. luoguP2016 战略游戏

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能了 ...

  7. 前端中的script标签

    script标签中的重要属性! . 浏览器解析行内脚本的方式决定了它在看到字符串时,会将其当成结束的 标签.想避免这个问题,只需要转义字符"\" ①即可: 要包含外部文件中的 Ja ...

  8. 大数据系列4:Yarn以及MapReduce 2

    系列文章: 大数据系列:一文初识Hdfs 大数据系列2:Hdfs的读写操作 大数据谢列3:Hdfs的HA实现 通过前文,我们对Hdfs的已经有了一定的了解,本文将继续之前的内容,介绍Yarn与Yarn ...

  9. 在线配置热加载配置 go-kratos.dev 监听key

    paladin https://v1.go-kratos.dev/#/config-paladin example Service(在线配置热加载配置) # service.go type Servi ...

  10. 研发流程 接口定义&开发&前后端联调 线上日志观察 模型变动

    阿里等大厂的研发流程,进去前先了解一下_我们一起进大厂 - SegmentFault 思否 https://segmentfault.com/a/1190000021831640 接口定义 测试用例评 ...