码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-30 21:28:42      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

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
 

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);
		}
	}
}


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

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

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48108517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!