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. Bootstrap-媒体查询-屏幕大小

    .container{padding:0 15px; margin:0 auto;} .container:before{ content: ''; display: table;/*防止第一个子元素 ...

  2. C# 基于Aspose.Cells的数据导出到Excel

    using Aspose.Cells;  void WriteToExcel(string filePath, List<object[]> datas, string sheetName ...

  3. Oracle使用rman备份数据库时出现cannot reclaim的错误

    1. 按照<2 day DBA>中的guide,设置fast recovery area. SQL> ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_S ...

  4. [4]传奇3服务器源码分析一 SelGate

    1. 2 留存 服务端下载地址: 点击这里

  5. Yii Restful api认证

  6. hdu5422 最大表示法+KMP

    #include <iostream> #include <algorithm> #include <string.h> #include <cstdio&g ...

  7. bash shell 编程练习

    原始文件: find /etc -name passwd 2>&1 | tee ee.log 1. cat -n 把 e.log 的文档内容加上行号后输入 e2.log 这个文档里: x ...

  8. numpy 数据处理

    np.meshgrid() meshgrid 传入两个一维数组,返回第一个二维数组用第一个传入的一维数组为行,第二个传入的一维数组为列返回的第二个数组是以第二个传入的一维数组为行,第一个一维数组为列 ...

  9. Axure RP Extension for Chrome修复

    Axure RP Extension for Chrome安装之前一直用 Firefox 浏览器浏览原型文件,一直用不惯,而且用 Firefox 的唯一目的就是看原型.其他都是用 Chrome 浏览器 ...

  10. flask 对URL进行安全验证

    对URL进行安全验证 虽然我们已经实现了重定向会上一个页面的功能,但是安全问题不容忽视,鉴于referer和next容易被串篡改的特性,我们需要对这些值进行验证,否则会形成开放重定向漏洞   以URL ...