Description
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 length n has 2^n 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 than d, 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 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<cstdio> #include<cmath> #include<cstring> #include<climits> #include<string> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> using namespace std; #define rep(i,j,k)for(i=j;i<k;i++) #define per(i,j,k)for(i=j;i>k;i--) #define MS(x,y)memset(x,y,sizeof(x)) #define max(a,b) a>b?a:b #define min(a,b) a<b?a:b #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long LL; const int INF=0x7ffffff; #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r const int mod=9901; const int M=4e5+1; int i,j,k,n,m,d; int a[M],b[M]; int f[M]; int low(int x) { return x&-x; } int sum(int x) { int sum=0; while(x>0){ sum+=f[x]; sum%=mod; x-=low(x); } return sum; } void add(int x,int v) { while(x<=n){ f[x]+=v; f[x]%=mod; x+=low(x); } } int main() { while(~scanf("%d%d",&n,&d)){ MS(f,0); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+1+n); int ans=0; for(i=1;i<=n;i++){ int k=lower_bound(b+1,b+1+n,a[i])-b; int l=lower_bound(b+1,b+1+n,a[i]-d)-b; int r=upper_bound(b+1,b+1+n,a[i]+d)-b-1; int x=(sum(r)-sum(l-1)); x=(x+mod)%mod; ans=(ans+x)%mod; add(k,(x+1)%mod); } printf("%d\n",ans); } return 0; }