E - Just a Hook

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.

题目描述:t组数据,输入n,q,从1~n初值为1,q次修改操作,求最后的所有值的和,都写在代码里了,

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 100010;
int T, n, k, l, r, v, lazy[maxn * 4]; struct node {
int l, r, value; // value代表是区间和
}t[maxn * 4]; void pushup(int p) {
t[p].value = t[p << 1].value + t[p << 1 | 1].value; //节点更新
} void built(int p, int l, int r) { // p当前节点 l区间左值 r区间右值
t[p].l = l;
t[p].r = r;
if (l == r) {
t[p].value = 1; //按照题目要求 初始值为1
return;
}
int mid = l + r >> 1;
built(p << 1, l, mid); //建左儿子
built(p << 1 | 1, mid + 1, r); //建右儿子
pushup(p); //更新当前节点
} void pushdown(int p, int l, int r) { // p当前节点 l区间左值 r区间右值
if (lazy[p]) {
int mid = l + r >> 1;
lazy[p << 1] = lazy[p << 1 | 1] = lazy[p]; //给儿子打上标记
t[p << 1].value = (mid - l + 1)*lazy[p]; //更新左儿子
t[p << 1 | 1].value = (r - mid)*lazy[p]; //更新右儿子
lazy[p] = 0; //去掉当前点标记
}
} void update(int p, int l, int r, int v) { //p当前节点 l修改区间的左值 r修改区间的右值 v修改数值
if (l <= t[p].l&&t[p].r <= r) {
lazy[p] = v; //打上标记,不用继续向下走了
t[p].value = v*(t[p].r - t[p].l + 1); //注意当前节点的value 当前节点下有(t[p].r - t[p].l + 1)个叶子,
//每个叶子都要变成v,所以当前节点就是(叶子数)*v
return;
}
pushdown(p, t[p].l, t[p].r); //不满足,向下一层打标记
int mid = t[p].l + t[p].r >> 1;
if (l <= mid)update(p << 1, l, r, v); //更新左儿子
if (r>mid)update(p << 1 | 1, l, r, v); //更新右儿子
pushup(p);
} int main() {
scanf("%d", &T);
for (int i = 1; i <= T; i++) {
memset(lazy, 0, sizeof(lazy)); //不要忘记初始化!!! scanf("%d%d", &n, &k);
built(1, 1, n);
while (k--) {
scanf("%d%d%d", &l, &r, &v);
update(1, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n", i, t[1].value);
}
return 0;
}

Just a Hook:线段树+区间修改的更多相关文章

  1. HDU1698Just a Hook(线段树 + 区间修改 + 求和)

    题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...

  2. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

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

  3. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  4. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  5. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  6. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

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

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

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

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

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

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

    Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...

随机推荐

  1. 2019 wishes

    1. 永恒目标:爱自己,依靠自己,全家人身心健康. 2. 投稿4篇+,发表2+,不管什么刊物,书稿交给出版社.//改动一下,尽量发高质量杂志和期刊. 3. 带着儿子一起学习怎么和别人主动打招呼,做个有 ...

  2. 生成Html 测试报告

    1.引入HTMLTestRunner 下载HTMLTestRunner.py 从http://tungwaiyip.info/software/HTMLTestRunner.html 将下载的HTML ...

  3. Linux -- 目录基本操作(1)

    cd 切换目录 1.切换到指定目录下 #cd 相对/绝对目录 [root@localhost ~]# cd /home/tom/demo [root@localhost demo]# 2.切换到某个用 ...

  4. Web—10-前端性能优化

    前端性能优化 从用户访问资源到资源完整的展现在用户面前的过程中,通过技术手段和优化策略,缩短每个步骤的处理时间从而提升整个资源的访问和呈现速度.网站的性能直接会影响到用户的数量,所有前端性能优化很重要 ...

  5. jdk1.8换成1.7

    电脑中装了jdk1.7,然后又装了1.8, 后来项目需要1.7,就把path环境变量中的java_home改成了1.7. 然后控制台输入java_version,后提示如下: Error: Regis ...

  6. c#的传输组件dotnetty

    牛皮不多了,绩效吹起.... 最近一直看大家写的东西,了解的内容不少,我的牛皮也差不多吹完了.... 最后在说说最近测试的dotnetty.去年弄下来试了,不行,最近又弄下来了看看,可以了.哇哈哈哈哈 ...

  7. 非空校验在oracle和mysql中的用法

    oracle判断是否为null nvl(参数1,参数2) :如果参数1为null则返回参数2,否则返回参数1 mysql判断是否为null ifnull(参数1,参数2) :如果参数1为null则返回 ...

  8. previewImage.js图片预览缩放保存插件

    previewImage.js好用的图片预览缩放保存插件

  9. Python学习笔记十一:模块

    标准库(内置模块) time与datetime 模块 时间表示方式 1.时间戳 2.格式化的字符串 3.元组形式 时间戳就是一个秒数 x=time.time(),从1970年1月1日0时0分0秒到现在 ...

  10. SSL&TlS握手

    SSL/TLS简介 •SSL:安全套接字层(secure socket layer) •TLS:传输层安全协议(transport layer security) SSL和TLS都是加密协议,旨在基于 ...