Just a Hook:线段树+区间修改
E - Just a Hook
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 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
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:线段树+区间修改的更多相关文章
- HDU1698Just a Hook(线段树 + 区间修改 + 求和)
题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...
- 题解报告: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 ...
- 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序加上一个线 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- 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 ...
随机推荐
- centos6.4 安装code::blocks
今天下午闲着没事尝试在自己的PC中的CentOS上装一个Code::Blocks,因为是Linux菜鸟折腾了一下午才基本算搞定但依然有疑惑: 在网上各种谷哥度娘最后才发现还是官方的文档最靠谱:看这里. ...
- vue.js数组追加合并与对象追加合并的
今天在做懒加载的时候遇到的问题,在网上搜索找到的答案不是很清晰,就来写一下,方便以后使用. 直接上图吧 官方连接:https://cn.vuejs.org/v2/guide/reactivity.ht ...
- 洛谷P3690 【模板】Link Cut Tree (LCT)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- 19-3-7Python中小数据池、数据类型的补充、set集合
一.小数据池(了解) “id” 获取内存地址 “==” 比较等号两端的值是否相等 “is” 身份运算:判断的是两个对象的内存地址是否相同. 代码块:一个文件就是一个代码块.(函数.类都是 ...
- Java调用WeChat's API总结
微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务:而后者仅仅能够浏览页面,通过用户的输入信息与用户互动. 本人根 ...
- PhpStorm中实现代码自动换行
方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...
- Spark SQL join的三种实现方式
引言 join是SQL中的常用操作,良好的表结构能够将数据分散到不同的表中,使其符合某种规范(mysql三大范式),可以最大程度的减少数据冗余,更新容错等,而建立表和表之间关系的最佳方式就是join操 ...
- 07.centos7构建 IntelliJ IDEA(简称IDEA)开发环境
一.安装IDEA 进入官网下载linux版的社区便,IDEA分为社区版和旗舰版,社区版免费,并且基本满足spark开发需求. 解压安装 目录为/opt/idea 注意:centos命令行界面下是无法安 ...
- springboot(eureka子项目)+idea+jsp 404问题
我是这么解决的 对于单一项目,加入以下jar包即可 <!--前台页面的支持--> <dependency> <groupId>javax.servlet</g ...
- 20155328 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告
20155328 <Java程序设计>实验一(Java开发环境的熟悉) 实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发: 打开windows ...