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 ...
随机推荐
- Excel替换应用
* 是通配符 ,代表任意字符 ?也是通配符, 代表单个字符替换回车 : 按住alt键不放,然后依次通过数字键盘输入1和0两个数字,放开数字键后再放开alt键.
- 解决Android应用安装快完毕时提示签名冲突
最近开发了一个Android手机应用,自己用Eclipse调试安装没问题,使用其他人调试生成的bin下的apk就会出现问题,安装到最后提示"安装签名冲突"错误,想了一下估计是没有给 ...
- vconfig使用帮助
====================================================== VCONFIG(8) ...
- MySQL使用正则表达式比较字段中的数字
今天遇到一个问题,需要对表中的一个类json字段的内容进行筛选,而筛选的条件是值要大于某个值.因为值的位数并不确定,考虑使用正则表达式进行筛选. 字段格式 类json的key-value字段,示例如下 ...
- H5移动端中必备技能
Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面<meta name="viewport" content="width=device-wid ...
- 第五章——搭建S3C6410开发板的测试环境
搭建S3C6410开发板的测试环境 通过本章的学习学会了如何在开发板上安装Android.开发板是学习和开发嵌入式技术的主要硬件设备,想要顺利的通过linux驱动访问硬件,是不能在PC板上模拟的,需要 ...
- sql关于Group by
SELECT JBGS.XMID, SUM(JBGS.JBGS * JBYXXS.YXXS) / (SELECT SUM(B.GS) FROM T_XMCBHZ B WHERE B.XMID= ...
- Codeforces Round #174 (Div. 2)
A. Cows and Primitive Roots 暴力. B. Cows and Poker Game 模拟. C. Cows and Sequence 线段树维护. D. Cow Progra ...
- redis学习
wget http://labfile.oss.aliyuncs.com/files0422/redis-2.8.9.tar.gz .tar.gz cd redis- make make instal ...
- Kindle Unlimited上的技术书籍
直达链接:Kindle Unlimited 前不久,亚马逊在中国也推出了电子书包月服务.消息不灵通的我过了好久才看到这个消息,随后第一时间上官网查看具体情况. ...