我是靠谱客的博主 坚强铅笔,最近开发中收集的这篇文章主要介绍HDU4325-Flowers Flowers,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Flowers

                                                                    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                            Total Submission(s): 3290    Accepted Submission(s): 1630


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
2012 Multi-University Training Contest 3
 

Recommend
zhoujiaqi2010
 

题意:给你n多花的开放时间,问一个时间有多少花开放

解题思路:先将所有时间离散化,然后就是维护树状数组,区间修改求单点


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, q;
int x[300009], t[300009],a[100009],l[100009],r[100009],m;

int lowbit(int k)
{
	return k&-k;
}

void update(int k,int val)
{
	while (k <m)
	{
		x[k]+=val;
		k += lowbit(k);
	}
}

int getsum(int k)
{
	int sum = 0;
	while (k)
	{
		sum += x[k];
		k -= lowbit(k);
	}
	return sum;
}

int main()
{
	int T,cas=0;
	scanf("%d", &T);
	while (T--)
	{
		int cnt = 1;
		memset(x, 0, sizeof x);
		scanf("%d%d", &n,&q);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &l[i], &r[i]);
			t[cnt++] = l[i];
			t[cnt++] = r[i];
		}
		for (int i = 1; i <= q; i++) scanf("%d", &a[i]), t[cnt++] = a[i];
		sort(t+1, t + cnt);
		m = unique(t+1, t + cnt) - t;
		for (int i = 1; i <= n; i++)
		{
			int ll = lower_bound(t+1, t + m, l[i]) - t;
			int rr = lower_bound(t+1, t + m, r[i]) - t;
			update(ll, 1); update(rr + 1, -1);
		}
		printf("Case #%d:n", ++cas);
		for (int i = 1; i <= q; i++)
		{
			int aa = lower_bound(t, t + m, a[i]) - t;
			printf("%dn", getsum(aa));
		}
	}
	return 0;
}

最后

以上就是坚强铅笔为你收集整理的HDU4325-Flowers Flowers的全部内容,希望文章能够帮你解决HDU4325-Flowers Flowers所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部