結果

問題 No.877 Range ReLU Query
ユーザー tentententen
提出日時 2020-12-16 09:03:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,454 ms / 2,000 ms
コード長 2,578 bytes
コンパイル時間 2,564 ms
コンパイル使用メモリ 79,924 KB
実行使用メモリ 80,772 KB
最終ジャッジ日時 2024-04-25 22:28:10
合計ジャッジ時間 19,598 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
54,372 KB
testcase_01 AC 197 ms
56,768 KB
testcase_02 AC 197 ms
56,964 KB
testcase_03 AC 204 ms
57,468 KB
testcase_04 AC 161 ms
54,760 KB
testcase_05 AC 186 ms
56,620 KB
testcase_06 AC 172 ms
56,788 KB
testcase_07 AC 180 ms
54,664 KB
testcase_08 AC 201 ms
57,368 KB
testcase_09 AC 169 ms
54,464 KB
testcase_10 AC 186 ms
56,564 KB
testcase_11 AC 1,360 ms
77,440 KB
testcase_12 AC 1,403 ms
76,548 KB
testcase_13 AC 1,224 ms
75,500 KB
testcase_14 AC 1,172 ms
75,912 KB
testcase_15 AC 1,454 ms
77,408 KB
testcase_16 AC 1,402 ms
78,736 KB
testcase_17 AC 1,430 ms
78,820 KB
testcase_18 AC 1,368 ms
80,432 KB
testcase_19 AC 1,452 ms
80,772 KB
testcase_20 AC 1,383 ms
79,592 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 q = sc.nextInt();
		PriorityQueue<Unit> units = new PriorityQueue<>();
		for (int i = 1; i <= n; i++) {
		    units.add(new Unit(i, sc.nextInt()));
		}
		PriorityQueue<Query> queries = new PriorityQueue<>();
		for (int i = 0; i < q; i++) {
		    sc.nextInt();
		    queries.add(new Query(i, sc.nextInt(), sc.nextInt(), sc.nextInt()));
		}
		BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
		BinaryIndexedTree bit2 = new BinaryIndexedTree(n + 1);
		long[] ans = new long[q];
		while (queries.size() > 0) {
	        Query x = queries.poll();
	        while (units.size() > 0 && units.peek().value >= x.value) {
	            Unit u = units.poll();
	            bit.add(u.id, u.value);
	            bit2.add(u.id, 1);
	        }
	        ans[x.id] = bit.getSum(x.left, x.right) - bit2.getSum(x.left, x.right) * x.value;
		}
		StringBuilder sb = new StringBuilder();
		for (long x : ans) {
		    sb.append(x).append("\n");
		}
		System.out.print(sb);
	}
	
	static class Query implements Comparable<Query> {
	    int id;
	    int left;
	    int right;
	    int value;
	    
	    public Query(int id, int left, int right, int value) {
	        this.id = id;
	        this.left = left;
	        this.right = right;
	        this.value = value;
	    }
	    
	    public int compareTo(Query another) {
	        return another.value - value;
	    }
	}
	
	static class Unit implements Comparable<Unit> {
	    int id;
	    int value;
	    
	    public Unit(int id, int value) {
	        this.id = id;
	        this.value = value;
	    }
	    
	    public int compareTo(Unit another) {
	        return another.value - 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