Just a Hook 线段树 区间更新
Just a Hook
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<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>
#include<stack>
using namespace std;
typedef long long LL;
#define MAXN 100009
#define N 25
#define INF 0x3f3f3f3f /*
线段树 区间更新!
*/
struct node
{
int l, r, data, laz;
int sum;
}T[MAXN*];
void Build(int p, int l, int r)
{
T[p].l = l, T[p].r= r, T[p].data = , T[p].laz = ;
T[p].sum = ;
if (l == r)
{
T[p].sum = ;
return;
}
int mid = (l + r) / ;
Build(p << , l, mid);
Build((p << ) | , mid + , r);
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
}
void update(int p, int l, int r, int v)
{
int mid = (T[p].l + T[p].r) / ;
if (T[p].l >= l&&T[p].r <= r)
{
T[p].data = v;
T[p].laz = ;//这一块被成块更新过
T[p].sum = (r - l + )*v;
return;
}
if (T[p].laz)//如果被更新过,那么说明这一块内的颜色会有多种
{
T[p].laz = ;
update(p << , T[p].l, mid, T[p].data);
update((p << ) | , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
update(p << , l, r, v);
else if (l > mid)
update((p << ) | , l, r, v);
else
{
update(p << , l, mid, v);
update((p << ) | , mid + , r, v);
}
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
} int main()
{
int t, n, l, r, v, q;
int cas = ;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
Build(, , n);
while (q--)
{
scanf("%d%d%d", &l, &r, &v);
update(, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n", cas++, T[].sum);
}
}
Just a Hook 线段树 区间更新的更多相关文章
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- HDU1698:Just a Hook(线段树区间更新)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 51nod1459 迷宫游戏
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你 ...
- Spring抽象JDBC,使用JdbcTemplate
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- C#与正则表达式的例子
一个很好的文章,但是并没有测试 连接
- [ CERC 2014 ] Vocabulary
\(\\\) \(Description\) 给出三个长度分别为 \(lenA,lenB,lenC\) 的三个字符串 \(A,B,C\) ,其中字符集只包括所有小写字母以及 \(?\) 号. 现在将所 ...
- Js变量类型
值类型和引用类型 值类型(基本类型):5种,Number String Boolean null undefined var a=10; var b=a; a=2; console.log(b); a ...
- MAMP中Python安装MySQLdb
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Py ...
- 并发编程学习笔记(11)----FutureTask的使用及实现
1. Future的使用 Future模式解决的问题是.在实际的运用场景中,可能某一个任务执行起来非常耗时,如果我们线程一直等着该任务执行完成再去执行其他的代码,就会损耗很大的性能,而Future接口 ...
- 网站卡测试用 PageSpeed Insights
这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...
- gson序列化后整形变浮点问题解决方案
字段值是json格式的字符串.我需要将这个字段反序列化为List<Map>形式,但是在反序列化后,id变为了1.0. 百度了很多然并卵,最后改用了阿里的fastjson,没问题.(jack ...
- idea使用maven install命令打包(springboot),jar运行时出现没有主清单属性
原因是:我的项目里除了springboot启动类还自定义了多个main来搞了点小demo,就因为这个原因我花了近一天的时间才找清楚原因. 解决方案:找到多余的main方法,注释或删除掉. (下面可以忽 ...