結果

問題 No.876 Range Compress Query
ユーザー MarcusAureliusAntoninus
提出日時 2019-09-06 22:20:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 3,074 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 197,560 KB
最終ジャッジ日時 2025-01-07 16:52:25
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:95:47: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘__gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type*’ {aka ‘long int*’} [-Wformat=]
   95 |         for (int i{1}; i <= N; i++) scanf("%lld", &a[i]);
      |                                            ~~~^
      |                                               |
      |                                               long long int*
      |                                            %ld
main.cpp:111:35: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int64_t*’ {aka ‘long int*’} [-Wformat=]
  111 |                         scanf("%lld", &x);
      |                                ~~~^   ~~
      |                                   |   |
      |                                   |   int64_t* {aka long int*}
      |                                   long long int*
      |                                %ld
main.cpp:127:36: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘int64_t’ {aka ‘long int’} [-Wformat=]
  127 |                         printf("%lld\n", bit.get(left, right - 1) + 1);
      |                                 ~~~^     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                    |                              |
      |                                    long long int                  int64_t {aka long int}
      |                                 %ld
main.cpp:93:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   93 |         scanf("%d%d", &N, &Q);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:95:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |         for (int i{1}; 

ソースコード

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