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开始到气 ...
随机推荐
- Hibernate中的脏检查和缓存清理机制
脏检查 Session到底是如何进行脏检查的呢?当一个Customer对象被加入到Session缓存中时,Session会为Customer对象的值类型的属性复制一份快照.当Session清理缓存时, ...
- netty Failed to submit an exceptionCaught() event异常
最近测试netty开发的服务端应用在关闭时,出现下列异常: ->###WARN#########nioEventLoopGroup-3-2###io.netty.util.internal.lo ...
- 使用svcutil.exe 工具来生成调用文件
svcutil.exe http://localhost:9065/ServiceDemo.svc?wsdl 这将生成一个配置文件和一个包含客户端类的代码文件. 下面我们就用这个是怎么生成的: 1,打 ...
- UIAlertController弹出提示框
#import "RootViewController.h" #import "RootView.h" #define kColor arc4random() ...
- 【转】IOS动画的实现,其实很简单
动画效果提供了状态或页面转换时流畅的用户体验,在iOS系统中,咱们不需要自己编写绘制动画的代码,Core Animation提供了丰富的api来实现你需要的动画效果.UIKit只用UIView来展示动 ...
- OC中代理的使用
通常代理的使用需要以下几个步骤: 1.制定协议.协议可以在委托对象的.h中声明,也可以在单独的.h中声明.制定协议后,在协议中声明需要代理对象来实现的方法. 2.设置代理属性.制定协议后需要为委托对象 ...
- 测试管理_下属谈话[持续更新ing]
作为测试部门的管理者,在工作绩效评定.工作安排.工作问题提出等时候,都需要与下属进行面对面谈话,怎么进行有效的谈话,这是一个值得思考和锻炼的问题. 谈话的内容: 谈近阶段工作的回顾 谈工作中的困难(是 ...
- js日期时间函数
日期时间脚本库方法列表 Date.prototype.isLeapYear 判断闰年Date.prototype.Format 日期格式化Date.prototype.DateAdd 日期计算Date ...
- node中简单scoket介绍
/*socket.io是一个基于 node.js 的项目,其主要作用是将WebScoket 协议应用到所有的浏览器.该模块主要应用于实时的长连接 多请求的项目中,例如在线联网游戏中.实时聊天.实施股票 ...
- spinner下拉列表
xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l ...