玄关(可能懒惰标记打错了?)

P3372 【模板】线段树 1

wangziwenhk @ 2024-03-28 15:46:03

#include <bits/stdc++.h>
#define p2 (p<<1)
#define p3 ((p<<1)+1)
using namespace std;
const int MAXN = 1e5;
int n,m;
int tree[4*MAXN];
int lazy[4*MAXN];
int a[MAXN];
void push_down(int l,int r,int p){
    int mid = (l+r)>>1;
    tree[p2] += (mid-l+1)*lazy[p];
    tree[p3] += (r-mid)*lazy[p];
    lazy[p2] += lazy[p];
    lazy[p3] += lazy[p];
    lazy[p] = 0;
}
void push_up(int p){
    tree[p] = tree[p2]+tree[p3];
}
int question(int l,int r,int li,int re,int p){
    if(l<=li && re<=r)return tree[p];
    push_down(li,re,p);
    int mid = (li+re)>>1,sum=0;
    if(l<=mid)sum+= question(l,r,li,mid,p2);
    if(r>mid)sum+= question(l,r,mid+1,re,p3);
    return sum;
}
void update(int l, int r, int value, int li, int re, int p){
    if(l<=li && re<=r){
        tree[l]+=value*(re-li+1);
        lazy[p]+=value;
        return;
    }
    push_down(li,re,p);
    int mid = (li+re)>>1;
    if(l<=mid)update(l,r,value,li,mid,p2);
    if(r>mid)update(l,r,value,mid+1,re,p3);
    push_up(p);
}
void build(int l,int r,int p){
    if(l==r){
        tree[p] = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(l,mid,p2);
    build(mid+1,r,p3);
    push_up(p);
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    build(1,n,1);
    while(m--){
        int op;
        scanf("%d",&op);
        if(op==1){
            int l,r,value;
            scanf("%d%d%d",&l,&r,&value);
            update(l,r,value,1,n,1);
        }
        if(op==2){
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%d\n", question(l,r,1,n,1));
        }
    }
}

by IamZZ @ 2024-03-28 16:10:47

@wangziwenhk

保证任意时刻数列中所有元素的绝对值之和 \leq10^{18}

不开long long吗?

别的好像没事……


by wangziwenhk @ 2024-03-28 16:11:47

@IamZZ 不是这个问题,我不是指WA,我是指样例都过不了


by wangziwenhk @ 2024-03-28 16:13:35

@IamZZ 在吗


by ka_da_Duck @ 2024-03-28 16:15:19

@wangziwenhk

要开 long long,还有一处错放代码里了

#include <bits/stdc++.h>
#define int long long
#define p2 (p<<1)
#define p3 ((p<<1)+1)
using namespace std;
const int MAXN = 1e5;
int n,m;
int tree[4*MAXN];
int lazy[4*MAXN];
int a[MAXN];
void push_down(int l,int r,int p){
    int mid = (l+r)>>1;
    tree[p2] += (mid-l+1)*lazy[p];
    tree[p3] += (r-mid)*lazy[p];
    lazy[p2] += lazy[p];
    lazy[p3] += lazy[p];
    lazy[p] = 0;
}
void push_up(int p){
    tree[p] = tree[p2]+tree[p3];
}
int question(int l,int r,int li,int re,int p){
    if(l<=li && re<=r)return tree[p];
    push_down(li,re,p);
    int mid = (li+re)>>1,sum=0;
    if(l<=mid)sum+= question(l,r,li,mid,p2);
    if(r>mid)sum+= question(l,r,mid+1,re,p3);
    return sum;
}
void update(int l, int r, int value, int li, int re, int p){
    if(l<=li && re<=r){
        tree[p]+=value*(re-li+1); // 你这里写错了,是p不是l
        lazy[p]+=value;
        return;
    }
    push_down(li,re,p);
    int mid = (li+re)>>1;
    if(l<=mid)update(l,r,value,li,mid,p2);
    if(r>mid)update(l,r,value,mid+1,re,p3);
    push_up(p);
}
void build(int l,int r,int p){
    if(l==r){
        tree[p] = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(l,mid,p2);
    build(mid+1,r,p3);
    push_up(p);
}
signed main(){
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    build(1,n,1);
    while(m--){
        int op;
        scanf("%lld",&op);
        if(op==1){
            int l,r,value;
            scanf("%lld%lld%lld",&l,&r,&value);
            update(l,r,value,1,n,1);
        }
        if(op==2){
            int l,r;
            scanf("%lld%lld",&l,&r);
            printf("%lld\n", question(l,r,1,n,1));
        }
    }
}

by ka_da_Duck @ 2024-03-28 16:17:12

@wangziwenhk 这份能过


by wangziwenhk @ 2024-03-28 16:18:27

@ka_da_Duck 好的,谢谢已关


by wangziwenhk @ 2024-03-28 16:23:21

@wangziwenhk 此贴结


|