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 ...
随机推荐
- nRF5 SDK for Mesh(一) 介绍和下载源码
一: 官网介绍地址:http://www.nordicsemi.com/Products/Bluetooth-low-energy/nRF5-SDK-for-Mesh Nordic offers a ...
- .net core 实践笔记(二)--EF连接Azure Sql
** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/9902098.html 笔者使用了常见的三层架构,Api展示层注入了Swa ...
- $.ajax 完整参数
jquery中的ajax方法参数 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意 ...
- 偏前端-纯css,手写轮播-(焦点切换 和 自动轮播 只可选择一种,两者不可共存)
现在我们一般都是在网上找个轮播插件,各种功能应有尽有,是吧!!~大家似乎已经生疏了手写是什么感觉.万一哪天想不起来,人家要手写,就尴尬了!~~跟我一起复习一下吧 不多说:效果图看一下: 高度不能是固定 ...
- transform动画的一个3D的正方体盒子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- laravel5.5源码笔记(六、中间件)
laravel中的中间件作为一个请求与响应的过滤器,主要分为两个功能. 1.在请求到达控制器层之前进行拦截与过滤,只有通过验证的请求才能到达controller层 2.或者是在controller中运 ...
- Flume的介绍和简单操作
Flume是什么 Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数 ...
- banwagon vps装wordpress
http://www.banwagong.com/213.html http://www.banwagong.com/225.html http://www.banwagong.com/230.htm ...
- SSM框架及例子(转)
SSM 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis 博客地址:http://blog.csdn.net/qq598535550/article/detai ...
- Zabbix学习之路(六)TCP状态监控
TCP状态监控 Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB:或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了***,或是是time_wait值比 ...