【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. Greenplum-cc-web监控软件安装时常见错误

     错误error: 1.no pg_hba.conf entry for host “::1”, user “gpmon”, database “gpperfmon”, SSL off 解决: vi ...

  2. javascript报错集锦

    1.JS 异常之 missing ) after argument list 错误释疑报错原因:不是字符串就输出啦

  3. 浮点数向偶数舍入的问题 Round-to-Even for Floating Point

    Round-To-Even在于To-Up , To-Down, To-towards-Zero对比中,在一定数据量基础上,更加精准.To-Up的平均值比真实数值偏大,To-Down偏小.   例如有效 ...

  4. NSIS 资料

    官方 http://nsis.sourceforge.net/Main_Page NSIS官方插件全集 http://az.eliang.com/aq_2013041703.html NSIS 衿华客 ...

  5. What's new in JDK 8

    (1)http://docs.oracle.com/javase/8/docs/technotes/guides/language/enhancements.html#javase8 (2)https ...

  6. 存储过程打印超过8000的VARCHAR字符的问题

    DECLARE @info NVARCHAR(MAX) --SET @info to something big PRINT CAST(@info AS NTEXT) 这样就可以输出超过8000的字符 ...

  7. Springboot @webfilter @order filter过滤器

    我们使用@WebFilter注解的时候发现注解里面没有提供可以控制执行顺序的参数 @WebFilter 的属性 属性名 类型 描述 filterName String 指定过滤器的 name 属性,等 ...

  8. Git学习笔记(三)

    Git提交相关内容 在Git提交时,会保存一个提交对象,该对象包括一个指向暂存区内容快照的指针,包括本次提交作者等相关附属信息,包括零个或多个指向该提交对象的父对象指针:首次提交时是没有祖先,普通提交 ...

  9. 8 -- 深入使用Spring -- 8... Spring整合Hibernate

    8.8 Spring整合Hibernate 8.8.1 Spring提供的DAO支持 8.8.2 管理Hibernate的SessionFactory 8.8.3 实现DAO组件的基类 8.8.4 传 ...

  10. DropDownListFor的种种纠结(禁止转载)

    严重禁止转载,好多爬虫软件为了浏览到处抓东西,真缺德 具有键“CorpType”的 ViewData 项属于类型“System.Int64”,但它必须属于类型“IEnumerable<Selec ...