結果
問題 | No.833 かっこいい電車 |
ユーザー |
![]() |
提出日時 | 2021-03-08 18:08:18 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
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);}}