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个 ...
随机推荐
- AI:拿来主义——预训练网络(一)
我们已经训练过几个神经网络了,识别手写数字,房价预测或者是区分猫和狗,那随之而来就有一个问题,这些训练出的网络怎么用,每个问题我都需要重新去训练网络吗?因为程序员都不太喜欢做重复的事情,因此答案肯定是 ...
- 对象深拷贝deepCopy
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1); } function deepCopy(targ ...
- DSO 运行 —— dso_ros + Android 手机摄像头
转载请注明出处,谢谢 原创作者:Mingrui 原创链接:https://www.cnblogs.com/MingruiYu/p/12425855.html 本文要点: dso 配置安装 dso 离线 ...
- 《第31天:JQuery - 轮播图》
源码下载地址:链接:https://pan.baidu.com/s/16K9I... 提取码:0ua2 写这篇文章,当做是对自已这一天的一个总结.写轮播图要准备的东西:三张尺寸大小一样的图片.分为三个 ...
- JZOJ 1776. 经济编码 (Standard IO)
1776. 经济编码 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 为降低资料储存的空间或增加资料传送 ...
- Django进行数据迁移时,报错:(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1")
进行数据迁移时: 第一步: 命令:python manage.py makemigrations 在对应的应用里面的migrations文件夹中产生了一个0001_initial.py文件 第二步:执 ...
- Yuchuan_Linux_C 编程之四动态库(共享库)制作
一.整体大纲 二.共享库的制作 1. 命名规则: lib + 名字 + .so 2. 制作步骤: 1) 生成与位置无关的代码 (生成与位置无关的.o) 2) 将.o打包成共享库(动态库) 3. ...
- 使用R进行空间自相关检验
「全局溢出」当一个区域的特征变化影响到所有区域的结果时,就会产生全局溢出效应.这甚至适用于区域本身,因为影响可以传递到邻居并返回到自己的区域(反馈).具体来说,全球溢出效应影响到邻居.邻居到邻居.邻居 ...
- 【WPF学习】第五十七章 使用代码创建故事板
在“[WPF学习]第五十章 故事板”中讨论了如何使用代码创建简单动画,以及如何使用XAML标记构建更复杂的故事板——具有多个动画以及播放控制功能.但有时采用更复杂的故事板例程,并在代码中实现全部复杂功 ...
- OpenWrt tcpdump 抓包
路由器用的是 网件的 4300 刷的是石像鬼双固件 1, 安装支持库opkg updateopkg install libpcapopkg install tcpdump 2, 设置条件开始捕获tcp ...