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 ...
随机推荐
- Python_Mix*re模块,元字符,量词
模块: 模块就是一组功能的集合,你要和某个东西打交道,而这个东西本身和Python没有关系,这个东西本身就存在,Python提供了一个功能的集合,专门负责和这个东西打交道. 模块的类型: 内置模块 不 ...
- 文件属性和ls -lhi
第1章 无法上网及拍错过程 远程连接拍错过程 1. 查看路是否通畅 2. 是否有拦击 iptables(防火墙) selinux 3. 查看是否有条件 ...
- python全栈考题 3.30
1.执行Python 脚本的两种方式 1.>>python ../pyhton.py 2. >>python.py #必须在首行有 #!/usr/bin/env ...
- javascript数据基本类型和引用数据类型区别
基本类型和引用数据类型区别 1.基本数据类型和引用数据类型 javascript中有两种数据类型,分别是基本数据类型和引用数据类型: 基本数据(原始数据)类型指的是简单的数据段,而引用数据类型则指的是 ...
- asp解码.net传参
get传参时 asp默认使用gbk解码?了解的朋友指点下,谢谢 .net向asp传参时注意urlencode加密编码使用gb2312 附:任何语言的数据交互都要确保接收双方的编码一致
- webix的Form绑定支持数组Array
绑定的原理 form.setValues:把树形对象,压平展开成一维的.比如: var data = { id: 11, name: { first: 'alex', last: 'wu' } }; ...
- java-js知识库之一——canvas绘制9*9乘法表
不知不觉一年又要过去了,软件这一行入坑快两年了,一直不知道这两年干了些啥,也不知道自己到底会些什么,工作也是些简单的东西,谁都能做,对未来也是很茫然.今天和同事优化数据库,头都是懵的,很多东西都感觉似 ...
- java的main函数组成
package test;/*public static void main(String[] args)主函数特殊之处:1.格式是固定的2.被jvm(虚拟机)所识别和调用 public:因为权限必须 ...
- 利用 Eclipse IDE 的强大功能远程调试 Java 应用程序
II. Eclipse 连接套接字模式下的 VM 调用示例(具体引用实践) 说明:不管采用哪种方式,调试的源代码都在eclipse的环境下 一.调试方式一(将目标应用程序作为调试的服务器,eclips ...
- 小白的Redis学习(一)-SDS简单动态字符串
本文为读<Redis设计与实现>的记录.该书以Redis2.9讲解Redis相关内容.请注意版本差异. Redis使用C语言实现,他对C语言中的char类型数据进行封装,构建了一种简单动态 ...