結果

問題 No.877 Range ReLU Query
ユーザー tentententen
提出日時 2020-12-16 09:18:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,684 ms / 2,000 ms
コード長 2,957 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 79,432 KB
実行使用メモリ 81,372 KB
最終ジャッジ日時 2024-04-25 22:28:33
合計ジャッジ時間 21,938 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
54,212 KB
testcase_01 AC 212 ms
56,848 KB
testcase_02 AC 212 ms
56,940 KB
testcase_03 AC 224 ms
57,560 KB
testcase_04 AC 179 ms
54,376 KB
testcase_05 AC 207 ms
56,592 KB
testcase_06 AC 205 ms
56,604 KB
testcase_07 AC 194 ms
54,744 KB
testcase_08 AC 220 ms
57,280 KB
testcase_09 AC 187 ms
54,588 KB
testcase_10 AC 211 ms
57,116 KB
testcase_11 AC 1,539 ms
77,060 KB
testcase_12 AC 1,609 ms
76,940 KB
testcase_13 AC 1,343 ms
74,204 KB
testcase_14 AC 1,440 ms
77,056 KB
testcase_15 AC 1,553 ms
76,376 KB
testcase_16 AC 1,510 ms
78,324 KB
testcase_17 AC 1,551 ms
77,552 KB
testcase_18 AC 1,554 ms
78,340 KB
testcase_19 AC 1,684 ms
81,372 KB
testcase_20 AC 1,548 ms
77,708 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);
		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);
	        }
	        ans[x.id] = bit.getSum(x.left, x.right) - bit.getCount(x.left, x.right) * (long)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;
    int[] counts;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
        counts = new int[size];
    }
    
    public void add(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                counts[idx]++;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getCount(int from, int to) {
        return getCount(to) - getCount(from - 1);
    }
    
    public int getCount(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += counts[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
    
    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