【bzoj4355】Play with sequence 线段树区间最值操作
题目描述
输入
输出
样例输入
5 3
6 4 6 6 4
2 1 5 -5
1 3 4 4
3 1 5
样例输出
2
题解
线段树区间最值操作
考虑到操作1的 $C\le 0$ ,因此 $0$ 只可能出现在最小值。所以要统计 $0$ 的个数,只需要统计:最小值是不是0、最小值个数即可。
对于操作1直接区间赋值,操作2我们拆成两个操作:区间+C直接加,区间最大值操作参考 吉老师的Segment tree Beats! ,维护最小值、严格次小值即可。
注意标记下传顺序:区间赋值>区间加>区间最大操作。
同样,区间最大操作可以不维护标记,直接下传最小值。
时间复杂度 $O(n\log^2 n)$ (吉老师表示PPT里的证明是萎的...复杂度证明参考集训队论文)
#include <cstdio>
#include <algorithm>
#define N 1200010
#define inf 1ll << 62
#define lson l , mid , x << 1
#define rson mid + 1 , r , x << 1 | 1
using namespace std;
typedef long long ll;
ll mn[N] , se[N] , cov[N] , add[N];
int mc[N];
inline void pushup(int x)
{
int ls = x << 1 , rs = x << 1 | 1;
if(mn[ls] < mn[rs]) mn[x] = mn[ls] , mc[x] = mc[ls] , se[x] = min(se[ls] , mn[rs]);
if(mn[ls] > mn[rs]) mn[x] = mn[rs] , mc[x] = mc[rs] , se[x] = min(mn[ls] , se[rs]);
if(mn[ls] == mn[rs]) mn[x] = mn[ls] , mc[x] = mc[ls] + mc[rs] , se[x] = min(se[ls] , se[rs]);
}
inline void pushdown(int l , int r , int x)
{
int ls = x << 1 , rs = x << 1 | 1;
if(~cov[x])
{
int mid = (l + r) >> 1;
mn[ls] = cov[x] , mc[ls] = mid - l + 1 , se[ls] = inf , cov[ls] = cov[x] , add[ls] = 0;
mn[rs] = cov[x] , mc[rs] = r - mid , se[rs] = inf , cov[rs] = cov[x] , add[rs] = 0;
cov[x] = -1;
}
if(add[x])
{
mn[ls] += add[x] , se[ls] += add[x] , add[ls] += add[x];
mn[rs] += add[x] , se[rs] += add[x] , add[rs] += add[x];
add[x] = 0;
}
if(mn[ls] < mn[x]) mn[ls] = mn[x];
if(mn[rs] < mn[x]) mn[rs] = mn[x];
}
void build(int l , int r , int x)
{
cov[x] = -1;
if(l == r)
{
scanf("%lld" , &mn[x]) , mc[x] = 1 , se[x] = inf;
return;
}
int mid = (l + r) >> 1;
build(lson) , build(rson);
pushup(x);
}
void cover(int b , int e , ll c , int l , int r , int x)
{
if(b <= l && r <= e)
{
mn[x] = c , mc[x] = r - l + 1 , se[x] = inf , cov[x] = c , add[x] = 0;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) cover(b , e , c , lson);
if(e > mid) cover(b , e , c , rson);
pushup(x);
}
void update(int b , int e , ll a , int l , int r , int x)
{
if(b <= l && r <= e)
{
mn[x] += a , se[x] += a , add[x] += a;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) update(b , e , a , lson);
if(e > mid) update(b , e , a , rson);
pushup(x);
}
void vmax(int b , int e , int l , int r , int x)
{
if(mn[x] >= 0) return;
if(b <= l && r <= e && se[x] > 0)
{
mn[x] = 0;
return;
}
pushdown(l , r , x);
int mid = (l + r) >> 1;
if(b <= mid) vmax(b , e , lson);
if(e > mid) vmax(b , e , rson);
pushup(x);
}
int query(int b , int e , int l , int r , int x)
{
if(b <= l && r <= e) return mn[x] ? 0 : mc[x];
pushdown(l , r , x);
int mid = (l + r) >> 1 , ans = 0;
if(b <= mid) ans += query(b , e , lson);
if(e > mid) ans += query(b , e , rson);
return ans;
}
int main()
{
int n , m , opt , x , y;
ll z;
scanf("%d%d" , &n , &m);
build(1 , n , 1);
while(m -- )
{
scanf("%d%d%d" , &opt , &x , &y);
if(opt == 1) scanf("%lld" , &z) , cover(x , y , z , 1 , n , 1);
if(opt == 2) scanf("%lld" , &z) , update(x , y , z , 1 , n , 1) , vmax(x , y , 1 , n , 1);
if(opt == 3) printf("%d\n" , query(x , y , 1 , n , 1));
}
return 0;
}
【bzoj4355】Play with sequence 线段树区间最值操作的更多相关文章
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 【bzoj4695】最假女选手 线段树区间最值操作
题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- HDU 3911 Black And White(线段树区间合并+lazy操作)
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- C# Disable CTRL-ALT-DEL, ALT-TAB, ALT-F4, Start Menu and so on…
使用C#禁用系统的某些特定按键 原文地址:http://www.tamas.io/c-disable-ctrl-alt-del-alt-tab-alt-f4-start-menu-and-so-on/ ...
- Java SE教程
第0讲 开山篇 读前介绍:本文中如下文本格式是超链接,可以点击跳转 >>超链接<< 我的学习目标:基础要坚如磐石 代码要十份规范 笔记要认真详实 一.java内容介绍 ...
- Spring中的TransactionProxyFactoryBean作用及配置(转)
问: 原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703 看AOP的时候发现spring中有个org.springframework. ...
- 【转】自动化测试 - Appium + Python史上最全最简环境搭建步骤
一,为什么是Appium借一张图: 1.1 Appium优点 l 开源 l 跨架构:NativeApp.Hybird App.Web App l 跨设备:Android.iOS.Firefox ...
- java.lang.Boolean.valueOf(String s)
简单说,就是s为true(这四个字母大小写任意)时,返回值为true,否则为false public class one { public static void main(String[] args ...
- Google TensorFlow for GPU安装、配置大坑
Google TensorFlow for GPU安装.配置大坑 从本周一开始(12.05),共4天半的时间,终于折腾好Google TensorFlow for GPU版本,其间跳坑无数,摔得遍体鳞 ...
- Phaser游戏框架与HTML Dom元素之间的通信交互
本想按照PHASER的HTML Dom元素官方实例:http://labs.phaser.io/index.html?dir=game%20objects/dom%20element/&q= ...
- Oracle数据库及图形化界面安装教程详解
百度云盘oracle数据库及图形化界面安装包 链接: https://pan.baidu.com/s/1DHfui-D2n1R6_ND3wDziQw 密码: f934 首先在电脑D盘(或者其他不是C盘 ...
- VPS挂机赚美刀详细介绍–Alexamaster操作流程
跟 vps 主机打交道时间长了,手里也渐渐积累了些闲置的 vps.让它们这么闲着吧,感觉有些浪费资源:用起来吧,暂时又没有好的项目.一直听说通过 vps挂机可以赚回主机成本,甚至可以盈利.正好这两天有 ...
- python—2.x中如何使用中文
python2.x 默认使用ASCII编码格式 python3.x 默认使用UTF-8编码格式 在python2.x文件的第一行增加一下代码,解释器会以utf-8编码来处理python文件. # *_ ...