HDU4325--Flowers--树状数组,离散化
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, 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
很简单的一道树状数组的改段求点的模版题,基本思想是:tree[]为树状数组,N个元素,tree[i]存放的是i至N被加了多少,
所以,修改[l,r]区间值的操作就变为了,
update(l,1);
update(r+1,-1);
查询某点的操作就变为了查询此点的前缀和
需要注意的是,此题目的数据范围太大,需要进行离散化1 (关于离散化,会再写一篇博客)
```c++
#include
#include
#include
#include
#include
using namespace std;
const int M=100100;
int tree[M];
int q[M];
int N=0;
map ls;
struct node
{
int le;
int ri;
}a[M];
int lowbits(int x)
{
return x&(-x);
}
void update(int i,int val)
{
for(;i0;i-=lowbits(i)){
sum+=tree[i];
}
return sum;
}
void init()
{
memset(tree,0,sizeof(tree));
ls.clear();
}
int main()
{
freopen("data.in","r",stdin);
int m,n;
int t;
int l,r;
int tem;
int casn=1;
cin>>t;
while(t--){
cout>n>>m;
for(int i=0;i>l>>r;
if(ls.find(l)==ls.end()) ls.insert(make_pair(l,1));
if(ls.find(r)==ls.end()) ls.insert(make_pair(r,1));
a[i].le=l;a[i].ri=r;
}
for(int i=0;i>l;
if(ls.find(l)==ls.end()) ls.insert(make_pair(l,1));
q[i]=l;
}
int i=1;
for(map::iterator ite=ls.begin();ite!=ls.end();ite++){
ite->second=i++;
}
N=i-1;
for(int i=0;i
HDU4325--Flowers--树状数组,离散化的更多相关文章
- HDU 4325 Flowers 树状数组+离散化
Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...
- 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 ...
- HDU4325 树状数组+离散化
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others) ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
随机推荐
- USB 设备插拔事件处理
Windows 系统下,设备连接至电脑或从电脑移除,系统会广播一条 WM_DEVICECHANGE 消息到所有应用程序,在程序的消息处理函数中可以对事件进行相应. 1: class C ...
- Sublime Text学习笔记
1.快捷键(Key Bindings) Preferences -> Key Bindings ->Default 会打开一个配置文件,里面全是配置信息 2.代码片段(Snippe ...
- ios 将图片变成圆形
#pragma mark - 将图片转换成圆形 -(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset { UIGraph ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- MFC下调试日志的打印
最近项目出现点小Bug,需要调试跟踪代码,于是乎写了份打印日志的代码. CLogFile.h文件 #if !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8 ...
- CentOS7 emacs安装
首先安装依赖库 依赖库: yum install gcc* yum install glib* yum install gtk* yum install ncurses* yum ...
- iOSNSDate的相关操作
//获取当前时间 时间根据格林威治时间显示 //时间 8小时 英国格林威治 7小时 NSDate *date = [NSDate date]; NSLog(@"%@",date ...
- linux中将程序加入到开机自动启动
如果将在linux中将命令或者程序设置为开机自动启动,只需要进入到将对应命令加入到/etc/rc.d/rc.local文件里即可,如下 打开文件,vi /etc/rc.d/rc.local #!/bi ...
- VC++中使用ADO方式操作ACCESS数据库
ADO(ActiveX Data Object)是Microsoft数据库应用程序开发的新接口,是建立在OLE DB之上的高层数据库访问技术,即使你对OLE DB,COM不了解也能轻松对付ADO,因为 ...
- CSS3秘笈:第一章
1.<div>和<span>标签: <div>和<span>标签:就像是一个空的容器,我们要往里面填充内容.一个div就是一个块,意味着它的前后都要空一 ...