結果

問題 No.789 範囲の合計
ユーザー htensaihtensai
提出日時 2020-06-11 09:45:28
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,863 bytes
コンパイル時間 3,125 ms
コンパイル使用メモリ 76,240 KB
実行使用メモリ 71,444 KB
最終ジャッジ日時 2023-09-06 08:05:54
合計ジャッジ時間 16,834 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
55,860 KB
testcase_01 AC 125 ms
56,112 KB
testcase_02 TLE -
testcase_03 AC 907 ms
64,840 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 869 ms
65,040 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 124 ms
56,000 KB
testcase_14 AC 125 ms
56,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] types = new int[n];
		int[] lefts = new int[n];
		int[] rights = new int[n];
		TreeMap<Integer, Integer> map = new TreeMap<>();
		for (int i = 0; i < n; i++) {
		    types[i] = sc.nextInt();
		    lefts[i] = sc.nextInt();
		    rights[i] = sc.nextInt();
		    if (types[i] == 0) {
		        map.put(lefts[i], 0);
		    }
		}
		int idx = 1;
		for (int x : map.keySet()) {
		    map.put(x, idx);
		    idx++;
		}
		BinaryIndexedTree bit = new BinaryIndexedTree(idx);
		long total = 0;
		for (int i = 0; i < n; i++) {
		    if (types[i] == 0) {
		        bit.add(map.get(lefts[i]), rights[i]);
		    } else {
		        if (map.firstKey() > rights[i] || map.lastKey() < lefts[i]) {
		            continue;
		        }
		        int min = map.ceilingEntry(lefts[i]).getValue();
		        int max = map.floorEntry(rights[i]).getValue();
		        total += bit.getSum(min, max);
		    }
		}
		System.out.println(total);
	}
}

class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[size];
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0