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 ...
随机推荐
- 以正确的方式开源 Python 项目
以正确的方式开源 Python 项目 大多数Python开发者至少都写过一个像工具.脚本.库或框架等对其他人也有用的工具.我写这篇文章的目的是让现有Python代码的开源过程尽可能清 晰和无痛.我不是 ...
- 搭建OA平台
公司没有OA平台的原因是: 1.暂时没有上的必要. 2.总厂有一个web版的用友的OA. 但最近法方总经理提到了共享日历日程的需求. 这个需求的主要要整合的是把outlook的日历功能共享.原来微软是 ...
- C++算术运算符与算术表达式
基本的算术运算符 在本章中主要介绍算术运算符与算术表达式,赋值运算符与赋值表达式,逗号运算符与逗号表达式,其他运算符将在以后各章中陆续介绍. 常见算数运算符 运算符 说明 举例 + 加法运算符,或正值 ...
- 【转】[Mysql] Linux Mysql 日志专题
原文链接:http://blog.csdn.net/xiaoxu0123/article/details/6258538 1, 设置存放的目录: [root@Linux etc]# more /etc ...
- Linux -FHS 标准
FHS (Filesystem Hierarchy Standard),其目的是让用户可以了解以安装软件通常放在那个文件下面. /bin 放置的是单用户维护模式下,能够被调用的命令.主要有cat,ch ...
- QTableWidget 导出到csv表格
跳槽到了新的公司,开始苦逼的出差现场开发,接触到了新的应用.有很多应用需要将Table导出成表格,可以把table导出成csv格式的文件.跟大伙分享一下: lass TableToExcle : pu ...
- WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇]
原文:WCF技术剖析之二十三:服务实例(Service Instance)生命周期如何控制[下篇] 在[第2篇]中,我们深入剖析了单调(PerCall)模式下WCF对服务实例生命周期的控制,现在我们来 ...
- OFBIZ分享:利用Nginx +Memcached架设高性能的服务
近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验.如:网易.淘宝.京东.凡客等 ...
- C++和JNI的数据转换
链接地址:http://blog.csdn.net/manymore13/article/details/19078713 转载地址:http://www.cnblogs.com/daniel-she ...
- NSThread的一些细节
1.NSThread创建方式(一个NSThread对象就代表一条线程)1.1>创建\启动线程(1)线程一启动,就会在thread中执行self的run方法NSTread *thread = [[ ...