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. js父页面和子页面相互取值

    iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...

  2. 【洛谷 P2604】 [ZJOI2010]网络扩容(最大流,费用流)

    题目链接 第一问就是简单的最大流. 第二问,保留第一问求完最大流的残量网络. 然后新建一个源点,向原源点连一条流量为k,费用为0的边. 然后所有边重新连一起(原来的边保留),费用为题目所给,最小费用即 ...

  3. node的简单爬虫

    最近在学node,这里简单记录一下. 首先是在linux的环境下,关于node的安装教程:   https://github.com/alsotang/node-lessons/tree/master ...

  4. 阿里分布式开源框架DUBBO 入门+ 进阶+ 项目实战视频教程

    史诗级Java/JavaWeb学习资源免费分享 欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回 ...

  5. Google Gapps – Download Gapps for Android【转】

    http://wiki.rootzwiki.com/Google_Apps http://productforums.google.com/forum/#!forum/apps http://www. ...

  6. shell脚本执行方式

    # BY THE WAY, 其实这块内容算是比较简单的,但是都比较常记得它最基本的两种方式,另外两种却忘记了 1. 利用sh或bash命令执行 sh test.sh bash test.sh 2. 在 ...

  7. Ubuntu下安装Python3.6并在终端输入Python就能显示Python3.6

      Ubuntu17.04自带Python2.7与Python3.5.3的版本,由于Python2与Python3有着一些差距可能需要安装更新Python3的版本,并且切换默认的Python解释器. ...

  8. untiy3d学习笔记

    Unity3d 记录 1.63讲 主要讲了menicam 从3D软件里面导出过后,注意如果是人物模型命名一定要非常清晰并且对称.选择到模型后等到到humanoid后可以使用menicam.然后使用me ...

  9. java基础9 main函数、this、static、super、final、instanceof 关键字

    一.main函数详解 1.public:公共的.权限是最大的,在任何情况都可以访问  原因:为了保证jvm在任何情况下都可以访问到main法2.static:静态,静态可以让jvm调用更方便,不需要用 ...

  10. CPU运行时间——time

    用途说明time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统时间的(这是date命令干的事情).但是今天我通过查看time命令的手册页,发现它能做的不仅仅是测量运行时间,还可以测量内存 ...