传送门:Just a Hook

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 Input
1 10 2 1 5 2 5 9 3
Sample Output
Case 1: The total value of the hook is 24.

题意:

输入一个n表示一段长度为n的区间,有n个编号为1~n的点,初始值全部为1。 有q个操作, 每个操作有3个数:l,r,v表示将区间l~r的所有元素修改为v。

题解:

因为修改很多值, 如果还是按照原来的更新方法, 每个结点更新一次的话,速度实在太慢。 一个点一个点的更新之所以慢 , 是因为每个被该点影响的点我们都需要更新。   为了能”顺便“更新, 我们在每个结点上多维护一个信息, 表示上次该区间修改的值是多少,然后然后每次向下更新之前将标记更新到儿子结点。

代码:

#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO                     \ios::sync_with_stdio(false); \cin.tie(0);                  \cout.tie(0);
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll mod = 1000000007LL;
const int N = 1 << 19;
ll dat[N],mark[N];
void build(int l, int r, int k) {dat[k] = mark[k] = 0;if(l==r) {dat[k] = 1; mark[k] = 0; return ;}build(l, (l+r)/2, k<<1);build((l+r)/2+1, r, k<<1|1);dat[k] = dat[k<<1] + dat[k<<1|1];
}
void down(int k,int len) {if(mark[k]) {dat[k<<1] = mark[k] * (len - len/2);dat[k<<1|1] = mark[k] *(len/2);mark[k<<1] = mark[k<<1|1] = mark[k];mark[k] = 0;}
}
void update(int a, int b, int k, int l, int r, int c) {if(a <= l && b >= r) {dat[k] = c * (r-l+1);mark[k] = c;}else if(a <= r && b >= l){down(k,r-l+1);update(a,b,k<<1,l,(l+r)/2,c);update(a,b,k<<1|1,(l+r)/2+1,r,c);dat[k] = dat[k<<1] + dat[k<<1|1];}
}
int main() {int T,n,q;scanf("%d",&T);_for(k, 1, T) {scanf("%d%d",&n,&q);build(1,n,1);int a,b,c;_for(i, 1, q) {scanf("%d%d%d",&a,&b,&c);update(a,b,1,1,n,c);}printf("Case %d: The total value of the hook is %lld.\n",k,dat[1]);}return 0;
}

转载于:https://www.cnblogs.com/GHzz/p/8921575.html

hdu 1698 Just a Hook(线段树区间修改)相关推荐

  1. HDU 1698 Just a Hook (线段树区间修改+区间查询)

    题目链接: 传送门 题意:Pudge对装备钩子进行若干次的强化,强化分为三种分别对应的价值是1,2,3,在经历过若干次操作后,输出钩子对应的总价值,每次强化都是对钩子进行区间修改 解题思路:在明白了题 ...

  2. hdu 1698 Just a Hook 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Let us number the consecutive metallic sticks of ...

  3. hdu 1698 Just a Hook(线段树区间更新·经典)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1698 数据:case,n,q,q行x,y,z.在长度为n的hook上进行q次区间更新,把它们的价值改变.最 ...

  4. HDU 1698 Just a Hook 线段树

    区间更新中 lazy标记的pushdown操作 风格还是傻崽的...感觉很好用... /********************* Template ************************/ ...

  5. hdu 4902 Nice boat(线段树区间修改,输出最终序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and ...

  6. 【模板】线段树区间修改

    区间修改: 区间修改过程类似于区间询问,例如将[ul, ur]内的所有元素都加上v,则进行如下操作: 当当前区间被区间[ul, ur]所包含时, 当前的节点值加上区间长度(r - l  + 1)乘以v ...

  7. python:线段树区间修改 + 区间查询 模板 + 坑点总结

    from functools import reduceclass SegTree:'''支持增量更新,覆盖更新,序列更新,任意RMQ操作基于二叉树实现初始化:O(1)增量更新或覆盖更新的单次操作复杂 ...

  8. hdu 5692 Snacks(dfs序+线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5692 解题思路:这道题是树节点的点权更新,而且涉及到子树,常用的思路是利用dfs序,用线段树来对区间进 ...

  9. 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)

    题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...

最新文章

  1. CentOS 7如何设置Linux开机自动获取IP地址
  2. 中兴c600olt数据配置_中兴上架Blade 20smart孝心版
  3. 浙江大学计算机考研大纲,2018年浙江大学研究生入学考试《计算机学科专业基础》(878)考试大纲...
  4. 微信小程序:获取地理定位和显示相应的城市名称。
  5. 深入理解傅立叶变换(详细)
  6. EA+svn实现UML的版本号控制
  7. android菱形imageview,ios – 在UICollectionView中,UIImageView应该是圆形视图而不是菱形...
  8. linux上安装memcached步骤
  9. linux自动异地备份,Linux本地加异地自动备份方案
  10. 牛顿法python代码_python 牛顿法实现逻辑回归(Logistic Regression)
  11. Vue开启Gzip打包异常:webpack打包报错Cannot read property ‘emit‘ of undefined
  12. JAVA转smali软件_Java2Smali(Java代码转Smali工具)
  13. Jquery多选框互相内容交换
  14. 知识图谱、Gremlin Traversal Language、RDF、Amazon Nepture 图数据库介绍
  15. FreeRTOS使用教程(配合CubeMX)
  16. VS2015 卸载与安装相关问题(包丢失以及没有WIN控制台)
  17. 徐小湛概率论与数理统计课件_考研数学 徐小湛教授线性代数90讲
  18. 一体机怎么修复音频服务器,多媒体教学一体机没有声音是怎么办?
  19. Eyoucms采集-易优实时数据采集-Eyoucms自动采集
  20. 金山打字通——绿色安全无捆绑下载

热门文章

  1. 利用反射动态实例化类执行方法并传值
  2. deque iterator not dereferencable 问题
  3. Visual Stdio平台工具集以及配置
  4. 大数据_Flink_数据处理_运行时架构5_slot和任务调度---Flink工作笔记0020
  5. 大数据之-Hadoop3.x_MapReduce_CombineTextInputFormat---大数据之hadoop3.x工作笔记0108
  6. webpack的简介---webpack工作笔记001
  7. npm的常用操作---npm工作笔记003
  8. Druid实用笔记001---Druid 介绍及配置
  9. 2015年6月24日日报
  10. Android九宫格闪烁,js实现九宫格点击按钮随机三个格子闪烁,发生错误