結果

問題 No.878 Range High-Element Query
ユーザー htensaihtensai
提出日時 2020-06-11 15:49:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,199 ms / 2,000 ms
コード長 3,189 bytes
コンパイル時間 4,020 ms
コンパイル使用メモリ 78,324 KB
実行使用メモリ 72,912 KB
最終ジャッジ日時 2023-09-06 08:14:25
合計ジャッジ時間 14,414 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,728 KB
testcase_01 AC 71 ms
49,248 KB
testcase_02 AC 88 ms
52,000 KB
testcase_03 AC 67 ms
50,912 KB
testcase_04 AC 71 ms
51,768 KB
testcase_05 AC 79 ms
52,084 KB
testcase_06 AC 57 ms
50,408 KB
testcase_07 AC 55 ms
49,816 KB
testcase_08 AC 92 ms
50,992 KB
testcase_09 AC 91 ms
50,800 KB
testcase_10 AC 61 ms
51,112 KB
testcase_11 AC 1,013 ms
72,912 KB
testcase_12 AC 966 ms
71,340 KB
testcase_13 AC 927 ms
66,672 KB
testcase_14 AC 809 ms
67,056 KB
testcase_15 AC 979 ms
70,888 KB
testcase_16 AC 1,199 ms
69,972 KB
testcase_17 AC 1,117 ms
68,208 KB
testcase_18 AC 1,149 ms
68,628 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]);
		Point[] points = new Point[n];
		String[] second = br.readLine().split(" ", n);
		for (int i = 0; i < n; i++) {
		    points[i] = new Point(i + 1, Integer.parseInt(second[i]));
		}
		Arrays.sort(points);
		TreeSet<Integer> num = new TreeSet<>();
		num.add(0);
		ArrayList<Order> list = new ArrayList<>();
		for (Point p : points) {
		    list.add(new Order(p.idx, num.lower(p.idx)));
		    num.add(p.idx);
		}
		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])));
		}
		BinaryIndexedTree counts = new BinaryIndexedTree(n + 1);
		Collections.sort(list);
		int[] ans = new int[q];
		for (Order x : list) {
		    if (x.isValue()) {
		        counts.add(x.idx, 1);
		    } else {
		        ans[x.idx] = counts.getSum(x.left, x.right);
		    }
		}
		StringBuilder sb = new StringBuilder();
		for (int x : ans) {
		    sb.append(x).append("\n");
		}
		System.out.print(sb);
	}
	
	static class Point implements Comparable<Point> {
	    int idx;
	    int value;
	    
	    public Point(int idx, int value) {
	        this.idx = idx;
	        this.value = value;
	    }
	    
	    public int compareTo(Point another) {
	        if (value == another.value) {
	            return idx - another.idx;
	        } else {
	            return another.value - value;
	        }
	    }
	}
	
	static class Order implements Comparable<Order> {
	    int idx;
	    int left;
	    int right;

	    public Order(int idx, int left, int right) {
	        this.idx = idx;
	        this.left = left;
	        this.right = right;
	    }
	    
	    public Order(int idx, int left) {
	        this(idx, left, 0);
	    }
	    
	    public boolean isValue() {
	        return right == 0;
	    }
	    
	    public int compareTo(Order another) {
	        if (left == another.left) {
	            return another.right - right;
	        } else {
	            return left - another.left;
	        }
	    }
	    
	    public String toString() {
	        return idx + ":" + left + ":" + right;
	    }
	}
}

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