题意:区间修改序列值,最后输出。

 //hdu1166
#include<iostream>
#include<cstdio>
#include<cstring>
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std;
const int maxn=;
int sum[maxn<<],add[maxn<<];//sum求和,add懒惰标记
int a[maxn]={},n=;//存原数组数据下标[1,n]
//更新节点信息,这里是求和
void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
//建树
void pushdown(int rt,int ln,int rn)
{//ln,rn为左子树,右子树的数量
if(add[rt])//下推标记
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*ln;
sum[rt<<|]+=add[rt]*rn;
add[rt]=;//清除本节点标记
}
}
void build(int l,int r,int rt)//rt表示当前节点编号
{
if(l==r)
{
sum[rt]=a[l];
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
} //点修改a[L]+=c;
void update(int L,int c,int l,int r,int rt)
{//l,r当前节点区间,rt当前节点编号,L需要修改的节点,c修改的值
if(l==r)
{
sum[rt]+=c;
return;
}
int m=(l+r)>>;
//根据条件判断调用左子树还是右子树
if(L<=m)
update(L,c,l,m,rt<<);
else
update(L,c,m+,r,rt<<|);
pushup(rt);//子节点更新,所以本节点也需要更新
} //区间修改a[r,t]+=c
void update_(int L,int R,int c,int l,int r,int rt)
{//L,R表示操作区间,l,r表示当前节点区间,rt表示当前节点编号
if(L<=l&&r<=R)//遍历的区间在操作区间内
{
sum[rt]+=c*(r-l+);
add[rt]+=c;
return;
}
int m=(l+r)>>;
pushdown(rt,m-l+,r-m);
if(L<=m)
update_(L,R,c,l,m,rt<<);
if(R>m)
update_(L,R,c,m+,r,rt<<|);
pushup(rt);
} //区间查询 a[l,r]的和
//下推标记函数
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>;
pushdown(rt,m-l+,r-m);
int ans=;
if(L<=m)
ans+=query(L,R,l,m,rt<<);
if(R>m)
ans+=query(L,R,m+,r,rt<<|);
return ans;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
memset(sum,,sizeof(sum));
memset(add,,sizeof(add));
build(,n,);
for(int i=;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
update_(a,b,,,n,);//区间修改 }
for(int i=;i<=n;i++)
{
if(i!=)
printf(" ");
printf("%d",query(i,i,,n,));//因为查询单点所以是i到i
}
printf("\n");
}
return ;
}

HDU1556 Color the ball [线段树模板]的更多相关文章

  1. hdu1556 Color the ball 线段树区间染色问题

    都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...

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

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

  3. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  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(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  6. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  7. HDU 1556 Color the Ball 线段树 题解

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

  8. Color the ball (线段树的区间更新问题)

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

  9. hdu 1556 Color the ball 线段树

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

随机推荐

  1. softmax和分类模型

    softmax和分类模型 内容包含: softmax回归的基本概念 如何获取Fashion-MNIST数据集和读取数据 softmax回归模型的从零开始实现,实现一个对Fashion-MNIST训练集 ...

  2. dbus探索

    一.参考网址 1.Dbus组成和原理

  3. WAMP常用环境配置

    自定义网站目录 修改目录位置 如下图,打开httpd.conf文件. 查找DocumentRoot(两处),做如下修改: #demo为自定义网站目录,下面不再说明 DocumentRoot " ...

  4. oi笔记——抽象的深度优先搜索

    oi笔记--抽象的深度优先搜索 例题: \(N个数中选K个数,选出的和要为sum\) 例题分析: 对于每个点,我们可以按"选"和"不选"进行搜索,如图: 或者0 ...

  5. linux 查看链接库的版本

    我们编译可执行文件的时候,会链接各种依赖库, 但是怎么知道依赖库的版本正确呢? 下面有几种办法: ldd 这是比较差的,因为打印结果更与位置相关 dpkg -l | grep libprotobuf ...

  6. Popular generalized linear models|GLMM| Zero-truncated Models|Zero-Inflated Models|matched case–control studies|多重logistics回归|ordered logistics regression

    ============================================================== Popular generalized linear models 将不同 ...

  7. memcached redis 本质区别是功能多少

    功能: 1.memcached 数据类型比较单一,数据淘汰策略单一,功能简单 2.redis 数据类型比较全面, 数据淘汰策略比较多,功能较强 有持久化能力,可以持久存储少量数据(数据量不会大于本机内 ...

  8. 关于Ueditor富文本编辑器的配置和使用心得

    一.环境和项目架构 本文章只是为了便于我个人记录日常笔记,如有错误或缺陷,请指出,文章仅供参考,如有转载请附上本文章链接. 介绍:将Ueditor富文本提交的内容直接生成Html文件,传到后台直接保存 ...

  9. TabControl+ListView

    #include <windows.h> #include <commctrl.h> #include <tlhelp32.h> #include "re ...

  10. 剑指offer【10】- 变态跳台阶

    题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 关于本题,前提是n个台阶会有一次n阶的跳法.分析如下: f(1) = 1 f(2) ...