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(线段树区间更新)的更多相关文章

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

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

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

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

  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 线段树 题解

    本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...

  7. Color the ball 线段树 区间更新但点查询

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  8. hdu1556Color the ball线段树区间更新

    题目链接 线段树区间更新更新一段区间,在更新区间的过程中,区间被分成几段,每一段的左右界限刚好是一个节点的tree[node].left和tree[node].right(如果不是继续分,直到是为止) ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

随机推荐

  1. Android中通过注解代替findViewById方法

    转自:http://www.2cto.com/kf/201405/302998.html 这篇文章主要讲解注解实现findViewById的功能,首先我们来熟悉一下在java中怎么定义一个注解和解析一 ...

  2. 第一章Android系统移植与驱动开发概述--读书笔记

    以前,初步学习过嵌入式Linux驱动开发的基础课程,对于驱动开发可以说是有了一点点微末的基础吧.首先我们要对Android嵌入式系统有一个初步的认识,Android系统发展到今天已经具备了完善的架构. ...

  3. 用UltraEdit软件替换回车换行的窍门

    转载:http://www.zhuantilan.com/jiqiao/46518.html 方法/步骤 1.打开一个原始文档,在文档中各个人物名称是以逗号分隔的,我们下面来尝试把逗号替换为换行符. ...

  4. ReportViewer改变图表类型

    /// <summary>    /// 切换成柱状图    /// </summary>    /// <param name="sender"&g ...

  5. C语言的基础

    任何事物的运行离不开两个部分,一个部分是"事物",一个部分是"运行",前者是状态,在C语言中表现为常量.变量等,后者是过程,在C语言中表现为语句.函数等. 语言 ...

  6. Java语言概要

    Java把源代码(SourceCode)翻译成字节码(ByteCode):javac MyClass.java,再在Java虚拟机(JVM)上执行字节码:java MyClass. Java是基于面向 ...

  7. Spring3.0之后->Spring MVC过滤器-HiddenHttpMethodFilter

    浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不支持,spring3.0添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET.POST.PUT ...

  8. 浏览器功能记住账号和密码解决方法(HTML解决方式)

    1.在input标签里应用html5的新特性autocomplete="off"  注:对chrome不管用.其他浏览器没试. 2.如果是一个输入框那就在当前input标签后面(一 ...

  9. python 后台爆破工具

    sys:使用sys模块获得脚本的参数 queue模块,创建一个“队列”对象 time 模块     Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数). fin ...

  10. web安全之sqlload_file()和into outfile()

    load_file() 条件:要有file_priv权限 知道文件的绝对路径 能使用union 对web目录有读权限 如果过滤啦单引号,则可以将函数中的字符进行hex编码 步骤: 1.读/etc/in ...