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 ...
随机推荐
- 转 Relinking Causes Many Warning on AIX
SYMPTOMS Relink returns many warnings Running make for target ioracle OPatch found the word "er ...
- Redis:存储对象的两种方式(序列化和json字符串)
方式一:序列化操作 public class SerializeUtil { /* * 序列化 * */ public static byte[] serizlize(Object ...
- 从c到cpp对static 关键字的总结 需要整理下!!!!!!!!!!!!!!!!!!!!!!
一个完整的程序,在内存中的分布情况如下: 具体分布图 自己看书去!!!!!1.栈区: 由编译器自动分配释放,像局部变量,函数参数,都是在栈区.会随着作用于退出而释放空间.3.堆区:程序员分配并 ...
- sql server 笔记(数据类型/新建、修改、删除数据表/)
1.数据类型: Character 字符串 / Unicode 字符串 / Binary 类型 / Number 类型 / Date 类型 / 其他数据类型 详解:http://www.w3sc ...
- Building the main Guest Additions module [FAILED]
虚拟机中的centos7安装vbox的增强工具报错 Building the main Guest Additions module [FAILED] 查看日志发现 unable to find th ...
- ubuntu install fonts
sudo apt-get install ttf-wqy-zenhei
- 【密码学】RSA密钥长度、明文长度和密文长度
本文介绍RSA加解密中必须考虑到的密钥长度.明文长度和密文长度问题,对第一次接触RSA的开发人员来说,RSA算是比较复杂的算法,天缘以后还会补充几篇RSA基础知识专题文章,用最简单最通俗的语言描述RS ...
- Unity C# ref与out
ref和out 都是按地址传递的,使用后都将改变原来的数值.ref 方法参数关键字使方法引用传递到方法的同一个变量.当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中.若要使用 r ...
- Java Programming Guidelines
This appendix contains suggestions to help guide you in performing low-level program design and in w ...
- Android环信即时通讯集成坑爹 注册报错208解决
第一种情况: 你用的环信APPkey 是授权模式的 只要改成开放模式就可以了 第二种情况: 上述情况在环信官网都有说明 但是这种情况 环信官网就没得说明 只说的是同步方法 注册 ...