HDU 1689 Just a Hook (线段树区间更新+求和)
Just a Hook
Problem Description
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
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.
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.
思路
初始所有节点值为1 , 更新[l,r]为2 或 3,再求区间和,涉及懒标记,直接套模板= =
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10 ;
const int inf = 0x3f3f3f3f;
struct node{
int l,r;
int add;
int sum;
}tree[maxn<<2];
int kase=0;
int n,m,t;
int a,b,c;
int val = 1;
int ans = 0;
void pushup(int k)
{
tree[k].sum = tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushdown(int k)
{
if(tree[k].add)
{
tree[k<<1].sum = (tree[k<<1].r-tree[k<<1].l+1)*tree[k].add;
tree[k<<1|1].sum = (tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].add;
tree[k<<1].add = tree[k].add;
tree[k<<1|1].add = tree[k].add;
tree[k].add = 0;
}
}
void build(int l,int r,int k)
{
tree[k].l = l; tree[k].r = r; tree[k].add = 0;//刚开始一定要清0
if(l == r){ tree[k].sum=1; return ; }
int mid = (l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
pushup(k);
}
void updata(int k)
{
if(a <= tree[k].l && b >= tree[k].r)
{
tree[k].sum = (tree[k].r-tree[k].l+1)*val;
tree[k].add = val;
return ;
}
pushdown(k);
int mid = (tree[k].l+tree[k].r)>>1;
if(a<=mid){ updata(k<<1); }
if(b>mid){ updata(k<<1|1); }
pushup(k);
}
void query(int k)
{
if(a <= tree[k].l && b >= tree[k].r)
{
ans +=tree[k].sum ;
return ;
}
pushdown(k);
int mid = (tree[k].l+tree[k].r)>>1;
if(a <= mid){ query(k<<1);}
if(b > mid){ query(k<<1|1);}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d",&m);
build(1,n,1);
while(m--)
{
scanf("%d%d%d",&a,&b,&val);
updata(1);
}
ans=0;a=1;b=n;//这里需初始化
query(1);
printf("Case %d: The total value of the hook is %d.\n",++kase,ans);
}
}
HDU 1689 Just a Hook (线段树区间更新+求和)的更多相关文章
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- (简单) 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 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- mybatis3 date 的处理
<if test="startTime!=null and startTime!=''"> <![CDATA[ and DATE_FORMAT(create_ti ...
- HDU1530 最大流问题
第一次写Dinic 然后贴一下 最基础的网络流问题 嘎嘎: #include <iostream> #include<cstdio> #include<string.h& ...
- Java输入输出流(IO)-----文件类File详解
1.java.io.File类简介 凡是与输入.输出相关的类.接口等都定义在java.io包下 File是一个类,可以有构造器创建其对象.此对象对应着一个文件(.txt .avi .doc .p ...
- Pycharm学习python路
import 模块之后是灰色的表明没有被引用过 lxml找不到的话用anaconda prompt :pip uninstall lxml 重新安装 用request时,写的reg无法正确解析网页,先 ...
- EasyUi通过OCUpload上传及POI上传 实现导入xls表格功能
Easyui上传文件案例 第一步:要想使用OCUpload首先前端需要导入js包 <script type="text/javascript" src=&qu ...
- javascript 链式写法
熟悉Jquery的同学都知道,它对dom的操作基本都链式调用的写法,这种给人感觉就是很简洁,易懂,而且最大的好处就是避免多次重复使用一个对象变量. 链式的实现方式:链式操作是在对象的方法中通过最后返回 ...
- Python+OpenCV图像处理(十一)—— 图像金字塔
简介:图像金字塔是图像中多尺度表达的一种,最主要用于图像的分割,是一种以多分辨率来解释图像的有效但概念简单的结构.简单来说,图像金字塔就是用来进行图像缩放的. 进行图像缩放可以用图像金字塔,也可以使用 ...
- LINUX搭建PySpider爬虫服务
1.环境搭建 yum update -y yum install gcc gcc-c++ -y yum install python-pip python-devel python-distribut ...
- java-工厂方法模式学习笔记
1.工厂模式分三种 1.1 普通工厂模式:就是建立一个工厂类,对实现了同一接口的一些类进行实例创建,如下图所示: 就以老司机开车(土豪开奔驰,宝马:屌丝骑自行车)为例,说明一下普通工厂模式: 首先,创 ...
- Qt 文本文件的打开、新建、保存以及另存为
我们平时在使用windows的notepad以及其他各种软件过程中,都会有保存文件和另存为两种功能,这两者不能混为一谈. 一.保存时有两种情况,一种是对于新创建的文件,一种是对于已有的文件,前者需要打 ...