【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. zabbix监控系列(5)之通过trap模式监控网络设备

  2. Ubuntu18.10下安装Qt5.12过程记录

    首先你得先安装Ubuntu操作系统(我是在VMWare14中安装的Ubuntu18.10版本). 阿里镜像:https://opsx.alibaba.com/mirror 我这里下载的文件为:ubun ...

  3. repo_file_in_folder

    -- Create table create table repo_file ( uuid ), create_time ), creator ), modify_time ), modifier ) ...

  4. 在Ubuntu中开启Soft AP功能

    在Ubuntu中开启Soft AP功能 1.查看采用的无线网卡是否支持Soft AP: 注意,可以看到有AP字样,表明支持.楼主比较背,在易迅上挑了个销量最高的netcore nw360,结果无法搭建 ...

  5. Dubbo -- 系统学习 笔记 -- 示例 -- 分组聚合

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 分组聚合 按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用group区分 ...

  6. php大数除法保留精度问题

    有人在群里问大数除法,要求保留精度的问题,发现普通的方法都不能保存精度,最后找了一下资料发现可以这样 这倒是个冷门知识,嗯哼

  7. ArcGIS应用

    1.ArcGIS Server发布资源报错:网络资源问题 有可能是跟网络相关的服务没有开启,开启相关服务器后有可能可以解决此问题. 还有可能通过此法解决:开始--控制面板--网络和共享中心--高级共享 ...

  8. hive-数据模型

    hive支持四种数据模型 • external table• table• partition• bucket 为了避免table名称冲突,hive用database作为顶层域名,如果不设定datab ...

  9. 【安全开发】Perl安全编码规范

    多年以来,Perl已经成为用于系统管理和WebCGI开发的功能最强的编程语言之一(几乎可以使用Perl做任何功能的程序).但其扩展应用,即作为Internet上CGI的开发工具,使得它经常成为Web服 ...

  10. 一句话木马:ASPX篇

    aspx木马收集: <%@ Page Language="Jscript"%><%eval(Request.Item["chopper"],& ...