#include<stdio.h>
#include<stdlib.h>
#include<deque>
#include<math.h>
using namespace std;
struct mypair{
int val;
int ind;
mypair(int a,int b):val(a),ind(b){}
};
int main()
{
int n, num;
scanf("%d %d", &n, &num);
int* v = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", &v[i]);
}
deque<mypair> minh_inc;
deque<mypair> maxh_dec;
int left_ind = 0;
int result = 0;
for (int i = 0; i < n; ++i) {
while ((!minh_inc.empty()) && v[i] < minh_inc.back().val) {
minh_inc.pop_back();
}
minh_inc.push_back(mypair(v[i], i));
while ((!maxh_dec.empty()) && v[i] > maxh_dec.back().val) {
maxh_dec.pop_back();
}
maxh_dec.push_back(mypair(v[i], i));
while (maxh_dec.front().val - minh_inc.front().val > num) {
if (maxh_dec.front().ind < minh_inc.front().ind) {
left_ind= (maxh_dec.front().ind + 1);
maxh_dec.pop_front();
}
else {
left_ind = (minh_inc.front().ind + 1);
minh_inc.pop_front();
}
}
result += i - left_ind + 1;
}
printf("%d", result);
return 0;
}
#include<stdlib.h>
#include<deque>
#include<math.h>
using namespace std;
struct mypair{
int val;
int ind;
mypair(int a,int b):val(a),ind(b){}
};
int main()
{
int n, num;
scanf("%d %d", &n, &num);
int* v = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
scanf("%d", &v[i]);
}
deque<mypair> minh_inc;
deque<mypair> maxh_dec;
int left_ind = 0;
int result = 0;
for (int i = 0; i < n; ++i) {
while ((!minh_inc.empty()) && v[i] < minh_inc.back().val) {
minh_inc.pop_back();
}
minh_inc.push_back(mypair(v[i], i));
while ((!maxh_dec.empty()) && v[i] > maxh_dec.back().val) {
maxh_dec.pop_back();
}
maxh_dec.push_back(mypair(v[i], i));
while (maxh_dec.front().val - minh_inc.front().val > num) {
if (maxh_dec.front().ind < minh_inc.front().ind) {
left_ind= (maxh_dec.front().ind + 1);
maxh_dec.pop_front();
}
else {
left_ind = (minh_inc.front().ind + 1);
minh_inc.pop_front();
}
}
result += i - left_ind + 1;
}
printf("%d", result);
return 0;
}