HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值。
分析:lazy标记主要体现在update上。
当l <= L && R <= r时,该结点的子结点值不再更新,取而代之的是给该结点一个lazy值,以记录下来该结点的子结点并没有更新。
当赋值的区间落在子结点上时,才将lazy标记传递,同时更新子结点相应的sum值。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int sum[MAXN << 2];
int lazy[MAXN << 2];
void build(int id, int L, int R, int v){
if(L == R){
sum[id] = v;
}
else{
int mid = L + (R - L) / 2;
build(id << 1, L, mid, v);
build(id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
void pushdown(int id, int L, int R){
if(lazy[id]){
lazy[id << 1] = lazy[id << 1 | 1] = lazy[id];
int mid = L + (R - L) / 2;
sum[id << 1] = (mid - L + 1) * lazy[id];
sum[id << 1 | 1] = (R - mid) * lazy[id];
lazy[id] = 0;
}
}
void update(int l, int r, int id, int L, int R, int v){
if(l <= L && R <= r){
sum[id] = (R - L + 1) * v;
lazy[id] = v;
}
else{
pushdown(id, L, R);
int mid = L + (R - L) / 2;
if(l <= mid) update(l, r, id << 1, L, mid, v);
if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
int main(){
int T;
scanf("%d", &T);
int kase = 0;
while(T--){
memset(sum, 0, sizeof sum);
memset(lazy, 0, sizeof lazy);
int N;
scanf("%d", &N);
build(1, 1, N, 1);
int Q;
scanf("%d", &Q);
while(Q--){
int X, Y, Z;
scanf("%d%d%d", &X, &Y, &Z);
update(X, Y, 1, 1, N, Z);
}
printf("Case %d: The total value of the hook is %d.\n", ++kase, sum[1]);
}
return 0;
}
HDU - 1698 Just a Hook (线段树---区间修改)的更多相关文章
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 just a hook 线段树,区间定值,求和
Just a Hook Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...
- HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
随机推荐
- android开发如何在页面之间传参
第一个页面跳转 传递值 Button bn1=(Button)findViewById(R.id.btn_Login); //跳转bn1.setOnClickListener(new OnClickL ...
- LaTeX 使用笔记
实现一个归类样式,如图: 代码: \left\{ \begin{aligned} 监督学习 \left\{ \begin{aligned} 回归 \\ 分类 \end{aligned} \right. ...
- 图解jvm--(三)类加载与字节码技术
类加载与字节码技术 1.类文件结构 根据 JVM 规范,类文件结构如下 ClassFile { u4 magic; //魔数 u2 minor_version; //小版本号 u2 major_ver ...
- 「BOI2007」Mokia
「BOI2007」Mokia 传送门 把查询拆成四部分然后容斥计算答案(二维前缀和) 然后 \(\text{CDQ}\) 分治算答案. 参考代码: #include <algorithm> ...
- C语言for循环嵌套示例
打印九九乘法表 #include <stdio.h> int main() { int n,i,j; for (i=1;i<=9;i++) printf("%-4d&q ...
- Scrapy 下载文件和图片
我们学习了从网页中爬取信息的方法,这只是爬虫最典型的一种应用,除此之外,下载文件也是实际应用中很常见的一种需求,例如使用爬虫爬取网站中的图片.视频.WORD文档.PDF文件.压缩包等. 1.Files ...
- 【转】彻底搞透Netty框架
本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...
- thinkphp5 + vue nginx配置
thinkphp5 + vue 配置 server { listen ; listen [::]:; # For https listen ssl; listen [::]: ssl; ssl_cer ...
- 4.8.2.JSDOM对象控制HTML元素详解
1 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title ...
- 腾讯2019秋招--小q爬塔(dp)
小Q爬塔 题目描述: 小Q 正在攀登一座宝塔,这些宝塔很特别.塔总共有 n 层,但是每两层之间的净高却不相同,所以造成了小Q 爬过每层的时间也不同.如果某一层的高度为 x,那么爬过这一层所需的时间也是 ...