hihocoder 1080 线段树(区间更新)
题目链接:http://hihocoder.com/problemset/problem/1080 , 两种操作的线段树(区间更新)。
这道题前一段时间一直卡着我,当时也是基础不扎实做不出来,今天又想了想其实还是比较简单的,也只能怪自己太弱了。
这道题坑就坑在是有两个操作:set和add,所以lazy标记数组就需要两个,但是有一点要考虑的是一个区间上set与add的先后关系——如果对于一个区间set和add标记同时存在,那么应该如果处理:一种情况set在add之前,那么就按照正常顺序来就可以了;另一种情况add在set之前,那么这个add操作就取消,直接set操作就可以了。
所以更新线段树的一个区间的时候,如果是set操作,那么就把add的标记清理掉;如果是add操作,那么就正常进行。这样的话就保证两个标记同时出现的时候是先set再add的,这样的话就不会出错。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
#define eps 1e-8
#define INF INT_MAX
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int MOD = ;
const int maxn = + ;
const int N = + ;
int sum[maxn << ] , col_add[maxn << ] , col_set[maxn << ];
void PushUp(int rt)
{
sum[rt] = sum[rt << ] + sum[rt << | ];
}
void build(int l , int r , int rt)
{
if(l == r) {
scanf("%d" , &sum[rt]);
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
PushUp(rt);
}
void PushDown(int rt , int len)
{ //在PushDown()函数里就是先执行set操作再执行add操作
if(col_set[rt]) {
col_set[rt << ] = col_set[rt << | ] = col_set[rt];
col_add[rt << ] = col_add[rt << | ] = ; //同样清除掉add的标记,当时少了这句Debug了好久
sum[rt << ] = (len - (len >> )) * col_set[rt];
sum[rt << | ] = (len >> ) * col_set[rt];
col_set[rt] = ;
}
if(col_add[rt] != ) {
col_add[rt << ] += col_add[rt];
col_add[rt << | ] += col_add[rt];
sum[rt << ] += (len - (len >> )) * col_add[rt];
sum[rt << | ] += (len >> ) * col_add[rt];
col_add[rt] = ;
}
}
void update(int L , int R , int x , int ch , int l , int r , int rt)
{
if(L <= l && R >= r) {
if(ch) {
sum[rt] = (r - l + ) * x;
col_set[rt] = x;
col_add[rt] = ; //清除掉add的标记
} else {
sum[rt] += (r - l + ) * x;
col_add[rt] += x;
}
return;
}
PushDown(rt , r - l + );
int m = (l + r) >> ;
if(L > m)
update(L , R , x , ch , rson);
else if(R <= m)
update(L , R , x , ch , lson);
else {
update(L , R , x , ch , lson);
update(L , R , x , ch , rson);
}
PushUp(rt);
}
int main()
{
int n , m , a , b , c , ch;
scanf("%d %d" , &n , &m);
build( , n + , );
while(m--) {
scanf("%d %d %d %d" , &ch , &a , &b , &c);
update(a + , b + , c , ch , , n + , );
printf("%d\n" , sum[]);
}
return ;
}
hihocoder 1080 线段树(区间更新)的更多相关文章
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
随机推荐
- es6- Generator函数实现长轮询
1.Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同. 语法上,首先可以把它理解成,Generator 函数是一个状态机,封装了多个内部状态.形式上,Gene ...
- [Xcode 实际操作]四、常用控件-(7)UIStepper控件的使用
目录:[Swift]Xcode实际操作 本文将演示步进控件的基本用法.步进控件常用于小范围数值的调整. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import ...
- 在 .NET Framework 中使用 StringBuilder 类
在 .NET Framework 中使用 StringBuilder 类 String 对象是不可变的.每次使用 System.String 类中的一个方法时,都要在内存中创建一个新的字符串对象,这就 ...
- 前端页面使用ace插件优化脚本
html页面:<pre id="editor" style="width: 100%;height: 800px;"></pre>(注: ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 牛客练习赛41D(思维转化)
AC通道 要点 思路:题解中将所求进行转化\[max\{相似度\} = max\{M-不相似度\} = M-min\{不相似度\}\]因此转化为求某01串T与所给众S串的最小不相似度,而最终答案是选取 ...
- 线段树模板(单点更新,区间更新,RMQ)
Bryce1010模板 1.单点更新 说明 单点更新,区间求和(你问我单点求和??你就不会把区间长度设为0啊?) • sum[]为线段树,需要开辟四倍的元素数量的空间. • build()为建树操作 ...
- 2017年江西理工大学C语言程序设计竞赛(初级组)
问题 A: Petr的盒子(初) #include <iostream> #include <stdio.h> #include <algorithm> using ...
- 把查询出来的结果进行修改再赋值给list
List<RivBillNoPatternL> list = this.jdbcTemplate.getJdbcOperations().query(sqlSb.toString(), p ...
- spring boot(10) 基础学习内容
A Spring boot(10) 基础学习内容 B SpringBoot(16) 基础学习内容