hdu2836之树状数组

Traversal

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 669    Accepted Submission(s): 245


Problem Description
I arrive at a big lake and I have to cross it. Luckily, I’m a very good jumper, but the lake is too big to be crossed in one jump. On the shore, I find N boxes of different heights, set in a certain order. If I throw a box into the lake, it will float and it will have the same height as on the shore. This is good, because I intend to throw some boxes into the lake and get from one shore to the other by jumping from box to box. The only things to consider are:
The lake is big, so I must throw at least 2 boxes, which means that in order to cross the lake I have to make at least 3 jumps.
Not all the boxes have to be thrown; some of them may be ignored.
The boxes can be thrown into the lake only in the order they are found on the shore and I have to jump on them in this order.
The height difference between two consecutive boxes I use must be at most H meters, because I can jump a lot in length, but I have some problems with jumping in height.The height of a box doesn’t change when I jump on it.
I’m always able to jump from the shore to a box and from a box to the shore, no matter what the height of the box is.

Facing so many possibilities that respect the above conditions, I begin counting the number of possibilities that I have, instead of actually crossing the lake. I quickly find the answer and I wonder whether you can also find it as fast as I did.

Task

Write a program that determines the number of possibilities to cross the lake in the above conditions. Since the number can be quite big, you only have to output the remainder of this number, when divided by 9901.
 

Input
There are multiple test cases. Each test case contains two integers N and H, separated by a space, representing the number of boxes and the maximum height difference between two consecutive boxes thrown into the lake. The following N lines contain the heights of the boxes, in the order the boxes are set on the shore. The (i+1)th line contains the height of the ith box.
 

Output
For each test case you should output a single line, containing the number of possibilities modulo 9901.

Constraints

1 < N < 100 001
0 < H < 100 000 001
The height of any box is a strictly positive integer and does not exceed 100 000 000
 

Sample Input
  
  
4 2 1 3 7 5
 

Sample Output
  
  
4
题意:给定一组序列,问有多少序列满足相邻元素的差的绝对值<=H,输出个数%9901

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=100000+10;
const int mod=9901;
int n,m,size;
int a[MAX],hash[MAX],c[MAX];

void Init(int num){
	for(int i=0;i<=num;++i)c[i]=0;
	size=1;
}

int search1(int x){//寻找>=x的第一个数 
	int left=1,right=size,mid;
	while(left<=right){
		mid=left+right>>1;
		if(hash[mid]>=x)right=mid-1;
		else left=mid+1;
	}
	return right+1;
}

int search2(int x){//寻找<=x最后的一个数 
	int left=1,right=size,mid;
	while(left<=right){
		mid=left+right>>1;
		if(hash[mid]<=x)left=mid+1;
		else right=mid-1;
	}
	return left-1;
}

int lowbit(int x){
	return x&(-x);
}

void Update(int x,int d){
	while(x<=size){
		c[x]=(c[x]+d)%mod;
		x+=lowbit(x);
	}
}

int Query(int x){
	LL sum=0;
	while(x>0){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum%mod;
}

int main(){
	while(~scanf("%d%d",&n,&m)){
		Init(n);
		for(int i=1;i<=n;++i)scanf("%d",a+i),hash[i]=a[i];
		sort(hash+1,hash+1+n);
		for(int i=2;i<=n;++i)if(hash[i] != hash[size])hash[++size]=hash[i];
		int sum=0;
		for(int i=1;i<=n;++i){
			int x=search1(a[i]-m),y=search2(a[i]+m),k=search1(a[i]);
			int ans=Query(y)-Query(x-1);
			ans=(ans%mod+mod)%mod;
			sum=(sum+ans)%mod;
			Update(k,ans+1);
		}
		cout<<sum<<endl;
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值