結果
問題 | No.833 かっこいい電車 |
ユーザー | htensai |
提出日時 | 2020-06-11 17:20:15 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 616 ms / 2,000 ms |
コード長 | 4,610 bytes |
コンパイル時間 | 2,801 ms |
コンパイル使用メモリ | 78,044 KB |
実行使用メモリ | 65,128 KB |
最終ジャッジ日時 | 2024-06-24 03:03:46 |
合計ジャッジ時間 | 15,900 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 527 ms
62,372 KB |
testcase_01 | AC | 61 ms
50,220 KB |
testcase_02 | AC | 60 ms
50,112 KB |
testcase_03 | AC | 70 ms
50,152 KB |
testcase_04 | AC | 63 ms
49,996 KB |
testcase_05 | AC | 66 ms
50,192 KB |
testcase_06 | AC | 66 ms
49,880 KB |
testcase_07 | AC | 66 ms
50,168 KB |
testcase_08 | AC | 62 ms
49,892 KB |
testcase_09 | AC | 65 ms
49,816 KB |
testcase_10 | AC | 540 ms
62,444 KB |
testcase_11 | AC | 616 ms
63,812 KB |
testcase_12 | AC | 336 ms
58,384 KB |
testcase_13 | AC | 269 ms
55,648 KB |
testcase_14 | AC | 556 ms
63,884 KB |
testcase_15 | AC | 384 ms
61,232 KB |
testcase_16 | AC | 325 ms
62,432 KB |
testcase_17 | AC | 295 ms
57,196 KB |
testcase_18 | AC | 555 ms
62,144 KB |
testcase_19 | AC | 308 ms
60,328 KB |
testcase_20 | AC | 173 ms
54,556 KB |
testcase_21 | AC | 540 ms
60,140 KB |
testcase_22 | AC | 385 ms
63,632 KB |
testcase_23 | AC | 337 ms
60,680 KB |
testcase_24 | AC | 403 ms
62,724 KB |
testcase_25 | AC | 568 ms
62,300 KB |
testcase_26 | AC | 337 ms
62,312 KB |
testcase_27 | AC | 445 ms
62,092 KB |
testcase_28 | AC | 396 ms
59,664 KB |
testcase_29 | AC | 420 ms
59,960 KB |
testcase_30 | AC | 540 ms
65,128 KB |
testcase_31 | AC | 510 ms
62,252 KB |
ソースコード
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; } }