停止工作了,求助

P3372 【模板】线段树 1

__LePetitPrince__ @ 2023-08-04 16:13:03

#include <stdio.h>
#define lc (o << 1)
#define rc (o << 1 | 1)
typedef long long LL;
const int S = 1e5 + 5;
LL sum[S * 4], tag[S * 4];
LL a[S];
int n, q;
void pushup(int o) {
    sum[o] = sum[lc] + sum[rc];
}
void pushdown(int o, int l, int r) {
    tag[lc] += tag[o];
    tag[rc] += tag[o];
    int mid = (l + r) / 2;
    sum[lc] += ((mid - l + 1) * tag[o]);
    sum[rc] += ((r - mid) * tag[o]);
    tag[o] = 0;
}
void build(int o, int l, int r) {
    if (l == r) {
        sum[o] = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(lc, l, mid);
    build(rc, mid + 1, r);
    pushup(o);
}
LL query(int o, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
        return sum[o];
    } 
    LL mid = (l + r) / 2, ans = 0;
    pushdown(o, l, r);
    if (ql <= mid) {
        ans += query(lc, l, mid, ql, qr);
    }
    if (mid < qr) {
        ans += query(rc, mid + 1, r, ql, qr);
    }
    return ans;
}/*
void update(int o, int l, int r, int p, int v) {
    if (l == r) {
        sum[o] += v;
        return;
    }
    int mid = (l + r) / 2;
    if (p <= mid) {
        update(lc, l, mid, p, v);
    } else {
        update(rc, mid + 1, r, p, v);
    }
    pushup(o);
}*/
void update(int o, int l, int r, int pl, int pr, int v) {
    if (pl <= l && r <= pr) {
        tag[o] += v;
        sum[o] += ((r - l + 1) * v);
        return;
    }
    if (r < pl || l > pr) {
        return;
    }
    pushdown(o, l, r);
    int mid = (l + r) / 2;
    update(lc, l, mid, pl, pr, v);
    update(rc, mid + 1, r, pl, pr, v);
    pushup(o);
}
int main() {
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
    }
    build(1, 1, n);
    LL op, x, y, k;
    while (q--) {
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%d%d", &x, &y, &k);
            update(1, 1, n, x, y, k);
        } else {
            scanf("%d%d%d", &x, &y);
            printf("%lld\n", query(1, 1, n, x, y));
        }
    }
    return 0;
}

by _5t0_0r2_ @ 2023-08-04 16:13:50

@LePetitPrince 是指本地停止工作了吗


by __LePetitPrince__ @ 2023-08-04 16:18:45

@Chtholly_is_so_cute 是


by _5t0_0r2_ @ 2023-08-04 16:20:03

@LePetitPrince 你先交上去试试,不通过我再告诉你


by __LePetitPrince__ @ 2023-08-04 16:21:04

@Chtholly_is_so_cute 全 RE 啊


by _5t0_0r2_ @ 2023-08-04 16:22:42

@LePetitPrince 没来得及看,你先对照检查一下:

#include<bits/stdc++.h>
#define int long long 
#define cal int mid = (l + r) / 2,lchild = root * 2,rchild = root * 2 + 1;
#define upd tree[root].val = tree[lchild].val + tree[rchild].val;
using namespace std;
const int N = 1e6 + 9,ROOT = 1;
int a[N];
struct node {
    int val,add;
} tree[N];
int len(int l,int r){
    return r - l + 1;
}
void push_down(int root,int l,int r){
    cal;
    tree[lchild].val += tree[root].add * len(l,mid) ;
    tree[rchild].val += tree[root].add * len(mid + 1,r);
    tree[lchild].add += tree[root].add;
    tree[rchild].add += tree[root].add;
    tree[root].add = 0;
}
void build(int l,int r,int root) {
    if(l == r) {
        tree[root].val = a[l];
        return;
    }
    cal
    build(l,mid,lchild);
    build(mid + 1,r,rchild);
    upd
}
void add_update(int s,int t,int l,int r,int root,int add) { //[s,t]表示查询区间
    if(s <= l && r <= t) { //当前区间为更新区间的子集,直接返回当前区间和
        tree[root].val += (r - l + 1) * add;
        tree[root].add += add;
        return;
    }
    cal
    push_down(root,l,r);
    if(s <= mid)
        add_update(s,t,l,mid,lchild,add);
    if(t > mid)
        add_update(s,t,mid + 1,r,rchild,add);
    upd
}
int getsum(int s, int t, int l, int r, int root) {
    if(s <= l && r <= t) //当前区间为求和区间的子集,直接返回当前区间和
        return tree[root].val;
    cal
    push_down(root,l,r);
    int sum = 0;
    if(s <= mid)
        sum += getsum(s,t,l,mid,lchild);
    if(t > mid)
        sum += getsum(s,t,mid + 1,r,rchild);
    return sum; 
}

int n,m;
int op,x,y,k;
signed main() {
    scanf("%lld%lld", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    build(1,n,1);
    for(int i = 1; i <= m; i++) {
        scanf("%lld%lld%lld", &op, &x, &y);
        if(op == 1) {
            scanf("%lld" ,&k);
            add_update(x,y,1,n,ROOT,k);
        }
        else
            printf("%lld\n", getsum(x,y,1,n,ROOT));
    }
}

by Zzzcr @ 2023-08-04 16:23:11

update写错了,应该和query一样,包含在被修改的区间里才能修改


by __LePetitPrince__ @ 2023-08-04 16:24:10

@Chtholly_is_so_cute ok,我先看看


by __LePetitPrince__ @ 2023-08-04 16:27:11

@Zzzcr 可是我有不在区间里的忽略掉啊


    if (r < pl || l > pr) {
        return;
    }

by __LePetitPrince__ @ 2023-08-04 16:28:12

我感觉烦的应该是什么低级错误(但我就是没看出来((


by 初星逝者 @ 2023-08-04 16:41:15

@LePetitPrince 第85行

scanf("%d%d%d", &x, &y);

| 下一页