HDU 1556【线段树区间更新】
这篇lazy讲的很棒:
https://www.douban.com/note/273509745/
if(tree[rt].l == l && r == tree[rt].r)
这里就是用到Lazy思想的关键时刻 正如上面说提到的,这里首先更新该节点的sum[rt]值,
然后更新该节点具体每个数值应该加多少即add[rt]的值,
注意此时整个函数就运行完了,直接return,而不是还继续向子节点继续更新,
这里就是Lazy思想,暂时不更新子节点的值。
那么什么时候需要更新子节点的值呢?
答案是在某部分update操作的时候需要用到那部分没有更新的节点的值的时候,
这时就掉用PushDown()函数更新子节点的数值。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct asd{
int left,right;
int ad;
int w;
};
asd q[N*4];
void build(int num,int L,int R)
{
q[num].left=L;
q[num].right=R;
if(L==R)
{
q[num].w=q[num].ad=0;
return;
}
build(2*num,L,(L+R)/2);
build(2*num+1,(L+R)/2+1,R);
q[num].w=q[num].ad=0;
}
int ss(int num)
{
return q[num].right-q[num].left+1;
}
void Pushdown(int num)
{
if(q[num].ad)
{
q[num*2].w+=q[num].ad*(ss(2*num));
q[num*2+1].w+=q[num].ad*(ss(2*num+1));
q[num*2].ad+=q[num].ad;
q[num*2+1].ad+=q[num].ad;
q[num].ad=0;
}
}
void Pushup(int num)
{
q[num].w=q[2*num].w+q[2*num+1].w;
}
void update(int num,int s,int t)
{
if(s==q[num].left&&q[num].right==t)
{
q[num].w+=t-s+1;
q[num].ad+=1;
return;
}
if(q[num].left==q[num].right)
return;
Pushdown(num);
int mid=(q[num].right+q[num].left)/2;
if(mid>=t)
update(2*num,s,t);
else if(mid<s)
update(2*num+1,s,t);
else
{
update(2*num,s,mid);
update(2*num+1,mid+1,t);
}
Pushup(num);
}
int query(int num,int s,int t)
{
if(s==q[num].left&&q[num].right==t)
return q[num].w;
Pushdown(num);
int ans=0;
int mid=(q[num].right+q[num].left)/2;
if(mid>=t)
ans+=query(2*num,s,t);
else if(mid<s)
ans+=query(2*num+1,s,t);
else
ans+=query(2*num,s,mid)+query(2*num+1,mid+1,t);
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int x,y;
build(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
update(1,x,y);
}
for(int i=1;i<=n;i++)
{
if(i>1) printf(" ");
printf("%d",query(1,i,i));
}
puts("");
}
return 0;
}
/*
6
1 6
1 6
1 6
1 3
2 5
3 6
*/
HDU 1556【线段树区间更新】的更多相关文章
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 区间更新重点在于懒惰标记. 当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r- ...
- HDU 1698 (线段树 区间更新) Just a Hook
有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...
- hdu 1556 线段树区间延迟更新好题
656mS #include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int x,y,yanchi ...
- HDU 3016 线段树区间更新+spfa
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
随机推荐
- 解决Linq.ToDictionary()时的键重复问题
今天在使用 Linq 的 ToDictionary() 时发生了异常,提示: System.ArgumentException: 已添加了具有相同键 因为以前一直没有遇到有相同键的情况,所以从来没关注 ...
- Recurrent neural networks are very powerful, because they combine two properties
https://www.cs.toronto.edu/~hinton/csc2535/notes/lec10new.pdf Distributed hidden state that allows t ...
- Hadoop实战-Flume之Hello world(九)
环境介绍: 主服务器ip:192.168.80.128 1.准备apache-flume-1.7.0-bin.tar文件 2.上传到master(192.168.80.128)服务器上 3.解压apa ...
- mongodb学习之:数据库
首先来介绍下Mongodb的基本概念: 左边一列是关系数据库的术语,右边这一列是NOSQL也就是mongodb的术语 database: database 数据库 tabl ...
- Algorithm: Euclid's algorithm of finding GCD
寻找最大公约数方法 代码如下: int gcd (int a, int b) { return b ? gcd (b, a % b) : a; } 应用:求最小公倍数 代码如下: int lcm (i ...
- Hexo建站过程总结
Hexo 是一个基于 Node.js 快速.简洁且高效的博客框架,可以将 Markdown 文件快速的生成静态网页,托管在 GitHub Pages 上. 由于原来博客的主机费用问题,我没有办法再在那 ...
- 配置composer代理
composer config -g repo.packagist composer https://packagist.phpcomposer.com
- Block和inline元素对比
所有的HTML元素都属于block和inline之一.block元素的特点是:总是在新行上开始:高度,行高以及顶和底边距都可控制:宽度缺省是它的容器的100%,除非设定一个宽度<div>, ...
- iOS7默认状态栏文字颜色为黑色,项目需要修改为白色。
1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的 AppDelegate中在 didFinishLaun ...
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...