This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://atcoder.jp/contests/abc331/tasks/abc331_f"
#include <iostream>
#include "../cpp/rolling-hash.hpp"
#include "../cpp/segtree.hpp"
RollingHash rh;
struct E {
RHString operator()() const { return RHString(rh); }
};
int main() {
int N, Q;
std::string S;
std::cin >> N >> Q >> S;
std::vector<RHString> init;
init.reserve(N);
for (char c : S) {
init.emplace_back(rh, c);
}
StaticSegTree<RHString, std::plus<RHString>, E> seg(init);
while (Q--) {
int q;
std::cin >> q;
if (q == 1) {
int x;
char c;
std::cin >> x >> c;
seg.set(x - 1, RHString(rh, c));
} else {
int L, R;
std::cin >> L >> R;
std::cout << (seg.prod(L - 1, R).is_palindrome() ? "Yes" : "No") << std::endl;
}
}
}
#line 1 "test/atcoder-abc331-f.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc331/tasks/abc331_f"
#include <iostream>
#line 2 "cpp/rolling-hash.hpp"
#include <string>
#include <vector>
#include <random>
#include <cassert>
#include <string_view>
#include <type_traits>
#line 2 "cpp/traits.hpp"
#include <iterator>
#line 5 "cpp/traits.hpp"
#include <utility>
namespace detail {
using std::begin, std::end;
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
} // namespace detail
template <class T>
struct is_range : detail::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#line 11 "cpp/rolling-hash.hpp"
struct RHString;
/**
* @brief ローリングハッシュ
*
* ビット演算に寄る高速化のためハッシュはMOD = 2^61-1で計算される
*
* 10^8個くらいなら99%の確率で衝突する組が存在しない
*/
class RollingHash {
constexpr static unsigned long long MASK30 = (1ULL << 30) - 1;
constexpr static unsigned long long MASK31 = (1ULL << 31) - 1;
constexpr static unsigned long long MOD = (1ULL << 61) - 1;
// a < MOD, b < MOD必須
constexpr static unsigned long long add(unsigned long long a, unsigned long long b) {
if((a += b) >= MOD) a -= MOD;
return a;
}
// a < MOD, b < MOD必須
constexpr static unsigned long long mul(unsigned long long a, unsigned long long b) {
__uint128_t c = static_cast<__uint128_t>(a) * b;
return add(static_cast<unsigned long long>(c >> 61), static_cast<unsigned long long>(c & MOD));
}
void expand(int n) {
while(static_cast<int>(power.size()) <= n) power.push_back(mul(power.back(), base));
}
public:
unsigned int base; //!< ハッシュの基数
std::vector<unsigned long long> power; //!< baseの累乗
/**
* @brief コンストラクタ
*
* @param base ハッシュの基数 省略するとランダム
*/
RollingHash(unsigned int base = 0) : base(base) {
if(base == 0) {
std::mt19937 mt(std::random_device{}());
this->base = std::uniform_int_distribution<unsigned int>(129, ~0U)(mt);
}
power = {1};
}
/**
* @brief 配列/文字列のイテレータ間のハッシュの計算(O(N))
*
* @tparam It イテレータ
* @param first 配列の開始イテレータ
* @param last 配列の終了イテレータ
* @return std::vector<unsigned long long> 先頭から各要素数分のハッシュ
*/
template <typename It>
std::vector<unsigned long long> build(It first, It last) {
std::vector<unsigned long long> res;
if constexpr (std::is_convertible_v<typename std::iterator_traits<It>::iterator_category, std::random_access_iterator_tag>) {
res.reserve(last - first + 1);
}
res.push_back(0);
for(; first != last; ++first) {
res.push_back(add(mul(res.back(), base), *first));
}
return res;
}
/**
* @brief 配列/文字列全体のハッシュの計算(O(N))
*
* @param s 配列/文字列
* @return std::vector<unsigned long long> 先頭から各文字数分のハッシュ
*/
template <typename R>
std::vector<unsigned long long> build(R&& s) {
using std::begin, std::end;
return build(begin(s), end(s));
}
/**
* @brief [l,r)のハッシュの計算(O(1))
*
* @param hash 先頭からのハッシュ
* @param l 区間の左端
* @param r 区間の右端
* @return unsigned long long [l,r)のハッシュ
*/
unsigned long long query(const std::vector<unsigned long long>& hash, int l, int r) {
expand(r - l);
return add(hash[r], MOD - mul(hash[l], power[r-l]));
}
friend RHString;
};
/**
* @brief ローリングハッシュによって管理される文字列型
*/
struct RHString {
RollingHash& rh;
size_t sz;
unsigned long long hash1; //!< 正順
unsigned long long hash2; //!< 逆順
/**
* @brief コンストラクタ
* 予めRollingHashをインスタンス化しておく必要がある
*/
RHString(RollingHash& rh) : rh(rh), sz(0), hash1(0), hash2(0) {}
RHString(RollingHash& rh, size_t sz, unsigned long long hash1, unsigned long long hash2) : rh(rh), sz(sz), hash1(hash1), hash2(hash2) {}
RHString(const RHString& o) : rh(o.rh), sz(o.sz), hash1(o.hash1), hash2(o.hash2) {}
/**
* @brief vectorなどで初期化する
*/
template <class R, std::enable_if_t<is_range_v<R> && !std::is_convertible_v<R, std::string_view>, std::nullptr_t> = nullptr>
RHString(RollingHash& rh, R&& v) : rh(rh) {
using std::begin, std::end, std::rbegin, std::rend;
sz = std::distance(begin(v), end(v));
hash1 = rh.build(begin(v), end(v)).back();
hash2 = rh.build(rbegin(v), rend(v)).back();
}
/**
* @brief charやunsigned long longなどで初期化する
*/
template <class T, std::enable_if_t<std::is_convertible_v<T, unsigned long long> && !std::is_convertible_v<T, std::string_view>, std::nullptr_t> = nullptr>
RHString(RollingHash& rh, T&& x) : rh(rh) {
sz = 1;
hash1 = x;
hash2 = x;
}
/**
* @brief 文字列(string, const char*, string_view)で初期化する
*/
RHString(RollingHash& rh, std::string_view s) : rh(rh) {
sz = std::distance(s.begin(), s.end());
hash1 = rh.build(s.begin(), s.end()).back();
hash2 = rh.build(s.rbegin(), s.rend()).back();
}
/**
* @brief 回文か否か
*/
bool is_palindrome() const {
return hash1 == hash2;
}
size_t size() const {
return sz;
}
void clear() {
sz = 0;
hash1 = 0;
hash2 = 0;
}
bool empty() const {
return sz == 0;
}
RHString& operator+=(const RHString& o) {
assert(&rh == &o.rh);
rh.expand(sz);
rh.expand(o.sz);
hash1 = rh.add(rh.mul(hash1, rh.power[o.sz]), o.hash1);
hash2 = rh.add(hash2, rh.mul(o.hash2, rh.power[sz]));
sz += o.sz;
return *this;
}
/**
* @brief 再代入する
* RollingHashは同じものである必要がある
*/
void assign(const RHString& o) {
assert(&rh == &o.rh);
sz = o.sz;
hash1 = o.hash1;
hash2 = o.hash2;
}
/**
* @brief vectorなどを再代入する
*/
template <class R, std::enable_if_t<is_range_v<R> && !std::is_convertible_v<R, std::string_view>, std::nullptr_t> = nullptr>
void assign(R&& v) {
using std::begin, std::end, std::rbegin, std::rend;
sz = std::distance(begin(v), end(v));
hash1 = rh.build(begin(v), end(v)).back();
hash2 = rh.build(rbegin(v), rend(v)).back();
}
/**
* @brief charやunsigned long longなどを再代入する
*/
template <class T, std::enable_if_t<std::is_convertible_v<T, unsigned long long> && !std::is_convertible_v<T, std::string_view>, std::nullptr_t> = nullptr>
void assign(T&& x) {
sz = 1;
hash1 = x;
hash2 = x;
}
/**
* @brief 文字列(string, const char*, string_view)を再代入する
*/
void assign(std::string_view s) {
sz = std::distance(s.begin(), s.end());
hash1 = rh.build(s.begin(), s.end()).back();
hash2 = rh.build(s.rbegin(), s.rend()).back();
}
/**
* @brief 再代入する
* RollingHashは同じものである必要がある
*/
RHString& operator=(const RHString& o) {
assign(o);
return *this;
}
/**
* @brief vectorなどを再代入する
*/
template <class R, std::enable_if_t<is_range_v<R> && !std::is_convertible_v<R, std::string_view>, std::nullptr_t> = nullptr>
RHString& operator=(R&& v) {
assign(v);
return *this;
}
/**
* @brief charやunsigned long longなどを再代入する
*/
template <class T, std::enable_if_t<std::is_convertible_v<T, unsigned long long> && !std::is_convertible_v<T, std::string_view>, std::nullptr_t> = nullptr>
RHString& operator=(T&& x) {
assign(x);
return *this;
}
/**
* @brief 文字列(string, const char*, string_view)を再代入する
*/
RHString& operator=(std::string_view s) {
assign(s);
return *this;
}
friend RHString operator+(const RHString& t1, const RHString& t2) {
RHString ret = t1;
ret += t2;
return ret;
}
friend bool operator==(const RHString& t1, const RHString& t2) {
assert(&t1.rh == &t2.rh);
return t1.sz == t2.sz && t1.hash1 == t2.hash1 && t1.hash2 == t2.hash2;
}
friend bool operator!=(const RHString& t1, const RHString& t2) { return !(t1 == t2); }
};
#line 2 "cpp/segtree.hpp"
/**
* @file segtree.hpp
* @brief セグメント木
*/
#line 9 "cpp/segtree.hpp"
#include <functional>
#include <ostream>
#line 2 "cpp/more_functional.hpp"
/**
* @file more_functional.hpp
* @brief 関数オブジェクトを定義する
*/
#include <limits>
#include <numeric>
#line 11 "cpp/more_functional.hpp"
namespace more_functional {
template <typename S>
struct Max {
const S operator()(const S& a, const S& b) const { return std::max(a, b); }
};
template <typename S>
struct Min {
const S operator()(const S& a, const S& b) const { return std::min(a, b); }
};
template <typename S, std::enable_if_t<std::is_integral_v<S>>* = nullptr>
struct Gcd {
constexpr S operator()(const S& a, const S& b) const { return std::gcd(a, b); }
};
template <typename S>
struct Zero {
S operator()() const { return S(0); }
};
template <typename S>
struct One {
S operator()() const { return S(1); }
};
template <typename S>
struct None {
S operator()() const { return S{}; }
};
template <typename S, std::enable_if_t<std::is_scalar_v<S>>* = nullptr>
struct MaxLimit {
constexpr S operator()() const { return std::numeric_limits<S>::max(); }
};
template <typename S, std::enable_if_t<std::is_scalar_v<S>>* = nullptr>
struct MinLimit {
constexpr S operator()() const { return std::numeric_limits<S>::lowest(); }
};
template <typename S>
struct Div {
S operator()(const S& a) const { return S(1) / a; }
};
} // namespace more_functional
#line 13 "cpp/segtree.hpp"
/**
* @brief セグメント木のCRTP基底クラス
*
* @tparam S モノイドの型
* @tparam ActualSegTree 派生クラス
*/
template <typename S, typename ActualSegTree>
class SegTreeBase {
S op(const S& a, const S& b) const { return static_cast<const ActualSegTree&>(*this).op(a, b); }
S e() const { return static_cast<const ActualSegTree&>(*this).e(); }
int n, sz, height;
std::vector<S> data;
void update(int k) { data[k] = op(data[2 * k], data[2 * k + 1]); }
class SegTreeReference {
SegTreeBase& segtree;
int k;
public:
SegTreeReference(SegTreeBase& segtree, int k) : segtree(segtree), k(k) {}
SegTreeReference& operator=(const S& x) {
segtree.set(k, x);
return *this;
}
operator S() const { return segtree.get(k); }
};
protected:
void construct_data() {
sz = 1;
height = 0;
while (sz < n) {
sz <<= 1;
height++;
}
data.assign(sz * 2, e());
}
void initialize(const std::vector<S>& v) {
for (int i = 0; i < n; i++) data[sz + i] = v[i];
for (int i = sz - 1; i > 0; i--) update(i);
}
public:
// Warning: 継承先のコンストラクタでconstruct_data()を必ず呼び出す!
SegTreeBase(int n = 0) : n(n) {}
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return S 値
*/
S get(int k) const {
assert(0 <= k && k < n);
return data[sz + k];
}
/**
* @brief 指定された要素の値を返す
*
* @param k インデックス
* @return S 値
*/
S operator[] (int k) const { return get(k); }
/**
* @brief 指定された要素への参照を返す
*
* @param k
* @return SegTreeReference 要素への参照 代入されるとset()が呼ばれる
*/
SegTreeReference operator[] (int k) { return SegTreeReference(*this, k); }
/**
* @brief 内容を出力する
*
* @tparam CharT 出力ストリームの文字型
* @tparam Traits 出力ストリームの文字型特性
* @param os 出力ストリーム
* @param rhs セグメント木
* @return std::basic_ostream<CharT, Traits>& 出力ストリーム
*/
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const SegTreeBase& rhs) {
for(int i = 0; i < rhs.n; i++) {
if(i != 0) os << CharT(' ');
os << rhs[i];
}
return os;
}
/**
* @brief 指定された要素の値をxに更新する
*
* @param k インデックス
* @param x 新しい値
*/
void set(int k, const S& x) {
assert(0 <= k && k < n);
k += sz;
data[k] = x;
for (int i = 1; i <= height; i++) update(k >> i);
}
/**
* @brief 指定された要素の値にxを作用させる
*
* @param k インデックス
* @param x 作用素
*/
void apply(int k, const S& x) { set(k, op(get(k), x)); }
/**
* @brief [l, r)の区間の総積を返す
*
* @param l 半開区間の開始
* @param r 半開区間の終端 0<=l<=r<=n
* @return S 総積
*/
S prod(int l, int r) const {
assert(0 <= l && l <= r && r <= n);
S left_prod = e(), right_prod = e();
l += sz;
r += sz;
while (l < r) {
if (l & 1) left_prod = op(left_prod, data[l++]);
if (r & 1) right_prod = op(data[--r], right_prod);
l >>= 1;
r >>= 1;
}
return op(left_prod, right_prod);
}
/**
* @brief すべての要素の総積を返す
*
* @return S 総積
*/
S all_prod() const { return data[1]; }
/**
* @brief (r = l or f(prod([l, r))) = true) and (r = n or f(prod([l, r+1))) = false)となるrを返す
* fが単調なら、f(prod([l, r))) = trueとなる最大のr
*
* @tparam F
* @param l 半開区間の開始 0<=l<=n
* @param f 判定関数 f(e) = true
* @return int
*/
template <typename F>
int max_right(int l, F f) const {
assert(f(e()));
assert(0 <= l && l <= n);
if (l == n) return n;
l += sz;
while (l % 2 == 0) l >>= 1;
S sum = e();
while(f(op(sum, data[l]))) {
sum = op(sum, data[l]);
l++;
while (l % 2 == 0) l >>= 1;
if (l == 1) return n;
}
while (l < sz) {
if (!f(op(sum, data[l * 2]))) l *= 2;
else {
sum = op(sum, data[l * 2]);
l = l * 2 + 1;
}
}
return l - sz;
}
/**
* @brief (l = 0 or f(prod([l, r))) = true) and (l = r or f(prod([l-1, r))) = false)となるlを返す
* fが単調なら、f(prod([l, r))) = trueとなる最小のl
*
* @tparam F
* @param r 半開区間の終端 0<=r<=n
* @param f 判定関数 f(e) = true
* @return int
*/
template <typename F>
int min_left(int r, F f) const {
assert(f(e()));
assert(0 <= r && r <= n);
if (r == 0) return 0;
r += sz;
while (r % 2 == 0) r >>= 1;
S sum = e();
while(f(op(data[r-1], sum))) {
sum = op(data[r-1], sum);
r--;
while (r % 2 == 0) r >>= 1;
if (r == 1) return 0;
}
while (r < sz) {
if (!f(op(data[r * 2 - 1], sum))) r *= 2;
else {
sum = op(data[r * 2 - 1], sum);
r = r * 2 - 1;
}
}
return r - sz;
}
};
/**
* @brief 積のファンクタが静的な場合のセグメント木の実装
*
* @tparam S モノイドの型
* @tparam Op 積のファンクタ
* @tparam E 積の単位元を返すファンクタ
*/
template <typename S, typename Op, typename E>
class StaticSegTree : public SegTreeBase<S, StaticSegTree<S, Op, E>> {
using BaseType = SegTreeBase<S, StaticSegTree<S, Op, E>>;
inline static Op operator_object;
inline static E identity_object;
public:
S op(const S& a, const S& b) const { return operator_object(a, b); }
S e() const { return identity_object(); }
/**
* @brief デフォルトコンストラクタ
*
*/
StaticSegTree() = default;
/**
* @brief コンストラクタ
*
* @param n 要素数
*/
explicit StaticSegTree(int n) : BaseType(n) {
this->construct_data();
}
/**
* @brief コンストラクタ
*
* @param v 初期の要素
*/
explicit StaticSegTree(const std::vector<S>& v) : StaticSegTree(v.size()) {
this->initialize(v);
}
};
/**
* @brief コンストラクタで関数オブジェクトを与えることで積を定義するセグメント木の実装
* テンプレート引数を省略することができ、ラムダ式が使える
*
* @tparam S モノイドの型
* @tparam Op 積の関数オブジェクトの型
*/
template <typename S, typename Op>
class SegTree : public SegTreeBase<S, SegTree<S, Op>> {
using BaseType = SegTreeBase<S, SegTree<S, Op>>;
Op operator_object;
S identity;
public:
S op(const S& a, const S& b) const { return operator_object(a, b); }
S e() const { return identity; }
/**
* @brief デフォルトコンストラクタ
*/
SegTree() = default;
/**
* @brief コンストラクタ
*
* @param n 要素数
* @param op 積の関数オブジェクト
* @param identity 単位元
*/
explicit SegTree(int n, Op op, const S& identity) : BaseType(n), operator_object(std::move(op)), identity(identity) {
this->construct_data();
}
/**
* @brief コンストラクタ
*
* @param v 初期の要素
* @param op 積の関数オブジェクト
* @param identity 単位元
*/
explicit SegTree(const std::vector<S>& v, Op op, const S& identity) : SegTree(v.size(), std::move(op), identity) {
this->initialize(v);
}
};
/**
* @brief RangeMaxQuery
*
* @tparam S 型
*/
template <typename S>
using RMaxQ = StaticSegTree<S, more_functional::Max<S>, more_functional::MinLimit<S>>;
/**
* @brief RangeMinQuery
*
* @tparam S 型
*/
template <typename S>
using RMinQ = StaticSegTree<S, more_functional::Min<S>, more_functional::MaxLimit<S>>;
/**
* @brief RangeSumQuery
*
* @tparam S 型
*/
template <typename S>
using RSumQ = StaticSegTree<S, std::plus<S>, more_functional::None<S>>;
/**
* @brief RangeProdQuery
*
* @tparam S 型
*/
template <typename S>
using RProdQ = StaticSegTree<S, std::multiplies<S>, more_functional::One<S>>;
/**
* @brief RangeXorQuery
*
* @tparam S 型
*/
template <typename S>
using RXorQ = StaticSegTree<S, std::bit_xor<S>, more_functional::Zero<S>>;
/**
* @brief RangeGcdQuery
*
* @tparam S 型
*/
template <typename S>
using RGcdQ = StaticSegTree<S, more_functional::Gcd<S>, more_functional::Zero<S>>;
namespace segtree {
template <typename T>
using TemplateS = typename T::S;
template <typename T>
struct TemplateOp {
TemplateS<T> operator()(const TemplateS<T>& a, const TemplateS<T>& b) const {
return T().op(a, b);
}
};
template <typename T>
struct TemplateE {
TemplateS<T> operator()() const {
return T().e();
}
};
}
template <typename T>
using TemplateSegTree = StaticSegTree<
segtree::TemplateS<T>,
segtree::TemplateOp<T>,
segtree::TemplateE<T>
>;
#line 7 "test/atcoder-abc331-f.test.cpp"
RollingHash rh;
struct E {
RHString operator()() const { return RHString(rh); }
};
int main() {
int N, Q;
std::string S;
std::cin >> N >> Q >> S;
std::vector<RHString> init;
init.reserve(N);
for (char c : S) {
init.emplace_back(rh, c);
}
StaticSegTree<RHString, std::plus<RHString>, E> seg(init);
while (Q--) {
int q;
std::cin >> q;
if (q == 1) {
int x;
char c;
std::cin >> x >> c;
seg.set(x - 1, RHString(rh, c));
} else {
int L, R;
std::cin >> L >> R;
std::cout << (seg.prod(L - 1, R).is_palindrome() ? "Yes" : "No") << std::endl;
}
}
}