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 (线段树区间更新+求和)的更多相关文章

  1. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  2. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  3. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  4. 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 ...

  5. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  8. 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 ...

  9. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  10. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递

    实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...

  2. MOG插件(葡萄牙语,略作翻译)

    这次记录下MOG大神的插件,自从我发现了这个插件,似乎开启了一个新世界诶~~~ 网址 https://atelierrgss.wordpress.com 1. MOG_YuruYuri.js CARA ...

  3. html5常用数学 公式的用法

    <script>        // alert(Math.PI);        // alert(Math.floor(3.16));    // var a=Math.ceil(3. ...

  4. html5-新增表单的小结details summary

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  5. Running tests on PyCharm using Robot Framework

    问题: I started using PyCharm with the robot framework, but i'm facing an issue. how can i run my test ...

  6. vim 命令学习(基础篇)

    [1]三种模式 vi的三种模式:命令模式.末行模式.编辑模式. 三种模式相互切换逻辑与命令图: 1.命令模式是vi的默认模式(即每打开一个文件时的初始模式). 2.命令模式切换至末行模式,末行模式切换 ...

  7. django之auth认证系统

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...

  8. awk和sed

    在正则表达式那章有两个比较好用的命令sed 和awk awk 是按指定分隔符分隔,默认空白键,第一个为$1,第二个为$2... 命令: FS分隔符

  9. redis相关问题

    什么是Redis?Redis 是一个使用 C 语言写成的,开源的 key-value 数据库..和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表 ...

  10. Axis2之wsdl2java工具

    本章主要介绍axis2的wsdl2java工具的使用. Axis2提供了一个wsdl2java命令可以根据WSDL文件自动产生调用WebService的代码.wsdl2java命令可以在<Axi ...