HDU 1556(线段树+dfs)

大水题......就不说了


 

/******************************
* author :crazy_石头
* data structure: 线段树
* created time:2013/11/4 19:53
* Pro:HDOJ 1556
* Judge Status:Accepted
* Memory:4108K
* Time:609MS
*******************************/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ll __int64
const int maxn=100000+5;

struct Tree
{
    int l,r,sum;//记录结点左右儿子及颜色数目;
}T[maxn<<2];

inline void build(int l,int r,int rt)
{
    T[rt].l=l;
    T[rt].r=r;
    T[rt].sum=0;

    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}

inline void update(int l,int r,int rt)
{
    if(T[rt].l==l&&T[rt].r==r)
    {
        T[rt].sum++;
        return ;
    }
    int mid=(T[rt].l+T[rt].r)>>1;
    if(r<=mid)
        update(l,r,rt<<1);
    else if(l>mid)
        update(l,r,rt<<1|1);
    else
    {
        update(l,mid,rt<<1);
        update(mid+1,r,rt<<1|1);
    }
}

ll res[maxn];

inline void dfs(int rt)
{
    for(int i=T[rt].l;i<=T[rt].r;i++)
    res[i]+=T[rt].sum;

    if(T[rt].l==T[rt].r)
        return;
    dfs(rt<<1);
    dfs(rt<<1|1);
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        build(1,n,1);
        rep(i,1,n)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            update(u,v,1);
        }
        memset(res,0,sizeof(res));
        dfs(1);
        rep(i,1,n)
        i==1?printf("%d",res[i]):printf(" %d",res[i]);
        printf("\n");
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Fog )

你可能感兴趣的:(HDU 1556(线段树+dfs))