結果

問題 No.789 範囲の合計
ユーザー 37zigen37zigen
提出日時 2018-03-18 23:41:51
言語 Java21
(openjdk 21)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,622 bytes
コンパイル時間 2,453 ms
コンパイル使用メモリ 75,944 KB
実行使用メモリ 62,300 KB
最終ジャッジ日時 2023-09-07 11:54:49
合計ジャッジ時間 5,920 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

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][];
		ArrayList<Integer> list=new ArrayList<>();
		for(int i=0;i<n;++i){
			int q=sc.nextInt();
			if(q==0){
				a[i]=new int[2];
				a[i][0]=q;
				a[i][1]=sc.nextInt();
				list.add(a[i][1]);
			}else if(q==1){
				a[i]=new int[3];
				a[i][0]=q;
				a[i][1]=sc.nextInt();
				a[i][2]=sc.nextInt();
				list.add(a[i][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){
			if(a[i][0]==0){
			    a[i][1]=Collections.binarySearch(list,a[i][1]);
			}else{
				a[i][1]=Collections.binarySearch(list,a[i][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],1);
			}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