For a set of sequences of integers {a1, a2,a3, ..., an}, we define a sequence {ai1,ai2, ai3, ..., aik} in which 1 ≤i1 < i2 < i3 < ... < ik ≤ n, as the sub-sequence of {a1, a2, a3, ..., an}. It is quite obvious that a sequence with the lengthn has 2n sub-sequences. And for a sub-sequence {ai1,ai2, ai3, ..., aik}, if it matches the following qualities:k ≥ 2, and the neighboring 2 elements have the difference not larger thand, it will be defined as a Perfect Sub-sequence. Now given an integer sequence, calculate the number of its perfect sub-sequence.
Input
Multiple test cases The first line will contain 2 integers n, d (2 ≤ n ≤ 100000, 1 ≤ d ≤ 10000000). The second line has n integers, representing the suquence.Output
The number of Perfect Sub-sequences mod 9901
Sample Input
4 2 1 3 7 5
Sample Output
4
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int nn=110000;
const int mod=9901;
int c[nn];
int n,d;
int lowbit(int x)
{
return x&(-x);
}
int setn(int i,int p)
{
while(i<=nn)
{
c[i]=(c[i]+p)%mod;
i+=lowbit(i);
}
}
int getsum(int p)
{
int ret=0;
while(p)
{
ret=(ret+c[p])%mod;
p-=lowbit(p);
}
return ret;
}
int dp[110000];
int a[110000],num[110000];
int x;
int main()
{
while(cin>>n>>d)
{
memset(c,0,sizeof(c));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
cin>>a[i];
num[i]=a[i];
}
sort(num+1,num+n+1);
//int uu=unique(num+1,num+n+1)-num-1;
int uu=n;
for(int i=1;i<=n;i++)
{
int id=lower_bound(num+1,num+uu+1,a[i])-num;
int l=lower_bound(num+1,num+uu+1,a[i]-d)-num;
int r=upper_bound(num+1,num+uu+1,a[i]+d)-num-1;
dp[i]=(getsum(r)-getsum(l-1)+mod)%mod;
dp[i]++;
//cout<<dp[i]<<" "<<getsum(r)<<" "<<getsum(l-1)<<endl;
setn(id,dp[i]);
}
int ans=0;
for(int i=1;i<=n;i++)
ans=(ans+dp[i])%mod;
cout<<((ans-n)%mod+mod)%mod<<endl;
}
}