HDU1698:Just a Hook(线段树区域更新模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1698
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 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.
each case, print a number in a line representing the total value of the
hook after the operations. Use the format in the example.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define N 1000010
using namespace std;
struct node
{
int l,r,lz,w;
} q[*N];
int n;
void pushup(int rt)
{
q[rt].w=q[rt<<].w+q[rt<<|].w;
}
void pushdown(int rt,int len)
{
if(q[rt].lz)
{
q[rt<<].lz=q[rt].lz;
q[rt<<|].lz=q[rt].lz;
q[rt<<].w=q[rt].lz*(len-(len>>));
q[rt<<|].w=q[rt].lz*(len>>);
q[rt].lz=;
}
}
void build(int l,int r,int rt)
{
q[rt].l=l;
q[rt].r=r;
q[rt].w=;
q[rt].lz=;
if(l==r)
return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
return ;
}
void update(int lf,int rf,int l,int r,int rt,int key)
{
if(lf<=l&&rf>=r)
{
q[rt].w=key*(r-l+);
q[rt].lz=key;
return ;
}
pushdown(rt,(r-l+));
int mid=(l+r)>>;
if(lf<=mid) update(lf,rf,l,mid,rt<<,key);
if(rf>mid) update(lf,rf,mid+,r,rt<<|,key);
pushup(rt);
}
int main()
{
int T,m,s1,s2,key;
scanf("%d",&T);
for(int z=; z<=T; z++)
{
scanf("%d",&n);
build(,n,);
scanf("%d",&m);
for(int i=; i<m; i++)
{
scanf("%d%d%d",&s1,&s2,&key);
update(s1,s2,,n,,key);
}
printf("Case %d: The total value of the hook is %d.\n",z,q[].w);
}
}
HDU1698:Just a Hook(线段树区域更新模板题)的更多相关文章
- 【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)
学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表 ...
- hdu1698 Just a hook 线段树区间更新
题解: 和hdu1166敌兵布阵不同的是 这道题需要区间更新(成段更新). 单点更新不用说了比较简单,区间更新的话,如果每次都更新到底的话,有点费时间. 这里就体现了线段树的另一个重要思想:延迟标记. ...
- 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) Tota ...
- FZU Problem 2171 防守阵地 II (线段树区间更新模板题)
http://acm.fzu.edu.cn/problem.php?pid=2171 成段增减,区间求和.add累加更新的次数. #include <iostream> #include ...
- UESTC 1591 An easy problem A【线段树点更新裸题】
An easy problem A Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- Just a Hook 线段树 区间更新
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
随机推荐
- 使用MFC WinInet进行FTP中文件的简单上传和下载功能
建立基于对话框的MFC应用程序CMfcFtpWinInetDlg: 1.首先Dlg类中包含头文件 #include "afxinet.h" 2.添加成员变量: C++ Code ...
- linux中,查看某个命令是来自哪个RPM包或者是通过哪个RPM包安装的
需求描述: 今天在测试ssh命令到底是哪个RPM包,安装之后生成的,找了一些文档 在这里进行记录下,主要是rpm -qf命令的使用,查询文件在哪个包里. 操作过程: 1.通过whereis 定位ssh ...
- Python 爬虫知识点 - 淘宝商品检索结果抓包分析
一.抓包基础 在淘宝上搜索“Python机器学习”之后,试图抓取书名.作者.图片.价格.地址.出版社.书店等信息,查看源码发现html-body中没有这些信息,分析脚本发现,数据存储在了g_page_ ...
- Runtime应用(三)实现NSCoding的自动归档和自动解档
当我们需要将一个对象进行归档时,都要让该对象的类遵守NSCoding协议,再实现归档和接档方法.例如有一个Person类,该类有两个成员变量 @property (nonatomic,copy) NS ...
- PostgreSQL数据库smallint、bigint转到Oracle,要用什么类型替代? 是number么,那长度分别是多少?
个人意见,仅供参考:smallint是有符号或无符号2字节的整数,范围是0-65,536,5位整数bigint是有符号或无符号8字节的整数,范围是0-18,446,744,073,709,551,61 ...
- Date Json格式转换Date格式
CreateTime=\/Date(1458722493663+0800)\/ var CreateTime="/Date(1458722493663+0800)/";var st ...
- 【PHP】通过header发送自定义数据
发送header: 我们定义了三个参数,token.language.region,放入header发送过去 <?php $url = 'http://www.example.com'; $he ...
- java基础---->Java关于复制的使用(一)
这里简单记录一下java中关于浅复制和深复制的知识.很多时候,一个人选择了行走,不是因为欲望,也并非诱惑,他仅仅是听到了自己内心的声音. java中的复制clone方法 一.java对象的浅复制 一个 ...
- C/C++程序编译流程
单个文件的编译过程 多个文件的编译过程
- Android 微信分享,分享到朋友圈与分享到好友,以及微信登陆
extends:http://www.cnblogs.com/android100/p/Android-qq.html 一.申请你的AppID http://open.weixin.qq.com/ 友 ...