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. 梯度下降法&牛顿法

    梯度下降法 在机器学习任务中,需要最小化损失函数\(L(\theta)\),其中\(\theta\)是要求解的模型参数.梯度下降法是一种迭代方法,用到损失函数的一阶泰勒展开.选取初值\(\theta ...

  2. C3P0连接池的配置与使用

    1.下载c3p0-0.9.1.2.jar 下载地址:http://download.csdn.net/detail/chunxiaqiudong5/9661922 2.添加配置文件c3p0-confi ...

  3. redis 安装 配置 及启动

    linux下安装redis及其中遇到的问题的解决方法1.将下载好的压缩包放到/usr/local目录下# tar xzf redis-3.0.2.tar.gz# cd redis-3.0.2# mak ...

  4. ionic3 监听软键盘的高度

    ionic1 和普通cordova的大家都知道 就是看ionic3 和4 https://blog.csdn.net/sean_css/article/details/70243893 ionic c ...

  5. Windows 安装Redis程序

    一.系统环境 1.硬件系统:Windows7 64位 2.软件环境: Redis 64位 3.2.100.Redis Desktop Manager. 二.Redis安装 下载地址:https://g ...

  6. 关于pythond在终端中运行

    下载python并安装后,如果想要在终端中直接运行,我们需要配置环境变量. 在计算机右击选择属性,,选择高级属性,点击环境变量,,即可新建环境变量, ,然后可以在终端中运行python解释器.

  7. 数据采集与分析的那些事——从数据埋点到AB测试

    作者:网易有数郑栋. 一.为什么企业需要一套完善的用户行为埋点和分析平台 产品初创期间,需要分析天使用户的行为来改进产品,甚至从用户行为中得到新的思路或发现来调整产品方向:产品成长过程,通过对用户行为 ...

  8. 树莓派GPIO控制LED彩灯

    树莓派使用GPIO接口来控制LED灯,自制五彩炫光的节日彩灯. 1.硬件准备 a. 树莓派(Raspberry Pi)一个 b. 彩色RGB二极管 c. 杜邦线 d. 5V电源引脚 以上所有零件均可在 ...

  9. C语言编程学习不难学,是你没找对方法!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  10. 20155220java实验二 面向对象程序设计 实验报告

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 (一)单元测试 (1) 三种代码 伪代码 产 ...