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. C中测试时间代码

  2. 【Java】一个小程序,计算它包含的代码所需的耗时

    写一个小程序,用来计算它包含的代码所需的耗时.虽然简单,测试代码是否耗时还是有点用的,不用重新写嘛~ import java.util.Date; import java.util.concurren ...

  3. 动态创建的DOM元素进行事件绑定

    http://files.cnblogs.com/files/xsmhero/jquery.livequery.js <script type="text/javascript&quo ...

  4. FIFO页面置换算法

    本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4:为例: #include <stdio.h> #define Init ...

  5. NSUrl的打印

    1转自 http://my.oschina.net/wangdk/blog/165554: NSURL *url = [NSURL URLWithString:@"http://www.ba ...

  6. 使用easyui时 进入一个新页面 前要经过一个页面混乱的时候 才到正常的页面去

    var width = $(window).width(); var height = $(window).height(); var html = "<div id='loading ...

  7. HttpHelper类登录淘宝联盟并下载淘宝客订单xls

    本次开发环境与工具如下:IE9.0浏览器 + IE抓包插件HttpWatch +WIN7 64位系统 + VS2005 IDE + .NET 2.0框架本想上传HttpWatch抓包插件,但由于文件超 ...

  8. SPSS常用基础操作(2)——连续变量离散化

    首先说一下什么是离散化以及连续变量离散化的必要性. 离散化是把无限空间中无限的个体映射到有限的空间中去,通俗点讲就是把连续型数据切分为若干“段”,也称bin,离散化在数据分析中特别是数据挖掘中被普遍采 ...

  9. lodash接触:string-capitalize

    1.capitalize : _.capitalize([string='']) Capitalizes the first character of string. 参数 [string=''] ( ...

  10. 个人对beta发布的观点

    内容:五个小组的beta发布 时间:2016年11月10日 13:40-15:15 地点:传媒西楼202 发布顺序: 1.飞天小女警(选礼物) 2.金州勇士(在线考试) 3.新蜂(俄罗斯方块) 4.天 ...