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

Sample Input

Sample Output
Case : The total value of the hook is .

题意:

  输入T,表示T组样例,输入n,表示有n个节点,最初所有点的值为1,然后给定X、Y、Z,将从X到Y改为权值为Z,计算权值和。

思路:

  线段树区间更新的模板。

代码:

#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
int l,r;
int w;
}tree[];
void build(int l,int r,int n)
{
tree[n].l = l;
tree[n].r = r;
tree[n].w = ;
if(l == r)
return ;
int temp = (l+r)/;
build(l,temp,*n);
build(temp+,r,*n+);
}
void update(int l,int r,int w,int n)
{
if(tree[n].l == l&&tree[n].r == r)
{
tree[n].w = w;
return;
}
if(tree[n].w!=-)//将值更改为-1,代表这个区间内的值都已经更改
{
tree[*n].w = tree[*n+].w = tree[n].w;
tree[n].w = -;
}
int temp=(tree[n].l+tree[n].r)/;
if(r <= temp)
update(l,r,w,*n);
else if(l>temp)
update(l,r,w,*n+);
else
{
update(l,temp,w,*n);
update(temp+,r,w,*n+);
}
}
int find(int n)//查找
{
if(tree[n].w != -)//如果节点不为-1,表示这个区间内的值都是tree[n].w
{
return (tree[n].r-tree[n].l+)*tree[n].w;
}
else//如果节点为-1,递归下去。
{
return find(*n)+find(*n+);
}
}
int main()
{
int T;
scanf("%d",&T);
for(int i=; i<=T; i++)
{
int n;
scanf("%d",&n);
build(,n,);
int p;
scanf("%d",&p);
while(p--)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
update(a,b,w,);
}
printf("Case %d: ",i);
printf("The total value of the hook is %d.\n",find());
}
}

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

  1. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  2. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

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

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

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

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

  5. [HDU] 1698 Just a Hook [线段树区间替换]

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

  6. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  7. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

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

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

  9. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  10. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. Islands and Bridges(POJ2288+状压dp+Hamilton 回路)

    题目链接:http://poj.org/problem?id=2288 题目: 题意:求Hamilton 路径权值的最大值,且求出有多少条权值这么大的Hamilton路径. 思路:状压dp,dp[i] ...

  2. 学习webpack

    前言 webpack前端工程中扮演的角色越来越重要,它也是前端工程化很重要的一环.本文将和大家一起按照项目流程学习使用wbepack,妈妈再也不用担心我不会使用webpack,哪里不会看哪里.这是一个 ...

  3. 购物车中的input输入框只能输入数字和输入为0的时候默认为1

    <input type="text" value="1" onkeyup="value=(parseInt((value=value.repla ...

  4. ThinkPHP的输出和模型使用

    1.假设在v层需要输出一个变量怎么办呢?即如同在html当中输出php代码. 可以直接使用{$name}代替.花括号被称之为标识符.可以通过修改配置项('TMPL_L_DELIM'=>'< ...

  5. APP版本号记录

    VoLTE版本: VT_BV0800V1.0.0B06 800M版本: NETARTIST_BV0800V1.0.0B01 看详细版本号:9831275#

  6. 使用常见的网络命令查看当前网络状态——Mac OS X篇

    转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...

  7. django入门--django-blog-zinnia搭建个人博客

    1.安装python 选择合适python2.7及以上版本安装https://www.python.org/downloads/ 2.建立虚拟环境 这不是必须的,但是建议使用,为每个项目单独引入依赖, ...

  8. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  9. <node.js爬虫>制作教程

    前言:最近想学习node.js,突然在网上看到基于node的爬虫制作教程,所以简单学习了一下,把这篇文章分享给同样初学node.js的朋友. 目标:爬取 http://tweixin.yueyishu ...

  10. JavaScript 七种数据类型

    在 JavaScript 规范中,共定义了七种数据类型,分为 “基本类型” 和 “引用类型” 两大类,如下所示: 基本类型:String.Number.Boolean.Symbol.Undefined ...