HDU1556 Color the ball [线段树模板]
题意:区间修改序列值,最后输出。
//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 [线段树模板]的更多相关文章
- hdu1556 Color the ball 线段树区间染色问题
都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- 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 Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- HDU 1556 Color the Ball 线段树 题解
本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点 ...
- Color the ball (线段树的区间更新问题)
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
随机推荐
- Python 安装gevent,在导入gevent之后就报错了
错误信息如下 RuntimeWarning: greenlet.greenlet size changed, may indicate binary incompatibility. Expected ...
- es和数据库关系对比
es类比传统关系型数据库: Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -& ...
- JAVA 算法练习(一)
用java写了几道编程题目,分享给大家 语法和C语言几乎一样,不懂 java 会 c 也可以看明白的. 最大连续数列和 题目说明 对于一个有正有负的整数数组,请找出总和最大的连续数列.给定一个int数 ...
- Vue.js——3.增删改查
vue 写假后台 bootstrap 做的样式 代码 <!DOCTYPE html> <html lang="en"> <head> < ...
- python中的API学习
URL: url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该 ...
- 吴裕雄--天生自然ShellX学习笔记:Shell test 命令
Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 实例演示: num1=100 num2=100 if test $[num1] -eq $[num2 ...
- UML-逻辑架构和包图-概述
回顾前几章学习了用例模型,本章开始学习设计模型.
- Win 10 Ctrl + Space 冲突
1. 说明 在IDE里面Ctrl + space 会与 Windows 输入法相互冲突,并且用Ctrl + Space 切换中英文也很不常用(常用直接shift切换). 2. 操作 控制面板——时钟. ...
- mysql 获取数据库和表结构信息
SELECT * FROM information_schema.`TABLES` where TABLE_SCHEMA = '数据库名';SELECT * FROM information_sche ...
- Linux-进程关系
(1).无关系 (2).父子进程关系 (3).进程组(group):由若干个进程构成一个进程组 (4).会话(session):由若干个进程组构成一个会话