結果

問題 No.876 Range Compress Query
ユーザー htensaihtensai
提出日時 2020-06-12 08:47:02
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,371 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 74,352 KB
実行使用メモリ 66,492 KB
最終ジャッジ日時 2023-09-06 08:57:07
合計ジャッジ時間 9,205 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,440 KB
testcase_01 WA -
testcase_02 AC 46 ms
49,300 KB
testcase_03 WA -
testcase_04 AC 51 ms
47,504 KB
testcase_05 AC 51 ms
49,388 KB
testcase_06 AC 72 ms
51,984 KB
testcase_07 AC 75 ms
50,300 KB
testcase_08 AC 64 ms
51,244 KB
testcase_09 AC 74 ms
50,448 KB
testcase_10 AC 61 ms
51,252 KB
testcase_11 AC 483 ms
62,392 KB
testcase_12 AC 444 ms
63,004 KB
testcase_13 AC 436 ms
62,844 KB
testcase_14 AC 487 ms
62,892 KB
testcase_15 AC 394 ms
62,300 KB
testcase_16 AC 490 ms
66,400 KB
testcase_17 AC 488 ms
66,492 KB
testcase_18 AC 481 ms
63,068 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);
		long[] arr = new long[n + 2];
		String[] second = br.readLine().split(" ", n);
		int prev = Integer.parseInt(second[0]);
		for (int i = 1; i < n; i++) {
		    int x = Integer.parseInt(second[i]);
		    arr[i] = x - prev;
		    prev = x;
		    if (arr[i] != 0) {
		        bit.add(i, 1);
		    }
		}
		arr[n]  = prev;
		bit.add(n, 1);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ");
		    int type = Integer.parseInt(line[0]);
		    int left = Integer.parseInt(line[1]);
		    int right = Integer.parseInt(line[2]);
		    if (type == 1) {
		        int x = Integer.parseInt(line[3]);
		        arr[left - 1] += x;
		        if (left > 1) {
		            if (arr[left - 1] == 0) {
		                bit.add(left - 1, -1);
		            } else if (arr[left - 1] == x) {
		                bit.add(left - 1, 1);
		            }
		        }
		        arr[right] -= x;
		        if (arr[right] == 0) {
		            bit.add(right, -1);
		        } else if (arr[right] == -x) {
		            bit.add(right, 1);
		        }
		    } else {
		        sb.append(bit.getSum(left, right)).append("\n");
		    }
		}
		System.out.print(sb);
	}
}

class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[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 int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0