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

这里比較难想的就是:

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. CSS 规避脱标之两种用法

    大家好,我是小强老师,今天讲解一小点知识哈 对比了才知道什么好 看不出,很漂亮吧! 有木有倾国倾城的美色. 呵呵,好多东西也是这样的,好的东西只有对比了才觉得好. 我们知道我们网页布局 有三模式.   ...

  2. VC命令行编译参数介绍

    CL.exe是控制Microsoft C和C++编译器与链接器的32位工具.编译器产生通用对象文件格式(COFF)对象(.obj)文件.链接器产生可执行文件(.exe)或动态链接库文件(DLL). 注 ...

  3. 结构体中使用#define定义宏

    struct  hostent {   char    *h_name;        /* official name of host */   char    **h_aliases;    /* ...

  4. 第五章:输入输出(IO)管理

     I/O设备概念: 指计算机内部除中央处理器和内存之外的全部设备,通常也称为外部设备.  I/O设备分类:   ·按交互对象分类: ·人机交互设备 ·与计算机或其它电子设备交互的设备 ·计算机间的同信 ...

  5. WinForm - 格式化DataGridView单元格数据

    效果: 代码: /// <summary> /// 格式化数据 /// </summary> private void dataGridView1_CellFormatting ...

  6. [转]-bash: wget: command not found的两种解决方法

    wget 时提示 -bash:wget command not found,很明显没有安装wget软件包.一般linux最小化安装时,wget不会默认被安装,这里是CentOS 6.5 64位系统 解 ...

  7. initialize和init区别

    Objective-C很有趣的一个地方是,它非常非常像C.实际上,它就是C语言加上一些其他扩展和一个运行时间(runtime). 有了这个在每个Objective-C程序中都会起作用的附加运行时间,给 ...

  8. 如何判断是否安装了VC RUNTIME

    先得说下GUID,它是Globally Unique Identifier的简称,中文翻译为“全球唯一标示符”,在Windows系统中也称之为Class ID,缩写为CLSID.对于不同的应用程序,文 ...

  9. Android学习笔记:ActionBar使用介绍

    一.基本概念 最权威和官方的介绍请看google的api文档 http://developer.android.com/training/basics/actionbar/setting-up.htm ...

  10. CairoSVG - Convert SVG to PNG or PDF - Contents

    CairoSVG - Convert SVG to PNG or PDF - Contents User Documentation Author Guillaume Ayoub Date 2011- ...