【BZOJ4355】Play with sequence

Description

维护一个长度为N的序列a,现在有三种操作:
1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C。
2)给出参数U,V,C,对于区间[U,V]里的每个数i,将a[i]赋值为max(a[i]+C,0)。
3)给出参数U,V,输出a[U],a[U+1],...,a[V-1],a[V]里值为0的数字个数。

Input

第一行包含两个正整数N,M(1<=N,M<=300000),分别表示序列长度和操作个数。
第二行包含N个整数,其中第i个数表示a[i](0<=a[i]<=10^9),描述序列的初始状态。
接下来M行描述M个操作,保证1<=U<=V<=N,对于操作1,0<=C<=10^9,对于操作2,|C|<=10^9。

Output

输出若干行,每行一个整数,依次回答每个操作3的问题。

Sample Input

5 3
6 4 6 6 4
2 1 5 -5
1 3 4 4
3 1 5

Sample Output

2

题解:懒了直接粘题解+证明

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <utility>
#define lson x<<1
#define rson x<<1|1
#define mp(A,B) make_pair(A,B)
#define F first
#define S second
using namespace std;
const int maxn=300010;
typedef long long ll;
typedef pair<ll,int> pli;
const ll NON=123456789123456ll;
ll n1[maxn<<2],n2[maxn<<2],ts[maxn<<2],tc[maxn<<2],tn[maxn<<2];
int cnt[maxn<<2];
ll v[maxn];
int n,m;
inline void add(int x,int l,int r,ll v)
{
n1[x]+=v,n2[x]+=v;
if(tc[x]!=NON) tc[x]+=v;
else ts[x]+=v;
if(tn[x]!=NON) tn[x]+=v;
}
inline void cov(int x,int l,int r,ll v)
{
n1[x]=v,n2[x]=NON,cnt[x]=r-l+1,ts[x]=0,tc[x]=v,tn[x]=NON;
}
inline void con(int x,int l,int r,ll v)
{
if(n1[x]<v) n1[x]=v;
if(tn[x]==NON||tn[x]<v) tn[x]=v;
}
inline void pushup(int x)
{
if(n1[lson]==n1[rson]) n1[x]=n1[lson],n2[x]=min(n2[lson],n2[rson]);
else if(n1[lson]<n1[rson]) n1[x]=n1[lson],n2[x]=min(n2[lson],n1[rson]);
else n1[x]=n1[rson],n2[x]=min(n1[lson],n2[rson]);
cnt[x]=0;
if(n1[x]==n1[lson]) cnt[x]+=cnt[lson];
if(n1[x]==n1[rson]) cnt[x]+=cnt[rson];
}
inline void pushdown(int l,int r,int x)
{
int mid=(l+r)>>1;
if(ts[x]) add(lson,l,mid,ts[x]),add(rson,mid+1,r,ts[x]),ts[x]=0;
if(tc[x]!=NON) cov(lson,l,mid,tc[x]),cov(rson,mid+1,r,tc[x]),tc[x]=NON;
if(tn[x]!=NON) con(lson,l,mid,tn[x]),con(rson,mid+1,r,tn[x]),tn[x]=NON;
}
void upadd(int l,int r,int x,int a,int b,ll v)
{
if(a<=l&&r<=b)
{
add(x,l,r,v);
return ;
}
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=mid) upadd(l,mid,lson,a,b,v);
if(b>mid) upadd(mid+1,r,rson,a,b,v);
pushup(x);
}
void upcov(int l,int r,int x,int a,int b,ll v)
{
if(a<=l&&r<=b)
{
cov(x,l,r,v);
return ;
}
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=mid) upcov(l,mid,lson,a,b,v);
if(b>mid) upcov(mid+1,r,rson,a,b,v);
pushup(x);
}
void upcon(int l,int r,int x,int a,int b,ll v)
{
if(a<=l&&r<=b)
{
if(v<=n1[x]) return ;
if(v<n2[x])
{
con(x,l,r,v);
return ;
}
}
pushdown(l,r,x);
int mid=(l+r)>>1;
if(a<=mid) upcon(l,mid,lson,a,b,v);
if(b>mid) upcon(mid+1,r,rson,a,b,v);
pushup(x);
}
pli query(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return mp(n1[x],cnt[x]);
pushdown(l,r,x);
int mid=(l+r)>>1;
if(b<=mid) return query(l,mid,lson,a,b);
if(a>mid) return query(mid+1,r,rson,a,b);
pli sl=query(l,mid,lson,a,b),sr=query(mid+1,r,rson,a,b),ret;
if(sl.F==sr.F) ret.F=sl.F,ret.S=sl.S+sr.S;
else if(sl.F<sr.F) ret=sl;
else ret=sr;
return ret;
}
void build(int l,int r,int x)
{
tc[x]=tn[x]=NON;
if(l==r)
{
n1[x]=v[l],n2[x]=NON,cnt[x]=1;
return ;
}
int mid=(l+r)>>1;
build(l,mid,lson),build(mid+1,r,rson);
pushup(x);
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd();
int i,a,b,c,op;
for(i=1;i<=n;i++) v[i]=rd();
build(1,n,1);
for(i=1;i<=m;i++)
{
op=rd(),a=rd(),b=rd();
if(op==1) c=rd(),upcov(1,n,1,a,b,c);
if(op==2) c=rd(),upadd(1,n,1,a,b,c),upcon(1,n,1,a,b,0);
if(op==3)
{
pli tmp=query(1,n,1,a,b);
if(tmp.F) puts("0");
else printf("%d\n",query(1,n,1,a,b).S);
}
}
return 0;
}//4 3 9 5 7 4 1 3 3 2 2 1 3 -7 3 2 4

【BZOJ4355】Play with sequence 线段树的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. 【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]里的每个数 ...

  3. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  4. 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 ...

  5. hdu4893Wow! Such Sequence! (线段树)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  6. HDU 6047 Maximum Sequence(线段树)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...

  7. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  8. hdu 5828 Rikka with Sequence 线段树

    Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...

  9. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  10. hdu-5805 NanoApe Loves Sequence(线段树+概率期望)

    题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 262144/131072 ...

随机推荐

  1. jekins job configure找不到remote trigger(script)

    今天想测试一下,remote的方式启动一个job,但是在“构建触发器”一栏根本找不到remote trigger,很惊讶的是在网上所有的doc或者demo里都是有这个选项的. 最后,终于找到了原因: ...

  2. linux如何通过脚本来修改用户的密码?脚本自动化修改用户密码?

    需求描述: linux环境中在创建用户的时候,涉及到修改用户的密码,一般是通过passwd命令进行修改,需要多次的确认,这里考虑通过一条命令直接对密码进行修改 不需要进行交互的方式.在此记录. 操作过 ...

  3. mysql数据库中,如何对json数据类型的值进行修改?通过json_set函数对json字段值进行修改?

    需求描述: 今天在看mysql中存放json数据类型的问题,对于json数据进行修改的操作, 在此记录下. 操作过程: 1.创建包含json数据类型的表,插入基础数据 mysql> create ...

  4. 自动构建工具Grunt

    摘要: 大部分项目在部署之前都需要做的就是js.css文件的压缩.合并,以及一些文件的错误检查,甚至是将LESS文件转换成css文件,coffeescript文件转化成js文件等等.但是项目开发是分迭 ...

  5. Java按钮控件数组实现计算器界面

    编写程序,通过按钮数组来管理界面中的所有按钮控件,从而使用最少的代码实现模拟的计算器界面. 思路如下: 创建一个类,通过extends使其继承窗体类JFrame: 创建一个JFrame对象,使用JFr ...

  6. 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)

    (接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...

  7. MvvmLight学习篇—— Mvvm Light Toolkit for wpf/silverlight系列(导航)

    系列一:看的迷迷糊糊的 一.Mvvm Light Toolkit for wpf/silverlight系列之准备工作 二.Mvvm Light Toolkit for wpf/silverlight ...

  8. mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置

    mybatis generator配置,Mybatis自动生成文件配置,Mybatis自动生成实体Bean配置 ============================== 蕃薯耀 2018年3月14 ...

  9. Oracle之表空间基于时间点的恢复

    记一次优化过程中:一次误操作,在不影响其他表空间的情况下:采用表空间基于时间点的恢复(TSPITR)方法恢复数据的过程. 1.TSPITR恢复原理    TSPITR目前最方便的方法是使用RMAN进行 ...

  10. 【代码审计】MIPCMS 远程写入配置文件Getshell

    0x00 环境准备 MIPCMS官网:https://www.mipcms.cn 网站源码版本:MIPCMS内容管理系统 V3.1.0(发布时间:2018-01-01) 程序源码下载:http://w ...