結果

問題 No.3753 Certainly a Cretan
コンテスト
ユーザー kwm_t
提出日時 2026-10-03 03:06:27
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 897 ms / 2,500 ms
+ 321µs
コード長 12,184 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,975 ms
コンパイル使用メモリ 392,440 KB
実行使用メモリ 51,160 KB
最終ジャッジ日時 2026-10-03 03:06:49
合計ジャッジ時間 10,559 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
using mint = modint998244353;
const int mod = 998244353;
const int INF = 1e9;
// const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; }
#ifndef KWM_T_MATH_MODINT_BINOM_HPP
#define KWM_T_MATH_MODINT_BINOM_HPP

#include <vector>
#include <cassert>

namespace kwm_t::math::modint {
/**
 * @brief 二項係数・順列・多項係数(modint用)
 *
 * 典型用途:
 *   nCk, nPk, 多項係数
 *
 * 計算量:
 *   前計算 O(N)
 *   クエリ O(1)
 *
 * @tparam Mint modint型 (例: atcoder::static_modint<MOD>)
 *
 * 制約 / 注意:
 *   - mod は素数
 *   - n < mod(階乗が0にならない範囲)
 *
 * 使用例:
 *   Binom<mint> C(2000000);
 *   C.com(n, k);
 *
 * verified:
 */
template <class Mint>
struct Binom {
	std::vector<Mint> fact, ifact;

	Binom(int n = 2000006) {
		init(n);
	}

	void init(int n) {
		fact.resize(n + 1);
		ifact.resize(n + 1);

		fact[0] = 1;
		for (int i = 1; i <= n; ++i) {
			fact[i] = fact[i - 1] * i;
		}

		ifact[n] = fact[n].inv();
		for (int i = n; i >= 1; --i) {
			ifact[i - 1] = ifact[i] * i;
		}
	}

	// nCk
	Mint com(int n, int k) const {
		if (k < 0 || k > n) return 0;
		return fact[n] * ifact[k] * ifact[n - k];
	}

	Mint operator()(int n, int k) const {
		return com(n, k);
	}

	// nPk
	Mint perm(int n, int k) const {
		if (k < 0 || k > n) return 0;
		return fact[n] * ifact[n - k];
	}

	// nCk (nが大きくkが小さい場合)
	Mint com_sub(long long n, long long k) const {
		if (k < 0 || k > n) return 0;
		if (n - k < k) k = n - k;
		assert(k < (int)fact.size());

		Mint res = ifact[k];
		for (long long i = 0; i < k; ++i) {
			res *= (n - i);
		}
		return res;
	}

	// 多項係数
	template <class... Ints>
	Mint multinomial(int n, const Ints&... ms) const {
		Mint res = fact[n];
		int sum = 0;

		for (int m : {ms...}) {
			if (m < 0 || m > n) return 0;
			res *= ifact[m];
			sum += m;
		}

		if (sum > n) return 0;
		res *= ifact[n - sum];
		return res;
	}

	// 1/x
	Mint inv(int x) const {
		if (x < (int)fact.size()) {
			return fact[x - 1] * ifact[x];
		}
		return Mint(x).inv();
	}

	// nCk の逆数
	Mint inv_com(int n, int k) const {
		if (k < 0 || k > n) return 0;
		return ifact[n] * fact[k] * fact[n - k];
	}
};

} // namespace kwm_t::math::modint

#endif // KWM_T_MATH_MODINT_BINOM_HPP

#ifndef KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP
#define KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP

#include <vector>
#include <utility>
#include <iterator>

namespace kwm_t::algorithm {
/**
 * @brief Run Length Encoding(連長圧縮)
 *
 * 任意の forward iterator に対して適用可能
 *
 * 計算量:
 *   O(N)
 *
 * 制約:
 *   - 要素型に == が定義されていること
 * verified
 *  https://atcoder.jp/contests/abc452/submissions/74695137
 *  https://atcoder.jp/contests/awc0051/submissions/75170743
 */
template<class It>
std::vector<std::pair<typename std::iterator_traits<It>::value_type, int>>
run_length_encoding(It first, It last) {
	using T = typename std::iterator_traits<It>::value_type;
	std::vector<std::pair<T, int>> ret;

	if (first == last) return ret;

	It it = first;
	while (it != last) {
		It jt = it;
		int cnt = 0;
		while (jt != last && *jt == *it) {
			++jt;
			++cnt;
		}
		ret.emplace_back(*it, cnt);
		it = jt;
	}
	return ret;
}

// コンテナ版
template<class Container>
std::vector<std::pair<typename Container::value_type, int>>
run_length_encoding(const Container& c) {
	return run_length_encoding(c.begin(), c.end());
}

} // namespace kwm_t::algorithm

#endif // KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP


#ifndef KWM_T_DATASTRUCTURE_INTERVAL_MAP_HPP
#define KWM_T_DATASTRUCTURE_INTERVAL_MAP_HPP

