[题解]hdu5306 Gorgeous Sequence

Description

There is a sequence a of length n. We use ai to denote the i-th element in this sequence. You should do the following three types of operations to this sequence.

0 x y t: For every x≤i≤y, we use min(ai,t) to replace the original ai’s value.
1 x y: Print the maximum value of ai that x≤i≤y.
2 x y: Print the sum of ai that x≤i≤y

Input

The first line of the input is a single integer T, indicating the number of testcases.

The first line contains two integers n and m denoting the length of the sequence and the number of operations.

The second line contains n separated integers a1,…,an (∀1≤i≤n,0≤ai<231).

Each of the following m lines represents one operation (1≤x≤y≤n,0≤t<231).

It is guaranteed that T=100, ∑n≤1000000, ∑m≤1000000

Output

For every operation of type 1 or 2, print one line containing the answer to the corresponding query.

Sample Input

1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5

Sample Output

5
15
3
12

Solution

吉司机线段树模板,区间取 min
每个区间记录最大值 mx 、严格次大值 se 和最大值的个数t。
假设当前要对 x min,如果该区间 mxx 直接退出。如果该区间 se<x<mx ,直接利用 t mx修改当前区间的信息,打个标记退出。如果该区间 xse ,那么没法直接修改(当 x=se 时虽然可以维护区间和,但是没法维护次大值),我们就暴力递归左右区间。

代码:

#include<cstdio>
#include<algorithm>
using namespace std;

template<typename T>inline void read(T &x){
    T f=1;char ch=getchar();
    for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
    for(x=0;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
    x*=f;
}

typedef long long LL;
const int maxn=1000010,inf=0x7fffffff;
struct Segment_Tree{
    #define lc x<<1
    #define rc x<<1|1
    int L[maxn<<2],R[maxn<<2],t[maxn<<2];
    LL mx[maxn<<2],se[maxn<<2],sum[maxn<<2],tag[maxn<<2];
    void update(int x){
        if(mx[lc]>mx[rc]){
            mx[x]=mx[lc];
            t[x]=t[lc];
            se[x]=max(se[lc],mx[rc]);
        }
        else if(mx[lc]<mx[rc]){
            mx[x]=mx[rc];
            t[x]=t[rc];
            se[x]=max(mx[lc],se[rc]);
        }
        else{
            mx[x]=mx[lc];
            t[x]=t[lc]+t[rc];
            se[x]=max(se[lc],se[rc]);
        }
        sum[x]=sum[lc]+sum[rc];
    }
    void Build(int x,int *a,int l,int r){
        tag[x]=inf;
        if((L[x]=l)==(R[x]=r)){
            mx[x]=sum[x]=a[l];
            t[x]=1;se[x]=-inf;
            return;
        }
        int mid=(l+r)>>1;
        Build(lc,a,l,mid);Build(rc,a,mid+1,r);
        update(x);
    }
    void pushmin(int x,LL val){
        sum[x]-=(mx[x]-val)*t[x];
        mx[x]=tag[x]=val;
    }
    void pushdown(int x){
        if(tag[x]==inf)return;
        if(mx[lc]>tag[x])pushmin(lc,tag[x]);
        if(mx[rc]>tag[x])pushmin(rc,tag[x]);
        tag[x]=inf;
    }
    void Modify(int x,int l,int r,LL val){
        if(val>=mx[x])return;
        if(L[x]>=l&&R[x]<=r){
            if(val<mx[x]&&val>se[x])return pushmin(x,val),void();
            pushdown(x);
            Modify(lc,l,r,val);Modify(rc,l,r,val);
            return update(x),void();
        }
        pushdown(x);
        int mid=(L[x]+R[x])>>1;
        if(l<=mid)Modify(lc,l,r,val);
        if(r>mid)Modify(rc,l,r,val);
        update(x);
    }
    LL Qsum(int x,int l,int r){
        if(L[x]>=l&&R[x]<=r)return sum[x];
        pushdown(x);
        int mid=(L[x]+R[x])>>1;
        LL ans=0;
        if(l<=mid)ans+=Qsum(lc,l,r);
        if(r>mid)ans+=Qsum(rc,l,r);
        return ans;
    }
    LL Qmax(int x,int l,int r){
        if(L[x]>=l&&R[x]<=r)return mx[x];
        pushdown(x);
        int mid=(L[x]+R[x])>>1;
        LL ans=0;
        if(l<=mid)ans=max(ans,Qmax(lc,l,r));
        if(r>mid)ans=max(ans,Qmax(rc,l,r));
        return ans;
    }
}tree;
int T,n,m,a[maxn];

int main(){
    read(T);
    while(T--){
        read(n);read(m);
        for(int i=1;i<=n;i++)read(a[i]);
        tree.Build(1,a,1,n);
        while(m--){
            int opt,l,r,c;
            read(opt);read(l);read(r);
            if(!opt)read(c),tree.Modify(1,l,r,c);
            else if(opt==1)printf("%lld\n",tree.Qmax(1,l,r));
            else if(opt==2)printf("%lld\n",tree.Qsum(1,l,r));
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值