Counting Sequences

Counting Sequences

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 337    Accepted Submission(s): 115


Problem 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
 

Source
主要还是线段树了。。
#include<iostream>
#include<algorithm>
using namespace std;
#define MOD(x) (x)%9901
const int inf=0xfffffff;
const int N=100010;
struct seg
{
int l,r;
int sum;
};
seg s[N*3];
int dia[N],n,dis,old[N],cnt;
int dp[N];
int findpos(int num)
{
int mid,l=1,r=cnt,res;
while(l<=r)
{
mid=(l+r)>>1;
if(dia[mid]>=num)
{
res=mid;
r=mid-1;
}
else 
l=mid+1;
}
return res;
}
void maketree(int l,int r,int c)
{
s[c].l=l;
s[c].r=r;
s[c].sum=0;
if(l==r) return;
int mid=(l+r)>>1;
maketree(l,mid,c<<1);
maketree(mid+1,r,(c<<1)+1);
}
int find(int l,int r,int c)
{
int mid;
if(l<=s[c].l&&r>=s[c].r)
return MOD(s[c].sum);
else
{
mid=(s[c].l+s[c].r)>>1;
if(r<=mid)
return MOD(find(l,r,c<<1));
else if(l>mid)
return MOD(find(l,r,(c<<1)+1));
else
return MOD(find(l,mid,c<<1)+find(mid+1,r,(c<<1)+1));
}
}
void updata(int pos,int num,int c)
{
s[c].sum=MOD(s[c].sum+num);
if(s[c].l==s[c].r)
return ;
int mid = (s[c].l+s[c].r) >> 1;
if(pos<=mid)
updata(pos,num,c<<1);
else
updata(pos,num,(c<<1)+1);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&dis)!=EOF)
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
scanf("%d",&dia[i]);
old[i]=dia[i];
}
sort(dia+1,dia+1+n);
cnt=1;
for(i=2;i<=n;i++)
if(dia[i]!=dia[i-1])
dia[++cnt]=dia[i];
dia[++cnt]=inf;
int ll,rr,temp,nowpos,re,ans=0;
maketree(1,cnt,1);
for(i=1;i<=n;i++)
{
ll=findpos(old[i]-dis);
rr=findpos(old[i]+dis);
if(dia[ll]<old[i]-dis)
ll++;
if(dia[rr]>old[i]+dis)
rr--;
temp=find(ll,rr,1);
nowpos=findpos(old[i]);
if(dia[nowpos]!=old[i])
nowpos++;
ans=MOD(ans+temp);
dp[nowpos]=++temp;
updata(nowpos,temp,1);
}
printf("%d/n",ans);
}
return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值