HDU 1698 Just a Hook 线段树+lazy-target 区间刷新
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 30553 Accepted Submission(s): 15071
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.
Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents
the golden kind.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
题意:
就是说刚开始所有的棍子都是铜色,然后去成段的刷,铜色的赋值为1,银色的赋值为2,金色的赋值为3。当然,后来刷上的颜色会覆盖掉原来的颜色,最后输出整个区间的和。
思路:
线段树+lazy-target标记(pushdown函数,将自身的lazy标记传递给儿子结点,并计算儿子几点成段跟新的值)
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=100005;
int color[maxn<<2],sum[maxn<<2];
void pushup(int rt) {
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt, int m) {
if(color[rt]) {
color[rt<<1]=color[rt<<1|1]=color[rt];
sum[rt<<1]=(m-(m>>1))*color[rt];//左儿子区间跟新数值
sum[rt<<1|1]=(m>>1)*color[rt];//右儿子区间跟新数值
color[rt]=0;
}
}
void build(int l, int r, int rt) {
sum[rt]=1;color[rt]=0;
if(l==r) return;
int mid=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int L, int R, int c, int l, int r, int rt) {
if(L<=l&&r<=R) {
color[rt]=c;
sum[rt]=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);//传递标记
int mid=(l+r)>>1;
if(L<=mid) update(L,R,c,lson);
if(R>mid) update(L,R,c,rson);
pushup(rt);
}
int main() {
int t,cnt=1;
scanf("%d",&t);
while(t--) {
int n,up;
scanf("%d",&n);
build(1,n,1);
scanf("%d",&up);
int L,R,c;
for(int i=1;i<=up;i++) {
scanf("%d%d%d",&L,&R,&c);
update(L,R,c,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",cnt++,sum[1]);
}
return 0;
}
HDU 1698 Just a Hook 线段树+lazy-target 区间刷新的更多相关文章
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- 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 [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 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(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1698 Just a Hook(线段树成段更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- (简单) 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 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
随机推荐
- Linux进程管理与作业控制
进程和作业的关系:一个作业可以包含多个进程. 进程分类: 1. 交互进程:由一个shell启动的进程.交互进程既可以在前台运行,也可以在后台运行. 2. 批处理进程:这种进程和终端没有联系,是一个进程 ...
- MySql数据库的基本原理及指令
1.什么是数据库 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以通过SQL对数据库中的数据进行增加,修改,删除及查询操作. 2.简介 MySQL是一个开放源 ...
- Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类
[前言] 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件,Mybatis-Generator的作用就是充当了一个代码生成器的角色,使用代码生成器不仅可以简化我 ...
- C#中判断语句 if、if-else if、switch-case
1.if一般用于一个条件的判断: 2.if-else if 一般用于多个条件的判断: 3.switch-case一般用于多个条件的判断. 注:if-else if与switch-case的区别在于:一 ...
- app.get is not a function解决方案
在express4.x中app.js被申明为一个模块,而不是一个主程序入口,在文件的最后暴露出了这个模块,如下所示 app.js module.exports = app; 但是我们在routes目录 ...
- Python学习之一:Python2.7与opencv2.4安装配置
安装前准备: 1.确定所安装的电脑是32位还是64位系统:(作者电脑是64bit win10) 2.下载对应的安装包: (1)下载最新Python安装包:https://www.python.org/ ...
- Oracle 表空间扩充
Oracle 表空间扩充 一.现场环境: (1)操作系统:AIX (2)数据库:Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - ...
- gops - Go语言程序查看和诊断工具
想必 Java 的开发者没有不知道或者没用过 jps 这个命令的,这个命令是用来在主机上查看有哪些 Java 程序在运行的. 我刚用 Go 语言程序的时候也很苦恼,我部署在公司服务器上的 Go 程序, ...
- 简单类型对象 String
简单值不是对象,因此也没有属性方法,因此运行下面代码时 var s1 = “some text”; var s2 = s1.substring(2); 实际上是运行在read模式,字符串的值会 ...
- CentOS6软raid配置与管理
事先添加硬盘设备sdb.sdc.sdd.sde.无论是物理硬盘还是虚拟硬盘,最好使用同型号同大小的硬盘. 创建raid设备 支持raid0.1.4.5.6级别 # mdadm -C /dev/md0 ...