Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 29310    Accepted Submission(s): 14492

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

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.
 
 
 
 
//线段树的lazy应用,不难,题意:刚开始 1 -- n 的钩子价值都为 1 ,q 次操作,将 [l,r] 变为 z 价值,最后问总和
 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define MX 100005
struct Node
{
int l,r;
int lazy,sum;
}tree[MX*]; int n, q; void build(int l,int r,int k)
{
tree[k]=(Node){l,r,,};
if (l==r)
{
tree[k].sum = ;
return;
}
int mid = (l+r)>>;
build(l,mid,*k); build(mid+,r,*k+);
tree[k].sum = tree[*k].sum + tree[*k+].sum;
} void push_down(int k)
{
int l=tree[k].l, r=tree[k].r;
int mid = (l+r)>>, z = tree[k].lazy;
if (l==r||z==) return;
tree[*k].lazy=z; tree[*k].sum=z*(mid-l+);
tree[*k+].lazy=z; tree[*k+].sum=z*(r-mid);
tree[k].lazy=;
} void update(int l,int r,int k,int v)
{
if (l==tree[k].l&&r==tree[k].r)
{
tree[k].lazy=v;
tree[k].sum=v*(r-l+);
return;
}
push_down(k);
int mid = (tree[k].l+tree[k].r)>>;
if (r<=mid) update(l,r,*k,v);
else if (l>mid) update(l,r,*k+,v);
else update(l,mid,*k,v), update(mid+,r,*k+,v);
tree[k].sum = tree[*k].sum+tree[*k+].sum;
} int inqy(int l, int r, int k)
{
if (l==tree[k].l&&r==tree[k].r)
{
return tree[k].sum;
}
push_down(k);
int mid = (tree[k].l+tree[k].r)>>;
if (r<=mid) return inqy(l,r,*k);
else if (l>mid) return inqy(l,r,*k+);
return inqy(l,mid,*k) + inqy(mid+,r,*k+);
} int main()
{
int t;
scanf("%d",&t);
for (int cas=;cas<=t;cas++)
{
scanf("%d%d",&n,&q);
build(,n,);
while(q--)
{
int l, r, v;
scanf("%d%d%d",&l,&r,&v);
update(l,r,,v);
}
printf("Case %d: The total value of the hook is %d.\n",cas,inqy(,n,));
}
return ;
}

Just a Hook(线段树)的更多相关文章

  1. hdu_1698Just a Hook(线段树)

    hdu_1698Just a Hook(线段树) 标签: 线段树 题目链接 题意: 一个英雄的技能是发射一个长度为n的金属链,初始的金属链都是铁做的,标记为1,我们可以对于某个区间修改它的金属材质,如 ...

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

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

  3. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  4. HDU-1698 JUST A HOOK 线段树

    最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  5. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  7. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. hdu1698 Just a Hook 线段树:成段替换,总区间求和

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...

  9. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

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

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

随机推荐

  1. 装饰者模式对HttpServletRequest进行增强

    package cn.web.servlet; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpS ...

  2. 记录一次ceph recovery经历

    一次ceph recovery经历 背景 这是一个測试环境. 该环境中是cephfs 一共12个节点, 2个client.2个mds.8个osd mds: 2颗CPU,每一个4核.一共是8核. 128 ...

  3. ubuntu12.04下sun-java1.6-jdk配置

    1. 下载安装 2. 设置安装的默认程序 $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_2 ...

  4. Angular 学习笔记——service &constant

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  5. Tomcat、Weblogic、JBoss、GlassFish、Resin、Websphere弱口令及拿webshell方法总结 [复制链接]

    1.java应用服务器    Java应用服务器主要为应用程序提供运行环境,为组件提供服务.Java 的应用服务器很多,从功能上分为两类:JSP 服务器和 Java EE 服务器.1.1  常见的Se ...

  6. NinePatch

    将图片保存为扩展名为.9.png的格式直接放入Android Studio中的drawable文件夹,拖拉选择拉伸区域,如下图,即可制作出可拉伸背景

  7. 借助backtrace和demangle实现异常类Exception

    C++的异常类是没有栈痕迹的,如果需要获取栈痕迹,需要使用以下函数: #include <execinfo.h> int backtrace(void **buffer, int size ...

  8. CSS——如何清除浮动

    众所周知,平时在写HTML代码时,难免少不了使用Float样式,这样一来,假使您没有清除浮动,那么有浮动元素的父元素容器将元素将无法自动撑开.换句简单好理解的话来说,假如你在写CODE时,其中div. ...

  9. 最新版的 react-native 降级处理

    1.react-native 常见操作 (1)react-native init Demo (2)adb devices (3)react-native run-android (4)ipconfig ...

  10. javascript 冒泡排序算法

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...