线段树20pts求调
  • 板块P4314 CPU 监控
  • 楼主luqyou
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/13 19:34
  • 上次更新2024/8/13 21:23:15
查看原帖
线段树20pts求调
464732
luqyou楼主2024/8/13 19:34
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define fi first
#define sc second
#define pii pair<int,int>
#define pb push_back
const int maxn=1e5+10;
const int inf=1e18;
struct node{
    int l,r,maxx,hmaxx,tg1,tg2;
    node operator +(node x){
        if(l==-1) return x;
        if(x.l==-1) return (*this);
        node res;
        // cout<<"MERGE:"<<endl;
        // (*this).debug(),x.debug();
        res.tg1=0,res.tg2=inf,res.l=l,res.r=x.r,res.maxx=max(maxx,x.maxx),res.hmaxx=max(hmaxx,x.hmaxx);
        // cout<<"->",res.debug();
        return res;
    }
    void debug(){
        cout<<l<<" "<<r<<" "<<maxx<<" "<<hmaxx<<" "<<tg1<<" "<<tg2<<endl;
    }
}tr[maxn*4];
int n,m,a[maxn];
int ls(int u){
    return (u<<1);
}
int rs(int u){
    return (u<<1)|1;
}
bool ir(int L,int R,int l,int r){
    return (L<=l)&&(r<=R);
}
bool ofr(int L,int R,int l,int r){
    return (R<l)||(r<L);
}
void pushup(int u){
    if(tr[u].l==tr[u].r) return ;
    tr[u]=tr[ls(u)]+tr[rs(u)];
}
void build(int u,int l,int r){
    tr[u].l=l,tr[u].r=r,tr[u].tg1=0,tr[u].tg2=inf;
    if(l==r) return tr[u].maxx=tr[u].hmaxx=a[l],void();
    int mid=(l+r)>>1;
    build(ls(u),l,mid),build(rs(u),mid+1,r),pushup(u);
}
void maketg1(int u,int k){
    // cout<<"TG1:"<<endl;
    // tr[u].debug();
    tr[u].tg1+=k,tr[u].maxx+=k,tr[u].hmaxx=max(tr[u].hmaxx,tr[u].maxx);
    // tr[u].debug();
}
void maketg2(int u,int k){
    if(tr[u].tg1) tr[u].tg1=0;
    tr[u].tg2=k,tr[u].maxx=k,tr[u].hmaxx=max(tr[u].hmaxx,tr[u].maxx);
}
void pushdown(int u){
    if(tr[u].l==tr[u].r) return ;
    int k1=tr[u].tg1,k2=tr[u].tg2;
    // cout<<"PUSHDOWN:"<<u<<endl;
    // cout<<k1<<" "<<k2<<" "<<(k2!=inf)<<endl;
    tr[u].tg1=0,tr[u].tg2=inf;
    if(k2!=inf) maketg2(ls(u),k2),maketg2(rs(u),k2);
    if(k1) maketg1(ls(u),k1),maketg1(rs(u),k1);
}
void upd1(int u,int l,int r,int k){
    if(tr[u].tg1||tr[u].tg2!=inf) pushdown(u);
    if(ir(l,r,tr[u].l,tr[u].r)) maketg1(u,k);
    else if(!ofr(l,r,tr[u].l,tr[u].r)) upd1(ls(u),l,r,k),upd1(rs(u),l,r,k),pushup(u);
}
void upd2(int u,int l,int r,int k){
    if(tr[u].tg1||tr[u].tg2!=inf) pushdown(u);
    if(ir(l,r,tr[u].l,tr[u].r)) maketg2(u,k);
    else if(!ofr(l,r,tr[u].l,tr[u].r)) upd2(ls(u),l,r,k),upd2(rs(u),l,r,k),pushup(u);
}
node query(int u,int l,int r){
    if(tr[u].tg1||tr[u].tg2!=inf) pushdown(u);
    if(ir(l,r,tr[u].l,tr[u].r)) return tr[u];
    else if(!ofr(l,r,tr[u].l,tr[u].r)) return query(ls(u),l,r)+query(rs(u),l,r);
    else return {-1,-1,-1,-1,-1,-1};
}
void output(){
    cout<<"------SGT------"<<endl;
    for(int i=1;i<n*2;i++) tr[i].debug();
    cout<<"------END------"<<endl;
}
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,n);
    // output();
    cin>>m;
    while(m--){
        char op;
        int x,y,z;
        cin>>op>>x>>y;
        if(op=='Q'){
            node res=query(1,x,y);
            cout<<res.maxx<<endl;
        }
        if(op=='A'){
            node res=query(1,x,y);
            cout<<res.hmaxx<<endl;
        }
        if(op=='P'){
            cin>>z;
            upd1(1,x,y,z);
        }
        if(op=='C'){
            cin>>z;
            upd2(1,x,y,z);
        }
        // output();
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--) solve();
    return 0;
}
2024/8/13 19:34
加载中...