HDU 3450 Counting Sequences(线段树)
Counting Sequences
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 2335 Accepted Submission(s): 820
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.
4 2
1 3 7 5
4 思路:题目求满足要求的子串有多少个,那么我们应该固定序列的结尾点,dp[i],表示以i点为结尾的满足条件的序列有多少个,对于每一个点作为序列的最后一个点,都去找它前面所有满足条件的点k,然后加上dp[k],类似于动态规划,那么如何高效的求满足条件的点,即这个点前面的所有点,哪些是满足在a[i]-d,和a[i]+d.我们可以用线段树 另外,需要离散化一下,这里时间只有1秒钟,如果用map离散会超时,所以我们可以用二分法离散#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <map> using namespace std;
const int maxn=1e5;
typedef long long int LL;
LL sum[maxn*8+5];
int n,d;
void pushup(int node)
{
sum[node]=sum[node<<1]+sum[node<<1|1];
sum[node]%=9901;
}
void update(int node,int l,int r,int val,LL num)
{
if(l==r)
{
sum[node]+=num;
sum[node]%=9901;
return;
}
int mid=(l+r)>>1;
if(val<=mid)
update(node<<1,l,mid,val,num);
else
update(node<<1|1,mid+1,r,val,num);
pushup(node);
}
LL query(int node,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
return sum[node]%9901;
int mid=(l+r)>>1;
LL ret=0;
if(L<=mid) ret+=query(node<<1,l,mid,L,R);
if(R>mid) ret+=query(node<<1|1,mid+1,r,L,R);
return ret%9901;
}
int a[maxn+5];
int c[maxn+5];
int b[maxn+5];
int cot;
int ans; int fun1(int k,int tag)
{
int l=1,r=cot;
while(l<=r)
{
int mid=(l+r)/2;
if(k<a[mid])
r=mid-1;
else
l=mid+1;
}
if(tag) return l;
return r;
}
int fun2(int k)
{
int l=1,r=cot;
while(l<=r)
{
int mid=(l+r)/2;
if(k==a[mid])
return mid;
else if(k<a[mid])
r=mid-1;
else
l=mid+1;
}
return l;
}
int main()
{
while(scanf("%d%d",&n,&d)!=EOF)
{ memset(sum,0,sizeof(sum)); for( int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
c[i]=b[i];
}
sort(b+1,b+n+1);
cot=1;a[1]=b[1];
for(int i=2;i<=n;i++)
{
if(b[i]!=b[i-1])
a[++cot]=b[i];
}
for(int i=1;i<=n;i++)
{
int r=fun1(c[i],0)-1;
int l=fun1(c[i],1)+1;
int k=fun2(c[i]); LL num=query(1,1,n,l,r);
ans+=num;
ans%=9901;
update(1,1,n,k,(num+1)%9901);
}
printf("%d\n",ans);
} return 0;
}
HDU 3450 Counting Sequences(线段树)的更多相关文章
- hdu 3450 Counting Sequences
/* n*n暴力 这个很好想 */ #include<cstdio> #define maxn 100010 #define mod 9901 using namespace std; i ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- Hdu 3564 Another LIS 线段树+LIS
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 5091---Beam Cannon(线段树+扫描线)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- 32位嵌入式微处理器(processor)一览
32位嵌入式微处理器(processor)一览 由于嵌入式系统的专用型与定制性,与全球PC市场不同,没有一种微处理器或者微处理器公司可以主导嵌入式系统.本文分析了当前市场上主流的一些32位嵌入式微处理 ...
- C++ virtual继承
C++ virtual继承的还有一种名称是菱形继承.主要目的是用于解决从不同类继承来的同名数据成员在内存中有不同的拷贝.造成数据不统一的问题,以致于在进行类释放时造成内存泄漏. 将共同的基类作为虚基类 ...
- 2018.7.13vue知识小结
//配置是否允许vue-devtools检查代码,方便调试,生产环境中需要设置为false Vue.config.devtools=false; Vue.config.productionTip=fa ...
- dubbo接口测试
1.下载所需工具eclipse或intellij idea,下载maven,maven中config/settings.xml中要添加公司(测试人员公司)的maven库,不然被测dubbo服务jar包 ...
- 【转】搞清楚LzoCodec和LzopCodec
使用LZO过程会发现它有两种压缩编码可以使用,即LzoCodec和LzopCodec,下面说说它们区别: LzoCodec比LzopCodec更快, LzopCodec为了兼容LZOP程序添加了如 b ...
- 安装语言包(LANGUAGE PACKAGE)
by 枫竹丹青 一.说明 在SAP ECC 安装好后,如果是生产版本,通常只有两种语言--英语和德语.这时如需中文环境,则需定义支持的语言类型并导入语言包.如果安装的是IDES版,则系统中已包含了几十 ...
- 如何重设 MySQL 的 root 密码
MySQL下创建新用户.新数据库.设定访问权限控制都需要用到root密码.万一把root密码忘了,该怎么办? 幸运地是,重设密码很容易. 安全模式重置法 基本的思路是,以安全模式启动mysql,这样不 ...
- Bootstrap学习笔记(4)--导航栏
相关类: nav, nav-pills, nav-tags, nav-stacked ul里使用,导航格胶囊,方片外观,堆叠外观 navbar, navbar-header, navbar-brand ...
- UCOS2系统内核讲述_总体描述
Ⅰ.写在前面 学习本文之前可以参考我前面基于STM32硬件平台移植UCOS2的几篇文章,我将其汇总在一起: UCOS2_STM32F1移植详细过程(汇总文章) 要想学习,或使用系统配套的资源(如:信号 ...
- PLSQL 配置连接ORACLE数据库
http://blog.csdn.net/leather0906/article/details/6456095 PLSQL配置登录用户信息 http://www.2cto.com/database/ ...