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.

 

Source

2008 “Sunline Cup” National Invitational Contest

一般的线段树区间修改。但是这题有个坑处,就是区间值是直接替换而非增加值,所以pushdown的时候一定要把子节点的值也更新。←因为这个被坑了半小时青春

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
using namespace std;
const int mxn=;
int n,m;
int ans=;
struct tree{
int sum;
int add;
}tr[mxn*];
void Build(int l,int r,int rt){
if(l==r){
tr[rt].sum=;
tr[rt].add=;
return;
}
int mid=(l+r)>>;
Build(ls);
Build(rs);
tr[rt].sum=tr[rt<<].sum+tr[rt<<|].sum;
return;
}
void change(int L,int R,int x,int l,int r,int rt){
if(tr[rt].add){
int mid=(l+r)>>;
tr[rt<<].sum=(mid-l+)*tr[rt].add;//更新下面的值很重要!
tr[rt<<|].sum=(r-mid)*tr[rt].add;//更新!
tr[rt<<].add=tr[rt<<|].add=tr[rt].add;
tr[rt].add=;
}
if(L<=l && r<=R){
tr[rt].sum=(r-l+)*x;
tr[rt].add=x;
return;
}
int mid=(l+r)>>;
if(L<=mid)change(L,R,x,ls);
if(R>mid)change(L,R,x,rs);
tr[rt].sum=tr[rt<<].sum+tr[rt<<|].sum;
return;
} int main(){
int T;
scanf("%d",&T);
for(int ro=;ro<=T;ro++){
memset(tr,,sizeof tr);
scanf("%d",&n);
Build(,n,);
scanf("%d",&m);
int i,j;
int x,y,z;
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
change(x,y,z,,n,);
}
printf("Case %d: The total value of the hook is %d.\n",ro,tr[].sum); }
return ;
}

HDU1698 Just a Hook的更多相关文章

  1. 线段树---成段更新hdu1698 Just a Hook

    hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...

  2. HDU1698 Just a Hook —— 线段树 区间染色

    题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...

  3. HDU-1698 JUST A HOOK 线段树

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

  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. HDU1698 Just a Hook (区间更新)

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

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

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

  7. hdu1698 Just a Hook 线段树

    共有Q个更新,每次更新给更新的区间一个标记,表示该区间是在哪一次被更新,最后统计答案是以最近被更新的值为答案. AC代码: #include<cstdio> const int maxn= ...

  8. 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

    学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...

  9. hdu1698 Just a Hook 【区间修改】(模板题)

    题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...

随机推荐

  1. $watch方法

    监听一个model(表单),当一个model每次改变时,都会触发第二个参数函数 $scope.$watch('name',function(){});//name是model名<input ty ...

  2. css 字体不撑开默认块级元素问题

    问题原因是行高的元素没有随字体大小而改变,设置line-hight属性和字体同时变换

  3. [原创]CI持续集成系统环境---部署Gitlab环境完整记录

    Gitlab是一个代码托管平台,在实际工作中,对代码管理十分有用. 废话不多说,下面是对我自己搭建的Gitlab环境做一记录: (1)安装 ------------------------------ ...

  4. immutability-javascript

    https://www.sitepoint.com/immutability-javascript/

  5. Camera中对焦模式总结

    1,相机对焦模式有以下几种: auto  //自动 infinity //无穷远 macro //微距 continuous-picture //持续对焦 fixed //固定焦距   2,常见对焦模 ...

  6. U3D协程Coroutine之WWW与Update()的并行测试

    using System.Collections; using UnityEditor; using UnityEngine; using UnityEngine.UI; /************* ...

  7. 实战 SQL Server 2008 数据库误删除数据的恢复

    SQL Server中误删除数据的恢复本来不是件难事,从事务日志恢复即可.但是,这个恢复需要有两个前提条件: 1. 至少有一个误删除之前的数据库完全备份. 2. 数据库的恢复模式(Recovery m ...

  8. benchmark

    redis benchmark How many requests per second can I get out of Redis? Using New Relic to Understand R ...

  9. Got a packet bigger than 'max_allowed_packet' bytes

    昨天用导入数据的时候发现有的地方有这个错误.后来才发现我用RPM包装的MYSQL配置文件里面有old_passwords=1去掉就可以了. Got a packet bigger than ‘max_ ...

  10. linux(centos 6.4)下安装php memcache服务端及其客户端(详细教程)

    前言 在搭建个人博客时,由于没有使用任何框架,纯手工code前台和后台,导致遇到许多问题,其中一个问题就是mysql连接导致的页面相应速度异常低.在查询各种途径后,只能考虑使用memcache缓存.在 ...