結果

問題 No.854 公平なりんご分配
ユーザー MarcusAureliusAntoninus
提出日時 2019-07-26 22:07:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,374 bytes
コンパイル時間 3,276 ms
コンパイル使用メモリ 198,592 KB
最終ジャッジ日時 2025-01-07 08:00:06
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27 WA * 65
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:109:36: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘long int*’ [-Wformat=]
  109 |         for (auto& e: A) scanf("%lld", &e);
      |                                 ~~~^   ~~
      |                                    |   |
      |                                    |   long int*
      |                                    long long int*
      |                                 %ld
main.cpp:117:27: warning: format ‘%lld’ expects argument of type ‘long long int*’, but argument 2 has type ‘int64_t*’ {aka ‘long int*’} [-Wformat=]
  117 |                 scanf("%lld%d%d", &P, &L, &R);
      |                        ~~~^       ~~
      |                           |       |
      |                           |       int64_t* {aka long int*}
      |                           long long int*
      |                        %ld
main.cpp: In instantiation of ‘SegmentTree<T, Operation_t, Update_t>::SegmentTree(const std::vector<_Tp>&, Operation_t, Update_t, T) [with T = long int; Operation_t = std::multiplies<long int>; Update_t = main()::<lambda(const auto:28&, const auto:29&)>]’:
main.cpp:101:9:   required from ‘decltype(auto) makeSegmentTree(const std::vector<_Tp>&, Operation_t, Update_t, T) [with T = long int; Operation_t = std::multiplies<long int>; Update_t = main()::<lambda(const auto:28&, const auto:29&)>]’
main.cpp:110:30:   required from here
main.cpp:67:62: warning: narrowing conversion of ‘((((SegmentTree<long int, std::multiplies<long int>, main()::<lambda(const auto:28&, const auto:29&)> >*)this)->SegmentTree<long int, std::multiplies<long int>, main()::<lambda(const auto:28&, const auto:29&)> >::container_.std::vector<long int>::size() >> 1) - 1)’ from ‘std::vector<long int>::size_type’ {aka ‘long unsigned int’} to ‘unsigned int’ [-Wnarrowing]
   67 |                 for (unsigned int i{(container_.size() >> 1) - 1}; i 

ソースコード

diff #

#include <bits/stdc++.h>

int64_t calcGCD(int64_t a, int64_t b)
{
	while (a)
	{
		b %= a;
		std::swap(a, b);
	}
	return b;
}

///////////////////////
// 抽象化セグメント木 //
//////////////////////

// モノイドを扱えるが半群は扱えない
// identityはその演算の単位元としてのみ使用されるので、minの単位元としてINT64_MAXなどが許される
// std::minをそのまま渡してもオーバーロードを解決できないのでlambda式で包む必要がある
template <typename T, typename Operation_t, typename Update_t>
class SegmentTree {
private:

	std::vector<T> container_;
	// モノイドの演算
	Operation_t operate_;
	// 更新する値を返す
	Update_t assign_;
	// モノイドの単位元
	T identity_;

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

	// // デバッグ用出力関数(中身は適宜編集)
	// void debugOutput()
	// {
	// 	int line_break{2};
	// 	for (int i{1}; i < (int)container_.size(); i++)
	// 	{
	// 		// std::cout << container_[i] << ' ';
	// 		if (i + 1 == line_break)
	// 		{
	// 			putchar('\n');
	// 			line_break *= 2;
	// 		}
	// 	}
	// }

public:
	SegmentTree(const unsigned int array_size, Operation_t operate, Update_t assign, T identity)
		: operate_(operate), assign_(assign), identity_(identity)
	{
		build(array_size);
	}
	SegmentTree(const std::vector<T> &array, Operation_t operate, Update_t assign, T identity)
		: operate_(operate), assign_(assign), identity_(identity)
	{
		build(array.size());
		std::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));

		for (unsigned int i{(container_.size() >> 1) - 1}; i > 0; i--)
			container_[i] = operate_(container_[2 * i], container_[2 * i + 1]);
	}
	SegmentTree(const SegmentTree& st)
		: container_(st.container_), operate_(st.operate_), assign_(st.assign_), identity_(st.identity_){}
	SegmentTree(SegmentTree&& st)
		: container_(std::move(st.container_)), operate_(st.operate_), assign_(st.assign_), identity_(st.identity_){}

	// 配列内の半開区間[l,r)(0-indexed)の総和を返す
	bool get(const int left, const int right, int64_t num) const
	{
		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)
			{
				num /= calcGCD(num, container_[left_i]);
				left_i++;
			}
			if (right_i & 1)
			{
				right_i--;
				num /= calcGCD(num, container_[right_i]);
			}
		}
		return num == 1ll;
	}
};


template <typename T, typename Operation_t, typename Update_t>
decltype(auto) makeSegmentTree(const std::vector<T> &array, Operation_t operate, Update_t assign, T identity)
{
	return SegmentTree<T, Operation_t, Update_t>(array, operate, assign, identity);
}

int main()
{
	int N;
	scanf("%d", &N);
	std::vector<int64_t> A(N);
	for (auto& e: A) scanf("%lld", &e);
	auto segTree{makeSegmentTree(A, std::multiplies<int64_t>(), [](const auto& a, const auto& b) { return a; }, (int64_t)1)};
	int Q;
	scanf("%d", &Q);
	for (int i{}; i < Q; i++)
	{
		int64_t P;
		int L, R;
		scanf("%lld%d%d", &P, &L, &R);
		if (segTree.get(L - 1, R, P)) puts("Yes");
		else puts("NO");
	}

	return 0;
}
0