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. 使用LaTeX按IEEE模板写论文时的参考文献管理方法(BibTeX使用小结)

    之前用LaTeX写论文时,参考文献都是手动添加管理的,真是让人很抓狂.所以这次趁着假期,简单看了一下怎么使用BibTeX对参考文献进行管理,这里以IEEE的最新模板为例. 首先说明,我之前用的是MiK ...

  2. 第4章 springboot热部署 4-1 SpringBoot 使用devtools进行热部署

    /imooc-springboot-starter/src/main/resources/application.properties #关闭缓存, 即时刷新 #spring.freemarker.c ...

  3. Windows版本Apache+php的Xhprof应用__[2]

    [计划] “Windows版本Apache+php的Xhprof应用__[1]”中已经解决了下载,配置的问题,所以这里的工作是接着进行的,我们以调试一个 php代码的文件来看看是怎么用xhprof的. ...

  4. web网页 页面布局的几种方式(转)

    web网页 页面布局的几种方式 转载 2017年06月16日 12:19:40 2218 网页基本布局方式: (1)流式布局 Fluid 流布局与固定宽度布局基本不同点 就在于对网站尺寸的侧量单位不同 ...

  5. 在Ubuntu里安装Mysql5.7.23

    准备在Linux里安装Mysql,安装过程中遇到很多问题,这里记录下我成功安装的过程. 操作系统:Ubuntu 18.04 数据库:Mysql 5.7.23 安装步骤: 1.下载一个apt,下载mys ...

  6. 100741A Queries

    传送门 题目 Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my f ...

  7. 网络笔记-unity 实现AOP

    该文章来自网络,如有冒犯,请及时联系! 前提 引用以下文件 Microsoft.Practices.ObjectBuilder2.dll Microsoft.Practices.Unity.dll M ...

  8. setTimeout()和setInterval() 何时被调用执行(非多线程).RP

    定义 setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔 ...

  9. android smali代码注入 实战一

    有同学在通服里面干活,最近一直忙着4g基站搭建的干活,测试设备(android)测量移动网络数据,没有自动保存记录的功能,只能手动记录各种测试参数,不知道测试软件供应商是怎样想的,竟然不提供的这样的功 ...

  10. 阿里云ECS centos7.2 支持IPv6

    公司的项目因为服务器没有支持IPv6而被appstore给退回来了 第一部分 第一步:编辑 /etc/sysctl.conf 文件,将其中三条禁用IPv6的设置更改为: 第二步:使用命令启动启用IPv ...