#include <set>
#include <vector>
#include <optional>
#include <functional>
#include <cassert>
#include <tuple>

/**
 * @brief 半開区間 [l, r) の集合を管理するデータ構造(Interval Map)
 *
 * 典型用途:
 *   - 区間の追加・削除・値の更新
 *   - 区間のマージ管理(重なり・隣接の統合)
 *   - 区間被覆判定
 *   - 区間被覆を利用した貪欲・シミュレーション問題
 *
 * 計算量:
 *   - 各操作: O(k log N)
 *     (k は分割・削除・マージされる区間数)
 *
 * @tparam T
 *   - 区間に対応する値の型
 *
 * @param limit
 *   - 管理する座標の上限(番兵として使用)
 *
 * 制約 / 注意:
 *   - 半開区間 [l, r) を扱う
 *   - 内部で番兵(-INF, INF)を保持
 *   - 区間は常に「互いに素かつ同じ値の隣接区間がマージされた状態」を保つ
 *
 * 使用例:
 *   IntervalMap<int> st(1e9);
 *   st.assign(2, 5, 1);
 *   st.assign(3, 4, 2);
 *   bool ok = st.isCovered(2, 3);
 *
 * verified:
 *   https://atcoder.jp/contests/awc0053/submissions/75170416
 */
namespace kwm_t::data_structure {

template <typename T>
class IntervalMap {
private:
	struct Range {
		int l, r;
		T val;

		bool operator<(const Range& o) const {
			return l < o.l;
		}
	};

	std::set<Range> st;
	int INF;

	using iterator = typename std::set<Range>::iterator;

	bool isSentinel(const Range& x) const {
		return x.l == -INF || x.l == INF;
	}

	// --- split ---
	iterator split(
		int pos,
		const std::function<void(int, int, const T&)>& onInsert,
		const std::function<void(int, int, const T&)>& onErase
	) {
		auto it = std::prev(st.lower_bound({ pos + 1, 0, T() }));

		if (isSentinel(*it)) return std::next(it);
		if (it->l == pos) return it;
		if (it->r <= pos) return std::next(it);

		Range cur = *it;
		st.erase(it);

		onErase(cur.l, cur.r, cur.val);

		st.insert({ cur.l, pos, cur.val });
		onInsert(cur.l, pos, cur.val);

		auto res = st.insert({ pos, cur.r, cur.val });
		onInsert(pos, cur.r, cur.val);

		return res.first;
	}

	void eraseRange(
		iterator it,
		const std::function<void(int, int, const T&)>& onErase
	) {
		onErase(it->l, it->r, it->val);
		st.erase(it);
	}

	void addRange(
		int l,
		int r,
		const T& val,
		const std::function<void(int, int, const T&)>& onInsert
	) {
		onInsert(l, r, val);
		st.insert({ l, r, val });
	}

public:
	// --- 空構造 ---
	explicit IntervalMap(int limit)
		: INF(limit + 1)
	{
		st.insert({ -INF, -INF, T() });
		st.insert({ INF, INF, T() });
	}

	// --- 配列初期化 ---
	explicit IntervalMap(const std::vector<T>& data)
		: INF((int)data.size() + 1)
	{
		st.insert({ -INF, -INF, T() });
		st.insert({ INF, INF, T() });

		int n = (int)data.size();
		for (int i = 0; i < n;) {
			int j = i + 1;
			while (j < n && data[i] == data[j]) ++j;
			st.insert({ i, j, data[i] });
			i = j;
		}
	}

	// --- 区間代入 ---
	void insert(
		int l,
		int r,
		const T& val,
		const std::function<void(int, int, const T&)>& onInsert =
		[](int, int, const T&) {},
		const std::function<void(int, int, const T&)>& onErase =
		[](int, int, const T&) {}
	) {
		if (l >= r) return;

		auto itr = split(r, onInsert, onErase);
		auto itl = split(l, onInsert, onErase);

		// [l, r) に含まれる既存区間を削除
		auto it = itl;
		while (it != itr) {
			auto jt = std::next(it);
			eraseRange(it, onErase);
			it = jt;
		}

		int nl = l;
		int nr = r;

		// 左マージ
		it = std::prev(st.lower_bound({ l + 1, 0, T() }));
		if (!isSentinel(*it) && it->r == l && it->val == val) {
			nl = it->l;
			eraseRange(it, onErase);
		}

		// 右マージ
		it = st.lower_bound({ r, 0, T() });
		if (it != st.end() && !isSentinel(*it) &&
			it->l == r && it->val == val) {
			nr = it->r;
			eraseRange(it, onErase);
		}

		addRange(nl, nr, val, onInsert);
	}

	void insertPoint(
		int x,
		const T& val,
		const std::function<void(int, int, const T&)>& onInsert =
		[](int, int, const T&) {},
		const std::function<void(int, int, const T&)>& onErase =
		[](int, int, const T&) {}
	) {
		insert(x, x + 1, val, onInsert, onErase);
	}

