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 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
 

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值