結果

問題 No.833 かっこいい電車
ユーザー htensaihtensai
提出日時 2020-06-11 17:20:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 525 ms / 2,000 ms
コード長 4,610 bytes
コンパイル時間 2,195 ms
コンパイル使用メモリ 74,316 KB
実行使用メモリ 64,372 KB
最終ジャッジ日時 2023-09-06 08:18:45
合計ジャッジ時間 13,649 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 472 ms
62,612 KB
testcase_01 AC 44 ms
49,824 KB
testcase_02 AC 44 ms
49,408 KB
testcase_03 AC 45 ms
49,428 KB
testcase_04 AC 43 ms
49,596 KB
testcase_05 AC 44 ms
49,460 KB
testcase_06 AC 44 ms
49,320 KB
testcase_07 AC 45 ms
49,404 KB
testcase_08 AC 43 ms
49,508 KB
testcase_09 AC 43 ms
49,528 KB
testcase_10 AC 452 ms
62,144 KB
testcase_11 AC 525 ms
64,336 KB
testcase_12 AC 299 ms
59,108 KB
testcase_13 AC 242 ms
56,284 KB
testcase_14 AC 462 ms
64,152 KB
testcase_15 AC 327 ms
61,172 KB
testcase_16 AC 265 ms
60,976 KB
testcase_17 AC 249 ms
57,664 KB
testcase_18 AC 488 ms
62,188 KB
testcase_19 AC 271 ms
60,392 KB
testcase_20 AC 129 ms
55,092 KB
testcase_21 AC 465 ms
60,220 KB
testcase_22 AC 344 ms
63,820 KB
testcase_23 AC 263 ms
60,604 KB
testcase_24 AC 358 ms
64,372 KB
testcase_25 AC 479 ms
62,436 KB
testcase_26 AC 290 ms
62,708 KB
testcase_27 AC 388 ms
62,208 KB
testcase_28 AC 357 ms
59,760 KB
testcase_29 AC 359 ms
59,844 KB
testcase_30 AC 488 ms
63,988 KB
testcase_31 AC 450 ms
62,460 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]);
		BinaryIndexedTree bit = new BinaryIndexedTree(n + 1);
		String[] second = br.readLine().split(" ", n);
		for (int i = 0; i < n; i++) {
		    bit.add(i + 1, Integer.parseInt(second[i]));
		}
		SegmentTree st = new SegmentTree(n + 1);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int type = Integer.parseInt(line[0]);
		    int x = Integer.parseInt(line[1]);
		    if (type == 1) {
		        int left = st.getLeft(x);
		        int right = st.getRight(x + 1);
		        st.update(left, right, left, right);
		    } else if (type == 2) {
		        int left = st.getLeft(x);
		        int right = st.getRight(x + 1);
		        st.update(left, x, left, x);
		        st.update(x + 1, right, x + 1, right);
		    } else if (type == 3) {
		        bit.add(x, 1);
		    } else {
		        int left = st.getLeft(x);
		        int right = st.getRight(x);
		        sb.append(bit.getSum(left, right)).append("\n");
		    }
		}
		System.out.print(sb);
	}
}

class SegmentTree {
    static final int INF = Integer.MAX_VALUE;
    int size;
    int base;
    int[] lefts;
    int[] rights;
    
    public SegmentTree(int size) {
        this.size = size;
        base = 1;
        while (base < size) {
            base <<= 1;
        }
        lefts = new int[base * 2 - 1];
        rights = new int[base * 2 - 1];
        for (int i = 1; i <= size; i++) {
            lefts[base - 1 + i] = i;
            rights[base - 1 + i] = i;
        }
    }
    
    public int getLeft(int idx) {
        return getLeftTree(idx + base - 1);
    }
    
    public int getLeftTree(int idx) {
        if (idx == 0) {
            return lefts[idx];
        }
        int ans;
        if (idx % 2 == 1) {
            ans = getLeftTree((idx - 1) / 2);
        } else {
            ans = getLeftTree((idx - 1) / 2);
        }
        if (ans == 0) {
            return lefts[idx];
        } else {
            return ans;
        }
    }
    
    public int getRight(int idx) {
        return getRightTree(idx + base - 1);
    }
    
    public int getRightTree(int idx) {
        if (idx == 0) {
            return rights[idx];
        }
        int ans;
        if (idx % 2 == 1) {
            ans = getRightTree((idx - 1) / 2);
        } else {
            ans = getRightTree((idx - 1) / 2);
        }
        if (ans == 0) {
            return rights[idx];
        } else {
            return ans;
        }
    }

    public void update(int min, int max, int valueL, int valueR)  {
        update(min, max, 0, valueL, valueR, 0, base);
    }
    
    public void update(int min, int max, int idx, int valueL, int valueR, int left, int right) {
        if (min >= right || max < left) {
            return;
        }
        if (min <= left && right - 1 <= max) {
            lefts[idx] = valueL;
            rights[idx] = valueR;
            return;
        }
        update(min, max, 2 * idx + 1, valueL, valueR, left, (left + right) / 2);
        update(min, max, 2 * idx + 2, valueL, valueR, (left + right) / 2, right);
        if (lefts[idx] != 0) {
            int tmpL = lefts[idx];
            int tmpR = rights[idx];
            lefts[idx] = 0;
            rights[idx] = 0;
            if (left < min) {
                update(left, min - 1, idx, tmpL, tmpR, left, right);
            }
            if (max < right - 1) {
                update(max + 1, right, idx, tmpL, tmpR, left, right);
            }
        }
    }
 }

class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[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 long getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    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;
    }
}
0