結果

問題 No.876 Range Compress Query
ユーザー 37zigen37zigen
提出日時 2020-03-28 06:28:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,468 ms / 2,000 ms
コード長 3,914 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 76,112 KB
実行使用メモリ 91,608 KB
最終ジャッジ日時 2023-08-30 17:22:25
合計ジャッジ時間 16,809 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
40,080 KB
testcase_01 AC 194 ms
42,376 KB
testcase_02 AC 138 ms
40,124 KB
testcase_03 AC 178 ms
43,448 KB
testcase_04 AC 165 ms
41,112 KB
testcase_05 AC 167 ms
40,864 KB
testcase_06 AC 187 ms
42,908 KB
testcase_07 AC 192 ms
42,544 KB
testcase_08 AC 196 ms
42,896 KB
testcase_09 AC 189 ms
42,412 KB
testcase_10 AC 178 ms
41,480 KB
testcase_11 AC 1,407 ms
77,152 KB
testcase_12 AC 1,181 ms
85,632 KB
testcase_13 AC 1,337 ms
86,428 KB
testcase_14 AC 1,451 ms
90,044 KB
testcase_15 AC 1,275 ms
88,616 KB
testcase_16 AC 1,468 ms
90,316 KB
testcase_17 AC 1,400 ms
91,608 KB
testcase_18 AC 1,460 ms
89,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	class SegTree{
		int n=1;
		long[][] v;
		
		public SegTree(int n_) {
			while(n<n_)n*=2;
			v=new long[2*n][3];//[F,left,right]
		}
		
		void add(int k,long add) {
			k+=n;
			if(k>=v.length)return;
			v[k][0]=1;
			v[k][1]+=add;
			v[k][2]+=add;
			while(k!=1) {
				k=k/2;
				v[k]=merge(v[2*k],v[2*k+1]);
			}
		}
		
		long[] merge(long[] L,long[] R) {
			if(L[0]==0)return R;
			else if(R[0]==0)return L;
			else return new long[] {L[0]+R[0]+(R[1]==0?-1:0),L[1],R[2]};
		}
		
		int query(int a,int b) {
			return (int)query(1,n+1,a+1,b+1,1)[0];
		}
		
		long[] query(int l,int r,int a,int b,int k) {
			if(r<=a||b<=l)return new long[] {0,0,0};
			else if(a<=l&&r<=b)return v[k];
			else {
				long[] vl=query(l,(l+r)/2,a,b,2*k);
				long[] vr=query((l+r)/2,r,a,b,2*k+1);
				return merge(vl,vr);
			}
		}
	}
	
	
	void run() {
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int Q=sc.nextInt();
		long[] A=new long[N];
		for(int i=0;i<N;++i)A[i]=sc.nextLong();
		SegTree seg=new SegTree(N);
		for(int i=0;i<N;++i) {
			seg.add(i, A[i]-(i>0?A[i-1]:0));
		}
		PrintWriter pw=new PrintWriter(System.out);
		while(Q-->0) {
			int t=Integer.parseInt(sc.next());
			int l=Integer.parseInt(sc.next());
			int r=Integer.parseInt(sc.next());
			--l;--r;
			if(t==1) {
				long x=Integer.parseInt(sc.next());;
				seg.add(l, x);
				seg.add(r+1, -x);
			}else {
				pw.println(seg.query(l, r+1));
			}
		}
		pw.close();
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
	
	
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;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); 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() {
    	return (int)nextLong();
    }
}
0