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. 字符型设备驱动程序-first-printf以及点亮LED灯(二)

    编译这几个函数之前要学一下:Linux 的几个操作命令. 学习地址:http://edu.51cto.com/lesson/id-101824.html 重要的命令 有4个 :分别是 1.lsmod, ...

  2. G1 GC日志:Application time: 0.8766273 seconds

    启动日志一直循环: 1.159: Application time: 0.8766273 seconds 1.160: Total time for which application threads ...

  3. 部分用户间接性访问不了linux服务器解决方法

    linux的/etc/sysctl.conf中应设置 net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = 参考文章: https://ieevee.co ...

  4. 洛谷P1501 [国家集训队]Tree II(LCT)

    题目描述 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u2 v2:将树中原有的 ...

  5. 『C++』Temp_2019_03_14 不同类的循环引用

    #include <iostream> #include <string> #include <regex> using namespace std; //提前声明 ...

  6. mysql事件关闭解决办法

    Mysql 事件event_scheduler是OFF 开启 Event Scheduler,以下4种方式等效 SET GLOBAL event_scheduler = ON; SET @@globa ...

  7. centos7开机不进入图形界面

    centOS7开机不进入图形界面设置和centOS6系列不同的是,不再是直接改文件中的5就可以了. centOS7设置如下: systemctl get-default    //获取当前的默认tar ...

  8. jQuery笔记: 基本概念与jQuery核心

    目录 初识jQuery 为什么要使用jQuery? 如何使用jQuery? jQuery与js加载模式不同 jQuery入口函数的四种写法 jQuery的访问符冲突问题 jQuery核心函数和jQue ...

  9. sql for xml path 处理

    1.将下列结果集 做成 aa   语文,数学 bb    英语,语文 这种格式 使用 for xml  path  记得去重复 WITH cte AS(SELECT stu.studentname,c ...

  10. redis应用场景:实现简单计数器-防止刷单

    redis应用场景:实现计数器-防止刷单 最近由于双11要来临,公司需要在接口请求上,做一下并发限制的处理,或者做一个防止刷单的安全拦截:比如:一个接口请求,限制每秒请求总数为200次,超过200次就 ...