求助!!!为什么最后三个MLE

P3372 【模板】线段树 1

0x3ff @ 2024-05-08 15:30:27

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    static long[] sum;
    static int[] arr;
    static int[] lazy;
    static int n;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        n = scan.nextInt();
        int m = scan.nextInt();
        arr = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
        sum = new long[n << 2];
        lazy = new int[n << 2];
        build(1, 1, n);
        for(int i = 0; i < m; i++) {
            int type = scan.nextInt();
            if(type == 1) {
                update(1, 1, n, scan.nextInt(), scan.nextInt(), scan.nextInt());
            }else {
                System.out.println(query(1, 1, n, scan.nextInt(), scan.nextInt()));
            }
        }
        scan.close();
    }
    //构建线段树
    static void build(int i, int l, int r) {
        if(l == r) {
            sum[i] = arr[l - 1];
            return;
        }
        int mid = (l + r) >> 1;
        build(i << 1, l, mid);
        build(i << 1 | 1, mid + 1, r);
        sum[i] = sum[i << 1] + sum[i << 1 | 1];
    }
    //pushDown lazy
    static void pushDown(int i, int l, int r, int mid) {
        if(lazy[i] != 0) {
            lazy[i << 1] += lazy[i];
            lazy[i << 1 | 1] += lazy[i];
            sum[i << 1] += lazy[i] * (mid - l + 1);
            sum[i << 1 | 1] += lazy[i] * (r - mid);
            lazy[i] = 0;
        }
    }
    // x, y =>要更新的区间,   l, r =>当前的区间
    static void update(int i, int l, int r, int x, int y, int val) {
        if(x <= l && y >= r) {
            lazy[i] += val;
            sum[i] += val * (r - l + 1);
            return;
        }
        int mid = (l + r) >> 1;
        pushDown(i, l, r, mid);
        if(x <= mid) update(i << 1, l, mid, x, y, val);
        if(y >= mid + 1) update(i << 1 | 1, mid + 1, r, x, y, val);
        sum[i] = sum[i << 1] + sum[i << 1 | 1];
    }

    static long query(int i, int l, int r, int x, int y) {
        if(x <= l && y >= r) return sum[i];
        int mid = (l + r) >> 1;
        pushDown(i, l, r, mid);
        long ans = 0;
        if(x <= mid) ans += query(i << 1, l, mid, x, y);
        if(y >= mid + 1) ans += query(i << 1 | 1, mid + 1, r, x, y);
        return ans;
    }
}

by std202211071417 @ 2024-05-13 15:01:13

大概数值溢出


|