题目连接:

  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 ;
}

  

最新文章

  1. Document树的解析方法
  2. Bounce.js – 快速创建漂亮的 CSS3 动画效果
  3. AOP常用术语
  4. [转]关于NSAutoreleasePool&#39; is unavailable: not available in automatic reference counting mode的解决方法
  5. 挑战编程PC/UVa Stern-Brocot代数系统
  6. lifecycle of opensource products--x86-64
  7. NOIP 2013 提高组 day1 T2 火柴排队 归并 逆序对
  8. lua堆栈操作常用函数学习二
  9. IOS 学习笔记 2015-03-18
  10. ASP.NET MVC企业开发的基本环境
  11. 申请JetBrains学生免费注册码
  12. ●BZOJ 3527 [Zjoi2014]力
  13. (转)Vue种key的作用
  14. Windows 后台执行jar
  15. pygame 笔记-4 代码封装&amp;发射子弹
  16. 11、JPA-JPQL
  17. Revit API创建标注NewTag
  18. github优秀前端项目分享(转)
  19. Hadoop概念学习系列之关于hadoop-2.2.0和hadoop2.6.0的winutils.exe、hadoop.dll版本混用(易出错)(四十三)
  20. tyvj 创世纪 - 基环树

热门文章

  1. 【APUE】进程间通信之信号量
  2. ubuntu磁盘分区和挂载
  3. 跟着9张思维导图学JavaScript
  4. [Javascript] Use JavaScript&#39;s for-in Loop on Objects with Prototypes
  5. 从JVM的角度看JAVA代码--代码优化
  6. Vs2013在Linux开发中的应用(19): 启动gdb
  7. Linux - Ubuntu中文输入法安装(Ubuntu 12.04)
  8. [IT学习]Python 小项目 通讯录 思路
  9. Chapter1-data access reloaded:Entity Framework(下)
  10. MVC优缺点