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

这里比較难想的就是:

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;
}

转载于:https://www.cnblogs.com/mengfanrong/p/5184700.html

HDU 1556 Color the Ball 线段树 题解相关推荐

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

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

  2. HDU - 1556 Color the ball(树状数组)

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

  3. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

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

  4. HDOJ/HDU 1556 Color the ball(树状数组)

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

  5. hdu 1556 Color the ball

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

  6. HDU 1556 Color the ball (数状数组)

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

  7. 解题报告:hdu 1556 Color the ball(区间修改,单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...

  8. HDU 1556 Color the ball

    题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...

  9. HDU 1556 Color the ball - from lanshui_Yang

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

最新文章

  1. vmware 利用镜像 配置yum本地源
  2. #pragma execution_character_set的意义
  3. Android Stdio 添加jar或aar
  4. ROS系统 实现客户端Client和服务端Server
  5. Visual Studio 2017软件安装教程
  6. JDBC——基于Mysql的基本操作
  7. 清华大学《操作系统》(六):非连续内存分配 段式、页式、段页式存储管理
  8. 【转载保存】webCollector使用教程
  9. 如何用rake tasks 生成migration对应的sql
  10. mysql慢查询面试题_头条Java岗3面入职:事务+慢查询SQL+Redis+秒杀设计面试题等
  11. sql 从ip列表中查询ip段_IP地址段查询深度优化案例
  12. matlab数字信号处理与应用 张德丰,MATLAB数字信号处理与应用
  13. C_北理工乐学_结构
  14. boblog任意变量覆盖漏洞(二)
  15. Oracle函数——COALESCE
  16. 量子纠缠在量子计算机中的作用,解密量子计算机,量子叠加和量子纠缠是制胜关键...
  17. 我的个人博客网站是怎么制作的?
  18. 2021-09-10 网安实验-文件修复-BMP图片隐写
  19. vue怎么设置封面_微信朋友圈封面视频如何制作?微信朋友圈封面视频制作软件VUE功能介绍...
  20. ActiveMQ--概述

热门文章

  1. 用Unity实现传送门效果(一)
  2. 游戏光线追踪往事:十年技术轮回
  3. mongoDB add user in v3.0 问题的解决(Property 'addUser' of object admin is not a func)
  4. mongodb 索引建立问题
  5. TermServDevices错误的解决方案
  6. Oracle 中文分词CHINESE_VGRAM_LEXER与CHINESE_LEXER比较
  7. LogMiner学习笔记
  8. RHEL4-VNC服务配置
  9. 发现一个ps抠毛发简单快捷高质量的方法
  10. 001 GIt的基本操作