hdu1556-Color the ball

题目链接 http://bak.vjudge.net/problem/HDU-1556

 

解题思路

直接线段树。

 

代码

#include<stdio.h>
#include<string.h>
#define MAX_SIZE 100010
//#define LOCAL
struct node {
    int left, right;
    int count;
}tree[MAX_SIZE*4];
int now;
int n;
void build(int l, int r, int root)
{
    tree[root].left = l;
    tree[root].right = r;
    tree[root].count = 0;
    if(l == r) return;
    int mid = (l + r) / 2;
    build(l, mid, root*2);
    build(mid+1, r, root*2+1);
}
void update(int l, int r, int root)
{
    if(tree[root].left == l && r == tree[root].right) {
        tree[root].count++;
        return ;
    }
    if(tree[root].count != 0) {
        tree[2*root].count += tree[root].count;
        tree[2*root+1].count += tree[root].count;
        tree[root].count = 0;
    }
    int mid = (tree[root].left + tree[root].right) / 2;
    if(r <= mid) update(l, r, root*2);
    else if(mid < l) update(l, r, root*2+1);
    else {
        update(l, mid, root*2);
        update(mid+1, r, root*2+1);
    }
}
void cal(int root)
{
    if(tree[root].left == tree[root].right) {
        if(now != n) printf("%d ", tree[root].count);
        else printf("%d\n", tree[root].count);
        now++;
        return ;
    }
    if(tree[root].count != 0) {
        tree[2*root].count += tree[root].count;
        tree[2*root+1].count += tree[root].count;
        tree[root].count = 0;
    }
    cal(root*2);
    cal(root*2+1);
}
int main()
{
    #ifdef LOCAL
        freopen("ans.txt", "w", stdout);
    #endif
    scanf("%d", &n);
    while(n != 0) {
        build(1, n, 1);
        int x, y;
        now = 1;
        for(int i=0; i<n; i++) {
            scanf("%d%d", &x, &y);
            update(x, y, 1);
        }
        cal(1);
        scanf("%d", &n);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ZengWangli/p/5902067.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值