結果
問題 | No.878 Range High-Element Query |
ユーザー | htensai |
提出日時 | 2020-06-11 15:49:32 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,146 ms / 2,000 ms |
コード長 | 3,189 bytes |
コンパイル時間 | 3,092 ms |
コンパイル使用メモリ | 87,112 KB |
実行使用メモリ | 71,036 KB |
最終ジャッジ日時 | 2024-06-24 02:59:58 |
合計ジャッジ時間 | 13,478 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 60 ms
50,128 KB |
testcase_01 | AC | 92 ms
51,436 KB |
testcase_02 | AC | 98 ms
51,260 KB |
testcase_03 | AC | 87 ms
51,096 KB |
testcase_04 | AC | 93 ms
51,308 KB |
testcase_05 | AC | 94 ms
51,272 KB |
testcase_06 | AC | 65 ms
50,640 KB |
testcase_07 | AC | 61 ms
50,276 KB |
testcase_08 | AC | 102 ms
51,404 KB |
testcase_09 | AC | 101 ms
51,340 KB |
testcase_10 | AC | 85 ms
50,604 KB |
testcase_11 | AC | 1,128 ms
71,036 KB |
testcase_12 | AC | 1,054 ms
69,176 KB |
testcase_13 | AC | 843 ms
65,732 KB |
testcase_14 | AC | 784 ms
65,992 KB |
testcase_15 | AC | 963 ms
69,736 KB |
testcase_16 | AC | 1,143 ms
66,944 KB |
testcase_17 | AC | 1,131 ms
67,656 KB |
testcase_18 | AC | 1,146 ms
67,380 KB |
ソースコード
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; } }