結果

問題 No.789 範囲の合計
ユーザー 37zigen37zigen
提出日時 2018-03-20 10:40:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 534 ms / 1,000 ms
コード長 3,463 bytes
コンパイル時間 2,611 ms
コンパイル使用メモリ 75,460 KB
実行使用メモリ 66,304 KB
最終ジャッジ日時 2023-09-07 11:56:38
合計ジャッジ時間 7,968 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,332 KB
testcase_01 AC 44 ms
49,580 KB
testcase_02 AC 415 ms
63,992 KB
testcase_03 AC 317 ms
61,732 KB
testcase_04 AC 424 ms
63,844 KB
testcase_05 AC 339 ms
64,236 KB
testcase_06 AC 375 ms
63,856 KB
testcase_07 AC 256 ms
59,176 KB
testcase_08 AC 306 ms
61,840 KB
testcase_09 AC 327 ms
61,708 KB
testcase_10 AC 534 ms
66,304 KB
testcase_11 AC 391 ms
63,836 KB
testcase_12 AC 393 ms
63,748 KB
testcase_13 AC 44 ms
49,432 KB
testcase_14 AC 44 ms
49,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

class Main{
	public static void main(String[] args){
		new Main().run();
	}
	
	void run(){
		Scanner sc=new Scanner();
		int n=sc.nextInt();
		int[][] a=new int[n][3];
		ArrayList<Integer> list=new ArrayList<>();
		for(int i=0;i<n;++i){
			int q=sc.nextInt();	
			a[i][0]=q;
			a[i][1]=sc.nextInt();
			a[i][2]=sc.nextInt();
			list.add(a[i][1]);
                        if(q==1)
			list.add(a[i][2]);
		}
		Collections.sort(list);
		long ret=0;
		BIT bit=new BIT(list.size());
		for(int i=0;i<n;++i){
			a[i][1]=Collections.binarySearch(list,a[i][1]);
                        if(a[i][0]==1)
			a[i][2]=Collections.binarySearch(list,a[i][2]);
		}
		for(int i=0;i<n;++i){
			if(a[i][0]==0){
				bit.add(a[i][1],a[i][2]);
			}else{
				ret+=bit.sum(a[i][1],a[i][2]);
			}
		}
		System.out.println(ret);
	}
	
	class BIT{
		int n;
		long[] arr;
		public BIT(int n_){
			n=n_;
			arr=new long[n_+1];
		}
		
		void add(int k,long val){
			++k;
			while(k<=n){
				arr[k]+=val;
				k+=k&-k;
			}
		}

		long sum(int k){
			++k;
			long ret=0;
			while(k>0){
				ret+=arr[k];
				k-=k&-k;
			}
			return ret;
		}

		long sum(int l,int r){
			return sum(r)-sum(l-1);
		}
	}
	
	class Scanner {
        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 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;
        }
	}
}
0