[ 线段树 ] HDU5306 Gorgeous Sequence

吉利论文例题

#include<bits/stdc++.h>
using namespace std;
char nc() {
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
void Read(int& x) {
    char c=nc();
    for(;c<'0'||c>'9';c=nc());
    for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());
}
const int N=1000010;
typedef long long ll;
int k,n,m,x,y,T;
int a[N];
int mx[N<<2],se[N<<2],c[N<<2],p[N<<2];
ll s[N<<2];
void Up(int x) {
    mx[x]=max(mx[x<<1],mx[x<<1|1]);
    c[x]=0;
    if(mx[x<<1]==mx[x]) c[x]+=c[x<<1];
    if(mx[x<<1|1]==mx[x]) c[x]+=c[x<<1|1];
    se[x]=max(mx[x<<1]==mx[x]?se[x<<1]:mx[x<<1],mx[x<<1|1]==mx[x]?se[x<<1|1]:mx[x<<1|1]);
    s[x]=s[x<<1]+s[x<<1|1];
}
void Modify(int x,int y) {
    if(mx[x]<=y) return;
    if(y>se[x]) {
        s[x]-=1ll*c[x]*(mx[x]-y);
        mx[x]=p[x]=y;return;
    }
    p[x]=-1;
    Modify(x<<1,y);Modify(x<<1|1,y);
    Up(x);
}
void Down(int x) {
    Modify(x<<1,p[x]);Modify(x<<1|1,p[x]);
    p[x]=-1;
}
void Build(int x,int l,int r) {
    p[x]=-1;
    if(l==r) {
        mx[x]=s[x]=a[l];se[x]=-1;c[x]=1;
        return;
    }
    int Mid=l+r>>1;
    Build(x<<1,l,Mid);Build(x<<1|1,Mid+1,r);
    Up(x);
}
void Update(int x,int l,int r,int L,int R,int y) {
    if(l>R||r<L) return;
    if(l>=L&&r<=R) {
        Modify(x,y);
        return;
    }
    if(~p[x]) Down(x);
    int Mid=l+r>>1;
    Update(x<<1,l,Mid,L,R,y);
    Update(x<<1|1,Mid+1,r,L,R,y);
    Up(x);
}
ll Query1(int x,int l,int r,int L,int R) {
    if(l>R||r<L) return 0;
    if(l>=L&&r<=R) return s[x];
    if(~p[x]) Down(x);
    int Mid=l+r>>1;
    return Query1(x<<1,l,Mid,L,R)+Query1(x<<1|1,Mid+1,r,L,R);
}
int Query2(int x,int l,int r,int L,int R) {
    if(l>R||r<L) return 0;
    if(l>=L&&r<=R) return mx[x];
    if(~p[x]) Down(x);
    int Mid=l+r>>1;
    return max(Query2(x<<1,l,Mid,L,R),Query2(x<<1|1,Mid+1,r,L,R));
}
int main() {
    Read(T);
    while(T--) {
        Read(n);Read(m);
        for(int i=1;i<=n;i++) Read(a[i]);
        Build(1,1,n);
        while(m--) {
            Read(k);Read(x);Read(y);
            if(!k) Read(k),Update(1,1,n,x,y,k); else
                if(k==1) printf("%d\n",Query2(1,1,n,x,y));else printf("%lld\n",Query1(1,1,n,x,y));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值