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 ...
随机推荐
- Week3(9月26日):做完后,总结下
Part I:提问 =========================== 1.linq小回顾 (1)Movies控制器中Index动作,显示全部电影信息. public ActionResult ...
- MyEclipse8.5 中安装Spket插件
MyEclipse8.5 中安装Spket插件 安装: 1. Myeclipse8.5安装和以前低版本有些不同(没有software updates). 点击help->Myeclipse Co ...
- jquery 单击li防止重复加载的实现代码
因为加载内容比较慢,所以用户可能在li上不经意点击了两次,那么就会请求两次,这是我们不想看到的. 今天在javascript-jquery群上一筒子发了两个demo给我,他的方法是先将单击的li节点拷 ...
- 引用 U-boot给kernel传参数和kernel读取参数—struct tag
引用 清风徐徐 的 U-boot给kernel传参数和kernel读取参数—struct tag U-boot会给Linux Kernel传递很多参数,如:串口,RAM,videofb等.而Linux ...
- QScriptEngine
其实你有好多没有介绍 比如qt文字 我一直很迷惑qt的文字的长宽 qt文字的字间距 等等这些东西还有QProcess QProcess可能是qt调用c#的唯一方法了QScript要比你想象的重要,一个 ...
- JQuery遍历json数组的3种方法
这篇文章主要介绍了JQuery遍历json数组的3种方法,本文分别给出了使用each.for遍历json的方法,其中for又分成两种形式,需要的朋友可以参考下 一.使用each遍历 $(functio ...
- ios qq 分享 失败
1. TencentOAuth 是需要调用,但QQ代码共享是没有解释.共享代码如下面: TencentOAuth *auth = [[TencentOAuth alloc] initWithAppId ...
- 创建服务类PO
转载:https://blogs.sap.com/2014/03/04/creating-a-simple-service-po-using-bapipocreate1bapipochange/ Cr ...
- Delphi体系内部的4种消息传递办法(Send,Post,Perform,Dispatch)
一.什么是消息? 消息是windows对应用程序发送的有关‘发生了某种事件’的通知.例如点击鼠标,调整窗口大小或键盘上按下一个键,都会引起windows发送一条消息到应用程序中去,去通知应用程序发生了 ...
- perl 为什么要用引用来做对象呢?
perl 为什么要用引用来做对象呢? 因为一个重要的原因是 my 引用 脱离作用域,外部仍旧生效