题目连接:

  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. vue-alioss-组件封装

    <template> <div class="vui_alioss_upload"> <div @click="uloadImg()&quo ...

  2. Python中文GBK编码解决实例

    http://eatsalt.blog.163.com/blog/static/879402662009420508748/ #coding:gbk l=['我'.decode('gbk'),'我'. ...

  3. 【转】c++ 如何批量初始化数组 fill和fill_n函数的应用

    http://blog.csdn.net/sunquana/article/details/9153213 一. fill和fill_n函数的应用: fill函数的作用是:将一个区间的元素都赋予val ...

  4. vue + vue-lazyload 实现图片懒加载

    1.安装 npm i vue-lazyload -S 2.配置 main.js /***图片模板等懒加载 start ***/ import VueLazyload from 'vue-lazyloa ...

  5. 使用literal语法格式化字符串

    支持arm64之后,格式化字符串的时候会遇到一些问题,主要与NSInteger的定义有关: #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET ...

  6. LoadRunner中存储表格参数------关联数组

    主要用到 web_reg_save_param_ex函数("Scope=All",), sprintf( CProdNo,"{CProdNo_%d}",i ); ...

  7. seajs载入流程图

    近期读seajs源代码,整理出了主要逻辑的流程图(注意:是逻辑示意图).感兴趣的朋友能够看看,欢迎批评指正~ http://www.gliffy.com/go/publish/image/607216 ...

  8. 自己写的Android端HttpUtil工具类

    package com.sxt.jcjd.util; import java.io.IOException; import java.io.UnsupportedEncodingException; ...

  9. net start sshd 发生系统错误1069--cygwin安装过程

    net start sshd 发生系统错误1069 解决方法: services.msc调出服务,然后CYGWIN sshd服务->属性,修改账户的名字和密码(win7的登录名和密码) 可能还遇 ...

  10. scala wordcount kmeans

    scala wordcount   kmeans k-means算法的输入对象是d维向量空间的一些点,对一个d维向量的点集进行聚类. k-means聚类算法会将集合D划分成k个聚簇.