結果

問題 No.876 Range Compress Query
ユーザー htensai
提出日時 2020-06-12 08:47:02
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,371 bytes
コンパイル時間 2,219 ms
コンパイル使用メモリ 78,156 KB
実行使用メモリ 55,828 KB
最終ジャッジ日時 2024-06-24 03:40:01
合計ジャッジ時間 8,874 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 16 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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