[题解]hdu5306 Gorgeous Sequence

来源:互联网 发布:mysql的事务隔离级别 编辑:程序博客网 时间:2024/06/10 03:45

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

15 51 2 3 4 51 1 52 1 50 3 5 31 1 52 1 5

Sample Output

515312

Solution

吉司机线段树模板,区间取min
每个区间记录最大值mx、严格次大值se和最大值的个数t。
假设当前要对xmin,如果该区间mxx直接退出。如果该区间se<x<mx,直接利用tmx修改当前区间的信息,打个标记退出。如果该区间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;}