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 【线段树,区间修改 + 维护区间和】的更多相关文章
- HDU 1754 I Hate It 【线段树单点修改 维护区间最大值】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1754 I Hate It Time Limit: 9000/3000 MS (Java/Others ...
- HDU - 1754 线段树-单点修改+询问区间最大值
这个也是线段树的经验问题,待修改的,动态询问区间的最大值,只需要每次更新的时候,去把利用子节点的信息进行修改即可以. 注意更新的时候区间的选择,需要对区间进行二分. #include<iostr ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- HDU 1698 <线段树,区间set>
题目连接 题意: 一条长为N的铜链子,每个结点的价值为1.有两种修改,l,r,z; z=2:表示把[l,r]区间内链子改为银质,价值为2. z=3:表示把[l,r]区间内链子改为金质,价值为3. 思路 ...
- 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 ...
- HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- 流畅的python和cookbook学习笔记(五)
1.随机选择 python中生成随机数使用random模块. 1.从序列中随机挑选元素,使用random.choice() >>> import random >>> ...
- 谷歌在线appspot平台教你学Hacker(由浅如深)-XSS篇
练习链接 http://google-gruyere.appspot.com/ 点开是纯英文的 直接点翻译即可 一 .part1 http://google-gruyere.appspot.com/p ...
- 二、单层感知器和BP神经网络算法
一.单层感知器 1958年[仅仅60年前]美国心理学家FrankRosenblant剔除一种具有单层计算单元的神经网络,称为Perceptron,即感知器.感知器研究中首次提出了自组织.自学习的思想, ...
- Filter的常见应用
1.字符编码过滤器 实现功能,在a.jsp中填写用户名提交到b.jsp,在b.jsp中读取参数名. a.jsp <body> <form action="encoding/ ...
- Java List集合和哈希表
List集合和Set集合,先来看List集合. List集合存储元素的特点: 1.有序(List集合中的元素有下标):存进去是什么样,取出来还是什么样 2.可重复 可以结合以下的简单代码来看一看. i ...
- php 正则验证
PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判 ...
- js截取关键字之后的字符串
需求:截取下面字符串"="之后的所有字符 var str = "12345=6"; //要截取的字符串 var index = str.indexOf(&quo ...
- 如何创建一个基本JQuery的插件
如何创建一个基本的插件 有时您希望在整个代码中提供一些功能.例如,也许你想要一个单一的方法,你可以调用一个jQuery选择,对选择执行一系列的操作.在这种情况下,您可能需要编写一个插件. 链接jQue ...
- 1-4 Sass的基本特性-基础
[Sass]声明变量 定义变量的语法: 在有些编程语言中(如,JavaScript)声明变量都是使用关键词“var”开头,但是在 Sass 不使用这个关键词,而是使用大家都喜欢的美元符号“$”开头.我 ...
- Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
一.概述 Mongo DB 下载下来以后我们应该如何去安装启动和配置才能使用Mongo DB,本篇博客就给大家讲述一下Mongo DB的安装启动及配置详解. 二.安装 1.下载Mongo DB ...