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

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.

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.

思路

初始所有节点值为1 , 更新[l,r]为2 或 3,再求区间和,涉及懒标记,直接套模板= =

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10 ;
const int inf = 0x3f3f3f3f; struct node{
int l,r;
int add;
int sum;
}tree[maxn<<2]; int kase=0;
int n,m,t;
int a,b,c;
int val = 1;
int ans = 0;
void pushup(int k)
{
tree[k].sum = tree[k<<1].sum+tree[k<<1|1].sum;
}
void pushdown(int k)
{
if(tree[k].add)
{
tree[k<<1].sum = (tree[k<<1].r-tree[k<<1].l+1)*tree[k].add;
tree[k<<1|1].sum = (tree[k<<1|1].r-tree[k<<1|1].l+1)*tree[k].add; tree[k<<1].add = tree[k].add;
tree[k<<1|1].add = tree[k].add; tree[k].add = 0;
}
}
void build(int l,int r,int k)
{
tree[k].l = l; tree[k].r = r; tree[k].add = 0;//刚开始一定要清0
if(l == r){ tree[k].sum=1; return ; }
int mid = (l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
pushup(k);
}
void updata(int k)
{
if(a <= tree[k].l && b >= tree[k].r)
{
tree[k].sum = (tree[k].r-tree[k].l+1)*val;
tree[k].add = val;
return ;
}
pushdown(k);
int mid = (tree[k].l+tree[k].r)>>1;
if(a<=mid){ updata(k<<1); }
if(b>mid){ updata(k<<1|1); }
pushup(k);
}
void query(int k)
{
if(a <= tree[k].l && b >= tree[k].r)
{
ans +=tree[k].sum ;
return ;
}
pushdown(k);
int mid = (tree[k].l+tree[k].r)>>1;
if(a <= mid){ query(k<<1);}
if(b > mid){ query(k<<1|1);}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d",&m);
build(1,n,1);
while(m--)
{
scanf("%d%d%d",&a,&b,&val);
updata(1);
}
ans=0;a=1;b=n;//这里需初始化
query(1);
printf("Case %d: The total value of the hook is %d.\n",++kase,ans);
}
}

HDU 1689 Just a Hook (线段树区间更新+求和)的更多相关文章

  1. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  2. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  3. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  4. 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 ...

  5. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  8. 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 ...

  9. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  10. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

随机推荐

  1. Mongo数据两表关联创建视图示例

    表tblCard: {"cNo":"11","oRDate":ISODate("2017-08-01T00:00:00.000+0 ...

  2. windows系统快捷键

    1.我的键盘:windows键的开启,需要按住FN键+windows键. 2.windows键 + E,表示打开我的电脑. 3.windows键 + R,打开windows的命令行窗口. 4.wind ...

  3. php高并发,大流量

    一般使用LVS+PHP集群(1000台),就算日均80亿次请求,每秒有10万并发,那分到每台机器的请求只有100个.只要你的PHP程序不是太差,100QPS总没问题吧? 而真正的瓶颈在于数据库和存储系 ...

  4. hdu5489 树状数组+dp

    2015-10-06 21:49:54 这题说的是个给了一个数组,然后删除任意起点的一个连续的L个数,然后求最长递增子序列<是递增,不是非递减>,用一个树状数组维护一下就ok了 #incl ...

  5. Math对象属性

    2018-11-28 11:18:46

  6. 关于poi导出excel方式HSSFWorkbook(xls).XSSFWorkbook(xlsx).SXSSFWorkbook.csv的总结

    1.HSSFWorkbook(xls) import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermo ...

  7. 【2017-03-13】Tsql 数学函数、字符串函数、转换函数、时间日期函数

    一.数学函数(针对值类型操作) 1.ceiling():取上限 只要小数点后有数字大于0,整数位自动进1 2.floor():取下限 将小数点位舍去,不管小数点位大小 3.round(四舍五入的值,保 ...

  8. Gibbs Sampling深入理解

    二维Gibbs Sampling算法 Gibbs Sampling是高维概率分布的MCMC采样方法.二维场景下,状态(x, y)转移到(x’, y’),可以分为三种场景 (1)平行于y轴转移,如上图中 ...

  9. 20165305 苏振龙《Java程序设计》第二周学习总结

    代码托管(ch2,ch3) 脚本截图 教材内容总结 类型.变量与运算符 基本类型 整数(short.int.long) 字节(byte) 浮点数(float/double) 字符(char)将一个数字 ...

  10. Unable to open socket file: target process not responding or HotSpot VM not loaded

    Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can ...