結果
問題 | No.876 Range Compress Query |
ユーザー | tenten |
提出日時 | 2022-08-27 11:52:27 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 570 ms / 2,000 ms |
コード長 | 3,474 bytes |
コンパイル時間 | 2,332 ms |
コンパイル使用メモリ | 78,352 KB |
実行使用メモリ | 62,896 KB |
最終ジャッジ日時 | 2024-10-14 10:20:13 |
合計ジャッジ時間 | 9,789 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
50,404 KB |
testcase_01 | AC | 93 ms
51,496 KB |
testcase_02 | AC | 57 ms
50,320 KB |
testcase_03 | AC | 95 ms
51,456 KB |
testcase_04 | AC | 64 ms
50,304 KB |
testcase_05 | AC | 62 ms
50,488 KB |
testcase_06 | AC | 98 ms
51,328 KB |
testcase_07 | AC | 93 ms
51,476 KB |
testcase_08 | AC | 95 ms
51,572 KB |
testcase_09 | AC | 83 ms
51,352 KB |
testcase_10 | AC | 91 ms
51,284 KB |
testcase_11 | AC | 546 ms
61,092 KB |
testcase_12 | AC | 504 ms
60,932 KB |
testcase_13 | AC | 503 ms
60,812 KB |
testcase_14 | AC | 535 ms
60,932 KB |
testcase_15 | AC | 490 ms
61,712 KB |
testcase_16 | AC | 570 ms
62,896 KB |
testcase_17 | AC | 565 ms
62,780 KB |
testcase_18 | AC | 564 ms
62,884 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int q = sc.nextInt(); BinaryIndexedTree values = new BinaryIndexedTree(n + 2); for (int i = 1; i <= n; i++) { int x = sc.nextInt(); values.add(i, x); values.add(i + 1, -x); } BinaryIndexedTree counts = new BinaryIndexedTree(n + 1); for (int i = 1; i < n; i++) { if (values.getSum(i) != values.getSum(i + 1)) { counts.add(i, 1); } } StringBuilder sb = new StringBuilder(); while (q-- > 0) { if (sc.nextInt() == 1) { int left = sc.nextInt(); int right = sc.nextInt(); int x = sc.nextInt(); if (left > 1) { long lPrevMinus = values.getSum(left - 1); long lPrev = values.getSum(left); if (lPrevMinus == lPrev) { counts.add(left - 1, 1); } else if (lPrevMinus - lPrev == x) { counts.add(left - 1, -1); } } if (right < n) { long rPrev = values.getSum(right); long rPrevPlus = values.getSum(right + 1); if (rPrev == rPrevPlus) { counts.add(right, 1); } else if (rPrevPlus - rPrev == x) { counts.add(right, -1); } } values.add(left, x); values.add(right + 1, -x); } else { int left = sc.nextInt(); int right = sc.nextInt(); sb.append(counts.getSum(left, right - 1) + 1).append("\n"); } } System.out.print(sb); } } class BinaryIndexedTree { int size; long[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new long[size]; } 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 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; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }