【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. mysql 类型

    1.bigint 范围(-2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) ) 字节大小(8个字节) 2.int 范围(-2^31 ...

  2. Objective-C语法之扩展(Extension)的使用

    Objective-C 2.0增加了Class Extension用于解决两个问题: 可声明私有可读写的属性,而在类的声明中是同名的公开只读属性,从而来支持公开只读.私有可读写的属性 可声明私有方法: ...

  3. 为Hadoop集群选择合适的硬件配置

    随着Apache Hadoop的起步,云客户的增多面临的首要问题就是如何为他们新的的Hadoop集群选择合适的硬件. 尽管Hadoop被设计为运行在行业标准的硬件上,提出一个理想的集群配置不想提供硬件 ...

  4. input checkbox复选框点击获取当前选中状态jquery

    function checkAll(id) { //用is判断 // let checkStatus=$(id).is(':checked'); // console.log(checkStatus) ...

  5. 在阿里云创建子域名,配置nginx,使用pm2部署node项目到ubuntu服务器

    配置域名 在阿里云找到主域名 进入主域名之后,右上角添加解析,添加子域名, 记录类型选择cname,主机记录填写子域名的名称,记录值为主域名,至此阿里云已经配置好了. 检查nginx安装 首先检查服务 ...

  6. 远程执行命令和文件分发shell脚本

    deploy.conf node01,all,other,datanode,journalnode,zookeeper, node02,all,other,datanode,journalnode,z ...

  7. 【剑指Offer学习】【面试题23:从上往下打印二叉树】

    题目:从上往下打印出二叉树的每一个结点,同一层的结点依照从左向右的顺序打印. 二叉树结点的定义: public static class BinaryTreeNode { int value; Bin ...

  8. linux定时任务cron配置[转]

    实现linux定时任务有:cron.anacron.at等,这里主要介绍cron服务. 名词解释: cron是服务名称,crond是后台进程,crontab则是定制好的计划任务表. 软件包安装: 要使 ...

  9. 绑定方式开始服务&调用服务的方法

    1.编写activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...

  10. SpringBoot application.properties (application.yml)优先级从高到低

    SpringBoot application.properties(application.yml) 优先级从高到低 SpringBoot配置文件优先级从高到低 =================== ...