HDU - 3450 - Counting Sequences (线段树|树状数组 + 离散化)
2018-01-23 17:07
357 查看
Counting Sequences
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 2680 Accepted Submission(s): 952
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
题意:
有一个由n个数构成的序列,求其中有几个完美子序列。
完美子序列要满足相邻两元素之差不大于d,且序列中元素不少于两个
思路:
遍历序列中的每一个元素,求出以该元素为末尾元素的完美子序列个数。
假设该元素值为k,则以该元素为末尾元素的完美子序列个数 = 该元素前值在[k-d,k+d]区间内的元素个数 + 以这些元素为末尾元素的完美子序列个数
然后前面的个数已经求过了,就可以直接用,用区间求和,然后更新就行了,就是用线段树或树状数组
因为d比较大,然后元素大小也没有说明,所以就用离散化处理一下
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define LL long long
#define lx x<<1
#define rx x<<1|1
#define mid ((L+R)>>1)
const int mod = 9901;
const int N = 1e5 + 10;
int n,d,sum[N<<2],I,ANS;
LL a
,Rank
;
void setRank(){
sort(Rank+1,Rank+1+n);
I = 1;
for(int i=2;i<=n;i++) if(Rank[i]!=Rank[i-1]) Rank[++I] = Rank[i];
}
int Sum(int x,int l,int r,int L,int R){
if(l<=L&&R<=r) {
return sum[x];
}
int ans = 0;
if(mid>=l) ans = Sum(lx, l, r, L, mid) % mod;
if(mid<r) ans = (ans + Sum(rx, l, r, mid+1, R)) % mod;
return ans;
}
void update(int x,LL pos,int L,int R){
if(L==R) {
int l = lower_bound(Rank+1, Rank+1+I, Rank[pos]-d) - Rank;
int r = upper_bound(Rank+1, Rank+1+I, Rank[pos]+d) - Rank - 1;
int ad = Sum(1, l, r, 1, I);
ANS = (ANS + ad) % mod;
sum[x] = (sum[x] + ad + 1) % mod;
return;
}
if(pos<=mid) update(lx, pos, L, mid);
else update(rx, pos, mid+1, R);
sum[x] = (sum[lx] + sum[rx]) % mod;
}
int main()
{
while(scanf("%d%d",&n,&d)!=EOF){
memset(sum,0,sizeof sum);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
Rank[i] = a[i];
}
setRank();
ANS = 0;
for(int i=1;i<=n;i++){
int pos = lower_bound(Rank+1, Rank+1+I, a[i]) - Rank;
update(1, pos, 1, I);
}
printf("%d\n",ANS);
}
return 0;
}
相关文章推荐
- HDU_3450_Counting Sequences_树状数组_离散化
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
- HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
- 树状数组,线段树(区间成段更新,HDU 4267)
- HDU 4638 - Group(树状数组 / 线段树)
- HDU 5877 Weak Pair(dfs + 树状数组 + 离散化)
- hdu 1166 敌兵布阵 单点更新 树状数组 线段树 两种做法
- HDU-5792-World is Exploding(树状数组+离散化)
- hdu 1166 线段树树状数组 Java解法
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
- hdu 1166 敌兵布阵【线段树Or树状数组】
- HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
- hdu 1556 Color the ball (树状数组||线段树成段更新)
- hdu 1166 敌兵布阵(树状数组 | 线段树)
- hdu 1754 I Hate It(树状数组 | 线段树)
- hdu 3743 Frosh Week (离散化+树状数组,求逆序对)
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- HDU - 2492Ping pong(树状数组|权值线段树)
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)