結果

問題 No.905 Sorted?
ユーザー polylogKpolylogK
提出日時 2019-10-11 23:46:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,917 bytes
コンパイル時間 1,820 ms
コンパイル使用メモリ 173,492 KB
実行使用メモリ 15,716 KB
最終ジャッジ日時 2024-05-04 03:59:53
合計ジャッジ時間 4,411 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 2 ms
6,812 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,948 KB
testcase_07 AC 7 ms
6,940 KB
testcase_08 WA -
testcase_09 AC 73 ms
9,532 KB
testcase_10 WA -
testcase_11 AC 86 ms
15,576 KB
testcase_12 AC 91 ms
15,716 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 89 ms
15,588 KB
testcase_16 AC 109 ms
15,640 KB
testcase_17 AC 109 ms
15,604 KB
testcase_18 AC 96 ms
15,620 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename Monoid>
class segment_tree {
	using value_type = Monoid;
	using size_type = size_t;

	using binary_function = std::function<value_type(value_type, value_type)>;
	using checker = std::function<bool(value_type)>;
	
	const size_type size_;
	size_type height_;

	const value_type id;
	const binary_function bi_func;
	std::vector<value_type> data;
	
	private:
	const size_type get_height(const size_type& size) const {
		size_type height = 1;
		while(1 << height < size) height++;
		return height;
	}
	const size_type base_size() const {
		return 1 << height_;
	}
	void meld(const size_type& index) {
		data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]);
	}
	
	public:
	segment_tree(const size_type& size, const binary_function& bi_func, const value_type& id)
		: size_(size),
		  bi_func(bi_func),
		  id(id) {
		height_ = get_height(size);
		data.assign(base_size() << 1, id);
	}
	value_type fold(size_type left, size_type right) {
		value_type l_value = id,
				   r_value = id;

		for(left += base_size(), right += base_size();
			left < right;
			left >>= 1, right >>= 1) {
			if(left & 1)  l_value = bi_func(l_value, data[left++]);
			if(right & 1) r_value = bi_func(data[--right], r_value); 
		}
		return bi_func(std::move(l_value), std::move(r_value));
	}
	void update(size_type index, const value_type& value) {
		index += base_size();
		data[index] = bi_func(data[index], value);

		while(index >>= 1) meld(index);
	}
	void change(size_type index, const value_type& value) {
		index += base_size();
		data[index] = value;

		while(index >>= 1) meld(index);
	}
	const size_type search(const size_type & left, const checker & check) {
		value_type val = id;
		size_t base_size_ = base_size();
		auto find = [&](auto&& find, size_type k, size_type l, size_type r) -> int {
			if(l + 1 == r) {
				val = bi_func(val, data[k]);
				
				return (check(val) ? k - base_size_ : -1);
			}

			const size_type mid = (l + r) >> 1;
			if(mid <= left) return find(find, k << 1 ^ 1, mid, r);
			if(left <= l and !check(bi_func(val, data[k]))) {
				val = bi_func(val, data[k]);
				return -1;
			}

			const int left_ret = find(find, k << 1 ^ 0, l, mid);
			if(left_ret == -1) return find(find, k << 1 ^ 1, mid, r);
			return left_ret;
		};

		return find(find, 1, 0, base_size_); 
	}
	
	value_type operator[](const size_type& index) const {
		return data[index + base_size()];
	}
	const size_type size() const {
		return size_;
	}
	const bool empty() const {
		return data.empty();
	}
};

struct node {
	// 区間の端
	i64 l, r;
	// 区間が sorted かどうか
	bool sorted;

	node(i64 v) : l(v), r(v), sorted(true) {}
	node(i64 l, i64 r, bool sorted) : l(l), r(r), sorted(sorted) {}
};

int main() {
	// 広義単調増加用
	auto f1 = [](node a, node b) {
		return node(a.l,
					b.r,
					(a.sorted and b.sorted and a.r <= b.l));
		// 両方の区間が sorted で,なおかつ接続点でも sorted ならマージしても sorted
	};

	// 広義単調減少用
	auto f2 = [](node a, node b) {
		return node(a.l,
					b.r,
					(a.sorted and b.sorted and a.r >= b.l));
	};

	int n; scanf("%d", &n);
	segment_tree<node> A(n, f1, node(1LL << 60, -(1LL << 60), true));
	segment_tree<node> B(n, f2, node(-(1LL << 60), 1LL << 60, true));
	for(int i = 0; i < n; i++) {
		int a; scanf("%d", &a);

		A.change(i, node(a));
		B.change(i, node(a));
	}

	int q; scanf("%d", &q);
	while(q--) {
		int l, r; scanf("%d%d", &l, &r);

		printf("%d %d\n", A.fold(l, r + 1).sorted, B.fold(l, r + 1).sorted);
	}
	return 0;
}
0