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实现鼠标拖拽

    主要原理: 1.当鼠标按下时,记录鼠标坐标,用到的是 onmousedown: 2.当鼠标移动时,计算鼠标移动的坐标之差,用到的是 onmousemove: 3.当鼠标松开时,清除事件,用到的是 on ...

  2. Codeforces #504(div1+div2) 1023D Array Restoration(线段树)

    题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...

  3. 利用General框架进行三层架构开发

    三层架构是企业信息管理系统中一种比较流行的架构方式,如大家所知,三层架构将信息系统分为数据访问层(DAL).业务逻辑层(BLL).界面表示层(UI)三部分,三层架构的好处是根据系统中代码所处的层次将系 ...

  4. 总结:kathasis如何发送get请求获取数据

    1.进入前端页面,找到对应的模块,开始塞字段数据. 2.如果字段为基本类型,如String,比如website,则在前段界面,右击,inspect,找到对应的代码所处的jsp,跳转到该jsp,通过该j ...

  5. netty中的位操作

    看了PoolChunk源码,好多位操作,对这些位操作理解不到位,看起来很是吃力,不知道为什么要这么做,可能是性能更好 1:大小为2 的冥的数加1 怎么操作 size^1; 2: <=比较   m ...

  6. 基于PhpStorm对Yii框架进行的单元测试一【PhpUnit环境搭建】

    1.下载phpunit.phar 2.在phpstorm中配置phpunit库 3.不同版本phpunit 需要依赖的php解释器也不一样,如果运行时报错 可以适当调整php解释器的版本 至此进行ph ...

  7. Java50道经典习题-程序43 求奇数个数

    题目:求0—7所能组成的奇数个数,奇数中不包含重复数字. public class Prog43 { public static void main(String[] args) { //0-7能组成 ...

  8. 公司内部Wiki及搭建wiki系统-confluence

    Wiki 是一个协同著作平台或称开放编辑系统.我们可以用Wiki来建设帮助系统,知识库系统.国内公共wiki最著名就是百度百科.那公司内部为什么要使用wiki呢? 2.内部wiki的作用 1.鼓励分享 ...

  9. 图片展示js特效

    html 代码片段,做一个table表格 <table width="798" height="276" border="0" ali ...

  10. ubuntu安装软件-笔记

    手动安装过程 下载 - 配置文件 常用命令: sudo  dpkg -i ******.deb 问题&解决思路 问题1:依赖项 - 缺少某一个依赖包 解决尝试: 1 . sudo apt-ge ...