写到主函数不会了,有大佬帮一下这个小小蒟蒻吗

P3372 【模板】线段树 1

Astral_Veil @ 2024-07-13 21:39:45

在线等,挺急的

求大佬相助(-->前面也帮我看一下嘛<--)

#include<bits/stdc++.h>

using namespace std;
#include<bits/stdc++.h>
using namespace std;
const long long N = 5001000;
struct tree{
    int l,r;
    long long sum,lazy;
}tr[N*4];
void build(int v,int l,int r){
    if(l==r){
        tr[v]={l,l,nw[l],0};
        return;
    }
    int mid=l+r>>1;
    build(v<<1,l,mid);
    build(v<<1|1,mid+1,r);
    push_up(v);
}
void push_up(tree &u,tree &l,tree &r){
    u.sum=l.sum+r.sum;
    u.l=l.l,u.r=r.r;
}
void push_up(int u){
     push_up(tr[u],tr[u<<1],tr[u<<1|1]);
}

void update(int u,int l,int r,int k){
    if(l<=tr[u].l&&r>=tr[u].r){
        tr[u].lazy+=k;
        tr[u].sum+=(tr[u].r-tr[u].l+1)*k;
        return;
    }
    push_down(u);
    int mid=tr[u].l+tr[u].r>>1;
    if(l<=mid)update(u<<1,l,r,k);
    if(r>mid)update(u<<1|1,l,r,k);
    push_up(u);
}
void push_up(tree &u,tree &l,tree &r){
    u.sum=l.sum+r.sum;
    u.l=l.l,u.r=r.r;
}
void push_up(int u){
    push_up(tr[u],tr[u<<1],tr[u<<1|1]);
}
void push_down(int u){
    if(tr[u].lazy){
        auto &left=tr[u<<1],&right=tr[u<<1|1];
        left.sum+=tr[u].lazy*(left.r-left.l+1);
        right.sum+=tr[u].lazy*(right.r-right.l+1);
        left.lazy+=tr[u].lazy;
        right.lazy+=tr[u].lazy;
        tr[u].lazy=0;
    }
}
long long query(int u,int l,int r){
    if(l<=tr[u].l&&r>=tr[u].r)
        return tr[u].sum;
    push_down(u);
    int mid=tr[u].l+tr[u].r>>1;
    long long res=0;
    if(l<=mid)res+=query(u<<1,l,r);
    if(r>mid)res+=query(u<<1|1,l,r);
    return res;
}
void push_up(tree &u,tree &l,tree &r){
    u.sum=l.sum+r.sum;
    u.l=l.l,u.r=r.r;
}
void push_up(int u){
    push_up(tr[u],tr[u<<1],tr[u<<1|1]);
}
void push_down(int u){
    if(tr[u].lazy){
        auto &left=tr[u<<1],&right=tr[u<<1|1];
        left.sum+=tr[u].lazy*(left.r-left.l+1);
        right.sum+=tr[u].lazy*(right.r-right.l+1);
        left.lazy+=tr[u].lazy;
        right.lazy+=tr[u].lazy;
        tr[u].lazy=0;
    }
}
int main(){
    int n,m,in[N];
    cin>>n>>m;
    for (int i = 1; i <= n; i++){
        cin>>in[i];
        long long x=in[i]-in[i-1];
        add(i,x);
    }
    for (int i = 1; i <= m; i++){
        int a,b,c,d,e;
        cin>>a>>b>>c;
        if (a == 1){  
            cin>>d>>e;
        }else{
            cout << sum(b)<<endl;
        }
    }
}

|