HDU 1556 Color the ball(线段树区间更新)
Color the ball
我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点
【题目链接】Color the ball
【题目类型】线段树区间更新
&题意:
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
&题解:
线段树区间更新模板题
【时间复杂度】\(O(nlogn)\)
&代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
int N;
int seg[maxn<<2];
int add[maxn<<2];
//在build 和 updata时需要向上更新
//query时不需要
inline void puup(int no)
{
seg[no]+=seg[no<<1]+seg[no<<1|1];
}
//这题不需要建树 因为都是0
void build(int no,int b,int e)
{
if (b==e){
seg[no]=0;
return ;
}
int m=b+e>>1;
build(no<<1,b,m);
build(no<<1|1,m+1,e);
puup(no);
}
//query 和 updata时都要向下更新
void pudown(int no,int len)
{
if (add[no]){
//注意都是 +=
add[no<<1]+=add[no];
add[no<<1|1]+=add[no];
//这seg节点里存的是与add相乘
seg[no<<1]+=add[no]*(len-len/2);
//左儿子存多的 右儿子存少的 可以自己弄一个奇数区间试一下
seg[no<<1|1]+=add[no]*(len/2);
add[no]=0;
}
}
int query(int no,int b,int e,int l,int r)
{
//第一种情况 在[l,r]中间
if (l<=b&&e<=r){
return seg[no];
}
int m=b+e>>1;
pudown(no,e-b+1);
ll re=0;
//第二种情况 在疯狂的两个区间中
if (l<=m)
re+=query(no<<1,b,m,l,r);
if (m<r)
re+=query(no<<1|1,m+1,e,l,r);
return re;
}
void updata(int no,int b,int e,int l,int r,int xx)
{
//第一种情况 在[l,r]中间
if (l<=b&&e<=r){
add[no]+=xx;
seg[no]+=e-b+1;
return ;
}
pudown(no,e-b+1);
int m=b+e>>1;
//第二种情况 在疯狂的两个区间中
if (l<=m)
updata(no<<1,b,m,l,r,xx);
if (m<r)
updata(no<<1|1,m+1,e,l,r,xx);
puup(no);
}
int main()
{
while(cin>>N){
if (N==0) break;
//别忘初始化
memset(seg,0,sizeof(seg));
memset(add,0,sizeof(add));
// build(1,1,N);
for(int i=0;i<N;i++){
int u,v;
cin>>u>>v;
updata(1,1,N,u,v,1);
}
for(int i=1;i<=N;i++){
printf("%d%c",query(1,1,N,i,i),i==N?'\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 线段树 区间更新
水一下 #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 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 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- Color the ball 线段树 区间更新但点查询
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...
- hdu1556Color the ball线段树区间更新
题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- 使用WP8最新的AudioVideoCaptureDevice类制作录像应用
WP8出来好一段时间了,新出的AudioVideoCaptureDevice类自定义功能比WP7的CaptureSource强大的多,但网上比较全面的中文实例还比较少,分享一个最近做的小实例给大家参考 ...
- LPTHW 笨办法学python 37章 python关键字/关键词介绍
本章简要的介绍了各种关键词: and:[布尔运算]且 del: 删除变量(函数,类) from: 从某一个库或者文件读取 not:[布尔运算]非 while: while-loop 关键字,后跟循环条 ...
- eclipse导入项目前面有感叹号
1.项目上右击---build path---Config..----Libra----
- JavaScript 获取数组中最大值、最小值
笨方法 Array.prototype.max = function() { var max = this[0]; var len = this.length; for (var i = 1; i & ...
- DrawableLayout
提供一个在窗口顶层显示,可从窗口边缘拖出的container组件. DrawableLayout本身作为整个容器,先进行默认显示内容的布局,再进行拖出菜单的内容布局.也就是一个DL包含两个或三个子控件 ...
- 移动web开发和移动app开发的区分
1.移动web开发 这部分跟web前端开发差别不大,使用的技术都是html+css+js.区别为手机浏览器是webkit的天下,pc端是IE的天 下.手机网页可以理解成pc网页的缩小版加一些触摸特性. ...
- google 版本号49之后chrome的跨域设置
- Equls 和==的区别
对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == 返回 true.对于 string ...
- C++转义字符
R"()"括号中间的字符串可以去掉转义字符
- SqlServer阅读收集
1.根据字段名,查找相关表--INFORMATION_SCHEMA.COLUMNS SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME ...