HDU 4325 Flowers 树状数组+离散化
Flowers
Problem Description
is known to all, the blooming time and duration varies between
different kinds of flowers. Now there is a garden planted full of
flowers. The gardener wants to know how many flowers will bloom in the
garden in a specific time. But there are too many flowers in the garden,
so he wants you to help him.
Input
For
each case, the first line contains two integer N and M, where N (1
<= N <= 10^5) is the number of flowers, and M (1 <= M <=
10^5) is the query times.
In the next N lines, each line contains two integer Si and Ti (1 <= Si <= Ti <= 10^9), means i-th flower will be blooming at time [Si, Ti].
In the next M lines, each line contains an integer Ti, means the time of i-th query.
Output
each case, output the case number as shown and then print M lines. Each
line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
Sample Input
Sample Output
分析
我觉得你只要不傻,应该都能看出来这是个树状数组区间修改单点查询的板子题,但数据好像有点大,开数组是肯定开不下的,而最多就1e5朵花,所以在1e9之间有很多数字都被浪费掉了,所以使用离散化,离散化时记得将相同的数标记成一样的数字,还有询问也要离散化。
然鹅这道题我很好奇的试了试,没有离散化直接让开1e5的数组然后跑树状数组,让我非常吃惊的是,它A了! HDU数据好水 出题人的意思肯定是让你用离散化,所以……看代码吧
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
int tr[N+],a[N+];
struct Node{
int id,x;
bool operator <(const Node &A)const {
return x<A.x;//方便处理相等的情况
}
}p[N+];
//树状数组开始
int lowbit(int x){
return x&(-x);
}
void Add(int x,int w){
while(x<=N){
tr[x]+=w;
x+=lowbit(x);
}
}
int Sum(int x){
int ans=;
while(x){
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
//树状数组结束
int main(){
int n,cas=;
scanf("%d",&n);
while(n--){
printf("Case #%d:\n",++cas);
int totn,totm;
scanf("%d%d",&totn,&totm);
totn*=;
totm+=totn;
for(int i=;i<=totm;i++){
scanf("%d",&p[i].x);
p[i].id=i;
//所有数据一次读完,并记录i的值,因为结构体排序后顺序被打乱
}
sort(p+,p+totm+);
a[p[].id]=;
int k=;
for(int i=;i<=totm;i++){
if(p[i].x==p[i-].x)a[p[i].id]=k;//相等的值肯定也要离散成一样的呀
else a[p[i].id]=++k;
}
memset(tr,,sizeof(tr));
for(int i=;i<=totn;i+=){
Add(a[i],);
Add(a[i+]+,-);
}
for(int i=totn+;i<=totm;i++){
printf("%d\n",Sum(a[i]));
}
}
return ;
}
HDU 4325 Flowers 树状数组+离散化的更多相关文章
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...
- 利用id来进行树状数组,而不是离散化以后的val HDU 4417 离线+树状数组
题目大意:给你一个长度为n的数组,问[L,R]之间<=val的个数 思路:就像标题说的那样就行了.树状数组不一定是离散化以后的区间,而可以是id //看看会不会爆int!数组会不会少了一维! / ...
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
随机推荐
- oracle监控参数
Sar –u 检查CPU的繁忙程度列说明Usr用户模式下cpu运行所占的百分比Sys系统模式下cpu运行所占的百分比Wio因为有进程等待块I/O而使cpu处于闲置状态所占百分比IdleCpu为闲置状态 ...
- Vizceral小白入门
Vizceral小白入门 接到一个任务,要求将N个program可视化,能一目了然查看当前爬虫状态.记得之前做测试时,一个queue service前端可视化效果不错,经询问是用vizceral开源框 ...
- USB小白学习之路(4)HID键盘程序
HID键盘程序 1. 特别注意 需要特别注意,各个例程中的设备描述符,配置描述符等各种描述符都是已经配置好了的,我们需要做的只是在例程中将代码修改为自己需要的部分即可,一般情况下是不可以串搭配的. 2 ...
- Linux - 修改系统的max open files、max user processes(附ulimit的使用方法)【转载】
Linux - 修改系统的max open files.max user processes(附ulimit的使用方法)目录 1 问题说明2 修改max open files3 修改max user ...
- datatables异步获取数据、简单实用
IKC项目总结 一.认证难题管理模块 1. 如何使用datatables进行获取数据内容 datatables简介:Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何H ...
- GPUImage学习总结
GPUImage是iOS上一个基于OpenGL进行图像处理的开源框架,内置大量滤镜,架构灵活,可以在其基础上很轻松地实现各种图像处理功能. GPUImgae特性 1,丰富的输入组件 摄像头.图片.视频 ...
- springboot配置文件读取pom文件信息
解决的问题 springboot(当然别的也可以)多环境切换需要该配置文件,打包时不够方便. 解决: 配置文件能读取pom文件中的配置,根据命令选择不同配置注入springboot的配置文件中 pom ...
- 小白学 Python 数据分析(12):Pandas (十一)数据透视表(pivot_table)
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- 7-35 jmu-python-求三角形面积及周长 (10 分)
输入的三角形的三条边a.b.c,计算并输出面积和周长.假设输入三角形三边是合法整形数据. 三角形面积计算公式: ,其中s=(a+b+c)/2. import math #导入math库 math.s ...
- 授人以渔式解析原生JS写轮播图
需求与分析 需求:循环无缝自动轮播五张图,按左右箭头可以手动切换图片,鼠标点击轮播图下面按钮 1 2 3 4 5会跳转到对应的第1 2 3 4 5张图片.鼠标放到轮播图的图片上时不再自动轮播并且左右箭 ...