Ale

P3372 【模板】线段树 1

le99 @ 2024-10-29 19:00:24

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&-x
long long n,m,tree1[10000000],tree2[10000000],a,x,y,k,temp;
long long sum1(long long x){
    long long ans=0;
    while(x){
        ans+=tree1[x];
        x-=lowbit(x);
    }
    return ans;
}
long long sum2(long long x){
    long long ans=0;
    while(x){
        ans+=tree2[x];
        x-=lowbit(x);
    }
    return ans;
}
void update1(long long x,long long d){
    while(x<=n){
        tree1[x]+=d;
        x+=lowbit(x);
    }
}
void update2(long long x,long long d){
    while(x<=n){
        tree2[x]+=d;
        x+=lowbit(x);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    long long num=0;
    for(long long i=1;i<=n;i++){
        cin>>temp;
        update1(i,temp-num);
        update2(i,(i-1)*(temp-num));
        num=temp;
    }
    for(long long i=1;i<=m;i++){
        long long q,l,r,d;
        cin>>q;
        if(q==1){
            cin>>l>>r>>d;
            update1(l,d);
            update1(r+1,-d);
            update2(l,(l-1)*d);
            update2(r+1,r*(-d));
        }
        else{
            cin>>l>>r;
            printf("%lld\n",(r*sum1(r)-(l-1)*sum1(l-1))-(sum2(r)-sum2(l-1)));
        } 
    }
    return 0;
}

by Fengxijun @ 2024-10-29 19:01:06

@le99 太有实力了


by honghaojin @ 2024-10-29 19:09:21

无意义,jbl

22.炫耀通过了某个题目,但没有给出深刻的感想的。


by Grammar__hbw @ 2024-10-29 19:10:00

ALE=Accepted Limit Exceeded,所以不要再做题了qwq


|