結果

問題 No.876 Range Compress Query
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2019-09-06 22:20:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 203,464 KB
実行使用メモリ 6,740 KB
最終ジャッジ日時 2023-09-07 00:55:21
合計ジャッジ時間 3,991 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 69 ms
6,284 KB
testcase_12 AC 57 ms
6,200 KB
testcase_13 AC 58 ms
6,264 KB
testcase_14 AC 68 ms
6,324 KB
testcase_15 AC 51 ms
6,484 KB
testcase_16 AC 68 ms
6,416 KB
testcase_17 AC 69 ms
6,740 KB
testcase_18 AC 73 ms
6,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

/////////////////////
// Range Add Query //
/////////////////////

template<typename T = int64_t>
class RAddQ {
private:
	std::vector<T> container_;

	void build(const unsigned int array_size)
	{
		unsigned int length{1};
		while (length < array_size)
			length <<= 1;
		container_.resize(2 * length);
	}

public:
	RAddQ(const unsigned int array_size) { build(array_size); }
	RAddQ(const std::vector<T> &array)
	{
		build(array.size());
		std::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));
	}
	// left,rightは0-indexed、[left, right)の半開区間
	void update(const int left, const int right, const T added)
	{
		for (int left_i{std::max(0, left) + ((int)container_.size() >> 1)}, right_i{std::min((int)container_.size() >> 1, right) + ((int)container_.size() >> 1)};
			left_i < right_i; left_i >>= 1, right_i >>= 1
			)
		{
			if (left_i & 1)
			{
				container_[left_i] += added;
				left_i++;
			}
			if (right_i & 1)
			{
				right_i--;
				container_[right_i] += added;
			}
		}
	}
	// indexは0-indexed
	T get(const int index) const
	{
		T sum{};
		for (int add_place{index + ((int)container_.size() >> 1)}; add_place > 0; add_place >>= 1)
			sum += container_[add_place];
		return sum;
	}
};

///////////////////////////
// Range Sum Query (BIT) //
///////////////////////////

class RSumQBIT {
private:
	std::vector<int64_t> container_;

	int64_t getHelper(const int index) const
	{
		if (index < 0) return 0;
		if ((int)(container_.size()) <= index) return container_.back();
		int64_t sum{};
		for (int add_place{index}; add_place > 0; add_place -= add_place & -add_place)
			sum += container_[add_place];
		return sum;
	}

public:
	RSumQBIT(const int array_size)
		: container_(array_size + 1) {}
	// indexは0-indexed
	void update(const int index, const int64_t added)
	{
		for (int update_place{index + 1}; update_place < (int)(container_.size()); update_place += update_place & -update_place)
			container_[update_place] += added;
	}
	// left,rightは0-indexed、[left, right)の半開区間
	int64_t get(const int left, const int right) const
	{
		return -getHelper(left) + getHelper(right);
	}
};

int main()
{
	int N, Q;
	scanf("%d%d", &N, &Q);
	std::vector<int64_t> a(N + 2, -1);
	for (int i{1}; i <= N; i++) scanf("%lld", &a[i]);
	RAddQ<> raq(a);

	RSumQBIT bit(N + 2);
	for (int i{1}; i <= N; i++)
		if (a[i] != a[i + 1])
			bit.update(i, 1);
	
	for (int q_i{}; q_i < Q; q_i++)
	{
		int com, left, right;
		scanf("%d%d%d", &com, &left, &right);
		right++;
		if (com == 1)
		{
			int64_t x;
			scanf("%lld", &x);
			raq.update(left, right, x);

			int64_t left_prev{bit.get(left - 1, left)};
			if (raq.get(left - 1) != raq.get(left))
				bit.update(left - 1, -left_prev + 1);
			else
				bit.update(left - 1, -left_prev);

			int64_t right_prev{bit.get(right - 1, right)};
			if (raq.get(right - 1) != raq.get(right))
				bit.update(right - 1, -right_prev + 1);
			else
				bit.update(right - 1, -right_prev);
		}
		else
			printf("%lld\n", bit.get(left, right - 1) + 1);
	}

	return 0;
}
0