这题用线段树的话简直就是一个水题。。不过刚学树状数组,要用一下。

题意:每次给你a,b,表明a~b之间涂色,然后最后一次输出每个气球被涂色的次数。

要用树状数组就要考虑怎么转化为前缀和问题,这题可以这样做:每次输入a,b,令A[a] = 1,A[b+1] = -1; 然后更新和,查询的时候容易知:a~b之间都被涂了一次,求前缀和结果也为一次,多次插入a,b,性质不变,插入后即可直接输出。复杂度很小,这也是树状数组的好处。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100100 int c[N];
int n; int lowbit(int k)
{
return k&(-k);
} void insert(int k,int num)
{
while(k<=n)
{
c[k] += num;
k += lowbit(k);
}
} int getsum(int k)
{
int sum = ;
while(k>)
{
sum += c[k];
k -= lowbit(k);
}
return sum;
} void init()
{
int a,b,i;
memset(c,,sizeof(c));
for(i=;i<=n;i++)
{
scanf("%d%d",&a,&b);
insert(a,);
insert(b+,-);
}
} int main()
{
int j;
while(cin>>n && n)
{
init();
for(j=;j<n;j++)
{
cout<<getsum(j)<<" ";
}
cout<<getsum(j)<<endl;
}
return ;
}

代码参考了:http://blog.csdn.net/lulipeng_cpp/article/details/7816527

HDU 1556 Color the ball的更多相关文章

  1. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. hdu 1556 Color the ball(区间更新,单点求值)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  10. hdu 1556 Color the ball 线段树

    题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...

随机推荐

  1. 数码管问题(c++实现)

    描述:液晶数码管用七笔阿拉数字表示的十个数字,把横和竖的一 个短划都称为一笔,即7有3笔,8有7笔等.对于十个数字一种排列,要做到 两相邻数字都可以由另一个数字加上几笔或减去几笔组成,但不能又加又减. ...

  2. HTML 块元素

    分为3类 1. 结构块 只能包含块级元素.它们包含结构含义,但没有语义含义,也就是,不能说明内容是什么,只能说明其组织方式. <ol> <ul> <dl> < ...

  3. javascript数组浅谈2

    上次说了数组元素的增删,的这次说说数组的一些操作方法 join()方法: ,,] arr.join("_") //1_2_3 join方法会返回一个由数组中每个值的字符串形式拼接而 ...

  4. ABAP - 3D Graphs with SAP

    在ABAP设计中,程序员经常需要用图形显示报表的统计结果,我们可以使用函数:GRAPH_MATRIX_3D来达到图形显示.GRAPH_MATRIX_3D函数参数很多,但只有三个参数必须需要输入:Tab ...

  5. 2015年第12本(英文第8本):the Old Man and the Sea 老人与海

    书名:The Old Man and The Sea(老人与海) 作者:Ernest Hemingway 单词数:2.7万 不重复单词数:2600 首万词不重复单词数:1500 蓝思值:940 阅读时 ...

  6. Python基础(7)--函数

    本篇文章将介绍如何将语句组织成函数,以及参数概念以及在程序中的用途 本文地址:http://www.cnblogs.com/archimedes/p/python-function.html,转载请注 ...

  7. 查看一个软件ipa包的内容

    1.打开itunes     下载你要用的app 把ipa文件拉到桌面上              然后进行压缩

  8. 复杂对象的本地化(以Person为例)

    Person.h #import <Foundation/Foundation.h> @interface Person : NSObject <NSCoding> /// 姓 ...

  9. URL、表单数据、IP等处理类

    <?php class ev { public $cookie; public $post; public $get; public $file; public $url; public $G; ...

  10. org.dom4j.documentexception异常

    org.dom4j.documentexception 解决: 设置xml文件编码格式:<?xml version="1.0" encoding="UTF-8&qu ...