結果

問題 No.833 かっこいい電車
ユーザー htensaihtensai
提出日時 2020-06-11 17:13:19
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 4,553 bytes
コンパイル時間 4,441 ms
コンパイル使用メモリ 74,404 KB
実行使用メモリ 497,320 KB
最終ジャッジ日時 2023-09-06 08:18:01
合計ジャッジ時間 30,860 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 480 ms
62,408 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 45 ms
49,840 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 44 ms
49,312 KB
testcase_09 AC 44 ms
49,308 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 138 ms
55,404 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 460 ms
62,208 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];
            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