90分线段树求助!!!!!!!有没有好心人帮帮忙?

P1253 扶苏的问题

gghack_Nythix @ 2023-01-18 14:14:33

rt,昨天也是90分TLE,今天把代码按照别人的思路写了一遍,还是90分?

#include <bits/stdc++.h>
#define lid id<<1
#define rid id<<1|1
#define int long long
//id >> 1 lid id >> 1 | 1 rid
using namespace std;
const int MAX = 1000005,g = 1145141919810;
struct seg_tr{
    int l,r,mx,lazy,lazy2;
}tr[MAX * 5];
int a[MAX];
void pushdown(int id){
    if(tr[id].lazy2 != -g){
        tr[lid].lazy2 = tr[rid].lazy2 = tr[id].lazy2;
        tr[lid].lazy = tr[rid].lazy = 0;
        tr[lid].mx = tr[rid].mx = tr[id].lazy2;
        tr[id].lazy2 = -g;
    }
    if(tr[id].lazy){
        tr[lid].lazy += tr[id].lazy,tr[rid].lazy += tr[id].lazy;
        tr[lid].mx += tr[id].lazy;
        tr[rid].mx += tr[id].lazy;
        tr[id].lazy = 0;
    }
    return ;
}
void bulid_tr(int id,int l,int r){
    tr[id].l = l,tr[id].r = r,tr[id].lazy = 0,tr[id].lazy2 = -g;
    if(l == r){
        tr[id].mx = a[l];
    }
    else{
        int mid = (l + r) / 2;
        bulid_tr(lid,l,mid);
        bulid_tr(rid,mid + 1,r);
        tr[id].mx = max(tr[lid].mx,tr[rid].mx);
    }
}
void update(int id,int l,int r,int vl){
    pushdown(id);
    if(tr[id].l == l && tr[id].r == r){
        tr[id].mx += vl,tr[id].lazy += vl;
        return ;
    }
    int mid = (tr[id].l + tr[id].r) / 2;
    if(r <= mid){
        update(lid,l,r,vl);
    }
    else if(l > mid){
        update(rid,l,r,vl);
    }
    else{
        update(lid,l,mid,vl),update(rid,mid + 1,r,vl);
    }
    tr[id].mx = max(tr[lid].mx,tr[rid].mx);
    return ;
}
void change(int id,int l,int r,int vl){
    pushdown(id);
    if(tr[id].l == l && tr[id].r == r){
        tr[id].mx = vl,tr[id].lazy2 = vl,tr[id].lazy = 0;
        return ;
    }
    int mid = (tr[id].l + tr[id].r) / 2;
    if(r <= mid){
        change(lid,l,r,vl);
    }
    else if(l > mid){
        change(rid,l,r,vl);
    }
    else{
        change(lid,l,mid,vl),change(rid,mid + 1,r,vl);
    }
    tr[id].mx = max(tr[lid].mx,tr[rid].mx);
    return ;
}
int query(int id,int l,int r){
    pushdown(id);
    if(tr[id].l == l&& tr[id].r == r){
        return tr[id].mx;
    }
    int mid = (tr[id].l + tr[id].r) / 2;
    if(r <= mid){
        return query(lid,l,r);
    }
    if(l > mid){
        return query(rid,l,r);
    }
    return max(query(lid,l,mid),query(rid,mid + 1,r));
}
signed main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
    }
    bulid_tr(1,1,n);
    for (int i = 1; i <= m; i++)
    {
        int op,x,y,z;
        cin >> op;
        if(op == 1 || op == 2){
            cin >> x >> y >> z;
        }
        if(op == 1){
            change(1,x,y,z);
        }
        if(op == 2){
            update(1,x,y,z);
        }
        if(op == 3){
            cin >> x >> y;
            printf("%lld\n", query(1, x, y));
        }
    }
}

by gghack_Nythix @ 2023-01-18 14:15:01

蒟蒻线段树一直调不明白,求教!


by Acoipp @ 2023-01-18 14:16:45

输入优化


by Acoipp @ 2023-01-18 14:17:43

cin>>n>>m; 之前加上 ios::sync_with_stdio(false);


by Acoipp @ 2023-01-18 14:28:03

插一句,g=1145141919810 是会爆 \text{int} 的。


by Acoipp @ 2023-01-18 14:29:22

建议开一下 \text{long long},有可能会爆 \text{int}


by gghack_Nythix @ 2023-01-18 19:38:11

@dlsk_po 谢谢,已过


|