HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556

Color the ball

Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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: 2486
Memory: 1816 KB		Time: 764 MS
Language: G++		Result: Accepted
VJ RunId: 4328616		Real RunId: 14413462
*/
//声明,首先运用树状数组进行区间修改并且单点更新的前提是符合前辍和
//什么叫做符合前缀和,就是一个点的值能够通过前面的求和得到
//为什么树状数组可以这么做,本博客有说明
//此题目本博客还提供了线段树的解法
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 100000 + 5;
int  N, C[MAXN], a, b;

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

void add(int pos,int x){
    for(int i = pos; i <= N;i += lowbit(i)){
        C[i] += x;
    }
}

int query(int pos){
    int ret = 0;
    for(int i = pos;i > 0;i -= lowbit(i)){
        ret += C[i];
    }
    return ret;
}

int main(){
    while(~ scanf("%d", &N), N){
        memset(C, 0, sizeof(C));
        for(int i = 1;i <= N ;i ++){
            scanf("%d%d", &a, &b);
            add(a, 1);
            add(b + 1, -1);
        }
        for(int i = 1;i <= N;i ++){
            if(i != 1)printf(" ");
            printf("%d", query(i));
        }
        printf("\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-27 01:01:55

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)的相关文章

HDU - 3584 Cube (三维树状数组 + 区间修改 + 单点求值)

HDU - 3584 Cube Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. I

hdu 1556 Color the ball(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接: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个气球已经涂过几次颜色了,你能帮他算出每个气

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): 15491    Accepted Submission(s): 7731 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

hdu 4031 Attack(树状数组区间更新单点求值&amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

hdu 4031 Attack(树状数组区间更新单点求值&amp;amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we ha

HDU 1556 Color the ball【树状数组】

题意:给出n个区间,每次给这个区间里面的数加1,询问单点的值 一维的区间更新,单点查询,还是那篇论文里面讲了的 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #

HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)

HDU - 3584 Cube Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. I

HDU 1566 Color the ball(树状数组or线段树)

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便为骑上他的"小飞鸽"