您的位置:首页 > 其它

线段树+离散化+离线:HDU 4325

2016-08-04 18:49 218 查看
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

区间更新,点查询;

离散所有的点!包括每朵花的花期,还有查询的时间点!!!

然后离线处理!

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100009;
long long pos[maxn<<1],sum[maxn<<2],add[maxn<<2],q[maxn];
int n,mm,m;

struct node
{
int l,r;
} f[maxn];
void pushdown(int rt)
{
if(add[rt])
{
add[rt<<1]+=add[rt],add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt],sum[rt<<1|1]+=add[rt];
add[rt]=0;
}
}
void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
add[rt]++;
sum[rt]++;
return;
}
pushdown(rt);
int m=l+r>>1;
if(m<L) update(L,R,rson);
else if(m>=R) update(L,R,lson);
else
{
update(L,m,lson);
update(m+1,R,rson);
}
}
int query(int t,int l,int r,int rt)
{
if(l==r)
return sum[rt];
pushdown(rt);
int m=l+r>>1;
if(t>m) return query(t,rson);
else return query(t,lson);
}
int fin(int key,int high)
{
int low=0,mid;
while(low<=high)
{
mid=high+low>>1;
if(pos[mid]==key) return mid;
else if(pos[mid]>key) high=mid-1;
else low=mid+1;
}
}
int main()
{
int t;
scanf("%d",&t);
for(int ase=1; ase<=t; ase++)
{
scanf("%d%d",&n,&mm);
int cnt=0;
for(int i=1; i<=n; i++)
{
scanf("%d%d",&f[i].l,&f[i].r);
pos[cnt]=f[i].l,pos[cnt+1]=f[i].r,cnt+=2;
}
for(int i=1;i<=mm;i++)
{
scanf("%d",&q[i]);
pos[cnt++]=q[i];
}
sort(pos,pos+cnt);
m=1;
for(int j=1; j<cnt; j++)
if(pos[j]!=pos[j-1])
pos[m++]=pos[j];
memset(sum,0,sizeof sum);
memset(add,0,sizeof add);
for(int i=1; i<=n; i++)
{
int ll=fin(f[i].l,m-1);
int rr=fin(f[i].r,m-1);
update(ll,rr,0,m-1,1);
}
printf("Case #%d:\n",ase);
for(int i=1; i<=mm; i++)
{
int qq=fin(q[i],m-1);
printf("%d\n",query(qq,0,m-1,1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: