結果

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

ソースコード

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

	return 0;
}
0