题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1698

题目:

Problem Description
 
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

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.

 
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
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.
 
Output
 
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 
Sample Input
 
1
10
2
1 5 2
5 9 3
 
Sample Output
 
Case 1:The total value of the hook is 24.

思路:

懒惰标记法,延迟更新。没学过这个知识点的,可以先学习一下。网上有篇介绍该知识点的博客:http://blog.csdn.net/zip_fan/article/details/46775633

代码:

 #include <cstdio>
const int N=4e5;
struct node{
int l,r;
int lazy,sum;
}tree[N];
int n;
void pushup(int i){//更新父节点
tree[i].sum=tree[(i*)+].sum+tree[i*].sum;
}
void pushdown(int i){//更新子节点
if(tree[i].lazy){
tree[*i].lazy=tree[*i+].lazy=tree[i].lazy;//将子节点也懒惰标记
tree[*i].sum=(tree[*i].r-tree[*i].l+)*tree[*i].lazy;//sum会等于长度值*标记值
tree[*i+].sum=(tree[*i+].r-tree[*i+].l+)*tree[*i+].lazy;
tree[i].lazy=;//更新完,取消该节点的标记
}
}
void build(int bg,int ed,int i){
if(i>*n) return;
tree[i].l=bg;
tree[i].r=ed;
tree[i].lazy=;//多个测试样例,注意初始化
if (bg == ed) tree[i].sum=;
else{
int mid=(bg+ed)/;
build(bg, mid, *i);
build(mid+, ed, *i+);
pushup(i);//回溯更新父节点 }
}
void update(int bg,int ed,int i,int v){
if(bg<=tree[i].l && tree[i].r<=ed){
tree[i].lazy=v;
tree[i].sum=(tree[i].r-tree[i].l+)*v;
return ;
}
pushdown(i);//当用到该节点时,就向下更新
int mid=(tree[i].r+tree[i].l)/;
if(ed<=mid) update(bg, ed, *i, v);//该区间完全在左子树
else if(bg>mid) update(bg, ed, *i+, v);//该区间完全在右子树
else if(bg<=mid && ed>mid){//既在左子树又在右子树
update(bg, mid, *i, v);
update(mid+, ed, *i+, v);
}
pushup(i);//回溯更新父节点
}
int main(){
int t,m;
scanf("%d",&t);
for (int i=; i<=t; i++) {
scanf("%d%d",&n,&m);
build(, n, );
for (int j=; j<m; j++) {
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
update(x, y, , v);
}
printf("Case %d: The total value of the hook is %d.\n",i,tree[].sum);
}
return ;
}

HDU 1698 Just a Hook(线段树成段更新)的更多相关文章

  1. HDU 1698 Just a Hook(线段树成段更新)

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

  2. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  3. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  4. hdu698 Just a Hook 线段树-成段更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 很简单的一个线段树的题目,每次更新采用lazy思想,这里我采用了增加一个变量z,z不等于0时其绝 ...

  5. 线段树---成段更新hdu1698 Just a Hook

    hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...

  6. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  7. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

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

  9. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  10. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. spring 事务无效解决方法

    (原) spring 事务目前有二种,注解式和声明式,以前都是以公司里的框架写好的,没有学习的机会,今天抽空好好试了下,结果遇到好多问题. 1.注解式 最开始是这么玩的,发现数据进数据库了,没有起作用 ...

  2. POJ 3685 二分套二分

    Matrix Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that e ...

  3. Redis 小白指南(三)- 事务、过期、消息通知、管道和优化内存空间

    Redis 小白指南(三)- 事务.过期.消息通知.管道和优化内存空间 简介 <Redis 小白指南(一)- 简介.安装.GUI 和 C# 驱动介绍> 讲的是 Redis 的介绍,以及如何 ...

  4. ASP.NET MVC5(一):ASP.NET MVC概览

    ASP.NET MVC概览 ASP.NET MVC是一种构建Web应用程序的框架,它将一般的MVC(Model-View-Controller)模式应用于ASP.NET框架. 1.ASP.NET MV ...

  5. 02-2--数据库MySQL:DDL(Data Definition Language:数据库定义语言)操作数据库中的表(二)

    DDL对数据库的操作:http://blog.csdn.net/baidu_37107022/article/details/72334560 DDL对数据库中表的操作 1)方法概览 2)演示 //创 ...

  6. DELPHI XE8 远程调试

    最近公司项目遇到问题需要远程调试搜索了一下怎么用 发现网上能找到最新的是XE2上的说明现在已经有一些不同了 按照上面的方法不能调试成功 经过测试XE8的方法如下:1.项目编译设置:2.在被调试电脑上运 ...

  7. 加载jquery插件注意了

    1.尽量放在</body>之前,不要放在</head>标签之前,如果执意要放也要放在css之后,例如: <link href="style.css" ...

  8. PHP·笔记(函数总结)

    PHP 指 PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名) PHP 是一种服务器端的脚本语言,类似 ASP PHP 脚本在服务器上执行 PHP 支持 ...

  9. [BZOJ2707]走迷宫

    Description Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿 ...

  10. ssh别名登录密钥登录

    在centos上使用别名和是用密钥登录: vim /root/.ssh/config  #输入下列内容 Host * User root   #以root登录 ServerAliveInterval ...