您的位置:首页 > 其它

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

2015-08-30 20:04 316 查看


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.sum=node.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.cover)
{
node[tr<<1].sum+=node.cover;
node[tr<<1|1].sum+=node.cover;
node[tr<<1].cover+=node.cover;
node[tr<<1|1].cover+=node.cover;
node.cover=0;
}
}
void update(int L,int R,int l,int r,int tr)
{
if(L<=l&&r<=R)
{
node.cover++;
node.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.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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: