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. 传奇的通迅协议与base64算法

    传奇的数据根本就没加密. 只要把#(数字)!去掉,中间的那些全部减0x3c,然后,四个字节一组,每个字节取低六位组成6*4=24位,然后分成3个字节,这个是很经典的base64的算法.不过小改了一些, ...

  2. nginx的学习材料

    1. 章亦春 关于nginx的讲解 http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#2 2. ...

  3. Swift3.0 进制转换

    Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...

  4. Use CLR Profiler

    Use CLR Profiler 第一次翻译对我而言比较长的E文,有很多不足之处,请见谅.(个人的习惯GC又做了名词又做了名词) 原文:http://msdn.microsoft.com/en-us/ ...

  5. Linux 守护进程一

    守护进程是一个后台进程,它无需用户输入就能运行,经常是提供某种服务. LInux作为服务器,主要的进程也都是为系统或用户提供后台服务功能. 常见的守护进程有Web服务器.邮件服务器以及数据库服务器等等 ...

  6. C#泛型委托,匿名方法,匿名类

    class Test { delegate K proxy<T, K>(T t, K k); //泛型委托,注意返回值的写法,返回值的类型K先于其声明proxy<T,K>中的K ...

  7. 封装WCF客户端调用

    在之前的博客中,我记录过如何利用SvcUtil.exe工具生成客户端的代理文件,然后调用的情形. 今天我要讲解的是利用代码直接对服务端进行调用.好处在于,一是不会生成那么大的引用文件,其次是可以方便控 ...

  8. [CareerCup] 8.8 Othello Game 黑白棋游戏

    8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. Wh ...

  9. edgesForExtendedLayout、extendedLayoutIncludesOpaqueBars、automaticallyAdjustsScrollViewInsets属性详解 )——转载

    edgesForExtendedLayout: 在ios7适配中,布局问题是一个很头痛也很重要的问题,因为在ios7中viewController使用了全屏布局的方式,也就是说导航栏和状态栏都是不占实 ...

  10. Opencv step by step - ROI

    什么是ROI?ROI就是region of interest ,就是你感兴趣的图像部分,在图像处理中,可能同时要处理多个ROI. Opencv有ROI的API,但是只能同时处理一个(书本上说的,未验证 ...