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 ...
 
随机推荐
- js发送post请求下载文件
			
大家都知道ajax是不能直接下载文件的,所以一般都是通过一个超链接的形式去下载一个文件 但是当牵扯到需要发送很多数据到服务器上再下载的时候超链接的形式就有些太过勉强了 如下是一个工具方法(依赖jque ...
 - c# 遍历文件夹及其所有文件
			
利用VS创建一个winform应用程序,遍历指定文件夹(photos)内的所有文件夹及其文件.具体程序如下: namespace 遍历文件夹及其所有文件 { public partial class ...
 - 入Lucene的第一个坑
			
兴致勃勃的下载了Lucene6的Jar包,打算跑个Demo看下它神奇的魅力,结果一运行就出错了 Exception in thread "main" java.lang.Unsup ...
 - C++数组与指针
			
指向数组元素的指针 一个变量有地址,一个数组包含若干元素,每个数组元素都在内存中占用存储单元,它们都有相应的地址.指针变量既然可以指向变量,当然也可以指向数组元素(把某一元素的地址放到一个指针变量中) ...
 - #pragma pack(push,1)与#pragma pack(pop)
			
这是给编译器用的参数设置,有关结构体字节对齐方式设置, #pragma pack是指定数据在内存中的对齐方式. #pragma pack (n) 作用:C编译器将按照n个字节对 ...
 - C++ const 限定符
			
C++ const 限定符 作用:把一个对象转换成一个常量 用法:const type name = value; 性质:1. 定义时必须初始化,定义后不能被修改.2. 类中的const成员变量必须通 ...
 - codility上的练习(5)
			
codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O ...
 - iOS现有工程 集成 Cordova/Ionic
			
首先, 新建 Cordova 项目就不说了, 步骤: http://ionicframework.com/getting-started/ , cordova生成的项目用cdv_project称呼, ...
 - word-break与word-wrap
			
本文列举了兼容 IE 和 FF 的换行 CSS 推荐样式,详细介绍了word-wrap同word-break的区别. 兼容 IE 和 FF 的换行 CSS 推荐样式 最好的方式是 以下是引用片段: ...
 - HighCharts学习
			
http://www.stepday.com/topic/?369 http://www.helloweba.com/view-blog-156.html