佬 帮我看一下为什么RE了

P3372 【模板】线段树 1

mod_1155 @ 2024-02-28 12:41:55

#include<bits/stdc++.h>
using namespace std;
void build_tree(int arr[], int tree[], int node, int start, int end) {
    if (start == end) {
        tree[node] = arr[start];
    }
    else {
        int mid = (start + end) / 2;
        int left_node = 2 * node + 1;
        int right_node = 2 * node + 2;
        build_tree(arr, tree, left_node, start, mid);
        build_tree(arr, tree, right_node, mid + 1, end);
        tree[node] = tree[left_node] + tree[right_node];
    }
}
void update_tree(int arr[], int tree[], int node, int start, int end, int L,int R, int val) {
    if (start == end) {
        arr[start] += val;
        tree[node] += val;
    }
    int mid = (start + end) / 2;
    int left_node = 2 * node + 1;
    int right_node = 2 * node + 1;
    if (L <= mid) update_tree(arr, tree, left_node, start, mid, L, R, val);
    if (R > mid) update_tree(arr, tree, right_node, mid + 1, end, L, R, val);
    tree[node] = tree[left_node] + tree[right_node];
 }

int query_tree(int arr[], int tree[], int node, int start, int end, int L, int R) {
    if (L > end || R < start) {
        return 0;
    }
    else if (L <= start && R >= end) {
        return tree[node];
    }
    else if (start == end) {
        return tree[node];
    }else{
    int mid = (start + end) / 2;
    int left_node = 2 * node + 1;
    int right_node = 2 * node + 2;
    int sum_left = query_tree(arr, tree, left_node, start, mid, L, R);
    int sum_right = query_tree(arr, tree, right_node, mid + 1, end, L, R);
    return sum_left + sum_right;
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    int arr[1000010] = {};
    int tree[1000010] = {};
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    build_tree(arr, tree, 0, 0, n - 1);
    while (m--) {
        int t,L,R;
        cin >> t>>L>>R;
        if (t == 1) {
            int j;
            cin >> j;
            update_tree(arr, tree, 0, 0, n-1, L, R, j);
        }
        else {
            cout << query_tree(arr, tree, 0, 0, n-1, L, R) << endl;
        }
    }
}

by leiaxiwo @ 2024-02-28 12:47:59

@mod_1155 每个函数(哪怕void都return)都加个返回值,不然会有奇怪问题


by userLCX @ 2024-02-28 12:53:00

@liverxiwo ???

@mod_1155 update_tree函数 start == end 时需要return。


by leiaxiwo @ 2024-02-28 12:55:49

@userLCX 你试试嘛,谷的特性


by userLCX @ 2024-02-28 12:57:20

@liverxiwo 你玩原神吗?


by leiaxiwo @ 2024-02-28 13:00:55

@userLCX 问这个干什么


by userLCX @ 2024-02-28 13:02:56

@liverxiwo 没什么,随便问问


by lonely_seele @ 2024-02-28 13:04:56

@liverxiwo 你玩原神吗?


by leiaxiwo @ 2024-02-28 13:06:39

@userLCX 显而易见不玩


by Ew_Cors @ 2024-02-28 13:18:17

@liverxiwo 你说的这个不成立。


by userLCX @ 2024-02-28 13:32:38

@liverxiwo 原神是一款非常好玩的游戏,快来玩!11


|