【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 ...
随机推荐
- 20155339 《Java程序设计》实验五网络编程与安全实验报告
20155339 <Java程序设计>实验五网络编程与安全实验报告 实验内容 实验一 1.两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/67667 ...
- 20155339 2016-2017-2 《Java程序设计》第3周学习总结
20155339 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 认识对象 类与对象 1.定义类时使用class关键词再加名称,例如 class Cl ...
- 微信小程序标签页切换
WXML中: <view class="swiper-tab"> <view class="swiper-tab-list {{currentTab== ...
- KVM虚拟化的安装
kvm介绍 一.KVM的历史 Kvm在2007年2月正式合并Linux2.6.20核心中,成为内核源代码的一部分.2008年9月4日,RedHat公司收购了Qumranet,开始在RHEL中用kvm取 ...
- 创龙OMAPL138的SPI FLASH读写
1. 目前最大的疑问是OMAPL138和DSP6748的DSP部分是完全一样的吗(虽然知道芯片完全是引脚兼容的)?因此现在使用OMAPL138的DSP内核去读写一下外部的SPI FLASH芯片,先看下 ...
- dp合集 广场铺砖问题&&硬木地板
dp合集 广场铺砖问题&&硬木地板 很经典了吧... 前排:思想来自yali朱全民dalao的ppt百度文库免费下载 后排:STO朱全民OTZ 广场铺砖问题 有一个 W 行 H 列的广 ...
- [bzoj1500][luogu2042][cogs339][codevs1758]维修数列(维护数列)
先给自己立一个flag 我希望上午能写完 再立一个flag 我希望下午能写完. 再立一个flag 我希望晚上能写完... 我终于A了... 6700+ms...(6728) 我成功地立了3个flag. ...
- linux 同步 rsync的使用——远程服务器同步配置
一.背景介绍 由于需要和其他系统进行对接.文件的逻辑地址通过接口传递,而文件的实体需要通过服务器间的同步进行传输.在同事的建议下选择了rsync. 二.RSYNC介绍 RSYNC 有多种方式进行同步, ...
- selenium 各种很奇葩的异常
问题1:使用selenium3+java的脚本模拟登陆时,总是提示用户名,密码错误 解决方法:1 在执行输入用户名和密码的代码之前,加上driver.navigate().refresh(); QQ群 ...
- 中国天气网 城市代码 sql语句
mysql的 下载地址:http://download.csdn.net/detail/songzhengdong82/6252651