题目链接: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. netcore使用AutoMapper

    说明:以下是使用过程中看到的其他博主写的,地址如下: 地址一:https://cloud.tencent.com/developer/article/1395155 地址二:https://www.c ...

  2. Cow Exhibition (01背包)

    "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...

  3. [Istio]Kubernetes集群部署Istio 1.0

    大部分内容都是可以根据https://istio.io/docs/setup/kubernetes/quick-start/来处理的,这里主要谈部署时一些细节的问题 首先,当我们按照 istio 官方 ...

  4. 【枚举】Southwestern Europe Regional Contest H - Sheldon Numbers

    https://vjudge.net/contest/174235#problem/H [题意] 求[x,y]之间有多少个Sheldon Number Sheldon Number是二进制满足以下条件 ...

  5. 关于datetime,date,timestamp,year,time时间类型小结

    关于datetime,date,timestamp,year,time时间类型 datetime占用8个字节 日期范围:”1000-01-01 00:00:00” 到”9999-12-31 23:59 ...

  6. captcha库报错"OSError: cannot open resource"

    问题描述 在win平台上python虚拟环境下使用captcha库生成验证码报错OSError: cannot open resource 代码 from captcha.image import I ...

  7. 《大话设计模式》Python版代码实现

    上一周把<大话设计模式>看完了,对面向对象技术有了新的理解,对于一个在C下写代码比较多.偶尔会用到一些脚本语言写脚本的人来说,很是开阔眼界.<大话设计模式>的代码使用C#写成的 ...

  8. C#的特性学习草稿

    原文发布时间为:2008-11-22 -- 来源于本人的百度文章 [由搬家工具导入] 举个简单的例子: 先定义个特性 从Attribute继承,并标明用法 [AttributeUsage(Attrib ...

  9. maven 编译出错 Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean

    eclipse在使用maven的tomcat控件编译java程序时,报错 Failed to execute goal org.apache.maven.plugins:maven-clean-plu ...

  10. nginx 安装过程中的not found

    linux 发行版本:centos zlib not found openssl not found yum install zlib-devel yum install openssl-devel