Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45600    Accepted Submission(s): 21730

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
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.
 
题意:在DotA的游戏中,对于大多数英雄而言,Pudge的肉钩实际上是最可怕的。钩子由几根相同长度的连续金属棒组成。现在,帕吉希望在钩子上做一些操作。让我们将钩子的连续金属棒从1到N编号。对于每次操作,Pudge可以将连续的金属棒(从X到Y编号)改为铜棒,银棒或金棒。钩的总值计算为N个金属棒的值的总和。更准确地说,每种棒的价值计算如下:对于每个铜棒,值为1.对于每个银棒,值为2.对于每个金棒,值为3. Pudge想知道执行操作后钩子的总值。你可能会认为原来的钩子是由铜棒组成的。输入输入由几个测试用例组成。输入的第一行是案例的数量。不超过10个案例。对于每种情况,第一行包含整数N,1 <= N <= 100,000,这是Pudge肉钩的枝条数,第二行包含整数Q,0 <= Q <= 100,000,这是操作次数。接下来的Q行,每行包含三个整数X,Y,1 <= X <= Y <= N,Z,1 <= Z <= 3,它定义了一个操作:将从X到Y编号的枝条改为金属种类Z,其中Z = 1代表铜质种,Z = 2代表银种,Z = 3代表金种。输出对于每种情况,在一行中打印一个数字,表示操作后挂钩的总值。使用示例中的格式。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
const int maxn = + ;
int t, n, x, y, z, q, tre[maxn * ], laz[maxn * ], kase = ;
void push_up(int num)
{
tre[num] = tre[num * ] + tre[num * + ];
}
void pushdown(int num, int x, int y)
{
if (laz[num])
{
int mid = (x + y) / ;
laz[num * ] = laz[num * + ] = laz[num];
tre[num * ] = (mid - x + ) * laz[num];
tre[num * + ] = (y - mid) * laz[num];
laz[num] = ;
}
}
void build(int x, int y, int num)
{
int mid = (x + y) / ;
laz[num] = ;
if (x == y)
{
tre[num] = ; return;
}
build(x, mid, num * );
build(mid + , y, num * + );
push_up(num);
}
void update(int le, int ri, int z, int x, int y, int num)
{
int mid = (x + y) / ;
if (le <= x && y <= ri)
{
laz[num] = z;
tre[num] = z * (y - x + );//先更新节点,但不继续往下更新,节点的值恰好是节点所在子树的和
return;
}
pushdown(num, x, y);
if (le <= mid)
update(le, ri, z, x, mid, num * );
if (mid < ri)
update(le, ri, z, mid + , y, num * + );
push_up(num);
}
int query(int le, int ri, int x,int y,int num)
{
if (le == ri)
{
return tre[num];
}
pushdown(num, x, y);
int mid = (le + ri) / ;
if (x <= mid)
return query(le, mid, x, y, num * );
else
return query(mid + , ri, x, y, num * + );
}
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
build(, n, );
while (q--)
{
scanf("%d%d%d", &x, &y, &z);//将[x,y]的值更新为z
update(x, y, z, , n, );
}
printf("Case %d: The total value of the hook is %d.\n", ++kase, tre[]);
}
return ;
}

hdu1698 线段树(区间更新~将区间[x,y]的值替换为z)的更多相关文章

  1. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  2. hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDU 2795 Billboard (线段树单点更新 && 求区间最值位置)

    题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明 ...

  4. nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     In the traditional RMQ (Range Minimum Q ...

  5. hdu 1754 线段树 单点更新 动态区间最大值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  7. hdu1698 线段树区间更新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  9. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

随机推荐

  1. js实现导航栏的吸顶操作

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. ROS探索总结(四)——简单的机器人仿真

    前边我们已经介绍了ROS的基本情况,以及新手入门ROS的初级教程,现在就要真正的使用ROS进入机器人世界了.接下来我们涉及到的很多例程都是<ROS by Example>这本书的内容,我是 ...

  3. Ros学习topic——小海龟

    ROS Topics 1.rqt_graph:创建一个显示当前系统运行情况的动态图形 安装 $ sudo apt-get install ros-<distro>-rqt $ sudo a ...

  4. ruby 数组与散列

    def say_goodnight(name) result ="Good night ." +name return result end def say_goodmorning ...

  5. STM32 C++编程 004 Adc (数模转换)类

    使用 C++ 语言给 STM32 编写一个 Adc 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的专栏 ...

  6. [转]JQ中$(window).load和$(document).ready区别与执行顺序

    一.$(window).load().window.onload=function(){}和$(document).ready()方法的区别 1.$(window).load() 和window.on ...

  7. SqlServer压缩数据库日志

    )--数据库名称 )--数据库日志文件名称 --替换成自己的文件名称 select @dbName='dbname' select @dbNamelog='dbname_log' ) set @sql ...

  8. Python3下的paramiko模块

    paramiko模块是基于Python实现的SSH远程安全连接,用于SSH远程执行命令.文件传输等功能. 默认Python没有,需要手动安装:pip install paramiko SSH密码认证远 ...

  9. 【机器学习】李宏毅机器学习-Keras-Demo-神经网络手写数字识别与调参

    参考: 原视频:李宏毅机器学习-Keras-Demo 调参博文1:深度学习入门实践_十行搭建手写数字识别神经网络 调参博文2:手写数字识别---demo(有小错误) 代码链接: 编程环境: 操作系统: ...

  10. 接口和抽象类有什么区别(Java基础)

    接口可以多实现 Java中抽象类只能单继承 接口   相对于类来说    可以实现多个接口 抽象相对于 类来说 只能单一继承 一个类只能继承一个抽象类,但可以实现多个接口. 抽象类中可以包含抽象方法和 ...