結果

問題 No.876 Range Compress Query
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2019-09-06 22:03:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,202 bytes
コンパイル時間 2,208 ms
コンパイル使用メモリ 204,660 KB
実行使用メモリ 6,880 KB
最終ジャッジ日時 2023-09-06 23:56:36
合計ジャッジ時間 3,968 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 68 ms
6,316 KB
testcase_12 AC 58 ms
6,168 KB
testcase_13 AC 58 ms
6,168 KB
testcase_14 AC 68 ms
6,344 KB
testcase_15 AC 52 ms
6,548 KB
testcase_16 AC 67 ms
6,548 KB
testcase_17 AC 67 ms
6,880 KB
testcase_18 AC 71 ms
6,536 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));
		for (int i{((int)container_.size() >> 1) - 1}; i > 0; i--)
			container_[i] = container_[2 * i] + container_[2 * i + 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);
	for (auto& e: a) scanf("%lld", &e);
	RAddQ<> raq(a);

	RSumQBIT bit(N);
	for (int i{}; i < N - 1; 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);
		left--;
		if (com == 1)
		{
			int64_t x;
			scanf("%lld", &x);
			raq.update(left, right, x);
			if (left > 0)
			{
				int64_t prev{bit.get(left - 1, left)};
				if (raq.get(left - 1) != raq.get(left))
					bit.update(left - 1, -prev + 1);
				else
					bit.update(left - 1, -prev);
			}
			if (right < N)
			{
				int64_t prev{bit.get(right - 1, right)};
				if (raq.get(right - 1) != raq.get(right))
					bit.update(right - 1, -prev + 1);
				else
					bit.update(right - 1, -prev);
			}
		}
		else
			printf("%lld\n", bit.get(left, right - 1) + 1);
	}

	return 0;
}
0