hdu4325 Flowers

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

这题需要用到离散化,因为10^9建立线段树会超时,而给的数字总共只有2*n+m,所以可以先离散化,(这里注意因为最后询问的时候所查询的时间可能没有在前n对出现,如果只对n对数字离散化,后面询问的时候会出错),我的离散化是先构造一个结构体储存输入的2*n+m的数的数字num和编号id,然后对关键词num排序,去重后用map<int,int>匹配编号,匹配完后再对id排序复原。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 300006
int sum,pos[maxn];
struct node{
	int l,r,sum;
}b[4*maxn];

struct edge{
	int id,num;
}a[maxn];

bool cmp1(edge a,edge b){
	return a.num<b.num;
}
bool cmp2(edge a,edge b){
	return a.id<b.id;
}

void build(int l,int r,int i)
{
	int mid;
	b[i].l=l;b[i].r=r;b[i].sum=0;
	if(l==r)return;
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
}

void update(int l,int r,int i)
{
	int mid;
	if(b[i].l==l && b[i].r==r){
		b[i].sum++;return;
	}
	mid=(b[i].l+b[i].r)/2;
	if(r<=mid)update(l,r,i*2);
	else if(l>mid)update(l,r,i*2+1);
	else {
		update(l,mid,i*2);
		update(mid+1,r,i*2+1);
	}
}

void question(int id,int i)
{
	int mid;
	if(b[i].l==b[i].r){
		sum=b[i].sum;return;
	}
	b[i*2].sum+=b[i].sum;
	b[i*2+1].sum+=b[i].sum;
	b[i].sum=0;
	mid=(b[i].l+b[i].r)/2;
	if(id<=mid)question(id,i*2);
	else question(id,i*2+1);
}

int main()
{
	int n,m,i,j,T,h,c,d,t;
	map<int,int>hash;
	scanf("%d",&T);
	for(h=1;h<=T;h++){
		printf("Case #%d:\n",h);
		scanf("%d%d",&n,&m);
		build(1,maxn,1);
		for(i=1;i<=n;i++){
			scanf("%d%d",&a[i].num,&a[i+n].num);
			a[i].id=i;a[i+n].id=i+n;
		}
		for(i=1;i<=m;i++){
			scanf("%d",&a[i+2*n].num);
			a[i+2*n].id=i+2*n;
		}
		sort(a+1,a+2*n+m+1,cmp1);
		hash[a[1].num]=1;t=1;
		for(i=2;i<=2*n+m;i++){
			if(a[i].num!=a[i-1].num){
				t++;hash[a[i].num]=t;
			}
		}
		sort(a+1,a+2*n+m+1,cmp2);
		for(i=1;i<=n;i++){
			c=hash[a[i].num];d=hash[a[i+n].num];
			//printf("%d %d\n",c,d);
			update(c,d,1);
		}

		for(i=1;i<=m;i++){
			c=hash[a[i+2*n].num];
			sum=0;
			question(c,1);
			printf("%d\n",sum);
		}
	}
	return 0;
}

时间: 2024-10-05 11:41:45

hdu4325 Flowers的相关文章

HDU4325——二分——Flowers

http://acm.hdu.edu.cn/showproblem.php?pid=4325 /* upper_bound 找大于a[i]的最近的下标 lower_bound 找大于等于a[i]的最近的下标 1 2 4 4 4 5 6 ... l r l r l l l ... 此时q = 4 upper_bound 值为4 lower_bound 值为2 l : (4,5] r : [4,5) l - r : [4] */ /**********************************

Flowers(二分水过。。。)

Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2579    Accepted Submission(s): 1265 Problem Description As is known to all, the blooming time and duration varies between different kinds

BNUOJ 1038 Flowers(BFS)

Flowers Time Limit: 1000ms Memory Limit: 65535KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next 春天到了,师大的园丁们又开始忙碌起来了. 京师广场上有一块空地,边界围成了一个多边形,内部被划分成一格一格的.园丁们想在这个多边形内的每一格内种植一些花. 现在请你帮忙计算一下一共最多可以种多少花.

Codeforces Round #261 (Div. 2) 459B. Pashmak and Flowers(数学题,组合)

题目链接:http://codeforces.com/problemset/problem/459/B B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak decided to give Parmida a pair of flowers from the garden.

605. Can Place Flowers(LeetCode)

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

USACO 保护花朵 Protecting the Flowers, 2007 Jan

Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园, 正在啃食他精心培育的花朵!约翰要立即采取行动,挨个把它们全部关回牛棚. 约翰牵走第 i 头奶牛需要 T i 分钟,因为要算来回时间,所以他实际需要 2 · T i 分钟.第 i 头奶牛 如果还在花园里逍遥,每分钟会啃食 Di 朵鲜花.但只要约翰抓住了它,开始牵走它的那刻开始,就 没法吃花了.请帮

HDU 4325 Flowers(树状数组)

Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3150    Accepted Submission(s): 1549 Problem Description As is known to all, the blooming time and duration varies between different kinds

【CodeForces 621C】Wet Shark and Flowers

题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i andi + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too. Each shark will grow some number of flowers si. For i

(线段树 区间运算求点)Flowers -- hdu -- 4325

http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2577    Accepted Submission(s): 1263 Problem Description As is known to all, the blooming t