BZOJ4355: Play with sequence
BZOJ4355: Play with sequence
https://lydsy.com/JudgeOnline/problem.php?id=4355
分析:
- 模板题。
- 把\(2\)操作看成先区间加再区间取\(max\)。
- 查询转化成求最小值个数。
- 需要维护\(3\)个标记。
- 这里我比较naive地维护了三个标记,多维护了一个最小值需要加的值。
- 然后下传的时候对左右儿子判断是否应该下传。
- 实际上不需要,直接修改最小值即可。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define N 1200050
#define ls p<<1
#define rs p<<1|1
typedef long long ll;
const ll inf = 1ll<<60;
int n,m;
ll a[N],mn[N],ci[N],tag1[N],tag2[N],tag3[N];
int cnt[N],len[N];
void giv1(int p,ll v) {
tag1[p]=v;
tag2[p]=0;
tag3[p]=0;
mn[p]=v;
ci[p]=inf;
cnt[p]=len[p];
}
void giv2(int p,ll v) {
tag2[p]+=v;
mn[p]+=v;
ci[p]+=v;
}
void giv3(int p,ll v) {
tag3[p]+=v;
mn[p]+=v;
}
void pushdown(int p) {
if(tag1[p]!=-1) giv1(ls,tag1[p]),giv1(rs,tag1[p]),tag1[p]=-1;
if(tag2[p]) giv2(ls,tag2[p]),giv2(rs,tag2[p]),tag2[p]=0;
if(tag3[p]) {
if(mn[ls]+tag3[p]==mn[p]) giv3(ls,tag3[p]);
if(mn[rs]+tag3[p]==mn[p]) giv3(rs,tag3[p]);
tag3[p]=0;
}
}
void pushup(int p) {
mn[p]=min(mn[ls],mn[rs]);
if(mn[ls]==mn[rs]) ci[p]=min(ci[ls],ci[rs]),cnt[p]=cnt[ls]+cnt[rs];
else {
if(mn[ls]<mn[rs]) ci[p]=min(ci[ls],mn[rs]),cnt[p]=cnt[ls];
else ci[p]=min(ci[rs],mn[ls]),cnt[p]=cnt[rs];
}
}
void build(int l,int r,int p) {
tag1[p]=-1;
len[p]=r-l+1;
if(l==r) {
mn[p]=a[l];
ci[p]=inf;
cnt[p]=1;
return ;
}
int mid=(l+r)>>1;
build(l,mid,ls); build(mid+1,r,rs);
pushup(p);
}
void updadd(int l,int r,int x,int y,int v,int p) {
if(x<=l&&y>=r) {
giv2(p,v); return ;
}
int mid=(l+r)>>1;
pushdown(p);
if(x<=mid) updadd(l,mid,x,y,v,ls);
if(y>mid) updadd(mid+1,r,x,y,v,rs);
pushup(p);
}
void updcov(int l,int r,int x,int y,int v,int p) {
if(x<=l&&y>=r) {
giv1(p,v); return ;
}
int mid=(l+r)>>1;
pushdown(p);
if(x<=mid) updcov(l,mid,x,y,v,ls);
if(y>mid) updcov(mid+1,r,x,y,v,rs);
pushup(p);
}
void updmax(int l,int r,int x,int y,int p) {
if(mn[p]>=0) return ;
if(x<=l&&y>=r&&ci[p]>0) {
giv3(p,-mn[p]); return ;
}
if(l==r) {giv1(p,0); return ;}
int mid=(l+r)>>1;
pushdown(p);
if(x<=mid) updmax(l,mid,x,y,ls);
if(y>mid) updmax(mid+1,r,x,y,rs);
pushup(p);
}
int query(int l,int r,int x,int y,int p) {
if(x<=l&&y>=r) return (mn[p]==0)*cnt[p];
int mid=(l+r)>>1,re=0;
pushdown(p);
if(x<=mid) re+=query(l,mid,x,y,ls);
if(y>mid) re+=query(mid+1,r,x,y,rs);
return re;
}
int main() {
scanf("%d%d",&n,&m);
int i,opt,x,y,z;
for(i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,n,1);
while(m--) {
scanf("%d%d%d",&opt,&x,&y);
if(opt==1) {
scanf("%d",&z);
updcov(1,n,x,y,z,1);
}else if(opt==2) {
scanf("%d",&z);
updadd(1,n,x,y,z,1);
updmax(1,n,x,y,1);
}else {
printf("%d\n",query(1,n,x,y,1));
}
}
}
BZOJ4355: Play with sequence的更多相关文章
- 2018.07.30 bzoj4355: Play with sequence(线段树)
传送门 维护区间覆盖成非负数,区间变成max(xi+a,0)" role="presentation" style="position: relative;&q ...
- BZOJ4355: Play with sequence(吉司机线段树)
题意 题目链接 Sol 传说中的吉司机线段树??感觉和BZOJ冒险那题差不多,就是强行剪枝... 这题最坑的地方在于对于操作1,$C >= 0$, 操作2中需要对0取max,$a[i] > ...
- bzoj 4695: 最假女选手 && Gorgeous Sequence HDU - 5306 && (bzoj5312 冒险 || 小B的序列) && bzoj4355: Play with sequence
算导: 核算法 给每种操作一个摊还代价(是手工定义的),给数据结构中某些东西一个“信用”值(不是手动定义的,是被动产生的),摊还代价等于实际代价+信用变化量. 当实际代价小于摊还代价时,增加等于差额的 ...
- bzoj4355 Play with sequence(吉司机线段树)题解
题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 赋值为\(x\) \(2.\)区间\([L,R]\) 赋值为\(max(a[i] + x, 0)\) \(3.\)区间 ...
- Segment Tree Beats 区间最值问题
Segment Tree Beats 区间最值问题 线段树一类特殊技巧! 引出:CF671C Ultimate Weirdness of an Array 其实是考试题,改题的时候并不会区间取最值,区 ...
- 【BZOJ4355】Play with sequence 线段树
[BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...
- 【bzoj4355】Play with sequence 线段树区间最值操作
题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...
- BZOJ4355:Play with sequence——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4355 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1] ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
随机推荐
- Django 进阶篇之 Form验证
Django Form验证 在实际的生产环境中比如登录和验证的时候,我们一般都使用Jquery+ajax来判断用户的输入是否为空,假如JS被禁用的话,咱们这个认证屏障是不是就消失了呢?(虽然一般不会禁 ...
- 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...
- Linux基础三---打包压缩&vim&系统的初始化和服务
一,常用命令——tar&vim 1. tar [参数] 文件名 [路径] 参数: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令! ...
- PHP的异常处理、错误的抛出及错误回调函数
一.错误.异常和等级常量表 error:不能再编译期发现运行期的错误,不如试图echo输出一个未赋值的变量,这类问题往往导致程序或逻辑无法继续下去而需要中断. exception:程序执行过程中出现意 ...
- nodejs实现静态托管
const express = require("express"); const app = express(); /* 语法1: app.use(express.static( ...
- Eclipse SVN插件设置
项目开发中,开发人员用SVN来管理代码,在和服务器同步时,需要避免上传不必要的一些编译文件,如.class,.log,target等文件,这里需要设置同步选项. 打开Eclipse ---> W ...
- 手把手教你使用eclipse+qemu+gdb来单步调试ARM内核【学习笔记】
平台信息:linux4.0 平台:qemu 作者:庄泽彬 说明:笨叔叔的Linux视频的笔记 一.编译linux源码 export CROSS_COMPILE=arm-linux-gnueabi- e ...
- JSP DAO(Model)
示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...
- Oracle数据库连接生成DataX的job-Json
package com.bbkj.main; import com.bbkj.DbUtils.ConnectionPoolManager; import com.bbkj.DbUtils.DbUtil ...
- Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'company' in 'class java.lang.String'
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...