【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. UNIX环境编程学习笔记(14)——文件I/O之临时文件

    lienhua342014-10-01 ISO C 标准 I/O 库提供了个两个函数 tmpnam 和 tmpfile 以帮助创建临时文件, #include <stdio.h> char ...

  2. SSH上传和下载文件

    备个份: 一,ssh上传文件 scp file username@hostIP:文件地址 例: [zhangy@BlackGhost ~]$ scp test.sql zhangying@192.16 ...

  3. 黏性Session和非黏性Session

    黏性Session和非黏性Session黏性Session:此模式下同一会话中的请求都被派送到同一个tomcat实例上,这样我们就无须在多台服务器之间实现session共享了,这是其好处,不好的地方就 ...

  4. MathType怎么编辑半开半闭区间

    数学中的公式有很多,涉及到各种各样的样式,这些公式都会用到不同的符号,每一个符号用在不同数学问题的公式中,都会有其特定的意义,比如括号.括号这个符号在除了能够表示优先运算之外,还可以代表区间的意思,小 ...

  5. mysql执行SQL语句时报错:[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space left on device)

    问题描述: 今天一同事在mysql中执行SQL语句的时候,报了/tmp空间不足的问题,报错如下: [SQL] SELECT f.prov as 字段1, MAX( CASE f.flag_name W ...

  6. 关于Android中Animation的停止【转载】

    转载自:http://blog.csdn.net/easonx1990/article/details/8231520 最近遇到一个需求,通过在GridView上改变焦点,并且GridView上每个i ...

  7. Cordova 问题点备忘

    1 cordova File插件问题 cordova 5.0创建本地文件夹 目录变成了 file:///data/user/0/com.xxx.xxx/xxx 4.0 是 file:///storag ...

  8. HTML 引用

    关于 HTML 引用: (1) <q> 和 <blockquote> 用于实现长短不一的引用语(2) <q> 用于短的引用,<blockquote> 用 ...

  9. codeblocks编译pthread多线程问题

    默认的编译选项是没有pthread的,所以要自己添加: 参考:http://hi.baidu.com/u_soa/item/9d6cc40b7e9d76eb3499024d 错误: undefined ...

  10. 如何在taro的map循环中使用if条件渲染

    在taro的jsx中,鉴于编译的机制,官方明确的表示了不能在map循环中使用if循环, 但是呢,官方也给出了解决办法,那就是提取变量或者是用三目运算嵌套的方法: 链接奉上:https://github ...