結果
問題 | No.1099 Range Square Sum |
ユーザー | 37zigen |
提出日時 | 2020-06-26 23:46:28 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 792 ms / 2,000 ms |
コード長 | 3,972 bytes |
コンパイル時間 | 2,322 ms |
コンパイル使用メモリ | 78,552 KB |
実行使用メモリ | 70,468 KB |
最終ジャッジ日時 | 2024-07-05 00:40:39 |
合計ジャッジ時間 | 13,595 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
36,808 KB |
testcase_01 | AC | 51 ms
36,508 KB |
testcase_02 | AC | 50 ms
36,752 KB |
testcase_03 | AC | 50 ms
36,680 KB |
testcase_04 | AC | 49 ms
36,304 KB |
testcase_05 | AC | 51 ms
36,508 KB |
testcase_06 | AC | 51 ms
36,632 KB |
testcase_07 | AC | 50 ms
36,348 KB |
testcase_08 | AC | 52 ms
36,552 KB |
testcase_09 | AC | 51 ms
36,680 KB |
testcase_10 | AC | 50 ms
36,304 KB |
testcase_11 | AC | 105 ms
39,220 KB |
testcase_12 | AC | 107 ms
39,048 KB |
testcase_13 | AC | 105 ms
38,772 KB |
testcase_14 | AC | 107 ms
38,916 KB |
testcase_15 | AC | 106 ms
39,160 KB |
testcase_16 | AC | 104 ms
39,004 KB |
testcase_17 | AC | 106 ms
39,032 KB |
testcase_18 | AC | 107 ms
38,808 KB |
testcase_19 | AC | 106 ms
39,492 KB |
testcase_20 | AC | 107 ms
39,116 KB |
testcase_21 | AC | 786 ms
70,328 KB |
testcase_22 | AC | 774 ms
70,260 KB |
testcase_23 | AC | 773 ms
70,288 KB |
testcase_24 | AC | 792 ms
70,212 KB |
testcase_25 | AC | 768 ms
70,420 KB |
testcase_26 | AC | 757 ms
70,404 KB |
testcase_27 | AC | 749 ms
70,384 KB |
testcase_28 | AC | 762 ms
70,468 KB |
testcase_29 | AC | 767 ms
70,392 KB |
testcase_30 | AC | 765 ms
70,368 KB |
ソースコード
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());} }