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.

思路:

0:代表线段被覆盖了。

1,2,3:分别代表值。

先建立线段树:

【1,10】

1

/                       \

【1,5】                   【6,10】

1                           1

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

1            1                 1           1

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1       1       1     1         1       1      1      1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

1        1                        1        1

插入1 5 2后:

【1,10】

/                       \

【1,5】                   【6,10】

1

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

1            1                 1           1

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1      1        1     1          1      1       1       1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

插入5 9 3后:

【1,10】

/                       \

【1,5】                   【6,10】

/       \                  /         \

【1,3】    【4,5】            【6,8】   【9,10】

0

/    \        /    \            /     \      /     \

【1,2】 【3,3】【4,4】【5,5】    【6,7】【8,8】【9,9】【10,10】

1                             1       1

/     \                          /      \

【1,1】 【2,2】                  【6,6】  【7,7】

1         1                        1        1

将红色的值加起来就是答案24了。

#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = ;
int n;//hooks的个数
int q;//operations的个数
struct Node
{
int l;//左端点
int r;//右端点
int v;//价值
}tree[MAXN * ];
void BuildTree(int t,int l,int r)
{
tree[t].l = l;
tree[t].r = r;
tree[t].v = ;
if (l == r)//为叶节点
return ;
BuildTree(t + t,l,(l + r) / );//建立左子树
BuildTree(t + t + ,(l + r) / + ,r);//建立右子树
}
void Update(int t,int l,int r,int v)
{
if (tree[t].l == l && tree[t].r == r)//区间匹配
{
tree[t].v = v;//将value覆盖
return ;
}
if (tree[t].v == v)//如果值相同,就没必要更改
return ;
if (tree[t].v > )//区间tree[t].l到tree[t].r的值要更改
{
tree[t + t + ].v = tree[t + t].v = tree[t].v;
tree[t].v = ;
}
if (r <= (tree[t].l + tree[t].r) / )//在左孩子
Update(t + t,l,r,v);
else if (l > (tree[t].l + tree[t].r) / )//在右孩子
Update(t + t + ,l,r,v);
else//横跨中点
{
Update(t + t,l,(tree[t].l + tree[t].r) / ,v);
Update(t + t + ,(tree[t].l + tree[t].r) / + ,r,v);
}
}
int GetValue(int t)
{
int ans = ;
if (tree[t].v > )
ans += (tree[t].r - tree[t].l + ) * tree[t].v;
else
{
ans += GetValue(t + t);
ans += GetValue(t + t + );
}
return ans;
}
int main(void)
{
int t,x,y,z,i = ;
scanf("%d",&t);
while (t --)
{
scanf("%d",&n);
BuildTree(,,n);//建立线段树
scanf("%d",&q);
while (q --)
{
scanf("%d%d%d",&x,&y,&z);
Update(,x,y,z);//更新区间的值
}
printf("Case %d: The total value of the hook is %d.\n",i ++,GetValue());//得到value的总值
}
return ;
}

G - Just a Hook的更多相关文章

  1. Flask初学者:g对象,hook钩子函数

    Flask的g对象 作用:g可以可以看作是单词global的缩写,使用“from flask import g”导入,g对象的作用是保存一些在一次请求中多个地方的都需要用到的数据,这些数据可能在用到的 ...

  2. vue.js中英文api

    全局配置 Vue.config is an object containing Vue's global configurations. You can modify its properties l ...

  3. Storyboards Tutorial 03

    这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...

  4. 文件图标SVG

    ​<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...

  5. GET/POST/g和钩子函数(hook)

    GET请求和POST请求: 1. get请求: * 使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求. * 传参:get请求传参是放在url中,并且是通过`?`的 ...

  6. [MetaHook] Surface hook

    Hook ISurface function. #include <metahook.h> #include <vgui/ISurface.h> using namespace ...

  7. 理解钩子Hook以及在Thinkphp下利用钩子使用行为扩展

    什么是钩子函数 个人理解:钩子就像一个”陷阱”.”监听器”,当A发送一个消息到B时,当消息还未到达目的地B时,被钩子拦截调出一部分代码做处理,这部分代码也叫钩子函数或者回调函数 参考网上说法 譬如我们 ...

  8. Linux LSM(Linux Security Modules) Hook Technology

    目录 . 引言 . Linux Security Module Framework Introduction . LSM Sourcecode Analysis . LSMs Hook Engine: ...

  9. c c++ 函数入口和出口的hook(gcc 编译选项),然后打印出函数调用关系的方法

    GCC Function instrumentation机制可以用来跟踪函数的调用关系,在gcc中对应的选项为“-finstrument-functions”.可查看gcc的man page来获取更详 ...

随机推荐

  1. 关于javascript模式一书中var white = new Array(256).join(“ ”)

    直接进入正题 var white = new Array(256).join(" ") 运行后,我们会发现white.length的长度是255,这个是为什么呢?书上没有给出解答, ...

  2. mysql统计一张表中条目个数的方法

    统计一张表中条目的个通常的SQL语句是: select count(*) from tableName; #or ) from tableName; #or 统计一个列项,如ID select cou ...

  3. CodeForces 705C Thor (模拟+STL)

    题意:给定三个操作,1,是x应用产生一个通知,2,是把所有x的通知读完,3,是把前x个通知读完,问你每次操作后未读的通知. 析:这个题数据有点大,但可以用STL中的队列和set来模拟这个过程用q来标记 ...

  4. ssi整合报错org.apache.struts2.convention.ConventionsServiceImpl.determineResultPath(ConventionsServiceImpl.java:100)

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

  5. curl用法

    简介 curl是一个和服务器交互信息(发送和获取信息)的命令行工具,支持DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, L ...

  6. 自己学会汉化DevExpress控件[转]

    1. 文档导读 本文档以颜色区分内容的重要性和不同性,阅读本文档时请注意以下事项: 1. 红色部分表示需要注意的重点内容:(加粗的尤甚) 2. 蓝色部分表示相应于前版本新增的内容: 3. 紫色部分表示 ...

  7. HTML第一天学习笔记

  8. Parallax Occlusion Mapping

    如上图,本来是采样original texture coordinates点的颜色,其实却采样了correcter texture coordinates点的颜色. 而且会随着视线的不同看到凹凸程度变 ...

  9. 在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

    使用这个框架可以极大的简化在开发Android过程中的代码.提高开发的效率.这里简单说一下配置方式.和使用办法. 项目的地址为:AndroidAnnotations Jar包下载地址:3.0.1 下载 ...

  10. TFS代码签入指导

    1. 如果文件没有被放入到TFS中, 那么它是不存在的. 这一点是最好被理解的, 如果你的代码没有被签入到代码管理中,那么就不可能被团队的其他人获取的得到. 具体如何将文件纳入到TFS中请参考 Pla ...