結果

問題 No.877 Range ReLU Query
ユーザー htensaihtensai
提出日時 2020-06-11 10:55:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 920 ms / 2,000 ms
コード長 2,849 bytes
コンパイル時間 3,140 ms
コンパイル使用メモリ 82,540 KB
実行使用メモリ 79,496 KB
最終ジャッジ日時 2023-08-08 04:41:01
合計ジャッジ時間 14,422 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
47,676 KB
testcase_01 AC 74 ms
51,128 KB
testcase_02 AC 67 ms
51,876 KB
testcase_03 AC 88 ms
51,044 KB
testcase_04 AC 47 ms
49,796 KB
testcase_05 AC 58 ms
50,592 KB
testcase_06 AC 58 ms
50,900 KB
testcase_07 AC 55 ms
49,924 KB
testcase_08 AC 82 ms
51,584 KB
testcase_09 AC 49 ms
49,832 KB
testcase_10 AC 69 ms
50,788 KB
testcase_11 AC 899 ms
71,656 KB
testcase_12 AC 846 ms
67,172 KB
testcase_13 AC 767 ms
67,440 KB
testcase_14 AC 777 ms
67,068 KB
testcase_15 AC 920 ms
77,992 KB
testcase_16 AC 882 ms
76,368 KB
testcase_17 AC 905 ms
78,940 KB
testcase_18 AC 917 ms
78,424 KB
testcase_19 AC 901 ms
79,096 KB
testcase_20 AC 903 ms
79,496 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));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int q = Integer.parseInt(first[1]);
		BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
		BinaryIndexedTree counts = new BinaryIndexedTree(n + 1);
		ArrayList<Order> list = new ArrayList<>();
		String[] second = br.readLine().split(" ", n);
		for (int i = 1; i <= n; i++) {
		    list.add(new Order(i, Integer.parseInt(second[i - 1])));
		}
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 4);
		    list.add(new Order(i, Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[3])));
		}
		Collections.sort(list);
		long[] ans = new long[q];
		for (Order x : list) {
		    if (x.isValue()) {
		        bit.add(x.idx, x.value);
		        counts.add(x.idx, 1);
		    } else {
		        long tmp = bit.getSum(x.left, x.right) - counts.getSum(x.left, x.right) * x.value;
		        ans[x.idx] = tmp;
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (long x : ans) {
		    sb.append(x).append("\n");
		}
		System.out.print(sb);
	}
	
	static class Order implements Comparable<Order> {
	    int idx;
	    int left;
	    int right;
	    int value;
	    
	    public Order(int idx, int left, int right, int value) {
	        this.idx = idx;
	        this.left = left;
	        this.right = right;
	        this.value = value;
	    }
	    
	    public Order(int idx, int value) {
	        this(idx, 0, 0, value);
	    }
	    
	    public boolean isValue() {
	        return right == 0;
	    }
	    
	    public int compareTo(Order another) {
	        if (value == another.value) {
	            return right - another.right;
	        } else {
	            return another.value - value;
	        }
	    }
	    
	    public String toString() {
	        return idx + ":" + left + ":" + right + ":" + value;
	    }
	}
}

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