题目链接 HDU 1698

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.

大概题意:

一条长度为N得链子, 有Q次操作,每次操作把区间 X~Y 的金属换成 Z;有金,银,铜三种金属分别对应3,2,1三种价值。

求Q次操作后,求整条链子的总价值。

思路:

成段更新,区间求和,本次用一维数组表示法建线段树

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson l, m, k<<1 ///左儿子
#define rson m+1, r, k<<1|1 ///右儿子
using namespace std; const int MAXN = ; int st[MAXN<<]; ///st数组里面记录的是标记 0,1, 2, 3; 0 代表杂色
int flag; void down(int &k)
{
st[k<<] = st[k<<|] = st[k]; ///左儿子和右儿子均设为该颜色
st[k] = ; ///设为杂色 ??避免后面重复操作??
} void build(int l, int r, int k) ///建立线段树,一开始都初始化为 1
{
st[k] = flag;
if(l == r) return;
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int &L, int &R, int l, int r, int k) ///更新L--R,要从1--N, st[1] 开始
{
if(L <= l && R >= r) ///所在区间在更新区间内
{
st[k] = flag; ///整段更新为新颜色
return;
}
if(st[k]) ///如果该结点为纯色,则左右儿子颜色也为该颜色
{
down(k);
}
int m = (l+r)>>;
if(L <= m) update(L, R, lson);
if(R > m) update(L, R, rson);
} int query(int l, int r, int k) ///查询区间颜色之和
{
if(st[k]) return st[k]*(r-l+); ///如果该段为纯色,则成段想乘得结果 int m = (l+r)>>, t1, t2;
t1 = query(lson);
t2 = query(rson);
return t1+t2;
} int main()
{
int T, N, t = ;
int L, R, q;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &q);
flag = ;
build(, N, ); ///建立线段树
while(q--)
{
scanf("%d%d%d", &L, &R, &flag);
update(L, R, , N, );
}
printf("Case %d: The total value of the hook is %d.\n", t++, query(, N, ));
}
return ;
}

HDU 1698 【线段树,区间修改 + 维护区间和】的更多相关文章

  1. HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...

  2. HDU - 1754 线段树-单点修改+询问区间最大值

    这个也是线段树的经验问题,待修改的,动态询问区间的最大值,只需要每次更新的时候,去把利用子节点的信息进行修改即可以. 注意更新的时候区间的选择,需要对区间进行二分. #include<iostr ...

  3. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

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

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

  5. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  6. Hdu 1698(线段树 区间修改 区间查询)

    In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...

  7. HDU 1698 <线段树,区间set>

    题目连接 题意: 一条长为N的铜链子,每个结点的价值为1.有两种修改,l,r,z; z=2:表示把[l,r]区间内链子改为银质,价值为2. z=3:表示把[l,r]区间内链子改为金质,价值为3. 思路 ...

  8. HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))

    To the moon Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. C#语言-05.委托和事件

    a. 委托:是一种定义方法签名的类型,可以与具有兼容签名的任何方法关联.所谓兼容的方法,是指这个方法和委托的方法签名具有相同的返回类型和参数 i. 语法:delegate 方法签名; . 方法签名是方 ...

  2. .Net程序员玩转Android系列之二~Android Framework概要(1)

    从windows操作系统说起 人们总是喜欢从将陌生的事物和自己所了解的东西关联起来,以加深对未知事物的了解,这一讲我们从windows操作系统说起,逐步引领带大家走入android的世界.写任何程序都 ...

  3. Java - 避免不必要的对象

    通常,我们更喜欢重用一个对象而不是重新创建一个.如果对象是不可变的,它就始终可以被重用. 下面是一个反面例子: String s = new String("stringette" ...

  4. 游标的小知识(转载and整理)

    一.游标(用来存储多条查询数据的一种数据结构(结果集),它有一个指针,用来从上往下移动,从而达到遍历每条记录的作用) 游标也可以理解为逐行返回SQL语句的结果集 如何编写一个游标? 1.声明游标 de ...

  5. Ubuntu 16.04 开启BBR加速

    BBR(Bottleneck Bandwidth and RTT)是Google推出的一个提高网络利用率的算法,可以对网络进行加速,用来干什么大家心里都有B数 Ubuntu开启BBR的前提是内核版本必 ...

  6. js控制input text字符键入/字符长度限制/字母自动大写

    功能: 1.仅允许指定字符键入 2.限制长度 实现代码: <input type="text" style="width: 6em" name=" ...

  7. IIS6服务器的请求流程(图文&源码)

    1.IIS 7开发与管理完全参考手册  http://book.51cto.com/art/200908/146040.htm 2.Web服务IIS 6   https://technet.micro ...

  8. 定制化移动办公APP:打造企业专属的“钉钉”“纷享销客”,实现企业办公管理一体化

    一.项目背景 随着信息化社会的高速发展,市场竞争日益激烈,传统的管理和办公系统多且复杂,用户需要使用多个系统才可完成一项工作,而且各个系统的界面和风格存在差异,造成了信息查找不便,大大降低了用户的工作 ...

  9. freess(未测试)

    freess 使用 nodejs 配合 shadowsocks-windows 实现FQ (windows) 使用方法: 如果你没有安装nodejs请先安装,访问 https://nodejs.org ...

  10. 微软Azure虚拟机备份服务在中国发布

    近期,Azure虚拟机备份服务在微软智能云上发布. 相关功能阐述: Azure IaaS虚拟机备份服务针对Windows操作系统,提供了应用一致性的备份技术:同时针对Linux操作系统,提供了文件系统 ...