Flowers

Problem Description

As
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

The first line contains a single integer t (1 <= t <= 10), the number of test cases.
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

For
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

2
1 1
5 10
4
2 3
1 4
4 8
1
4
6

Sample Output

Case #1:
0
Case #2:
1
2
1

分析

  我觉得你只要不傻,应该都能看出来这是个树状数组区间修改单点查询的板子题,但数据好像有点大,开数组是肯定开不下的,而最多就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 树状数组+离散化的更多相关文章

  1. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  3. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  4. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  5. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  6. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  7. HDU 5256 - 序列变换 ,树状数组+离散化 ,二分法

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数.请输出最少需要修改多少 ...

  8. 利用id来进行树状数组,而不是离散化以后的val HDU 4417 离线+树状数组

    题目大意:给你一个长度为n的数组,问[L,R]之间<=val的个数 思路:就像标题说的那样就行了.树状数组不一定是离散化以后的区间,而可以是id //看看会不会爆int!数组会不会少了一维! / ...

  9. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

随机推荐

  1. HTML中的<%%>是什么意思

    背景: 今天在nutzwk框架中看到这段代码. 在index.html界面 <% layout("/layouts/platform.html"){ %> <di ...

  2. 初识Spring JdbcTemplate

    JdbcTemplate 概述 JdbcTemplate是Spring提供的一个模板类,它是对jdbc的封装.用于支持持久层的操作.具有简单,方便等特点. pom.xml <!--依赖版本--& ...

  3. go语言指南之切片练习

    题目: 实现 Pic.它应当返回一个长度为 dy 的切片,其中每个元素是一个长度为 dx,元素类型为 uint8 的切片.当你运行此程序时,它会将每个整数解释为灰度值(好吧,其实是蓝度值)并显示它所对 ...

  4. 为什么MySQL分库分表后总存储大小变大了?

    1.背景 在完成一个分表项目后,发现分表的数据迁移后,新库所需的存储容量远大于原本两张表的大小.在做了一番查询了解后,完成了优化. 回过头来,需要进一步了解下为什么会出现这样的情况. 与标题的问题的类 ...

  5. vue 组件中添加样式不生效

    如何产生 在开发项目中遇到在组件中添加样式不生效的情况.具体场景如下 //// vue 组件 <template> <div class="box" data-v ...

  6. 复盘MySQL分页查询优化方案

    一.前言 MySQL分页查询作为Java面试的一道高频面试题,这里有必要实践一下,毕竟实践出真知. 很多同学在做测试时苦于没有海量数据,官方其实是有一套测试库的. 二.模拟数据 这里模拟数据分2种情况 ...

  7. HDFS文件系统基操--Java实现

    Java实现对HDFS文件系统的基本操作 1.准备好jar包 2.创建一个类 1. 测试连接 @Test //测试是否连接成功 public void test() { //添加配置 ==> c ...

  8. 在Tomcat上发布Web项目的方式

    一. Tomcat的使用: 安装:解压压缩包即可 注意:安装目录不能有中文 目录结构: ①bin:可执行文件 ②conf:可执行文件 ③lib:依赖的jar包 ④logs:日志文件 ⑤:temp:临时 ...

  9. 为什么你学习了scrum之后还不会实施敏捷? (敏捷学习之旅一)

    标题党, 其实这个主题改为"如何能快速的学习与实施敏捷"更贴切. 我在一家大型的外资金融企业,公司最近在大面积的实施敏捷转型,我的团队首先被选为试点团队,并实施得很好,最近我也和不 ...

  10. DBProxy快速入门

    1. DBProxy安装 1.1 安装依赖项 CentOS yum install -y Percona-Server-devel-55.x86_64 Percona-Server-client-55 ...