結果
問題 | No.833 かっこいい電車 |
ユーザー | tenten |
提出日時 | 2021-03-08 18:08:18 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,124 ms / 2,000 ms |
コード長 | 3,280 bytes |
コンパイル時間 | 2,150 ms |
コンパイル使用メモリ | 77,376 KB |
実行使用メモリ | 71,376 KB |
最終ジャッジ日時 | 2024-10-10 05:22:46 |
合計ジャッジ時間 | 25,426 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,059 ms
71,288 KB |
testcase_01 | AC | 135 ms
54,060 KB |
testcase_02 | AC | 134 ms
54,020 KB |
testcase_03 | AC | 140 ms
54,368 KB |
testcase_04 | AC | 137 ms
54,092 KB |
testcase_05 | AC | 134 ms
54,340 KB |
testcase_06 | AC | 133 ms
53,760 KB |
testcase_07 | AC | 144 ms
54,064 KB |
testcase_08 | AC | 132 ms
53,908 KB |
testcase_09 | AC | 131 ms
54,308 KB |
testcase_10 | AC | 963 ms
69,172 KB |
testcase_11 | AC | 1,124 ms
69,400 KB |
testcase_12 | AC | 579 ms
59,100 KB |
testcase_13 | AC | 532 ms
59,416 KB |
testcase_14 | AC | 968 ms
69,252 KB |
testcase_15 | AC | 706 ms
61,560 KB |
testcase_16 | AC | 630 ms
59,464 KB |
testcase_17 | AC | 645 ms
59,544 KB |
testcase_18 | AC | 1,016 ms
69,136 KB |
testcase_19 | AC | 541 ms
59,444 KB |
testcase_20 | AC | 366 ms
59,136 KB |
testcase_21 | AC | 982 ms
69,140 KB |
testcase_22 | AC | 778 ms
64,452 KB |
testcase_23 | AC | 674 ms
59,300 KB |
testcase_24 | AC | 685 ms
64,380 KB |
testcase_25 | AC | 1,051 ms
69,504 KB |
testcase_26 | AC | 700 ms
60,100 KB |
testcase_27 | AC | 872 ms
67,700 KB |
testcase_28 | AC | 729 ms
63,488 KB |
testcase_29 | AC | 822 ms
64,444 KB |
testcase_30 | AC | 847 ms
66,448 KB |
testcase_31 | AC | 873 ms
71,376 KB |
ソースコード
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(); BinaryIndexedTree bit = new BinaryIndexedTree(n + 1); for (int i = 1; i <= n; i++) { bit.add(i, sc.nextInt()); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < q; i++) { int type = sc.nextInt(); int idx = sc.nextInt(); if (type == 1) { bit.connect(idx); } else if (type == 2) { bit.separate(idx); } else if (type == 3) { bit.add(idx, 1); } else { sb.append(bit.getValue(idx)).append("\n"); } } System.out.print(sb); } } class BinaryIndexedTree { int size; long[] tree; int[] group; public BinaryIndexedTree(int size) { this.size = size; tree = new long[size]; group = new int[size]; for (int i = 1; i < size; i++) { addGroup(i, 1); } } 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 void connect(int idx) { if (getGroupSum(idx) < getGroupSum(idx + 1)) { addGroup(idx + 1, -1); } } public void separate(int idx) { if (getGroupSum(idx) == getGroupSum(idx + 1)) { addGroup(idx + 1, 1); } } public void addGroup(int idx, int value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { group[idx] += value; idx += mask; } mask <<= 1; } } public long getSum(int from, int to) { return getSum(to) - getSum(from); } 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; } public int getGroupSum(int x) { int mask = 1; int ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += group[x]; x -= mask; } mask <<= 1; } return ans; } public long getValue(int idx) { int leftMin = 0; int rightMin = idx; while (rightMin - leftMin > 1) { int m = (leftMin + rightMin) / 2; if (getGroupSum(m) < getGroupSum(idx)) { leftMin = m; } else { rightMin = m; } } int leftMax = idx; int rightMax = size; while (rightMax - leftMax > 1) { int m = (leftMax + rightMax) / 2; if (getGroupSum(m) == getGroupSum(idx)) { leftMax = m; } else { rightMax = m; } } return getSum(leftMin, leftMax); } }