hdu 3450 树状数组
思路:二分查找位置,之后是裸的树状数组。
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 100010
#define Maxm 200010
#define LL int
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 100000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 9901
using namespace std;
int val[Maxn],sorted[Maxn],n,cnt;
LL C[Maxn];
void update(int pos,LL val)
{
int i;
while(pos<=n){
C[pos]+=val;
C[pos]%=Mod;
pos+=lowbit(pos);
}
}
LL Sum(int pos)
{
LL sum=;
while(pos){
sum+=C[pos];
pos-=lowbit(pos);
sum%=Mod;
}
return sum;
}
int main()
{
int d,i,j;
while(scanf("%d%d",&n,&d)!=EOF){
clr(C,);
for(i=;i<=n;i++){
scanf("%d",val+i);
sorted[i]=val[i];
}
sort(sorted+,sorted+n+);
cnt=;
for(i=;i<=n;i++)
if(sorted[i]!=sorted[cnt])
sorted[++cnt]=sorted[i];
int l,r,pos;
LL ans=,temp;
pos=lower_bound(sorted+,sorted+cnt+,val[])-sorted;
update(pos,);
for(i=;i<=n;i++){
temp=;
l=lower_bound(sorted+,sorted+cnt+,val[i]-d)-sorted;
r=lower_bound(sorted+,sorted+cnt+,val[i]+d)-sorted;
pos=lower_bound(sorted+,sorted+cnt+,val[i])-sorted;
if(sorted[l]>=val[i]-d) l--;
if(sorted[r]>val[i]+d||r>cnt) r--;
LL rr=Sum(r);
LL ll=Sum(l);
temp+=(rr-ll+Mod);
update(pos,temp+);
ans+=temp;
ans%=Mod;
}
printf("%d\n",ans%Mod);
}
return ;
}
hdu 3450 树状数组的更多相关文章
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- HDU 1934 树状数组 也可以用线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- 【模板】HDU 1541 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...
- hdu 5147 树状数组
题意:求满足a<b<c<d,A[a]<A[b],A[c]<A[d]的所有四元组(a,b,c,d)的个数 看到逆序对顺序对之类的问题一开始想到了曾经用归并排序求逆序对,结果 ...
随机推荐
- Apache与Tomcat 区别联系
Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...
- cJSON 使用笔记
缘 起 最近在stm32f103上做一个智能家居的项目,其中选择的实时操作系统是 rt_thread OS v1.2.2稳定版本,其中涉及到C和java(android)端数据的交换问题,经 ...
- Linux 网络编程 高级套接字
一.套接字选项: 有以下3中方式可以对套接字选项进行设置: getsockopt 和 setsockopt 函数 fcntl函数 ioctl函数 getsockopt和setsockopt函数用于获得 ...
- centos7.2安装配置
VMware12上安装Centos7,如果电脑是64位,这必须选择64位的centos系统,不然安装完会找不到网卡.安装过程中应当开启网卡选项. 安装完想用ifconfig命令查找IP地址会提示错误: ...
- BZOJ 1497: [NOI2006]最大获利 最小割
1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...
- C#操作Word生成目录
OperateWord ow = new OperateWord(); Microsoft.Office.Interop.Word.ApplicationClass ss = ow.WordAppli ...
- HTML 转义符
特殊字符 字符 十进制 转义字符 “ " " & & & < < < > > > 不断开空格(non-breaking ...
- 【Animation】 使用handler和Runnable实现某一个控件的抖动效果
布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tool ...
- [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup
The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...
- [ES6] 13. Using the ES6 spread operator ...
The spread operator (...) allows you to "explode" an array into its individual elements. S ...