Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2201 Accepted Submission(s): 1084
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.
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 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:
1068
4320
4324
2512
2514
解题思路:线段树简单题,注意离散化
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int tree[Max<<2],st[Max],ed[Max],q[Max],time[Max<<1];
void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
tree[rt]++;
return ;
}
int m=(l+r)>>1;
if(L<=m)
update(L,R,lson);
if(R>m)
update(L,R,rson);
}
int query(int pos,int ans,int l,int r,int rt)
{
ans+=tree[rt];
if(l==r)
return ans;
int m=(l+r)>>1;
if(pos<=m)
return query(pos,ans,lson);
else
return query(pos,ans,rson);
}
int main()
{
int i,k,t,m,n,cnt;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&t);
for(k=1;k<=t;k++)
{
cnt=0;
memset(tree,0,sizeof(tree));
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d",&st[i],&ed[i]);
time[cnt++]=st[i];time[cnt++]=ed[i];
}
for(i=1;i<=m;i++)
{
scanf("%d",&q[i]);
time[cnt++]=q[i];
}
sort(time,time+cnt);
cnt=unique(time,time+cnt)-time;
for(i=1;i<=n;i++)
{
st[i]=lower_bound(time,time+cnt,st[i])-time+1;
ed[i]=lower_bound(time,time+cnt,ed[i])-time+1;
update(st[i],ed[i],1,cnt,1);
}
printf("Case #%d:\n",k);
for(i=1;i<=m;i++)
{
q[i]=lower_bound(time,time+cnt,q[i])-time+1;
printf("%d\n",query(q[i],0,1,cnt,1));
}
}
return 0;
}