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 ...
随机推荐
- netcore webapi统一配置跨域问题
修改startup类中的configure方法
- SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递
实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...
- ABC3
Sql Server http://www.cnblogs.com/sunxi/p/4600152.html http://blog.csdn.net/dmz1981/article/details/ ...
- <7>Lua类的表的实例创建
根据上一节知识所述Lua中没有像C.C++.JAVA中的类概念,面向对象等 ,但我们可以模拟出来 如下 代码如下: --创建类的表 local Person = {} function Person: ...
- html5-css边框全
/*div{ width: 500px; height: 300px; background: rgb(122,30,60); border: 10px solid black ...
- 基于word2vec训练词向量(二)
转自:http://www.tensorflownews.com/2018/04/19/word2vec2/ 一.基于Hierarchical Softmax的word2vec模型的缺点 上篇说了Hi ...
- 输入一串字符,检查是否可以组成friend
"""输入一串字符,检查是否可以组成friend""" from collections import Counter def foo(nu ...
- springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢?
springboot maven项目,为什么build成功,build path也没错误,project-->clean 也没用,项目上面还是有个红x呢? 看错误信息有提示: Descript ...
- Android开发常见错误汇总
[错误信息] [2011-01-19 16:39:10 - ApiDemos] WARNING: Application does not specify an API level requireme ...
- 计算概论(A)/基础编程练习2(8题)/5:点和正方形的关系
#include<stdio.h> #include<math.h> int main() { // 输入坐标 float x, y; while(scanf("%f ...