Counting Sequences
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1777 Accepted Submission(s): 596
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
2010 ACM-ICPC Multi-University Training Contest(2)——Host by BUPT
题意:求给出串的子串中有相邻数字之差小于d的子串的个数。
分析:这题可以用树状数组和线段树做,两者的思路都差不多,首先都是对输入的数据离散化(d太大),然后都是以n为轴,建立树状数组(线段树),然后插入的时候要维护答案(DP),每次插入前先二分找出第一个大于等于a[i]-d所在的位置和最后一个小于等于a[i]+d所在的位置,然后只要查询这个区间就行了,插入是插入到a[i]所在的位置。
树状数组:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100010;
const int MOD=9901;
int c[MAXN],hash[MAXN],a[MAXN];
int cnt;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
while(x<MAXN)
{
c[x]+=val;
if(c[x]>=MOD)
c[x]%=MOD;
x+=lowbit(x);
}
}
int getsum(int x)
{
int ans=0;
while(x>0)
{
ans+=c[x];
if(ans>=MOD)
ans%=MOD;
x-=lowbit(x);
}
return ans;
}
int search1(int x)
{
int l=1,r=cnt,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(hash[mid]<x)
l=mid+1;
else
r=mid-1;
}
return r+1;
}
int search2(int x)
{
int l=1,r=cnt,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(hash[mid]<=x)
l=mid+1;
else
r=mid-1;
}
return l-1;
}
int main()
{
int n,d,i;
while(scanf("%d%d",&n,&d)==2)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
hash[i]=a[i];
}
sort(hash+1,hash+1+n);
cnt=1;
for(i=2;i<=n;i++)
if(hash[i]!=hash[cnt])
hash[++cnt]=hash[i];
int ans=0;
memset(c,0,sizeof(c));
for(i=1;i<=n;i++)
{
int y=search2(a[i]+d),x=search1(a[i]-d);
int k=search1(a[i]);
int sum=getsum(y)-getsum(x-1);
sum=(sum+MOD)%MOD;
ans=(ans+sum)%MOD;
update(k,sum+1);
}
printf("%d\n",ans);
}
return 0;
}
线段树:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=100010;
const int MOD=9901;
int cnt;
int a[MAXN],hash[MAXN];
struct seg
{
int l,r;
int num;
}tree[MAXN<<2];
void build(int l,int r,int k)
{
tree[k].l=l;
tree[k].r=r;
tree[k].num=0;
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
}
void update(int n,int d,int k)
{
if(tree[k].l==tree[k].r)
{
tree[k].num+=d;
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if(n<=mid)
update(n,d,k<<1);
else
update(n,d,k<<1|1);
tree[k].num=(tree[k<<1].num+tree[k<<1|1].num)%MOD;
}
int query(int l,int r,int k)
{
if(tree[k].l>=l&&tree[k].r<=r)
return tree[k].num;
if(tree[k].l==tree[k].r)
return tree[k].num;
int mid=(tree[k].l+tree[k].r)>>1;
if(r<=mid)
return query(l,r,k<<1);
else if(l>mid)
return query(l,r,k<<1|1);
else
return query(l,mid,k<<1)+query(mid+1,r,k<<1|1);
}
int search1(int x)
{
int l=1,r=cnt,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(hash[mid]<x)
l=mid+1;
else
r=mid-1;
}
return r+1;
}
int search2(int x)
{
int l=1,r=cnt,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(hash[mid]<=x)
l=mid+1;
else
r=mid-1;
}
return l-1;
}
int main()
{
int n,d,i;
while(scanf("%d%d",&n,&d)==2)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
hash[i]=a[i];
}
sort(hash+1,hash+n+1);
cnt=1;
for(i=2;i<=n;i++)
if(hash[cnt]!=hash[i])
hash[++cnt]=hash[i];
int ans=0;
build(1,cnt,1);
for(i=1;i<=n;i++)
{
int y=search2(a[i]+d),x=search1(a[i]-d);
int k=search1(a[i]);
int sum=query(x,y,1);
sum=(sum+MOD)%MOD;
ans=(ans+sum)%MOD;
update(k,(sum+1)%MOD,1);
}
printf("%d\n",ans);
}
return 0;
}