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. AC自动机——Uva 11468 子串

    题目链接:http://vjudge.net/contest/142513#problem/A 题意:给出一些字符和各自对应的选择概率,随机选择L次后将得到一个长度为L的随机字符串S.给出K个模版串, ...

  2. 批量运行R包

    #批量运行包:all.pcg <- c("data.table","ggplot2","rmarkdown","tidyr& ...

  3. Oracle介绍(初学者必须知道的)

    1.为什么学习数据库?(两个概念) 数据库的概念: 数据库是按照数据结构组织,存储和管理数据的仓库. 数据库,简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进行新增. ...

  4. spinner下拉框组件

    方法一代码如下: <string-array name="city_name"> <item>浙江</item> <item>上海& ...

  5. (五)AOS编程

    一.LOG AOS_LOG(index)  //断言,会打印出断言传进来的值 AOS_ASSERT(0);     //只会打印断言位置  return  AOS_FAIL;    //返回错误,函数 ...

  6. MySQL浮点计算存在的问题与解决方案

    如有疑问请联系微信:onesoft007    在计算机中,浮点数往往很难精确表示,那么浮点数运算结果也往往难以精确表示.MySQL同样也存在这个问题,并表现在如下几个方面. 问题 1.相同的输入,可 ...

  7. C#完成最简单的WebService创建及使用

    打开Visual Studio(我用的是2010)→文件→新建→项目→Visual C#→Web→ASP.NET Web 服务应用程序 打开Service1.asmx文件,会看到里面已经有个Hello ...

  8. css实现一段不够一行时居中显示,多于一行时两端对齐

    今天有遇到这个问题,不够一行要居中才好看,多于一行居中又很难看: 居中 两端对齐 一开始想用text-align-last:center; 可是这样结果是这样的: 单行的居中了 可是多行的最后一行也居 ...

  9. 并发编程 13—— 线程池的使用 之 配置ThreadPoolExecutor 和 饱和策略

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  10. Stack的pop和push操作

    #include <stack> #include <cstdio> using namespace std; int main(){ stack<int> s; ...