Just a Hook 线段树 区间更新
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.
InputThe 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.
OutputFor 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.
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>
#include<stack>
using namespace std;
typedef long long LL;
#define MAXN 100009
#define N 25
#define INF 0x3f3f3f3f /*
线段树 区间更新!
*/
struct node
{
int l, r, data, laz;
int sum;
}T[MAXN*];
void Build(int p, int l, int r)
{
T[p].l = l, T[p].r= r, T[p].data = , T[p].laz = ;
T[p].sum = ;
if (l == r)
{
T[p].sum = ;
return;
}
int mid = (l + r) / ;
Build(p << , l, mid);
Build((p << ) | , mid + , r);
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
}
void update(int p, int l, int r, int v)
{
int mid = (T[p].l + T[p].r) / ;
if (T[p].l >= l&&T[p].r <= r)
{
T[p].data = v;
T[p].laz = ;//这一块被成块更新过
T[p].sum = (r - l + )*v;
return;
}
if (T[p].laz)//如果被更新过,那么说明这一块内的颜色会有多种
{
T[p].laz = ;
update(p << , T[p].l, mid, T[p].data);
update((p << ) | , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
update(p << , l, r, v);
else if (l > mid)
update((p << ) | , l, r, v);
else
{
update(p << , l, mid, v);
update((p << ) | , mid + , r, v);
}
T[p].sum = T[p << ].sum + T[(p << ) | ].sum;
} int main()
{
int t, n, l, r, v, q;
int cas = ;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &q);
Build(, , n);
while (q--)
{
scanf("%d%d%d", &l, &r, &v);
update(, l, r, v);
}
printf("Case %d: The total value of the hook is %d.\n", cas++, T[].sum);
}
}
Just a Hook 线段树 区间更新的更多相关文章
- (简单) 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 ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- HDU1698:Just a Hook(线段树区间更新)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
- hdu1698 Just a Hook (线段树区间更新 懒惰标记)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- swoole多进程处理产生的问题
以前用swoole的时候,没有涉及到数据库连接,碰到问题没有那么多,后来公司业务原生来写swoole多进程,问题出现很多 1.多进程之间会产生进程隔离,global无效,不能共用一个mysql,red ...
- 对路径 obj 文件夹访问被拒绝
TFS 刚下载的项目,出现该问题. 解决方案: 将文件夹属性“只读”,取消
- 263 Ugly Number 丑数
编写程序判断给定的数是否为丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 6, 8 是丑数,而 14 不是,因为它包含了另外一个质因子 7.注意: 1 也可以被当做丑数. 输 ...
- 1102 A-B数对
题目描述 出题是一件痛苦的事情! 题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A-B=C的数 ...
- 全面介绍Android Studio中Git 的使用(一)
来源 :http://blog.csdn.net/gao_chun/article/details/49817229/
- jsp学习笔记 - 内置对象 pageContext
1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest(); 得到的对象只是属于ServletRequest类,httpServletReques ...
- CNN结构:可用于时序预测复合的DNN结构-AcGANs、误差编码网络 ENN
前言:模式识别问题 模式函数是一个从问题定义域到模式值域的一个单射. 从简单的贝叶斯方法,到只能支持二分类的原始支持向量机,到十几个类的分类上最好用的随机森林方法,到可以支持ImageNet上海量18 ...
- GAN生成图像论文总结
GAN Theory Modifyingthe Optimization of GAN 题目 内容 GAN DCGAN WGAN Least-square GAN Loss Sensi ...
- POJ_2536_Gopher II
题意:n只地鼠,m个地鼠洞,地鼠必须以v的速度在s秒内钻进洞且每个洞仅能容纳一只地鼠,问最少有几只地鼠会被老鹰吃掉. 分析:最大匹配问题,将s秒内地鼠能够跑到的洞与该地鼠连成一条边,在最后得到的图中使 ...
- POJ_2828_Buy Tickets
题意:插队问题: 2016.5.20,复习这道题. 总结:线段树基础不牢,建树,更新尚不熟悉,注意加强理解记忆. 主要理解:(单点更新,逆序插入) 发生插队时,前面的队伍是连续没有空位的,即pos:2 ...