Just a Hook

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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.
 
 
题目大意:给你T组测试数据,每组有n,m表示n个棒,每个棒起始都是铜棒,值为1。银棒为2,金棒为3。现在要让区间ui,vi内的棒变为wi类型的棒,问你最后棒的总值是多少。
解题思路:延迟标记。首先在区间更新时,应该保证要更新的那段区间内的结点的值是正确的,然后做延迟标记。在下放标记的时候,应该保证下放后的结点的值是正确的。这样在询问的时候,才能向上推出正确的结果。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn = 120000;
struct SegTree{
int val;
int lazy;
}segs[maxn*4];
void PushUp(int rt){
segs[rt].val = segs[rt*2].val + segs[rt*2+1].val;
}
void PushDown(int rt,int L,int R){
if(segs[rt].lazy){
segs[rt*2].lazy = segs[rt].lazy;
segs[rt*2+1].lazy = segs[rt].lazy;
segs[rt*2].val = (mid - L+1)*segs[rt].lazy;
segs[rt*2+1].val = (R-mid) * segs[rt].lazy;
}
segs[rt].lazy = 0;
}
void buildtree(int rt,int L,int R){
segs[rt].lazy = 0;
if(L == R){
segs[rt].val = 1;
return ;
}
buildtree(lson);
buildtree(rson);
PushUp(rt);
}
void Update(int rt,int L,int R,int l_ran,int r_ran,int _v){
if(l_ran <= L && R <= r_ran){
segs[rt].val = _v * (R-L+1);
segs[rt].lazy = _v;
return;
}
PushDown(rt,L,R);
if(l_ran <= mid){
Update(lson,l_ran,r_ran,_v);
}
if(r_ran > mid){
Update(rson,l_ran,r_ran,_v);
}
PushUp(rt);
}
int main(){
int T, n, m, cas = 0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
buildtree(1,1,n);
int u,v,w;
for(int i = 1; i <= m; i++){
scanf("%d%d%d",&u,&v,&w);
Update(1,1,n,u,v,w);
}
printf("Case %d: The total value of the hook is %d.\n",++cas,segs[1].val);
}
return 0;
}

  

HDU 1698——Just a Hook——————【线段树区间替换、区间求和】的更多相关文章

  1. HDU 1754 I Hate It(线段树单点替换+区间最值)

    I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...

  2. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  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 (Java/Others)Total ...

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

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

  6. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

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

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

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

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

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

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

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

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

随机推荐

  1. 以太坊系列之十八: 百行go代码构建p2p聊天室

    百行go代码构建p2p聊天室 百行go代码构建p2p聊天室 1. 上手使用 2. whisper 原理 3. 源码解读 3.1 参数说明 3.1 连接主节点 3.2 我的标识 3.2 配置我的节点 3 ...

  2. windows windows server2003 开机自动挂盘

    windows  windows server2003 开机自动挂盘 方案一: 设置任务计划:开机启动 方案二: 将执行文件放入启动文件夹

  3. 《Java多线程编程实战指南+设计模式篇》笔记

    线程的监视:工具:jvisualvm.exe 命令:jstack PID 原子性: volatile关键字: 显示锁:人为实现的程序员可控制的锁,包括synchronized和Lock下的实现类: 线 ...

  4. day3学python 字典+列表集合+文件读取

    字典+列表集合+文件读取 字典示例 ************************ 各地食品的三级菜单************************* 1.使用字典嵌套字典 2.采用死循环思路 3 ...

  5. golang并发练习代码笔记

    golang语言的精髓就是它的并发机制,十分简单,并且极少数在语言层面实现并发机制的语言,golang被成为网络时代的c语言,golang的缔造者也有c语言的缔造者,Go语言是google 推出的一门 ...

  6. 【转】c#中@的3种作用

    源地址:https://www.cnblogs.com/linkbiz/p/6380814.html

  7. 【ARC069F】Flags 2-sat+线段树优化建图+二分

    Description ​ 数轴上有 n 个旗子,第 ii 个可以插在坐标 xi或者 yi,最大化两两旗子之间的最小距离. Input ​ 第一行一个整数 N. ​ 接下来 N 行每行两个整数 xi, ...

  8. Nagios监控平台搭建及配置文件详解

    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员, ...

  9. [JSOI2009]计数问题 二维树状数组BZOJ 1452

    题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入输出格式 输入格式: 第一行有两个数N,M. 接下来 ...

  10. DHCP协议及基本实现原理

    DHCP(Dynamic Host Configuration Protocol):动态主机配置协议. DHCP的优缺点 DHCP服务优点:网络管理员可以验证IP地址和其它配置参数,而不用去检查每个主 ...