天天看點

HDU 1566 Color the ball(樹狀數組or線段樹) Color the ball

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11387    Accepted Submission(s): 5680

Problem Description N個氣球排成一排,從左到右依次編号為1,2,3....N.每次給定2個整數a b(a <= b),lele便為騎上他的“小飛鴿"牌電動車從氣球a開始到氣球b依次給每個氣球塗一次顔色。但是N次以後lele已經忘記了第I個氣球已經塗過幾次顔色了,你能幫他算出每個氣球被塗過幾次顔色嗎?  

Input 每個測試執行個體第一行為一個整數N,(N <= 100000).接下來的N行,每行包括2個整數a b(1 <= a <= b <= N)。

當N = 0,輸入結束。  

Output 每個測試執行個體輸出一行,包括N個整數,第I個數代表第I個氣球總共被塗色的次數。  

Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
        

Sample Output

1 1 1
3 2 1
        

Author 8600  

Source HDU 2006-12 Programming Contest  

Recommend LL   |   We have carefully selected several similar problems for you:   1542  1698  1541  3397  3333   

    思路:題意很容易懂隻是用一般的辦法會逾時,是以需要用到一些省時間的算法,樹狀數組和線段樹就恰恰符合這個條件

樹狀數組

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int a[100010];
int n;

int lowbit(int x)
{
     return x&(-x);
}

void build(int x,int y)
{
    while(x<=n)
    {
        a[x] += y;
        x += lowbit(x);
    }
}

int sum(int x)//求和
{
    int s=0;
    while(x>0)
    {
        s=s+a[x];
        x=x-lowbit(x);
    }
    return s;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0)
        {
            break;
        }
        int x,y;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            build(x,1);
            build(y+1,-1);
        }
        for(int i=1;i<=n;i++)
        {
            if(i == n)
            {
                printf("%d\n",sum(i));
            }
            else
            {
                printf("%d ",sum(i));
            }
        }
    }
    return 0;
}
           

線段樹:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

const int maxn = 100010;

struct node
{
    int l;
    int r;
    int lz;
    int ans;
} q[maxn<<4];

int n;

void pushdown(int rt,int lr)
{
    if(q[rt].lz)
    {
        q[rt<<1].ans += (lr-(lr>>1))*q[rt].lz;
        q[rt<<1|1].ans += (lr>>1)*q[rt].lz;
        q[rt<<1].lz += q[rt].lz;
        q[rt<<1|1].lz += q[rt].lz;
        q[rt].lz = 0;
    }
}

void build(int l,int r,int rt)
{
    q[rt].l = l;
    q[rt].r = r;
    q[rt].ans = 0;
    q[rt].lz = 0;
    if(l == r)
    {
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    q[rt].ans = q[rt<<1].ans + q[rt<<1|1].ans;
}

void updata(int ll,int rr,int k,int l,int r,int rt)
{
    if(ll>r || rr<l)
    {
        return ;
    }
    if(ll<=l && rr>=r)
    {
        q[rt].ans = q[rt].ans + (r-l+1)*k;
        q[rt].lz += k;
        return ;
    }
    pushdown(rt,r-l+1);
    int mid = (l+r)>>1;
    if(mid>=ll)
    {
        updata(ll,rr,k,l,mid,rt<<1);
    }
    if(rr>mid)
    {
        updata(ll,rr,k,mid+1,r,rt<<1|1);
    }
    q[rt].ans = q[rt<<1].ans + q[rt<<1|1].ans;
}

void qurry(int l,int r,int rt)
{
    if(l == r)
    {
        if(l == n)
        {
            printf("%d\n",q[rt].ans);
        }
        else
        {
            printf("%d ",q[rt].ans);
        }
        return ;
    }
    pushdown(rt,r-l+1);
    int mid = (l+r)>>1;
    qurry(l,mid,rt<<1);
    qurry(mid+1,r,rt<<1|1);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0)
        {
            break;
        }
        build(1,n,1);
        int x,y;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            updata(x,y,1,1,n,1);
        }
        qurry(1,n,1);
    }
    return 0;
}