結果

問題 No.833 かっこいい電車
ユーザー tentententen
提出日時 2021-03-08 18:08:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,077 ms / 2,000 ms
コード長 3,280 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 77,848 KB
実行使用メモリ 71,376 KB
最終ジャッジ日時 2024-04-18 12:12:30
合計ジャッジ時間 24,383 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,077 ms
71,320 KB
testcase_01 AC 129 ms
54,012 KB
testcase_02 AC 127 ms
53,916 KB
testcase_03 AC 136 ms
54,000 KB
testcase_04 AC 132 ms
54,024 KB
testcase_05 AC 135 ms
53,968 KB
testcase_06 AC 131 ms
54,084 KB
testcase_07 AC 130 ms
53,900 KB
testcase_08 AC 114 ms
54,192 KB
testcase_09 AC 132 ms
54,152 KB
testcase_10 AC 902 ms
69,108 KB
testcase_11 AC 1,075 ms
69,304 KB
testcase_12 AC 522 ms
59,304 KB
testcase_13 AC 518 ms
59,316 KB
testcase_14 AC 878 ms
69,176 KB
testcase_15 AC 688 ms
61,340 KB
testcase_16 AC 590 ms
59,284 KB
testcase_17 AC 588 ms
59,392 KB
testcase_18 AC 1,019 ms
68,548 KB
testcase_19 AC 600 ms
59,604 KB
testcase_20 AC 346 ms
59,252 KB
testcase_21 AC 918 ms
69,432 KB
testcase_22 AC 778 ms
63,876 KB
testcase_23 AC 647 ms
59,548 KB
testcase_24 AC 787 ms
63,732 KB
testcase_25 AC 1,004 ms
69,224 KB
testcase_26 AC 675 ms
59,536 KB
testcase_27 AC 878 ms
66,380 KB
testcase_28 AC 693 ms
63,128 KB
testcase_29 AC 778 ms
64,376 KB
testcase_30 AC 758 ms
71,240 KB
testcase_31 AC 875 ms
71,376 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();
        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);
    }
}
0