题目链接:https://vjudge.net/problem/HDU-1698

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. 

InputThe 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. 
OutputFor 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.

 

题解:

经典的区间染色问题。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int sum[MAXN<<], setv[MAXN<<]; void push_up(int u)
{
sum[u] = sum[u*] + sum[u*+];
} void push_down(int u, int l, int r)
{
if(setv[u]>)
{
setv[u*] = setv[u*+] = setv[u];
sum[u*] = (r-l++)/*setv[u];
sum[u*+] = (r-l+)/*setv[u];
setv[u] = ;
}
} void build(int u, int l, int r)
{
if(l==r)
{
setv[u] = ; //每个鱼钩初始化为金属1
sum[u] = ;
return;
} int mid = (l+r)>>;
build(u*, l, mid);
build(u*+, mid+, r);
push_up(u);
setv[u] = ;
} void set_val(int u, int l, int r, int x, int y, int val)
{
if(x<=l && r<=y)
{
sum[u] = (r-l+)*val;
setv[u] = val;
return;
} push_down(u, l, r);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, val);
push_up(u);
} int query(int u, int l, int r, int x, int y)
{
if(x<=l && r<=y)
return sum[u]; push_down(u, l, r);
int ret = ;
int mid = (l+r)>>;
if(x<=mid) ret += query(u*, l, mid, x, y);
if(y>=mid+) ret += query(u*+, mid+, r, x, y);
return ret;
} int main()
{
int T, n, m;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &m); build(, , n);
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
set_val(, , n, x, y, z);
} int ans = query(, , n, , n);
printf("Case %d: The total value of the hook is %d.\n", kase, ans);
}
}

HDU1698 Just a Hook —— 线段树 区间染色的更多相关文章

  1. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

  2. hdu1698 Just a hook 线段树区间更新

    题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...

  3. hdu1698 Just a Hook (线段树区间更新 懒惰标记)

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

  4. hdu-------(1698)Just a Hook(线段树区间更新)

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

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

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

  6. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  7. hdu 5023(线段树区间染色,统计区间内颜色个数)

    题目描述:区间染色问题,统计给定区间内有多少种颜色? 线段树模板的核心是对标记的处理 可以记下沿途经过的标记,到达目的节点之后一块算,也可以更新的时候直接更新到每一个节点 Lazy操作减少修改的次数( ...

  8. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

  9. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

随机推荐

  1. 【01】恶趣味玩转 GitHub commit 历史记录

    [黑科技]恶趣味玩转 GitHub commit 历史记录   工具:https://github.com/gelstudios/gitfiti 效果截图:预览地址:https://github.co ...

  2. [luoguP2216] [HAOI2007]理想的正方形(二维单调队列)

    传送门 1.先弄个单调队列求出每一行的区间为n的最大值最小值. 2.然后再搞个单调队列求1所求出的结果的区间为n的最大值最小值 3.最后扫一遍就行 懒得画图,自己体会吧. ——代码 #include ...

  3. docker容器的导入导出

    导出容器docker export 导出容器快照到本地文件$ sudo docker ps -aCONTAINER ID        IMAGE               COMMAND      ...

  4. iOS 混合变换旋转 CGAffineTransform

    在ios 中, Core Graphics 提供了一系列的函数可以在一个变换的基础上做深层次的变换,如果做一个既要缩放又要旋转的变换,以下的方法比较实用. CGAffineTransformScale ...

  5. Spring Data Redis与Jedis的选择(转)

    说明:内容可能有点旧,需要在业务上做权衡. Redis的客户端有两种实现方式,一是可以直接调用Jedis来实现,二是可以使用Spring Data Redis,通过Spring的封装来调用.应该使用哪 ...

  6. Markdown中插入图片技巧收集

    在操作Markdown时图片应该是最头痛的一件事! 比如要发送一个md文件给对方,如果附带了图片时,那么就要一大堆文件包括图片发给对方等等,如果使用在线图片,那么这个服务器又是一大痛点,因为你不确定这 ...

  7. dtrace-debug

    https://www.objc.io/issues/19-debugging/dtrace/

  8. SAS学习笔记 - R的数据操作

    1.对象 1.1 对象及其内在属性 R中的处理数据就是对象,每个对象可以包含多个元素.对象有两个内在属性:类型和长度.类型是对象元素的基本种类,共四种:数值型,字符型,复数型和逻辑型.对象的类型和长度 ...

  9. sys.argv的妙用:python命令行参数列表的修改、增加、删除

    是否妙用取决于你怎么用 1.sys.argv是用来获取命令行参数的方法,本身是一个list.你搜其实用方法,这方面的介绍最多,这里不赘述 2.那么问题是:sys.argv可以赋值吗?可以扩充吗?可以修 ...

  10. How do you check if a variable is an array in JavaScript? [duplicate]

    https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript ...