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

【题意】给一个数字序列,问长度大于2的且相邻两个数的差的绝对值不大于d的情况对9901取余

【思路】对于当前的数A,那么以它为最后一个元素可以组成的情况是A-d到A+d的和,也可以这样想,A-d的已经组成了m种情况,那么在不影响小于d的情况下,可以直接将A放到A-d组成的左右序列中,那么直接加就可以了,而后更新A可以组成的情况,数据太大还需要离散化,然后二分找一下A-d和A+d的位置,数据可能没有这两个位置,那么找到第一个大于等于A-d和第一个小于等于A+d的位置更新就行,且不会影响结果。

参考:http://blog.csdn.net/dan__ge/article/details/51620024

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=+;
const int mod=;
int a[N],x[N],sum[*N];
int n,m,nn,cnt;
void build(int k,int l,int r)//建树
{
sum[k]=;
if(l==r) return ;
int mid=l+r>>;
build(*k,l,mid);
build(*k+,mid+,r);
} void update(int ll,int rr,int k,int l,int r)
{
if(l==r)
{
sum[k]=(sum[k]+rr)%mod;
return ;
}
int mid=l+r>>;
if(mid>=ll) update(ll,rr,k*,l,mid);
if(mid<ll) update(ll,rr,k*+,mid+,r);//
sum[k]=(sum[k*]+sum[k*+])%mod; } int query(int ll,int rr,int k,int l,int r)
{
if(ll<=l&&r<=rr) return sum[k]%mod;
int mid=l+r>>;
int res=;
if(ll<=mid) res+=query(ll,rr,k*,l,mid);
if(mid<rr) res+=query(ll,rr,k*+,mid+,r);
return res%mod;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
cnt=;
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
x[cnt++]=a[i];
x[cnt++]=a[i]-m;
x[cnt++]=a[i]+m;
}
sort(x,x+cnt);
nn=unique(x,x+cnt)-x;//nn去重后,不重复元素的个数;STLunqiue在STL中unique函数是一个去重函数,unique的功能是去除相邻的重复元素(只保留一个),
//其实它并不真正把重复的元素删除,是把重复的元素移到后面去了,然后依然保存到了原数组中,然后 返回去重后最后一个元素的地址,
//因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
build(,,nn);
int ans=;
for(int i=;i<n;i++)
{
int p=lower_bound(x,x+nn,a[i])-x;//Lower_bound是小于等于关键字的位置
int left=lower_bound(x,x+nn,a[i]-m)-x;
int right=lower_bound(x,x+nn,a[i]+m)-x; int c=query(left+,right+,,,nn);
update(p+,c+,,,nn);
ans=(ans+c)%mod;
}
printf("%d\n",ans);
}
return ;
}

Counting Sequences_线段树***的更多相关文章

  1. Counting Haybales (线段树)

    Counting Haybales 时间限制: 50 Sec  内存限制: 256 MB提交: 52  解决: 18[提交][状态][讨论版] 题目描述 Farmer John is trying t ...

  2. 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)

    描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...

  3. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

  4. [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)

    传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...

  5. hdu-5862 Counting Intersections(线段树+扫描线)

    题目链接: Counting Intersections Time Limit: 12000/6000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  6. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  7. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  8. HDU 3450 Counting Sequences(线段树)

    Counting Sequences Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Other ...

  9. HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

随机推荐

  1. 3D数学基础学习之向量一

    向量-数学定义 对数学家而言,向量就是一个数字列表,对程序员而言则是另一种相似的概念,数组. 向量-几何定义 a.向量的大小就是向量的长度(模),向量的长度非负 b.向量的方向描述了空间中向量的指向. ...

  2. didFinishLaunchingWithOptions

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  3. Zip it

    https://www.codewars.com/kata/zip-it/train/csharp using System; using System.Collections.Generic; us ...

  4. Quick-Cocos2d-x初学者游戏教程1

    Quick-Coco2d-x安装: Quick 安装完成后,在它的根目录下可以找到有两个名为setup_mac.sh.setup_win.bat的批处理脚本,它们分别是搭建Mac和Windows开发环 ...

  5. SQL Server索引调优系列

    http://www.cnblogs.com/chenmh/category/586612.html http://www.cnblogs.com/zhijianliutang/category/63 ...

  6. 《BI项目笔记》SSAS部署时发生的问题——元数据管理器中存在错误 解决办法

    在生成和部署期间出错.是否继续?解决办法: 用Microsoft SQL Server Management Studio 连接Analysis Services 然后删除多维数据库,重新布署.这样就 ...

  7. js中的fadeIn()

    一.fadeIn()淡入,fadeOut()淡出 fadeIn()和fadeOut()括号里边跟执行的时间

  8. Android Tips – 填坑手册

    出于: androidChina   http://www.androidchina.net/3595.html 学习 Android 至今,大大小小的坑没少踩,庆幸的是,在强大的搜索引擎与无私奉献的 ...

  9. 修改内联CSS(点击按钮连续改变文字大小、位置,.animate()方法)

    $(document).ready(function(){            $('#swticher-large').click(function(){                var $ ...

  10. 用Application和Session统计在线人数[转]

      在Global.asax全局变量文件中,修改其中的 Session_Start 和 Session_End 方法:代码如下: protected void Session_Start(object ...