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 ...
随机推荐
- 一个简单的app自动登录Python脚本案例
- Autorun.inf文件(2):改变硬盘分区图标
改变F盘图标. 原理:在f盘下新建一个Autorun.inf文件,文件内容是 [AutoRun]icon=favicon.ico准备名为favicon.ico图标文件,将其放在工程目录里,设计程序将它 ...
- 如何查看PYTHON Django的保存路径
如何查看PYTHON Django的保存路径 $ python -c " import sys sys.path = sys.path[1:] import django print(dja ...
- Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 随机生成UUID(GUID)的方法
- (NSString *)UUID { CFUUIDRef uuid_ref = CFUUIDCreate(NULL); CFStringRef uuid_string_ref= CFUUIDCre ...
- 手把手教你配置UltraEdit对Oracle的PLSQL着色
http://hi.baidu.com/kingbridge/blog/item/94e225ad5fad4b194b36d60d.html UltraEdit-32 12.1版本配置默认文件显示 ...
- linux 64位调用
linux系统中64位汇编和32位汇编的系统调用主要有以下不同:(1)系统调用号不同.比如x86中sys_write是4,sys_exit是1:而x86_64中sys_write是1, sys_exi ...
- [C++] Running time and Integer to String
std::string num2str(int64_t p_vint, int8_t p_radix) { char str[48] = { 0 }; int64_t temp = 0; int64_ ...
- git subtree 使用
这个是备忘录.原网页(https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec , http://cncc.bingj.co ...
- JavaScript 表单验证
长度限制: <script>function test(){if(document.a.b.value.length>50){alert("不能超过50个字符!" ...