HDOJ 题目4325 Flowers(线段树+离散化)

Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2493    Accepted Submission(s): 1235

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

Author

BJTU

Source

2012 Multi-University Training Contest 3

Recommend

zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4320 2514 4303 4308 4310

题目大意:有n朵花,每朵花都有相应的开花开始和截止时间,m次查询,每次查询问这个时间点,有几朵花是开的

ac代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[100020],b[100020],c[300030],q[100020];
struct s
{
	int sum,cover;
}node[100020<<2];
void build(int l,int r,int tr)
{
	node[tr].sum=node[tr].cover=0;
	if(l==r)
		return;
	int mid=(l+r)>>1;
	build(l,mid,tr<<1);
	build(mid+1,r,tr<<1|1);
}
int bseach(int key,int n)
{
	int l=0,r=n-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(c[mid]==key)
			return mid;
		if(key>=c[mid])
			l=mid+1;
		else
			r=mid-1;
	}
	return l;
}
void pushdown(int tr)
{
	if(node[tr].cover)
	{
		node[tr<<1].sum+=node[tr].cover;
		node[tr<<1|1].sum+=node[tr].cover;
		node[tr<<1].cover+=node[tr].cover;
		node[tr<<1|1].cover+=node[tr].cover;
		node[tr].cover=0;
	}
}
void update(int L,int R,int l,int r,int tr)
{
	if(L<=l&&r<=R)
	{
		node[tr].cover++;
		node[tr].sum++;
		return;
	}
	pushdown(tr);
	int mid=(l+r)>>1;
	if(L<=mid)
		update(L,R,l,mid,tr<<1);
	if(R>mid)
		update(L,R,mid+1,r,tr<<1|1);
}
int query(int pos,int l,int r,int tr)
{
	if(l==r)
	{
		return node[tr].sum;
	}
	int mid=(l+r)>>1;
	pushdown(tr);
	if(pos<=mid)
		query(pos,l,mid,tr<<1);
	else
		query(pos,mid+1,r,tr<<1|1);
}
int main()
{
	int t,cas=0;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,cnt=0,i,j;
		scanf("%d%d",&n,&m);
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&a[i],&b[i]);
			c[cnt++]=a[i];
			c[cnt++]=b[i];
		}
		for(i=0;i<m;i++)
		{
			scanf("%d",&q[i]);
			c[cnt++]=q[i];
		}
		sort(c,c+cnt);
		int k=unique(c,c+cnt)-c;
		build(1,k,1);
		for(i=0;i<n;i++)
		{
			int x=bseach(a[i],k)+1;
			int y=bseach(b[i],k)+1;
			update(x,y,1,k,1);
		}
		printf("Case #%d:\n",++cas);
		for(i=0;i<m;i++)
		{
			int p=bseach(q[i],k)+1;
			int ans=query(p,1,k,1);
			printf("%d\n",ans);
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 02:35:55

HDOJ 题目4325 Flowers(线段树+离散化)的相关文章

hdoj 4325 Flowers 线段树+离散化

hdoj 4325 Flowers 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4325 思路: 直接线段树,按照花的开放区间的大小建树,要注意虽然花的周期数据可能会达到1e9,这样的话线段树开四倍时不可能的.但是我们可以看到一共可能的数据时N行,那么每行两个数,再开4倍的区间.计算下来,在离散化的帮助下,我们只需要开8*N被的线段树即可. 另外询问的数据也需要放入离散化的范围,如果不这样做,有可能在询问时使用lower_bound函数会导致数

POJ 题目2528 Mayor&#39;s posters(线段树+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49564   Accepted: 14361 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ_2528 Mayor&#39;s poster(线段树+离散化)

题目请点我 题解: 这道题与之前的题目相比重点在于一个映射的预处理,题目所给的区间达到10000000,而最多只有10000个点,如果直接建树的话太过于空旷.把这些区间的左右节点一一对应,最多有4×10000个点,远小于之前的10000000,而且区间之间的对应关系也不会改变. 举个例子: 区间:[2,6],[4,8],[6,10] 我们进行下面对应: 2 4 6 8 10 1 2 3 4 5 则原区间变为[1,3],[2,4],[3,5].可以发现它们之间的覆盖关系并没有改变,但是却紧凑了很多

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

[UESTC1059]秋实大哥与小朋友(线段树, 离散化)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真是个沙茶…… 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓

【POJ】 2528 - Mayor&#39;s posters 【线段树+离散化】

题目: Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 47228   Accepted: 13719 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral

HDU 3743 Frosh Week (线段树+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 Frosh Week Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: 

【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11294   Accepted: 3091 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remembe