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开始到气 ...
随机推荐
- Web Service(一) 基础学习
1 基础的Web Service平台是XML+HTTP. 2 Web Service平台的元素包括:SOAP(Simple Object Access Protocol)简单对象访问协议: UDDI( ...
- [Architecture Design] 3-Layer基础架构
[Architecture Design] 3-Layer基础架构 三层式体系结构 只要是软件从业人员,不管是不是本科系出身的,相信对于三层式体系结构一定都不陌生.在三层式体系结构中,将软件开发所产出 ...
- css导航栏
几个导航栏也算对学过知识的回顾,总有新的收获,下面是学习过程中敲的代码: <!DOCTYPE HTML> <html> <head> <title>&l ...
- JavaScript this特性,静态方法 和实例方法,prototype
<script type="text/javascript"> function logs(str) { document.write(str + "< ...
- JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法
今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...
- C# DevExpress 的gridControl或gridView数据导出失败解决方法
来自:http://blog.csdn.net/lybwwp/article/details/8049464 谢谢 在使用DevExpress 的GridPanel控件的时候出现了一个莫名其妙的现象, ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q77-Q80)
Question 77You have a SharePoint list named Announcements.You have an event receiver that contains t ...
- Android 系统版本&API对照表
最新Android系统版本与API等级对应关系表 数据来源:http://d.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLe ...
- linux命令----网络地址
IP即时生效(重启后失效): ifconfig eth0 192.168.1.102 netmask 255.255.255.0 route add default gw 192.168.1.1 ...
- animation of android (1)
android把动画的模式分为:property animation,view animation,drawable animation. view animation:给出动画的起止状态,并且通过一 ...