Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2590 Accepted Submission(s): 1274
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
恩,题目大意就是说,每朵花都有它自己开放的时间段,然后问一个时间点有多少朵花开放。由于数据问题,要离散化
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100000
using namespace std;
int s[maxn],e[maxn],q[maxn];
int rec[maxn*3];
struct lnode
{
int l,r,s,c;
};
lnode node[maxn<<4];
int bsearch(int l,int r,int aim)
{
while(l<=r)
{
int mid=(l+r)>>1;
if(rec[mid]==aim)
return mid;
if(rec[mid]>aim)
r=mid-1;
else
l=mid+1;
}
}
void build(int o,int l,int r)
{
node[o].l=l;
node[o].r=r;
node[o].s=0;
node[o].c=0;
if(l==r)
return ;
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
}
void pushdown(int o)
{
if(node[o].c)
{
node[o<<1].c+=node[o].c;
node[o<<1|1].c+=node[o].c;
node[o<<1].s+=node[o].c;
node[o<<1|1].s+=node[o].c;
node[o].c=0;
}
}
void update(int o,int l,int r)
{
if(node[o].l==l&&node[o].r==r)
{
node[o].s+=1;
node[o].c+=1;
return ;
}
//pushdown(o);就是由于这里的问题一直WA
int mid=(node[o].l+node[o].r)>>1;
if(r<=mid)
update(o<<1,l,r);
else if(l>mid)
update(o<<1|1,l,r);
else
{
update(o<<1,l,mid);
update(o<<1|1,mid+1,r);
}
}
int query(int o,int aim)
{
if(node[o].l==node[o].r&&node[o].l==aim)
return node[o].s;
pushdown(o);
int mid=(node[o].l+node[o].r)>>1;
if(aim<=mid)
return query(o<<1,aim);
else
return query(o<<1|1,aim);
}
int main()
{
int t,n,m,cnt=0;
scanf("%d",&t);
while(t--)
{
int k=1;
build(1,1,maxn);
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
{
scanf("%d%d",&s[i],&e[i]);
rec[k++]=s[i];
rec[k++]=e[i];
}
for(int i=0;i<m;++i)
{
scanf("%d",&q[i]);
rec[k++]=q[i];
}
sort(rec+1,rec+k);
int tem=2;
for(int i=2;i<k;++i)
{
if(rec[i]!=rec[i-1])
rec[tem++]=rec[i];
}
for(int i=0;i<n;++i)
{
int l=bsearch(1,tem,s[i]);
int r=bsearch(1,tem,e[i]);
update(1,l,r);
}
printf("Case #%d:\n",++cnt);
for(int i=0;i<m;++i)
{
int c=bsearch(1,tem,q[i]);
printf("%d\n",query(1,c));
}
}
return 0;
}