题目连接:

  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 (线段树区间更新)的更多相关文章

  1. 暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)

    题目连接: http://poj.org/problem?id=3264 题目大意: 有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少? 解题思路: 在节点中存储max,min,然后查 ...

  2. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  3. 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 ...

  4. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  5. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  6. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  7. 暑期训练狂刷系列——Hdu 3506 Largest Rectangle in a Histogram (单调栈)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目大意: 给出一个数列An,问以Ai为最小值的区间内有多少个元素? 解题思路: 手动模拟一个 ...

  8. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  9. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. Myeclipse配置jad

    下载地址:http://pan.baidu.com/s/1bnpMEuF 1.下载jad158g.win.zip 下载后解压.解压缩后将jad.exe拷贝到自定义的文件夹内:我这里用的是D:/jad/ ...

  2. SharePoint中取得ACL和组中用户数量

     SharePoint中取得ACL和组中用户数量 1. 取得ACL的数量: select COUNT(ra.PrincipalId) as [Count],p.ScopeUrl from [WSS_C ...

  3. IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12

    IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12 学习了:https://blog.csdn.net/sjzylc/article/details/47979815

  4. VB6 如何连接MYSQL数据库

    1 从官网下载MYSQL的ODBC,选择与自己操作系统对应的版本(前提是你安装了MYSQL) http://dev.mysql.com/downloads/connector/odbc/   2 安装 ...

  5. Mapreduce运行过程分析(基于Hadoop2.4)——(三)

    4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()->ReduceTask.run().ReduceTask.run方 ...

  6. 我的第一个开源控件-DragGridView

    我的第一个开源控件出炉了,希望各个小伙伴给个star,支持下.项目地址 1. 前言 因为项目须要,要做一个相似腾讯视频.频道管理.拖拽排序的效果.这个控件是在原地址 之上改造出来的.先看下效果图. 1 ...

  7. BootstrapValidator demo

    source:http://bv.doc.javake.cn/api/ BootstrapValidator is the best jQuery plugin to validate form fi ...

  8. 谈一谈以太坊虚拟机EVM的缺陷与不足

    首先,EVM的设计初衷是什么?它为什么被设计成目前我们看的样子呢?根据以太坊官方提供的设计原理说明,EVM的设计目标主要针对以下方面: 简单性(Simplicity) 确定性(Determinism) ...

  9. Webdriver中关于driver.navigate().to()和driver.get()使用的区别

    先是有一个父页上button弹开一个子页,总共有4个子页,必须前一个页上的必填信息录完,才能在这个页面触发下一个子页. 用driver.navigate().to(baseUrl2),直接跳转到第2个 ...

  10. Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)——无非是在传统遍历过程中修改叶子结点加入后继结点信息(传统是stack记录),然后再删除恢复

    先看看线索二叉树 n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域.利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索 ...