hdu 1556Color the ball (树状数组,更新区间,查询单点)

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12566    Accepted Submission(s): 6294

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

8600

Source

HDU 2006-12 Programming Contest

这道题和之前做的树状数组略有不同。

以前的都是更新单点,查询区间,这次反了过来。

做法差不多。

如果更新区间【x,y】增加1

那么只要 update (x,1),update (y+1,-1)

问单点的时候,sum(i)就是i点的值,而不是1..i的和。

可以看做告诉公路收费口的比喻,update (x,1)相当于入口

update (y+1,-1)相当于出口。

而sum(i)就相当于被几个告诉公路穿过,或者说i点属于几个高速公路,所以是求和

还记得noip2012的时候在tyvj上遇到了一道线段数的题,被@Ocean 海洋兄用了一个高速公路的神奇比喻解法A掉了。

现在回想,原来是用了树状数组。

/*************************************************************************
    > File Name: code/hdu/1556.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年08月07日 星期五 01时56分51秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E6+7;

int n;
int c[N];
int lowbit( int x)
{
    return x&(-x);
}
void update ( int x,int delta)
{
    for ( int i = x; i <= N ; i = i + lowbit (i))
    {
    c[i] = c[i] + delta;
    }
}
int sum ( int x)
{
    int res = 0;
    for ( int i = x ; i >= 1 ; i = i - lowbit (i))
    {
    res  = res + c[i];
    }
    return res;
}
int main()
{
    while (scanf("%d",&n)!=EOF && n)
    {
    memset(c,0,sizeof(c));
    for ( int i = 1; i <= n ; i ++)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        update (a,1);
        update (b+1,-1);
    }
    cout<<sum(1);
    for ( int i = 2 ; i <= n ; i ++)
    {
        cout<<" "<<sum(i);   //sum(i) 就是位置i的值,而不是和,
                //因为update 并没有更新区间,而是做了一个标记,具体可以用普通数组模拟一遍就明白了
    }
    cout<<endl;
    }

    return 0;
}
时间: 2024-08-02 11:02:41

hdu 1556Color the ball (树状数组,更新区间,查询单点)的相关文章

Luogu P3368 【模板】树状数组 2 [区间修改-单点查询]

P3368 [模板]树状数组 2 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含2或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k 操作2: 格式:2 x 含义:输出第x个数的值 输出格式: 输出

树状数组2 - 区间加 单点求和

树状数组 = O(logn) 单点修改 ,O(logn) 区间查询 如果要做到 区间修改 单点查询 我们就要加入差分的思想 用树状数组记录数组的差分 然后对差分进行前缀和就可以得到单点的数据 //ios::sync_with_stdio(false); #include<bits/stdc++.h> #define ll long long using namespace std; const int MAXN = 500010; int n,m; ll C[MAXN]; ll lowbit(

Color the ball 树状数组的区间修改

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

hdu 3333 Turing Tree (树状数组+离线处理+离散化)

Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3981    Accepted Submission(s): 1349 Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems a

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从