結果

問題 No.789 範囲の合計
ユーザー htensaihtensai
提出日時 2020-06-11 09:54:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 674 ms / 1,000 ms
コード長 2,044 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 75,280 KB
実行使用メモリ 62,996 KB
最終ジャッジ日時 2023-09-06 08:06:08
合計ジャッジ時間 10,078 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
47,492 KB
testcase_01 AC 43 ms
49,340 KB
testcase_02 AC 674 ms
62,804 KB
testcase_03 AC 406 ms
57,908 KB
testcase_04 AC 634 ms
62,240 KB
testcase_05 AC 622 ms
62,996 KB
testcase_06 AC 640 ms
62,740 KB
testcase_07 AC 362 ms
58,484 KB
testcase_08 AC 625 ms
62,696 KB
testcase_09 AC 594 ms
62,444 KB
testcase_10 AC 635 ms
60,688 KB
testcase_11 AC 544 ms
62,264 KB
testcase_12 AC 537 ms
62,856 KB
testcase_13 AC 43 ms
49,432 KB
testcase_14 AC 45 ms
49,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		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++) {
		    String[] line = br.readLine().split(" ", 3);
		    types[i] = Integer.parseInt(line[0]);
		    lefts[i] = Integer.parseInt(line[1]);
		    rights[i] = Integer.parseInt(line[2]);
		    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