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. Eclipse去除JavaScript验证错误

    这篇文章主要是对Eclipse去除js(JavaScript)验证错误进行了介绍.在Eclipse中,js文件常常会报错.可以通过如下几个步骤解决 第一步: 去除eclipse的JS验证: 将wind ...

  2. 纯CSS3制作九款可爱复古相机

    前言 掐指一算,快两个月没写博客分享了.好吧,我就只是在准备校招而已.现在已经有满意的offer了,所以我就回来啦!这两个月过得挺煎熬也挺充实的.具体细说估计得长篇大论,我就不闲扯了.总之呢,越努力, ...

  3. js的nextSibling,属性兼容IE和FF等浏览器

    Firefox中 空白字符,比如回车,空格等也算作一个Node 就是firstChild,nextsbiling这两个.下面给出函数吧.还是代码比较说明问题代码都是网上来的.不过要注意的是,getNe ...

  4. 09Spring_AOP介绍和java本身的动态代理以及cglib代理

    Aspect Oriented Programming 面向切面编程 1. 业界 AOP 实际上 OOP (面向对象编程 ) 延伸 ----  OOP编程语言. AOP设计思想,下面给出一张AOP的设 ...

  5. 最近火到不行的微信小程序的常识

    满网都是微信小程序,技术dog们不关注都不行了.先别忙着去学怎么开发小程序,先纠正一下你对微信小程序的三观吧~~~~ 小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想 ...

  6. Oracle PL/SQL中如何使用%TYPE和%ROWTYPE

    1. 使用%TYPE 在许多情况下,PL/SQL变量可以用来存储在数据库表中的数据.在这种情况下,变量应该拥有与表列相同的类型.例如,students表的first_name列的类型为VARCHAR2 ...

  7. js控制只能输入数字和小数点

    非常好用,代码示例如下: <input  onkeypress = "return event.keyCode>=48&&event.keyCode<=57 ...

  8. eclipse的一些初始化设置

    eclipse下载地址:http://www.eclipse.org/ 添加java注释模板 codetemplates.xml:Window->Preferences->Java-> ...

  9. iOS从App跳转至系统设置菜单各功能项的编写方法讲解

    跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSUR ...

  10. wen7安装oracle 11g出现"未找到文件 E:\development_tools\database\oracle\install_d\dbhome\owb\external\oc4j_applications\applications\WFMLRSVCApp.ear"

    从oracle官网上下载了window7 64位的oracle安装包win64_11gR2_database_1of2,安装后出现了错误: 解决方法:继续下载oracle官网上的文件2:win64_1 ...