HDU(1698),线段树区间更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
区间更新重点在于懒惰标记。
当你更新的区间就是整个区间的时候,直接sum[rt] = c*(r-l+1);col[rt] = c;后面的子区间就不管了,当你下次更新某一个区间的时候,把col[rt]从顶往下推(也没有推到底),推到合适的位置,刚好这个位置是我要更新的区间的子区间(可能被横跨了)就停下来。
在这里感谢网上的大牛,感谢杰哥,彬哥。我才能AC。
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27760 Accepted Submission(s):
13778
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.
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.
the total value of the hook after the operations. Use the format in the
example.
10
2
1 5 2
5 9 3
#include <stdio.h>
#include <algorithm> using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn = ; int sum[maxn<<];
int col[maxn<<]; void PushUp(int rt)
{
sum[rt] = sum[rt<<] + sum[rt<<|];
} void PushDown(int rt,int m)
{
if(col[rt])
{
col[rt<<] = col[rt<<|] =col[rt]; //往下推
sum[rt<<] = (m-(m>>))*col[rt]; //子区间更新
sum[rt<<|] = (m>>)*col[rt];
col[rt] = ; //该点已经推了
}
} void build(int l,int r,int rt)
{
col[rt] = ;
sum[rt] = ;
if(l==r) return;
int m=(l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt] = c;
sum[rt] = c*(r-l+);
return ;
}
PushDown(rt,r-l+);
int m = (l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
PushUp(rt);
} int main()
{
int T,n,m;
scanf("%d",&T);
for(int cases=;cases<=T;cases++)
{
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",cases,sum[]);
}
return ;
}
HDU(1698),线段树区间更新的更多相关文章
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- hdu 1698 线段树 区间更新 区间求和
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 1698 (线段树 区间更新) Just a Hook
有m个操作,每个操作 X Y Z是将区间[X, Y]中的所有的数全部变为Z,最后询问整个区间所有数之和是多少. 区间更新有一个懒惰标记,set[o] = v,表示这个区间所有的数都是v,只有这个区间被 ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- HDU 3016 线段树区间更新+spfa
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 1698 线段树 区间修改
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
- Just a Hook HDU - 1698Just a Hook HDU - 1698 线段树区间替换
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...
随机推荐
- 多线程编程_CyclicBarrier
1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 Cycl ...
- Hadoop2.X分布式集群部署
本博文集群搭建没有实现Hadoop HA,详细文档在后续给出,本次只是先给出大概逻辑思路. (一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 基于A ...
- http学习笔记(二):URL和资源
2.1浏览因特网资源 ------------总结:方案.主机.路径 方案:http,FTP,SMTP等 http(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连 ...
- stm32 定时器初步
今天学习STM32 的通用定时器:TIMx (TIM2.TIM3.TIM4 和 TIM5). 定时器比较复杂,功能繁多,这里我们就说最基本的计数功能. 1.定时器的时钟从哪里来? 定时器的时钟来源有 ...
- [转] asp.net core Introducing View Components
本文转自:http://www.c-sharpcorner.com/uploadfile/8c19e8/asp-net-5-getting-started-with-asp-net-mvc-6/ In ...
- SQL Server 创建用户
增加角色 role_for_nc 1.exec sp_addrole 'role_for_nc'; 创建一个 SQL Server 登录名wlzx,密码为"123",默认数据库为 ...
- git 提交各种情况下的处理方式
自己总结: 01.若在提交过程中有冲突,解决冲突后,git add . git rebase —continue git push for 02.git rebase vs git merge g ...
- SublimeText插件 : sass语法高亮
安装 :sublime中安装sass插件和sass build插件 就可以高亮显示sass 步骤:安装 sass插件和sass build (安装过程不细说) 步骤2:打开.sass文件,进行以下设 ...
- (生产)axios - 请求接口
参考:https://www.awesomes.cn/repo/mzabriskie/axios axios 介绍 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中 ...
- thinkphp 创建数据对象之data方法
创建数据对象:data()方法 1.功能:给模型对象$data赋值,将模型对象转化为数据对象 tip:模型对象与数据对象之间就差一个赋过值的$data; 2.方法:data()其源码如下: tip:源 ...