首页 > 代码库 > hdu1556-Color the ball

hdu1556-Color the ball

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

 

解题思路

直接线段树。

 

代码

#include<stdio.h>#include<string.h>#define MAX_SIZE 100010//#define LOCALstruct 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;}

 

hdu1556-Color the ball