	// --- 区間削除 ---
	void eraseRange(
		int l,
		int r,
		const std::function<void(int, int, const T&)>& onInsert =
		[](int, int, const T&) {},
		const std::function<void(int, int, const T&)>& onErase =
		[](int, int, const T&) {}
	) {
		if (l >= r) return;

		auto itr = split(r, onInsert, onErase);
		auto itl = split(l, onInsert, onErase);

		auto it = itl;
		while (it != itr) {
			auto jt = std::next(it);
			eraseRange(it, onErase);
			it = jt;
		}
	}
	void erasePoint(
		int x,
		const std::function<void(int, int, const T&)>& onInsert =
		[](int, int, const T&) {},
		const std::function<void(int, int, const T&)>& onErase =
		[](int, int, const T&) {}
	) {
		eraseRange(x, x + 1, onInsert, onErase);
	}

	// --- 被覆判定 ---
	bool isCovered(int l, int r) const {
		if (l >= r) return true;

		auto it = std::prev(st.lower_bound({ l + 1, 0, T() }));
		if (isSentinel(*it)) return false;

		return it->l <= l && r <= it->r;
	}

	bool isCovered(int x) const {
		return isCovered(x, x + 1);
	}

	// --- 値取得 ---
	std::optional<T> getValue(int x) const {
		auto res = get(x);
		if (!res) return std::nullopt;
		return std::get<2>(*res);
	}

	// --- 区間・値取得 ---
	std::optional<Range> get(int x) const {
		auto it = std::prev(st.lower_bound({ x + 1, 0, T() }));

		if (isSentinel(*it)) return std::nullopt;
		if (!(it->l <= x && x < it->r)) return std::nullopt;

		return *it;
	}

	// --- 先頭 ---
	std::optional<Range> front() const {
		auto it = std::next(st.begin());

		if (isSentinel(*it)) return std::nullopt;

		return *it;
	}

	// --- 末尾 ---
	std::optional<Range> back() const {
		auto it = std::prev(st.end());
		it = std::prev(it);

		if (isSentinel(*it)) return std::nullopt;

		return *it;
	}

	// --- 区間数 ---
	int rangeCount() const {
		return (int)st.size() - 2;
	}
};

} // namespace kwm_t::data_structure

#endif

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	kwm_t::math::modint::Binom<mint> binom;
	int n, q; cin >> n >> q;
	string s; cin >> s;

	auto cat = [&](int n, bool inv = false) -> mint {
		if (inv) return  binom.ifact[2 * n] * binom.fact[n] * binom.fact[n + 1];
		return binom.fact[2 * n] * binom.ifact[n] * binom.ifact[n + 1];
	};
	// +1:p,-1;q
	auto rastY = [&](int p, int q)->mint {
		if (p < 0 || q < 0 || p < q)return 0;
		return binom(p + q, p) - binom(p + q, p + 1);
	};
	// +1:p,-1;q
	auto rastN = [&](int p, int q)->mint {
		if (p < 0 || q < 0 || p > q)return 0;
		return binom(p + q, q) - binom(p + q, q + 1);
	};

	mint ans = 1;// 最終ブロックは自前で計算しろ
	int ngcnt = 0;
	{
		// naive
		auto rle = kwm_t::algorithm::run_length_encoding(s);
		rep(i, rle.size()) {
			int sz = rle[i].second;
			if (i != rle.size() - 1) {
				if (sz % 2 == 1) ngcnt++;
				else ans *= cat(sz / 2);
			}
		}
	}


	kwm_t::data_structure::IntervalMap<char> mp(INF);
	rep(i, n) mp.insertPoint(i, s[i]);

	auto add = [&](int l, int r, char c) -> void {
		if (r == n)return;
		int sz = r - l;
		if (sz % 2 == 0) ans *= cat(sz / 2);
		else ngcnt++;
	};
	auto del = [&](int l, int r, char c) -> void {
		if (r == n)return;
		int sz = r - l;
		if (sz % 2 == 0) ans *= cat(sz / 2, true);
		else ngcnt--;
	};

	while (q--) {
		int t; cin >> t;
		if (t == 1) {
			int i; cin >> i;
			i--;
			if (s[i] == 'Y') s[i] = 'N';
			else s[i] = 'Y';
			mp.insertPoint(i, s[i], add, del);
		}
		else {
			int k; cin >> k;
			if (ngcnt) {
				cout << 0 << endl;
				continue;
			}
			auto range = mp.get(n - 1).value();
			k -= range.l / 2;
			if (range.val == 'Y') {
				mint val = ans * rastY(range.r - range.l - k, k);
				cout << val.val() << endl;
			}
			else {
				mint val = ans * rastN(range.r - range.l - k, k);
				cout << val.val() << endl;
			}
		}
	}
	return 0;
}
0