HDU1698 Just a Hook
Description
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
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
the hook after the operations. Use the format in the example.
Sample Input
10
2
1 5 2
5 9 3
Sample Output
Source
一般的线段树区间修改。但是这题有个坑处,就是区间值是直接替换而非增加值,所以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的更多相关文章
- 线段树---成段更新hdu1698 Just a Hook
hdu1698 Just a Hook 题意:O(-1) 思路:O(-1) 线段树功能:update:成段替换 (由于只query一次总区间,所以可以直接输出1结点的信息) 题意:给一组棍子染色,不同 ...
- HDU1698 Just a Hook —— 线段树 区间染色
题目链接:https://vjudge.net/problem/HDU-1698 In the game of DotA, Pudge’s meat hook is actually the most ...
- HDU-1698 JUST A HOOK 线段树
最近刚学线段树,做了些经典题目来练手 Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu-------(1698)Just a Hook(线段树区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU1698 Just a Hook (区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu1698 Just a Hook 线段树:成段替换,总区间求和
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...
- hdu1698 Just a Hook 线段树
共有Q个更新,每次更新给更新的区间一个标记,表示该区间是在哪一次被更新,最后统计答案是以最近被更新的值为答案. AC代码: #include<cstdio> const int maxn= ...
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu1698 Just a Hook 【区间修改】(模板题)
题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...
随机推荐
- linux下查找某个目录下包含某个字符串的文件
有时候要找一些字符串,但是又不知道在哪个文件,只记得一些字符串 那么如何在linux下寻找包含某段文字的文件呢? 强大的find命令可以帮你完成不可能的任务. 比如我只记得我的程序里包含唯一的字符串“ ...
- 一篇搞定RSA加密与SHA签名|与Java完全同步
基础知识 什么是RSA?答:RSA是一种非对称加密算法,常用来对传输数据进行加密,配合上数字摘要算法,也可以进行文字签名. RSA加密中padding?答:padding即填充方式,由于RSA加密算法 ...
- 在PLSQL中不能使用中文作为查询条件查询数据
解决方法: 1.在oracle服务端的注册表中找到oracle-->key_oradb11g_home1,在右侧找到NLS_LANG,将其数值数据改为SIMPLIFIED CHINESE_CH ...
- Google play billing(Google play 内支付)
准备工作 1. 通过Android SDK Manager下载extras中的Google Play services和Google Play Billing Library两个包. 2. 把下载的. ...
- 解决memcached不能远程访问的问题
之前安装好memcached之后,一直是在本机连接使用的,没有出现问题,今天我改用从另一台机器连接到memcached时,却怎么也连接不上.后来一直想大概是防火墙的问题,关闭了防火墙后问题依然存在. ...
- pandas 修改 DataFrame 列名
问题: 有一个DataFrame,列名为:['$a', '$b', '$c', '$d', '$e'] 现需要改为:['a', 'b', 'c', 'd', 'e'] 有何办法? import pan ...
- MVC5 + EF6 + Bootstrap3 (13) 查看详情、编辑数据、删除数据
Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-rud.html 系列教程:MVC5 + EF6 + Boo ...
- brew-cask之本地安装应用
cask 固然好用,但是无奈很多资源在墙外,能下载的非常有限,就是能下载,也慢的要死.但是很多下载软件却可以下载这些资源,很奇怪,要么是有人FQ下载了,缓存到他们的服务器了,要么就是软件可以FQ下载. ...
- 使用OneNote的COM组件,实现OCR功能。
背景 在业务系统开发的过程中,很多情况下会去识别图片中的相关信息,并且把信息录入到系统中.现在希望通过自动化的方式录入,就有了以下的工作.在对比了几个OCR软件在中文识别方面的准确率后,决定使用微软的 ...
- 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来
代码: using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ...