首页 > ACM题库 > HDU-杭电 > HDU 4325-Flowers-线段树-[解题报告]HOJ
2015
05-23

HDU 4325-Flowers-线段树-[解题报告]HOJ

Flowers

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

2
1 1
5 10
4
2 3
1 4
4 8
1
4
6

样例输出:

Case #1:
0
Case #2:
1
2
1

题意:

   给出花的开花时间st[i]与凋谢时间ed[i],要求在一个时间点上开了多少花!!(花<10万,时间<1亿)

解法:

  线段树区间更新+离散化+区间求值!!!以前都有结构体来解决这些问题,但感觉牛人的代码的确够简洁,所以就忍不住拿来学学,过不其然,怎一个爽字了得!

/*
转自 傻崽大牛博客的一段话
离散化简单的来说就是只取我们需要的值来用,比如说区间[1000,2000],[1990,2012] 我们用不到[-∞,999][1001,1989][1991,1999][2001,2011][2013,+∞]这些值,所以我只需要1000,1990,2000,2012就够了,将其分别映射到0,1,2,3,在于复杂度就大大的降下来了
所以离散化要保存所有需要用到的值,排序后,分别映射到1~n,这样复杂度就会小很多很多
而这题的难点在于每个数字其实表示的是一个单位长度(并非一个点),这样普通的离散化会造成许多错误(包括我以前的代码,poj这题数据奇弱)
给出下面两个简单的例子应该能体现普通离散化的缺陷:
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖
为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]
如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<memory.h>
using namespace std;
const int maxn=100002;
int sum[maxn<<2],st[maxn],ed[maxn],x[maxn<<2],col[maxn<<2],n,m,pos[maxn];
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
void pushUP(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushDOWN(int rt,int m)
{
    if(col[rt])
    {
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        sum[rt<<1]+=(m-(m>>1))*col[rt];
        sum[rt<<1|1]+=(m>>1)*col[rt];
        col[rt]=0;
    }
}
void build(int rt,int l,int r)
{
    col[rt]=0;sum[rt]=0;
    if(l==r)
    {
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushUP(rt);
}
void updata(int rt,int l,int r,int L,int R,int c)
{
    if(L<=l&&r<=R)
    {
        col[rt]+=c;
        sum[rt]+=(r-l+1)*c;
        return;
    }
    pushDOWN(rt,r-l+1);
    int mid=(l+r)>>1;
    if(mid>=L)updata(lson,L,R,c);
    if(mid<R) updata(rson,L,R,c);
    pushUP(rt);
}
int query(int rt,int l,int r,int pos)
{
    if(l==r)
    {
        return sum[rt];
    }
    pushDOWN(rt,r-l+1);
    int mid=(l+r)>>1;
    if(mid>=pos) return query(lson,pos);
    else return query(rson,pos);
}
int bin(int data,int l,int r)
{
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(x[mid]==data) return mid;
        if(x[mid]<data)l=mid+1;
        else r=mid;
    }
}
int main()
{
    int cas=1,i,j,cnt,t,idx;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        cnt=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&st[i],&ed[i]);
            x[++cnt]=st[i];
            x[++cnt]=ed[i];
        }
         for(i=0;i<m;i++)
        {
            scanf("%d",&pos[i]);
            x[++cnt]=pos[i];
        }
        sort(x+1,x+cnt+1);
        int tmp=unique(x+1,x+cnt+1)-(x+1);//去重函数
        for(i=tmp;i>=2;i--)//利用大牛的一段话
        {
            if(x[i]!=x[i-1]+1)x[++tmp]=x[i-1]+1;
        }
        sort(x+1,x+tmp+1);
        build(1,1,tmp);
        for(i=0;i<n;i++)
        {
            int l=bin(st[i],1,tmp);
            int r=bin(ed[i],1,tmp);
            updata(1,1,tmp,l,r,1);
        }
        printf("Case #%d:\n",cas++);
        for(i=0;i<m;i++)
        {
            idx=bin(pos[i],1,tmp);
            printf("%d\n",query(1,1,tmp,idx));
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/azheng51714/article/details/7821258