結果

問題 No.1099 Range Square Sum
ユーザー 37zigen37zigen
提出日時 2020-06-26 23:46:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 867 ms / 2,000 ms
コード長 3,972 bytes
コンパイル時間 3,257 ms
コンパイル使用メモリ 75,324 KB
実行使用メモリ 82,384 KB
最終ジャッジ日時 2023-09-18 08:46:31
合計ジャッジ時間 14,625 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,224 KB
testcase_01 AC 44 ms
49,444 KB
testcase_02 AC 44 ms
49,544 KB
testcase_03 AC 44 ms
49,144 KB
testcase_04 AC 44 ms
49,452 KB
testcase_05 AC 44 ms
49,512 KB
testcase_06 AC 44 ms
49,708 KB
testcase_07 AC 44 ms
49,444 KB
testcase_08 AC 44 ms
47,216 KB
testcase_09 AC 44 ms
49,508 KB
testcase_10 AC 44 ms
49,212 KB
testcase_11 AC 101 ms
53,100 KB
testcase_12 AC 101 ms
53,704 KB
testcase_13 AC 104 ms
53,344 KB
testcase_14 AC 104 ms
53,224 KB
testcase_15 AC 107 ms
53,704 KB
testcase_16 AC 104 ms
53,156 KB
testcase_17 AC 103 ms
53,476 KB
testcase_18 AC 104 ms
53,476 KB
testcase_19 AC 102 ms
53,444 KB
testcase_20 AC 107 ms
53,708 KB
testcase_21 AC 867 ms
82,100 KB
testcase_22 AC 812 ms
82,048 KB
testcase_23 AC 802 ms
82,384 KB
testcase_24 AC 815 ms
82,356 KB
testcase_25 AC 810 ms
82,240 KB
testcase_26 AC 777 ms
82,212 KB
testcase_27 AC 773 ms
82,080 KB
testcase_28 AC 782 ms
82,072 KB
testcase_29 AC 779 ms
82,032 KB
testcase_30 AC 779 ms
81,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

class SegTree {
	int n=1;
	long[] sum1;
	long[] sum2;
	long[] lazy;

	public SegTree(int n_) {
		while (n<n_) n*=2;
		sum1=new long[2*n-1];
		sum2=new long[2*n-1];
		lazy=new long[2*n-1];
	}
	
	void push(int k,int l,int r) {
		sum2[k]+=2*sum1[k]*lazy[k]+lazy[k]*lazy[k]*(r-l);
		sum1[k]+=lazy[k]*(r-l);
		if (2*k+2<2*n-1) {
			lazy[2*k+1]+=lazy[k];
			lazy[2*k+2]+=lazy[k];
		}
		lazy[k]=0;
	}
	
	void merge(int k) {
		assert(lazy[k]==0);
		if (2*k+2<2*n-1) {
			sum1[k]=sum1[2*k+1]+sum1[2*k+2];
			sum2[k]=sum2[2*k+1]+sum2[2*k+2];
		}
	}
	
	long query(int a,int b,long add) {
		return query(0,n,a,b,0,add);
	}
	
	long query(int l,int r,int a,int b,int k,long add) {
		push(k,l,r);
		if (r<=a||b<=l) return 0;
		else if (a<=l&&r<=b) {
			lazy[k]+=add;
			push(k,l,r);
			return sum2[k];
		}
		else {
			long vl=query(l,(l+r)/2,a,b,2*k+1,add);
			long vr=query((l+r)/2,r,a,b,2*k+2,add);
			merge(k);
			return vl+vr;
		}
	}
}

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		long[] A=new long[N];
		SegTree seg=new SegTree(N);
		for (int i=0;i<N;++i) {
			A[i]=sc.nextLong();
			seg.query(i, i+1, A[i]);
		}
		
		int Q=sc.nextInt();
		
		for (int i=0;i<Q;++i) {
			//for (int j=0;j<N;++j) {
			//	System.out.print(seg.query(j, j+1, 0)+(j==N-1?"\n":" "));
			//}
			int type=sc.nextInt();
			if (type==1) {
				int l=sc.nextInt();
				int r=sc.nextInt();
				--l;--r;
				long x=sc.nextLong();
				seg.query(l, r+1, x);
			} else {
				int l=sc.nextInt();
				int r=sc.nextInt();
				--l;--r;
				System.out.println(seg.query(l, r+1, 0));
			}
		}
	}

	void tr(Object...o) {System.out.println(Arrays.deepToString(o));}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
        	ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    
    public double nextDouble() { return Double.parseDouble(next());}
}
0