結果
| 問題 | No.3753 Certainly a Cretan |
| コンテスト | |
| ユーザー |
kwm_t
|
| 提出日時 | 2026-10-03 02:22:31 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 442 ms / 2,500 ms |
| + 738µs | |
| コード長 | 10,610 bytes |
| 記録 | |
| コンパイル時間 | 4,039 ms |
| コンパイル使用メモリ | 392,360 KB |
| 実行使用メモリ | 43,356 KB |
| 最終ジャッジ日時 | 2026-10-03 02:22:50 |
| 合計ジャッジ時間 | 10,402 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge4_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 46 |
ソースコード
#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_DATA_STRUCTURE_INTERVAL_SET_HPP
#define KWM_T_DATA_STRUCTURE_INTERVAL_SET_HPP
#include <set>
#include <functional>
#include <utility>
/**
* @brief 半開区間集合 [l, r) を管理するデータ構造
*
* 典型用途:
* - 区間の union 管理
* - mex 管理
* - 区間の被覆判定
*
* 計算量:
* - 各操作 O(log N)
*
* @tparam T
* - 順序比較可能
*
* 制約 / 注意:
* - 半開区間 [l, r)
* - 番兵として (-INF, -INF), (INF, INF) を内部に持つ
*
* verified:
* - https://atcoder.jp/contests/abc430/submissions/75172066
* - https://atcoder.jp/contests/awc0078/submissions/76170297
*/
namespace kwm_t::data_structure {
template<typename T>
class IntervalSet {
public:
explicit IntervalSet(T limit)
: TINF(limit)
{
ranges_.emplace(-TINF, -TINF);
ranges_.emplace(TINF, TINF);
}
// [l,r) がカバーされているか
bool isCovered(T l, T r) const noexcept {
if (l >= r) return true;
auto it = std::prev(ranges_.upper_bound({ l, TINF }));
return (it->first <= l && r <= it->second);
}
bool isCovered(T x) const noexcept { return isCovered(x, x + 1); }
// カバー区間取得
std::pair<T, T> getCoveringRange(T l, T r) const noexcept {
auto it = std::prev(ranges_.upper_bound({ l, TINF }));
if (it->first <= l && r <= it->second) return *it;
return { -TINF, -TINF };
}
std::pair<T, T> getCoveringRange(T x) const noexcept {
return getCoveringRange(x, x + 1);
}
std::pair<T, T> front() const noexcept {
auto it = std::next(ranges_.begin());
return *it;
}
std::pair<T, T> back() const noexcept {
auto it = std::prev(std::prev(ranges_.end()));
return *it;
}
// --- 挿入 ---
void insertRange(
T l, T r,
const std::function<void(T, T)>& onInsert = [](T, T) {},
const std::function<void(T, T)>& onErase = [](T, T) {}
) {
if (l >= r) return;
auto it = std::prev(ranges_.upper_bound({ l, TINF }));
if (isCovered(l, r)) return;
// 左マージ
if (it->first <= l && l <= it->second) {
l = it->first;
eraseRange(it, onErase);
}
else {
++it;
}
// 右方向マージ
while (it->first <= r) {
r = std::max(r, it->second);
eraseRange(it, onErase);
}
addRange(l, r, onInsert);
}
void insertPoint(
T x,
const std::function<void(T, T)>& onInsert = [](T, T) {},
const std::function<void(T, T)>& onErase = [](T, T) {}
) {
insertRange(x, x + 1, onInsert, onErase);
}
// --- 削除 ---
void eraseRange(
T l, T r,
const std::function<void(T, T)>& onInsert = [](T, T) {},
const std::function<void(T, T)>& onErase = [](T, T) {}
) {
if (l >= r) return;
auto it = std::prev(ranges_.upper_bound({ l, TINF }));
// 1区間内
if (it->first <= l && r <= it->second) {
auto [L, R] = *it;
eraseRange(it, onErase);
if (L < l) addRange(L, l, onInsert);
if (r < R) addRange(r, R, onInsert);
return;
}
// 左端
if (it->first <= l && l < it->second) {
auto [L, R] = *it;
eraseRange(it, onErase);
addRange(L, l, onInsert);
}
else {
++it;
}
// 中央
while (it->second <= r) {
eraseRange(it, onErase);
}
// 右端
if (it->first < r && r < it->second) {
auto [L, R] = *it;
eraseRange(it, onErase);
addRange(r, R, onInsert);
}
}
void erasePoint(
T x,
const std::function<void(T, T)>& onInsert = [](T, T) {},
const std::function<void(T, T)>& onErase = [](T, T) {}
) {
eraseRange(x, x + 1, onInsert, onErase);
}
int rangeCount() const noexcept {
return (int)ranges_.size() - 2;
}
// mex
T mex(T x = 0) const noexcept {
if (!isCovered(x)) return x;
auto it = std::prev(ranges_.upper_bound({ x, TINF }));
return it->second;
}
void debug() const noexcept {
for (auto [l, r] : ranges_) {
if (l != -TINF && l != TINF) {
std::cout << "[" << l << "," << r << ")\n";
}
}
}
private:
std::set<std::pair<T, T>> ranges_;
T TINF;
void eraseRange(typename std::set<std::pair<T, T>>::iterator& it,
const std::function<void(T, T)>& onErase) {
onErase(it->first, it->second);
it = ranges_.erase(it);
}
void addRange(T l, T r,
const std::function<void(T, T)>& onInsert) {
onInsert(l, r);
ranges_.emplace(l, r);
}
};
} // namespace kwm_t::data_structure
#endif // KWM_T_DATA_STRUCTURE_INTERVAL_SET_HPP
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::IntervalSet<int> stY(INF), stN(INF);
rep(i, n) {
if (s[i] == 'Y')stY.insertPoint(i);
else stN.insertPoint(i);
}
auto add = [&](int l, int r) -> 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) -> 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') {
stY.erasePoint(i, add, del);
stN.insertPoint(i, add, del);
s[i] = 'N';
}
else {
stN.erasePoint(i, add, del);
stY.insertPoint(i, add, del);
s[i] = 'Y';
}
}
else {
int k; cin >> k;
if (ngcnt) {
cout << 0 << endl;
continue;
}
if (s[n - 1] == 'Y') {
auto [l, r] = stY.getCoveringRange(n - 1);
k -= l / 2;
mint val = ans * rastY(r - l - k, k);
cout << val.val() << endl;
}
else {
auto [l, r] = stN.getCoveringRange(n - 1);
k -= l / 2;
mint val = ans * rastN(r - l - k, k);
cout << val.val() << endl;
}
}
}
return 0;
}
kwm_t