HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题。
这里比較难想的就是:
1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点。
2 截取区间不能有半点差错。否则答案错误。
这两点卡了我下。看来我的线段树还是不够熟,须要多多练习。
线段树是二分法的高级应用,可是却不是简单应用,要锻炼好并应用好线段树思维还是比二分法难非常多的。
static const int SIZE = 100005;
static const int TREESIZE = SIZE<<2;
int arr[SIZE];
int segTree[TREESIZE]; inline int lChild(int r) { return r<<1; }
inline int rChild(int r) { return r<<1|1; } void pushDown(int rt)
{
segTree[lChild(rt)] += segTree[rt];
segTree[rChild(rt)] += segTree[rt];
} void build(int l, int r, int rt)
{
int mid = l + ((r-l)>>1); segTree[rt] = 0;
if(l == r) return; build(l, mid, lChild(rt));
build(mid+1, r, rChild(rt));
} void update(const int L, const int R, int l, int r, int rt)
{
if (L <= l && r <= R)
{
segTree[rt]++;
return ;
} int m = l + ((r-l)>>1); //注意怎样准确截取区间
if (R <= m) update(L, R, l, m, lChild(rt));
else if (L > m) update(L, R, m+1, r, rChild(rt));
else
{
update(L, m, l, m, lChild(rt));
update(m+1, R, m+1, r, rChild(rt));//须要截取准确区间
}
} void query(int l, int r, int rt)
{
if (l == r)
{
arr[l] = segTree[rt];
return;
}
pushDown(rt); int m = l + ((r-l)>>1);
query(l, m, lChild(rt));
query(m+1, r, rChild(rt));
} int main()
{
int n, a, b;
while (scanf("%d", &n) && n != 0)
{
fill(arr, arr+n+1, 0);
build(1, n, 1); for(int i = 0; i < n; i++)
{
scanf("%d %d",&a,&b);
update(a, b, 1, n, 1);
}
query(1, n, 1); for(int i = 1; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("%d\n",arr[n]);
}
return 0;
}
HDU 1556 Color the Ball 线段树 题解的更多相关文章
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- 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 (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
- hdu 1556 Color the ball 线段树 区间更新
水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...
- hdu 1556 Color the ball (树状数组)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 1556 Color the ball(树状数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
随机推荐
- hdu-4302-Holedox Eating-线段树-单点更新,有策略的单点查询
一開始实在是不知道怎么做,后来经过指导,猛然发现,仅仅须要记录某个区间内是否有值就可以. flag[i]:代表i区间内,共同拥有的蛋糕数量. 放置蛋糕的时候非常好操作,单点更新. ip:老鼠当前的位置 ...
- 在Eclipse/MyEclipse中安装spket插件
Spket ide是强大的工具包为了JavaScript和XML的开发,这个强大的编辑器对JavaScript, XUL/XBLand Yahoo! Widget的开发都有全面的支持 ,比如代码完毕, ...
- A2DP和AVRCP蓝牙音频传输协议的应用解释
A2DP全名是Advenced Audio Distribution Profile 蓝牙音频传输模型拹定.A2DP 规定了使用蓝牙非同步传输信道方式,传输高质量音乐文件数据的拹议堆栈软件和使用方法, ...
- 【BZOJ1132】【POI2008】Tro 计算几何 叉积求面积
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- django学习之Model(二)
继续(一)的内容: 1-跨文件的Models 在文件头部import进来,然后用ForeignKey关联上: from django.db import models from geography.m ...
- Cloud Engine
Cloud Engine:大杀器如何炼成 郑昀 创建于2016/6/18 最后更新于2016/6/19 点击查看我的<如何从零搭建一个技术平台>,这是一个系列.转载时请注明“转载自旁观 ...
- ubuntu 12.04 安装sublime2
add-apt-repository ppa:webupd8team/sublime-text-2 apt-get update apt-get install sublime-text 安装控制器: ...
- android APP 中微信分享功能实现 的总结
//花了很长时间最终完成了微信分享功能,中间走了很多弯路,在此做一下小结,希望对在应用中使用到微信分享的朋友有所帮助. 主要问题就是下面两个: 1.为什么运行了项目之后,微信分享只是闪了一下就没有了? ...
- 深入理解mysql之BDB系列(1)---BDB相关基础知识
深入理解mysql之BDB系列(1) ---BDB相关基础知识 作者:杨万富 一:BDB体系结构 1.1.BDB体系结构 BDB总体的体系结构如图1.1所看到的,包括五个子系统(见图1.1 ...
- webservice asmx 无法序列化接口 System.Collections.Generic.IList
转载自:http://www.cnblogs.com/chenhuzi/p/4178194.html 今天有位同事在方法里加了一个IList<entity> 的返回值,也没有测试,直接发布 ...