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;
}
时间: 2024-08-10 19:19:11

hdu1556-Color the ball的相关文章

hdu1556 Color the ball(线段树区间更新)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12814    Accepted Submission(s): 6401 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

HDU1556 Color the ball【树状数组】【区间更新】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: N个气球排成一排,从左到右编号为1~N,给N组数据,每次给2两个整数s,e,表示从s到e将 气球涂色.当涂到N次以后已经忘记了第i个气球被涂过几次颜色了.现在来计算出每个气球被 涂了几次颜色,并输出出来. 思路: 典型的更新区间,单点求值问题.直接模拟会超时,考虑用树状数组来做.单点更新中,树状 数组表示区间的和.在区间更新中,树状数组表示单个元素的变化. 这道题中,区间(s,e

HDU1556:Color the ball(简单的线段树区域更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗? Input 每个测试实例第一行为一个整数N,(N <= 100000).

HDU-1556 Color the ball 【差分数组】

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 <=

HDU1556 color the ball(区间修改,单点查询)

#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #define MAXN 100005 int c[MAXN]; int lowbit(int x) { return x&(-x); } void upda

Color the ball(hdu1556)(hash)或(线段树,区间更新)

Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9502 Accepted Submission(s): 4872 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球

Color the ball(树状数组+线段树)

Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b

HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8272    Accepted Submission(s): 4239 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌

线段树(求单结点) hdu 1556 Color the ball

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22122    Accepted Submission(s): 10715 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

HDU 1199 Color the Ball

Color the Ball Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 119964-bit integer IO format: %I64d      Java class name: Main There are infinite balls in a line (numbered 1 2 3 ....), and initially all of the