hdu1698 Just a Hook 线段树:成段替换,总区间求和
转载请注明出处:http://blog.csdn.net/u012860063
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
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.
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.
1
10
2
1 5 2
5 9 3
Case 1: The total value of the hook is 24.
题意:题意:有t组測试数据,n为钩子长度(1<=n<=100000),m为操作的次数。初始时,每一个钩子的价值为1,操作由三个数字组成x,y,z表示把区间[x,y]的钩子变成的价值变成z(1代表铜,2银,3金)。
代码例如以下:
//线段树功能:update:成段替换 (因为仅仅query一次总区间,所以能够直接输出1结点的信息) #include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
const int maxn = 111111;
//maxn是题目给的最大区间,而节点数要开4倍,确切的来说节点数要开大于maxn的最小2x的两倍
int col[maxn<<2];
int sum[maxn<<2];
void PushUp(int rt) //把当前结点的信息更新到父结点
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m)//把当前结点的信息更新给儿子结点
{
if (col[rt])
{
col[rt<<1] = col[rt] ;
col[rt<<1|1] = col[rt];
sum[rt<<1] = col[rt] * (m - (m >> 1));
sum[rt<<1|1] = col[rt] * (m >> 1);
col[rt] = 0;
}
}
void build(int l,int r,int rt)
{
col[rt] = 0;
sum[rt] = 1;//初始化每一个节点为1
if (l == r)
{
//scanf("%lld",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
col[rt] = c;
sum[rt] = c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
if (L <= m)
update(L , R , c , lson);
if (m < R)
update(L , R , c , rson);
PushUp(rt);
}
/*int query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
PushDown(rt , r - l + 1);
int m = (l + r) >> 1;
int ret = 0;
if (L <= m)
ret += query(L , R , lson);
if (m < R)
ret += query(L , R , rson);
return ret;
}*/
int main()
{
int N , Q ,T , K=0;
int a , b , c;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&Q);//N为节点数
build(1 , N , 1);//建树
while (Q--)//Q为询问次数
{
/* char op[2];
int a , b , c;
scanf("%s",op);
if (op[0] == 'Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a , b , 1 , N , 1));
}
else
{
scanf("%d%d%d",&a,&b,&c);//c为区间a到b添加的值
update(a , b , c , 1 , N , 1);
}*/
scanf("%d%d%d",&a,&b,&c);//c为区间a到b的改变值
update(a , b , c , 1 , N , 1); }
printf("Case %d: The total value of the hook is %d.\n",++K,sum[1]);
}
return 0;
}
hdu1698 Just a Hook 线段树:成段替换,总区间求和的更多相关文章
- UESTC-1057 秋实大哥与花(线段树+成段加减+区间求和)
秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 1698 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 (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- hdu698 Just a Hook 线段树-成段更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 很简单的一个线段树的题目,每次更新采用lazy思想,这里我采用了增加一个变量z,z不等于0时其绝 ...
- POJ 3225.Help with Intervals-线段树(成段替换、区间异或、简单hash)
POJ3225.Help with Intervals 这个题就是对区间的各种操作,感觉这道题写的一点意思都没有,写到后面都不想写了,而且更神奇的是,自己的编译器连结果都输不出来,但是交上就过了,也是 ...
- poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)
题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...
- poj_3468线段树成段更新求区间和
#include<iostream> #include<string.h> #include<cstdio> long long num[100010]; usin ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- 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. T ...
随机推荐
- OpenCV 开发环境环境搭建(win10+vs2015+opencv 3.0)
OpenCV 3.0 for windows(下载地址:http://opencv.org/): 本测试中,OpenCV安装目录:D:\Program Files\opencv,笔者操作系统为64位. ...
- linux定时任务crontab的设置
linux定时任务crontab的设置http://www.blogjava.net/freeman1984/archive/2010/09/23/332715.html vi /etc/cronta ...
- 在数组中找几个数的和等于某个数[LeetCode]
首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...
- php多图合并
function mergerImg($imgs) { list($max_width, $max_height) = getimagesize($imgs['dst']); $dests = ima ...
- 5.PHP 教程_PHP echo/print
PHP echo 和 print 语句 echo和print区别: echo-可以输出一个或多个字符串 print-只允许输出一个字符串,返回值总为1 提示:echo输出的速度比print快,echo ...
- c语言(3)--运算符&表达式&语句
计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * / % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...
- C++获取当前机器内网IP地址
/*头文件*/ #include "winsock2.h" #pragma comment(lib,"ws2_32.lib") /*Hui 获取当前服务器IP* ...
- JAVA堆与栈
数据类型: Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身 ...
- xp的停止更新对我们有什么影响?
微软与2001年推出windows xp系统,这款系统的成功毋庸置疑,但由于太过成功,微软在随后推出的vista系统和win7系统普及起来却异常困难.大多数人已经习惯了xp的操作,再加上一批铁杆旧电脑 ...
- 转:在控制台中调试AngularJS应用
在控制台中调试AngularJS应用 在创建AngularJS应用时,一个很棘手的问题是如何在Chrome,Firefox,以及IE的JavaScript控制台中访问深藏在应用中的数据和服务.本文将会 ...