Just a Hook (线段树)
给你n个数(初始时每个数的值为1),m个操作,每个操作把区间[l,r]里的数更新为c,问最后这n个数的和是多少。
区域更新用懒惰标记
#include<bits/stdc++.h>
using namespace std;
#define N 110000
#define lson L,m,pos<<1
#define rson m+1,R,pos<<1|1
#define mid m=(L+R)>>1
int sum[N<<];
int col[N<<]; void up(int pos)
{
sum[pos]=sum[pos<<]+sum[pos<<|];
}
void build(int L,int R,int pos)
{
col[pos]=;
if(L==R)
{
sum[pos]=;
return ;
}
int mid;
build(lson);
build(rson);
up(pos);
} void down(int pos,int m)
{
if(col[pos])
{
col[pos<<]=col[pos<<|]=col[pos];
sum[pos<<]=(m-(m>>))*col[pos];
sum[pos<<|]=(m>>)*col[pos];
col[pos]=;
}
}
void update(int l,int r,int v,int L,int R,int pos)
{
if(l<=L&&r>=R)
{
col[pos]=v;
sum[pos]=v*(R-L+);
return ;
}
down(pos,R-L+);
int mid;
if(l<=m)update(l,r,v,lson);
if(r>m) update(l,r,v,rson);
up(pos);
} int main() {
int T , n , m;
scanf("%d",&T);
for (int cas = ; cas <= T ; cas ++) {
scanf("%d%d",&n,&m);
build( , n , );
while (m --) {
int a , b , c;
scanf("%d%d%d",&a,&b,&c);
update(a , b , c , , n , );
}
printf("Case %d: The total value of the hook is %d.\n",cas , sum[]);
}
return ;
}
Just a Hook (线段树)的更多相关文章
- hdu_1698Just a Hook(线段树)
hdu_1698Just a Hook(线段树) 标签: 线段树 题目链接 题意: 一个英雄的技能是发射一个长度为n的金属链,初始的金属链都是铁做的,标记为1,我们可以对于某个区间修改它的金属材质,如 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- 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) Tota ...
- HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)
题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3, 初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...
- [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 线段树:成段替换,总区间求和
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Problem ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
随机推荐
- bzoj千题计划130:bzoj1305: [CQOI2009]dance跳舞
http://www.lydsy.com/JudgeOnline/problem.php?id=1305 每个人拆为喜欢(yes)和不喜欢(no)两个点 二分答案 1.每两个人之间只能跳一次 喜欢则 ...
- ZeroMQ API(六) 代理
1.zmq_proxy(3) 1.1 名称 zmq_proxy - 开始内置ZMQ代理 1.2 概要 int zmq_proxy(const void * frontend,const void * ...
- typedef 与 #define的区别
typedef 与 #define的区别 整理于一篇经典blog,经典原文地址http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.ht ...
- Cookie详解、ASP.NET核心知识(7)
无状态的http协议 1.回顾http协议 Http协议是请求响应式的,有请求才有响应,是无状态的,不会记得上次和网页“发生了什么”. 关于http协议的这种特点,黑兔在前面的这三篇博文中进行了详细的 ...
- php魔术函数 __clone()
原文地址: http://www.nowamagic.net/librarys/posts/php/32 PHP4面向对象功能一个很大的缺点,是将对象视为另一种数据类型,这使得很多常见的OOP方法无法 ...
- C# FileStream MemoryStream BufferedStream StreamReader StreamWriter
FileStream读取文件 , array.Length);//读取流中数据把它写到字节数组中file.Close();//关闭流string str =Encoding.Default.GetSt ...
- 2016.6.17——Valid Parentheses
Valid Parentheses 本题收获: 1.stack的使用 2.string和char的区别 题目: Given a string containing just the character ...
- Dream------scala--Tuple、Array、Map与文件操作
1.Tuple(元组) 一般使用中,假设一个函数返回多个值,我们可以使用tuple接受这个(val (x,y) = myfunction) package com.wls.scala.hello /* ...
- QEMU漏洞挖掘
转载:https://www.tuicool.com/articles/MzqYbia qemu是一个开源的模拟处理器硬件设备的全虚拟化仿真器和虚拟器. KVM(kernel virtual mach ...
- oracle11g创建修改删除表
oracle11g创建修改删除表 我的数据库名字: ORCL 密码:123456 1.模式 2.创建表 3.表约束 4.修改表 5.删除表 1.模式 set oracle_sid=OR ...