HDU_1556 Color the ball

//自己好好理解吧!
//线段树的题目变化很多
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX 100005
struct node
{
    int left;
    int right;
    int value;
};
node tree[MAX*4];
int n;
void Build_tree(int pos,int l,int r)
{
    tree[pos].left=l;
    tree[pos].right=r;
    tree[pos].value=0;
    int mid=(l+r)/2;
    if(l==r)
        return ;
    Build_tree(pos*2,l,mid);
    Build_tree(pos*2+1,mid+1,r);
}

void Update(int pos,int ss,int tt)
{
    if(ss==tree[pos].left&&tree[pos].right==tt)
    {
        tree[pos].value++;
        return ;
    }
    int mid=(tree[pos].left+tree[pos].right)/2;
    if(ss>mid)
        Update(pos*2+1,ss,tt);
    else if(tt<=mid)
        Update(pos*2,ss,tt);
    else
    {
        Update(pos*2,ss,mid);
        Update(pos*2+1,mid+1,tt);
    }

}
void ans(int pos,int sum)
{
    if(tree[pos].left==tree[pos].right)
    {
        printf("%d",sum+tree[pos].value);
        if(tree[pos].right!=n)
            printf(" ");
        return ;
    }
    ans(pos*2,sum+tree[pos].value);
    ans(pos*2+1,sum+tree[pos].value);
}
int main()
{
    int i,j,k,x,y;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        Build_tree(1,1,n);
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            Update(1,x,y);
        }
        ans(1,0);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(tree,HDU,datebase,segment)