求调,内存会溢出,要怎么修一下

P3372 【模板】线段树 1

l233866 @ 2024-12-11 15:28:52


import java.util.Scanner;

public class Main {
    static int[] A = new int[100001];
    static long[] V = new long[400001];
    static long[] F = new long[400001];
    static int n,m;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1;i <= n;i++){
            A[i] = sc.nextInt();
        }
        build(1,1,n);
        for (int i = 0;i < m;i++){
            int t = sc.nextInt();
            if(t == 1){
                int x,y;
                long v;
                x = sc.nextInt();
                y = sc.nextInt();
                v = sc.nextLong();
                insert(1,1,n,x,y,v);
            }else{
                int x,y;
                x = sc.nextInt();
                y = sc.nextInt();
                System.out.println(query(1,1,n,x,y,0));
            }
        }
        sc.close();

    }
    static void insert(int k,int l,int r,int x,int y,long v){
        if(l == x && r == y){
            V[k] += v;
            return;
        }
        F[k] += (y-x+1) * v;
        int mid = (l + r) / 2;
        if(y <= mid){
            insert(k*2,l,mid,x,y,v);
        }else if(x > mid){
            insert(k*2+1,mid+1,r,x,y,v);
        }else {
            insert(k*2,l,mid,x,mid,v);
            insert(k*2+1,mid+1,r,mid+1,y,v);
        }

    }

    static long query(int k,int l,int r,int x,int y,long v){
        v += V[k];
        if(l == x && r == y)
            return v * (r-l+1) + F[k];
        int mid = (l + r) / 2;
        if(y <= mid)
            return query(k*2,l,mid,x,y,v);
        else if(x > mid)
            return query(k*2+1,mid+1,r,x,y,v);
        else
            return query(k*2,l,mid,x,mid,v) + query(k*2+1,mid+1,r,mid+1,y,v);
    }

    static void build(int k,int l,int r){
        if(l == r){
            F[k] = A[l];
            return;
        }
        int mid = (l + r) / 2;
        build(k*2,l,mid);
        build(k*2+1,mid+1,r);
        F[k] = F[k*2] + F[k*2+1];

    }

}

by godess @ 2024-12-11 16:44:36

加java -Xss2m Main试试


by l233866 @ 2024-12-11 17:49:59

@godess 我在本地能跑起来,但是提交线上评测的话有三个会超过规定内存


by jgg545452 @ 2024-12-12 14:55:13

同问,8,9,10样例会提示内存溢出


|