HDU 1556 Color the ball
这题用线段树的话简直就是一个水题。。不过刚学树状数组,要用一下。
题意:每次给你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的更多相关文章
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball(区间更新,单点求值)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
随机推荐
- 机器学习实战 - 读书笔记(12) - 使用FP-growth算法来高效发现频繁项集
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第12章 - 使用FP-growth算法来高效发现频繁项集. 基本概念 FP-growt ...
- Rendering Problems: No Android SDK found. Please configure an Android SDK. 怎解决?
Rendering Problems No Android SDK found. Please configure an Android SDK.
- [Windows Phone] APP上架,遇到错误2001的解决方案。(Error:2001)
[Windows Phone] APP上架,遇到错误2001的解决方案.(Error:2001) 问题情景 最近在开始玩Windows Phone的开发,开发的过程中虽然有点小挫折,但是参考网络许多前 ...
- [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider
[ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...
- javascript --- 子对象访问父对象的方式
在传统面向对象的编程语言里,都会提供一种子类访问父类的特殊语法,引文我们在实现子类方法往往需要父类方法的额外辅助.在这种情况下,子类通常会调用父类中的同名方法,最终以便完成工作. javascript ...
- android布局--Android fill_parent、wrap_content和match_parent的区别
来自:http://www.cnblogs.com/nikyxxx/archive/2012/06/15/2551390.html 三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础 ...
- 2015第18本:从0到1,ZERO to ONE, Notes on startups, or how to build the future
<从0到1>中文版的副标题是”开创商业与未来的秘密“,题目大得吓人,英文副标题就谨慎了许多:Notes on startups, or how to build the future. 全 ...
- Android 自定义带刻度的seekbar
自定义带刻度的seekbar 1.布局 <span style="font-family:SimHei;font-size:18px;"><com.imibaby ...
- 搭建Android 5.0开发环境
1.Android SDK的安装 下载地址:http://developer.android.com/index.html 访问网站的话请自备梯子 选择:adt-bundle-windows-x86_ ...
- CocoaPod出现-bash: pod: command not found 解决办法
从过年来到公司 就不用自己电脑了 之前一直自己带电脑 昨天随便建了一个demo 使用cocoapods 发现 -bash: pod: command not found 刚开 ...