60分求助+悬关

P1253 扶苏的问题

Soaring_light @ 2023-10-25 20:13:15

smodify是加修改

cmodify是赋值修改

请路过的大佬帮忙改改把

#include<bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
#define int long long
using namespace std;
typedef long long ll;
int n,q,op,lk,rk,xk,a[1000005];
const int inf = 1e18+1;
struct node{
    int tot,cl,cr,stag,ctag;
}tree[2000006];
void build(ll x,ll l,ll r){
    tree[x].cl = l,tree[x].cr = r;
    tree[x].ctag = -inf,tree[x].stag = 0;
    if(l == r){
        tree[x].tot = a[l];return;
    }
    ll mid = (tree[x].cl + tree[x].cr)>>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    tree[x].tot = max(tree[ls].tot,tree[rs].tot);
    return;
}
void Cpushdown(ll x){
    if(tree[x].ctag != -inf){
        tree[ls].stag = tree[rs].stag = tree[x].stag = 0;
        tree[ls].tot = tree[rs].tot = tree[x].ctag;
        tree[ls].ctag = tree[rs].ctag = tree[x].ctag;
    }
    tree[x].ctag = -inf;
}
void Spushdown(ll x){
    if(tree[x].stag){
        Cpushdown(x);
        tree[ls].stag += tree[x].stag;
        tree[rs].stag += tree[x].stag;
        tree[ls].tot += tree[x].stag;
        tree[rs].tot += tree[x].stag;
    }
    tree[x].stag = 0;
}
void Smodify(ll x,ll l,ll r,ll num){
    if(tree[x].cl > r || tree[x].cr < l)return;
    if(tree[x].cl >= l && tree[x].cr <= r){
        tree[x].tot += num;tree[x].stag += num;
        return;
    }
    Spushdown(x);Cpushdown(x);
    int mid = (tree[x].cl + tree[x].cr)>>1;
    if(mid >= l)Smodify(ls,l,r,num);
    if(mid < r)Smodify(rs,l,r,num);
    tree[x].tot = max(tree[ls].tot,tree[rs].tot);
    return;
}
void Cmodify(ll x,ll l,ll r,ll num){
    if(tree[x].cl > r || tree[x].cr < l)return;
    if(tree[x].cl >= l && tree[x].cr <= r){
        tree[x].tot = num;tree[x].ctag = num;
        return;
    }
    Spushdown(x);Cpushdown(x);
    ll mid = (tree[x].cl + tree[x].cr)>>1;
    if(mid >= l)Cmodify(ls,l,r,num);
    if(mid < r)Cmodify(rs,l,r,num);
    tree[x].tot = max(tree[ls].tot , tree[rs].tot);
    return;
}
int Query(ll x,ll l,ll r){
    if(tree[x].cl >= l && tree[x].cr <= r){
        return tree[x].tot;
    }
    Cpushdown(x),Spushdown(x);
    int ans = -inf,mid = (tree[x].cl + tree[x].cr)>>1;
    if(mid >= l)ans = max(ans,Query(ls,l,r));
    if(mid < r)ans = max(ans,Query(rs,l,r));
    return ans;
}
signed main(int argc, char const *argv[]){
//  freopen("P1253_7.in","r",stdin);
//  freopen("anrs.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>q;
    for(int i = 1;i <= n;i++)
        cin>>a[i];
    build(1,1,n);
    while(q--){
        cin>>op>>lk>>rk;
        if(op == 1){
            cin>>xk;
            Cmodify(1,lk,rk,xk);
        }
        if(op == 2){
            cin>>xk;
            Smodify(1,lk,rk,xk);
        }
        if(op == 3)
            cout<<Query(1,lk,rk)<<endl;
    }
    return 0;
}

by Soaring_light @ 2023-10-25 20:13:54

一直在0-60分间盘旋


by chillLee @ 2023-10-26 18:26:26

至少目前为止可能存在一个问题:
您可能需要使用 long long 来存储数据,并且 inf 也应当用 long long
不过我用您的代码改完 long long 后,仍然是 60 分。

不妨考虑是否可能标记下传有问题:

  1. 当执行操作一时,直接将 ctag 赋值,但是应当清空该层的 stag
  2. 当施行操作二时,考虑 ctag 是否存在,若存在,则将 ctag 加上 xk ,不然将 stag 加上 xk ;
  3. 下传时,若 ctag 存在才下传 ctag ,否则下传 stag

|