题目链接: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 线段树(区间更新)的更多相关文章

  1. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  2. hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  3. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  4. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  5. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  6. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  7. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  8. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  9. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

随机推荐

  1. C# Object reference not set to an instance of an object.

    一.问题 Object reference not set to an instance of an object. (你调用的对象是空的) 二.解决问题 在使用 c# 的查询时,先筛选后在关联其他表 ...

  2. redis命令集

    查看使用运行服务:ping 关闭服务的连接:quit 切换数据库:select index 连接: redis-cli -h -a myPassword 查看密码: config get requir ...

  3. ARC085F(动态规划,线段树)

    #include<bits/stdc++.h>using namespace std;const int maxn = 0x3f3f3f3f;int mn[801000];int cost ...

  4. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

  5. PAT天梯赛L2-007 家庭房产

    题目链接:点击打开链接 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下列 ...

  6. thinkphp5引入百度编辑器

    在ThinkPHP的模板(html文件)中引入Ueditor  下载ueditor解压至public/static目录 在需要的页面引入js文件 <script type="text/ ...

  7. 关于递归函数返回值为null的问题

    public function gettopcateid($cate_id){ $pid=db('cate')->where('cate_id',$cate_id)->find(); if ...

  8. mysql执行计划 const eq_ref ref range index all

    explain:查询查询性能或者需要查看使用索引状态 一.type:连接类型  最关键的一列  效率(const>eq_ref>ref>range>index>all) ...

  9. object.wait为什么要和synchronized一块使用

    Object.wait 中JDK提供的doc文档 Causes the current thread to wait until another thread invokes the notify() ...

  10. Appium + Python自动化3 - 输入中文

    在做app自动化过程中会踩很多坑,咱们都是用中文的app,所以首先要解决中文输入的问题!本篇通过屏蔽软键盘,绕过手机的软键盘方法,解决中文输入问题. 一.定位搜索 1.打开淘宝点击搜索按钮,进入搜索页 ...