本题使用线段树自然能够,由于区间的问题。

这里比較难想的就是:

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 线段树 题解的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  3. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  4. hdu 1556 Color the ball (线段树+代码详解)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. hdu 1556 Color the ball 线段树

    题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...

  6. hdu 1556 Color the ball 线段树 区间更新

    水一下 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 ...

  7. hdu 1556 Color the ball (树状数组)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. hdu 1556 Color the ball(树状数组)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数[a,b]之间的气球 ...

  9. HDU 1556 Color the ball (树状数组 区间更新+单点查询)

    题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...

随机推荐

  1. json_response的用法

    传统的方法是当我们处理一个表单时,我们Post数据给服务器,服务器对数据进行处理后将数据返回给用户,此时部分写法是用页面刷新的方式将页面重新刷新一次呈现给用户,这样的话用户相当于读入了两次页面,人一多 ...

  2. ASP.NET MVC 5 学习教程:使用 SQL Server LocalDB

    原文 ASP.NET MVC 5 学习教程:使用 SQL Server LocalDB 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 ...

  3. Python函数式编程:Lambda表达式

    首先我们要明白在编程语言中,表达式和语句的区别. 表达式是一个由变量.常量.有返回值的函数加运算符组成的一个式子,该式子是有返回值的 ,如  a + 1 就是个表达式, 单独的一个常量.变量 或函数调 ...

  4. 基于visual Studio2013解决C语言竞赛题之0603打印素数

     题目

  5. Pencil OJ 02 安装

    Mongo 官方的安装方法 官方教程已经很好啦,这里就不罗嗦了. 源码编译 待补.我是从这里看到的. 遇到的问题 启动时的警告信息 2015-03-06T21:01:15.526-0800 I CON ...

  6. CreateFile函数使用方法详细介绍

    CreateFileThe CreateFile function creates or opens the following objects and returns a handle that c ...

  7. 应用程序无法正常启动0xc000007b

    参考: http://jingyan.baidu.com/article/ff42efa9181bbbc19e22022f.html DirectX修复工具: http://blog.csdn.net ...

  8. python idle 错误 subprocess didn&#39;t make connection

    今天打开python idle不反应.然后通过网上搜索让我在安装文件夹下点击idle.py 弹出如图所看到的的错误,进行了非常多尝试.任然没有得到解决.可是在尝试过程中发现了大家所说问题所在都是由于新 ...

  9. Oracle 11g RAC OCR 与 db_unique_name 配置关系 说明

    一. 问题一 在做RAC standby 的alert log里发现如下错误: SUCCESS: diskgroup DATA was mounted ERROR: failed toestablis ...

  10. Repeater控件

    一:http://www.cnblogs.com/foolin/archive/2011/08/31/2161342.html 二:http://www.cnblogs.com/shipfi/arch ...