暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=1698
题目大意:
有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3。为了对付每场战斗需要对组成钩子某个区间的棍子进行调整。问经过q次调整后钩子的总价值是多少?
解题思路:
线段树更新。因为数据范围的问题,每次更新到节点会TLE。用区间更新就会节省很多的时间,对每一个区间来讲,如果这个区间里面的棍子都是相同类型的,就用一个节点变量记录棍子类型,如果区间内棍子是不同类型,则将变量记录为-1。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int manx = ;
struct node
{
int l, r, x;
int Mid()
{
return (l+r)/;
}
} tree[manx];
void build (int root, int l, int r)
{//建树,棍子都为铜质
tree[root].l = l;
tree[root].r = r;
tree[root].x = ;
if (l == r)
return ;
build (*root+, l, tree[root].Mid());
build (*root+, tree[root].Mid()+, r);
}
void update (int root, int l, int r, int s)
{
if (tree[root].l==l && tree[root].r==r)
{//当前区间都为s类型的棍子
tree[root].x = s;
return ;
}
else if (tree[root].x != -)
{//当前区间棍子并不一样,需要把当前区间的状态转移到左右子树并标记当前区间
tree[*root+].x = tree[*root+].x = tree[root].x;
tree[root].x = -;
}
if (r <= tree[root].Mid())
update (*root+, l, r, s);
else if (l > tree[root].Mid())
update (*root+, l, r, s);
else
{
update (*root+, l, tree[root].Mid(), s);
update (*root+, tree[root].Mid()+, r, s);
}
}
int Sum (int root, int l, int s)
{
if (tree[root].x != -)//当前区间棍子类型一样,则不再向下查询
return tree[root].x * (s - l + );
else
{
return Sum(*root+, l, tree[root].Mid()) + Sum(*root+, tree[root].Mid()+, s);
}
}
int main ()
{
int t, l = ;
scanf ("%d", &t);
while (t --)
{
int n, q;
scanf ("%d %d", &n, &q);
build (, , n);
while (q --)
{
int a, b, x;
scanf ("%d %d %d", &a, &b, &x);
update (, a, b, x);
}
int res = Sum(, , n);
printf ("Case %d: The total value of the hook is %d.\n", ++l, res);
}
return ;
}
暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)的更多相关文章
- 暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)
题目连接: http://poj.org/problem?id=3264 题目大意: 有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少? 解题思路: 在节点中存储max,min,然后查 ...
- (简单) 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 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- HDU 1698 Just a Hook(线段树区间替换)
题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的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<= ...
- 暑期训练狂刷系列——Hdu 3506 Largest Rectangle in a Histogram (单调栈)
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目大意: 给出一个数列An,问以Ai为最小值的区间内有多少个元素? 解题思路: 手动模拟一个 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- [HDU] 1698 Just a Hook [线段树区间替换]
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- 表皮囊肿?wtf
https://baike.baidu.com/item/%E8%A1%A8%E7%9A%AE%E5%9B%8A%E8%82%BF/7852024?fr=aladdin
- Oracle冷备和热备脚本
Oracle冷备和热备脚本 冷备脚本: set feedback off set heading off set verify off set trimspool off set echo off ...
- vue 自定义 移动端筛选条件
1.创建组件 components/FilterBar/FilterBar.vue <template> <div class="filterbar" :styl ...
- SGU - 311 Ice-cream Tycoon(线段树)
Description You've recently started an ice-cream business in a local school. During a day you have m ...
- SQL yog过期后教你怎么让他不过期
打开注册表 安装sqlyog后,进入注册表编辑器,进入\HEYK_CURRENT_USER\Software\,找到以{}括起来的那项直接干掉! 1, regedit 2,修改 3,
- NYOJ 158 省赛来了
省赛来了 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 一年一度的河南省程序设计大赛又要来了. 竞赛是要组队的,组队形式:三人为一队,设队长一名.队员两名. 如今问题 ...
- CALayer与UIView的关系
CALayer属于Core Animation部分的内容,比较重要而不太好理解.以下是园子中看到的一篇文章的摘录: 1. UIView是iOS系统中界面元素的基础,所有的界面元素都是继承自它.它本身完 ...
- 云上领跑,快人一步:华为云抢先发布Redis5.0
12月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis 5.0产品,这是一个崭新的突破.目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Redis5.0 ...
- 【iOS系列】- 通知NSNotification的使用
[iOS系列]- 通知NSNotification的使用 1:属性 通知属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) ...
- 为Html.EditorForModel自定义模版
对于MVC视图渲染来说,大家应该不会陌生,但对于模型的渲染,不知道是否听说过,主要是说Model通过它属性的相关特性(DataType,UIHint)来将它们自动渲染到View上,这是一个比较不错的技 ...