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;
}
转载于:https://www.cnblogs.com/mengfanrong/p/5184700.html
HDU 1556 Color the Ball 线段树 题解相关推荐
- 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(树状数组)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- 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 (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 解题报告:hdu 1556 Color the ball(区间修改,单点查询)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N ...
- HDU 1556 Color the ball
题解:基础的树状数组区间修改,单点查询. #include <cstdio> #include <cstring> int c[100005],a,b,n; int modif ...
- HDU 1556 Color the ball - from lanshui_Yang
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动 ...
最新文章
- vmware 利用镜像 配置yum本地源
- #pragma execution_character_set的意义
- Android Stdio 添加jar或aar
- ROS系统 实现客户端Client和服务端Server
- Visual Studio 2017软件安装教程
- JDBC——基于Mysql的基本操作
- 清华大学《操作系统》(六):非连续内存分配 段式、页式、段页式存储管理
- 【转载保存】webCollector使用教程
- 如何用rake tasks 生成migration对应的sql
- mysql慢查询面试题_头条Java岗3面入职:事务+慢查询SQL+Redis+秒杀设计面试题等
- sql 从ip列表中查询ip段_IP地址段查询深度优化案例
- matlab数字信号处理与应用 张德丰,MATLAB数字信号处理与应用
- C_北理工乐学_结构
- boblog任意变量覆盖漏洞(二)
- Oracle函数——COALESCE
- 量子纠缠在量子计算机中的作用,解密量子计算机,量子叠加和量子纠缠是制胜关键...
- 我的个人博客网站是怎么制作的?
- 2021-09-10 网安实验-文件修复-BMP图片隐写
- vue怎么设置封面_微信朋友圈封面视频如何制作?微信朋友圈封面视频制作软件VUE功能介绍...
- ActiveMQ--概述