线段树0分求调!!!
查看原帖
线段树0分求调!!!
820210
AndyPomeloMarsGotoHE楼主2023/1/8 16:08

代码如下:

#include <bits/stdc++.h>
#define MAXN 500010
using namespace std;

long long Seq[MAXN],SegTree[MAXN*4],Lzy[MAXN*4];

inline void pushup(const int u){
    SegTree[u] = SegTree[u<<1] + SegTree[(u<<1)+1];
}

inline void build(int u,int L,int R){
    if (L==R){
        SegTree[u] = Seq[L];
        return;
    }
    int M = (L+R) >> 1;
    build(u<<1,L,M),build((u<<1)+1,M+1,R);
    pushup(u);
}

inline long long find(int u,int L,int R,int p){
    if (L==R) return SegTree[u];
    int M = (L+R) >> 1;
    if (M>=p) return find(u<<1,L,M,p);
    else return find((u<<1)+1,M+1,R,p);
}

inline void maketag(int u,int len,long long val){
    Lzy[u] += val;
    SegTree[u] += len*val;
}

inline void pushdown(int u,int L,int R){
    int M = (L+R) >> 1;
    maketag(u<<1,M-L+1,Lzy[u]);
    maketag((u<<1)+1,R-M,Lzy[u]);
    Lzy[u] = 0;
}

inline void update(int u,int L,int R,int p,long long val){
    if (L==R)  SegTree[u] += val;
    else{
        int M = (L+R) >> 1;
        if (M>=p) return update(u<<1,L,M,p,val);
        else return update((u<<1)+1,M+1,R,p,val);
        pushup(u);
    }
}

inline bool inrange(int L,int R,int l,int r){
    return (L>=l) && (R<=r);
}

inline bool outrange(int L,int R,int l,int r){
    return (L>r) || (R<l);
}

inline long long sectionfind(int u,int L,int R,int l,int r){
    if (inrange(L,R,l,r)) return SegTree[u];
    else if (!outrange(L,R,l,r)){
        int M = (L+R) >> 1;
        pushdown(u,L,R);
        return sectionfind(u<<1,L,M,l,r)+sectionfind((u<<1)+1,M+1,R,l,r);
    }
    else return 0;
}

inline void sectionupdate(int u,int L,int R,int l,int r,int val){
    if (inrange(L,R,l,r)) maketag(u,R-L+1,val);
    else if (!outrange(L,R,l,r)){
        int M = (L+R) >> 1;
        pushdown(u,L,R);
        sectionupdate(u<<1,L,M,l,r,val);
        sectionupdate((u<<1)+1,M+1,R,l,r,val);
        pushup(u);
    }
}

int main(){
    int N,M;
    cin >> N >> M;
    for (int i=1;i<=N;++i) cin >> Seq[i];
    build(1,1,N);
    for (int i=1;i<=M;++i){
        int op,x,y,k;
        cin >> op;
        if (op==1){
            cin >> x >> k;
            update(1,1,N,x,k);
        }
        else{
            cin >> x >> y;
            cout << sectionfind(1,1,N,x,y) << endl;
        }
    }
}

2023/1/8 16:08
加载中...