hdu 1698 (延迟标记+区间修改+区间求和)
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
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.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
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.
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
Source
2008 “Sunline Cup” National Invitational Contest
思路:第一个想法是对不同区间的不通材料的分别做判断求和,后来根据lazy标记可以发现,在标记下压的过程中其实已经给区间分类了,膜法
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
int sum[maxn<<2],add[maxn<<2];//lazy
void pushup(int rt) {//sum
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int rt,int m) {
if(add[rt]) {
add[rt<<1]=add[rt<<1|1]=add[rt];
sum[rt<<1]=(m-(m>>1))*add[rt];
sum[rt<<1|1]=(m>>1)*add[rt];
add[rt]=0;
}
}
void build(int l,int r,int rt) {
add[rt]=0;
if(l==r) {
sum[rt]=1;
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}
void update(int c,int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) {
add[rt]=c;
sum[rt]=c*(r-l+1);
return;
}
pushdown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m) update(c,L,R,l,m,rt<<1);
if(R>m) update(c,L,R,m+1,r,rt<<1|1);
pushup(rt);
}
int main() {
// freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++) {
int n;
scanf("%d",&n);
build(1,n,1);
int m;
scanf("%d",&m);
while(m--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(c,a,b,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",i,sum[1]);
}
return 0;
}
hdu 1698 (延迟标记+区间修改+区间求和)的更多相关文章
- HDU 1698 【线段树,区间修改 + 维护区间和】
题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...
- Just a Hook (HDU 1698) 懒惰标记
Just a Hook (HDU 1698) 题链 每一次都将一个区间整体进行修改,需要用到懒惰标记,懒惰标记的核心在于在查询前才更新,比如将当前点rt标记为col[rt],那么此点的左孩子和右孩子标 ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- 【codevs1690】开关灯 线段树 区间修改+区间求和(标记)
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- 【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))
[codevs1690]开关灯 2014年2月15日4930 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的 ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- 【树状数组区间修改区间求和】codevs 1082 线段树练习 3
http://codevs.cn/problem/1082/ [AC] #include<bits/stdc++.h> using namespace std; typedef long ...
- 区间修改区间求和cdq分治
https://www.luogu.org/problemnew/show/P3372 #include<bits/stdc++.h> #define fi first #define s ...
- bzoj2631 tree LCT 区间修改,求和
tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 4962 Solved: 1697[Submit][Status][Discuss] Des ...
随机推荐
- Spring———bean的创建方式,注入方式,复杂类型注入 概括
Spring相关概念和类 1.IOC inverse of control 控制反转 反转了创建对象的方式 以前:new 对象,管理和维护 ...
- Java集合类(转自hey平平)
一.集合与数组 数组(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知的情况下使用. 集合(只能存储对象,对象类型可以不一样)的长度可变,可在多数情况下使用. ...
- python全栈考题 3.30
1.执行Python 脚本的两种方式 1.>>python ../pyhton.py 2. >>python.py #必须在首行有 #!/usr/bin/env ...
- <转载> UE4的Actor类C++简单尝试
原文链接: 简书小小酥XX https://www.jianshu.com/p/2bcc80f0e789 一开始我用了一段时间UE4,发现如果用蓝图系统真的不太适合我的风格.因为之前一直都是用Un ...
- MySQL索引的原理,B+树、聚集索引和二级索引的结构分析
索引是一种用于快速查询行的数据结构,就像一本书的目录就是一个索引,如果想在一本书中找到某个主题,一般会先找到对应页码.在mysql中,存储引擎用类似的方法使用索引,先在索引中找到对应值,然后根据匹配的 ...
- Appium简介及工作原理
一.什么是Appium Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS.Android及FirefoxOS平台.Appium使用WebDriver ...
- SQL语句:如何让字符串转化数字
和前端联调的时候,突然出现一个状况,新增数据的时候,一直报系统错误,写下此文,留以后反复温习.菜鸟程序员一名~ 项目内容:新增产品信息 具体实现:1 获取基础信息,创建产品(调用接口传入的产品类型,如 ...
- kafka producer 0.8.2.1 示例
package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...
- 常用MySQL操作(一)
第二十四次课 常用MySQL操作(一) 目录 一.设置更改root密码 二.连接mysql 三.mysql常用命令 四.mysql用户管理 五.常用sql语句 六.mysql数据库备份恢复 七.扩展 ...
- memge和saveOrUpdate的区别
今天做hibernate开发的时候遇到这样一个错误: a different object with the same identifier value was already associated ...