結果

問題 No.2761 Substitute and Search
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-05-17 21:45:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 243 ms / 4,000 ms
コード長 6,015 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 122,484 KB
実行使用メモリ 30,336 KB
最終ジャッジ日時 2024-05-17 21:45:32
合計ジャッジ時間 4,481 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 225 ms
29,952 KB
testcase_05 AC 243 ms
29,952 KB
testcase_06 AC 90 ms
29,952 KB
testcase_07 AC 223 ms
30,208 KB
testcase_08 AC 92 ms
29,952 KB
testcase_09 AC 212 ms
30,336 KB
testcase_10 AC 88 ms
29,952 KB
testcase_11 AC 219 ms
30,080 KB
testcase_12 AC 165 ms
30,080 KB
testcase_13 AC 190 ms
30,208 KB
testcase_14 AC 160 ms
30,336 KB
testcase_15 AC 158 ms
30,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}


////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
  static constexpr unsigned long long M = (1ULL << 61) - 1;
  unsigned long long x;
  constexpr ModLong61() : x(0ULL) {}
  constexpr ModLong61(unsigned x_) : x(x_) {}
  constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
  constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong61 &operator*=(const ModLong61 &a) {
    const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
    x = (y >> 61) + (y & M);
    x = (x >= M) ? (x - M) : x;
    return *this;
  }
  ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
  ModLong61 pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong61 inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong61(y);
  }
  ModLong61 operator+() const { return *this; }
  ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
  ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
  ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
  ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
  template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
  template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
  template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
  template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong61 &a) const { return (x == a.x); }
  bool operator!=(const ModLong61 &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

#include <chrono>
#ifdef LOCAL
mt19937_64 rng(58);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif

const ModLong61 BASE = static_cast<unsigned long long>(rng());

////////////////////////////////////////////////////////////////////////////////


char buf[10010];

int N, L, Q;
vector<string> S;

int main() {
  for (; ~scanf("%d%d%d", &N, &L, &Q); ) {
    S.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%s", buf);
      S[i] = buf;
    }
    
    vector<ModLong61> pw(L + 1);
    pw[0] = 1;
    for (int j = 1; j <= L; ++j) pw[j] = pw[j - 1] * BASE;
    
    vector<vector<ModLong61>> bits(N, vector<ModLong61>(L, 0));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < L; ++j) {
        bAdd(bits[i], j, pw[j] * S[i][j]);
      }
    }
    
    for (; Q--; ) {
      int O;
      scanf("%d", &O);
      if (O == 1) {
        int K;
        char C, D;
        scanf("%d %c %c", &K, &C, &D);
        --K;
        for (int i = 0; i < N; ++i) if (S[i][K] == C) {
          S[i][K] = D;
          bAdd(bits[i], K, pw[K] * (D - C));
        }
      } else if (O == 2) {
        scanf("%s", buf);
        const string T = buf;
        const int TLen = T.size();
        ModLong61 tar = 0;
        for (int j = 0; j < TLen; ++j) {
          tar += pw[j] * T[j];
        }
        int ans = 0;
        for (int i = 0; i < N; ++i) {
          if (bSum(bits[i], TLen) == tar) {
            ++ans;
          }
        }
        printf("%d\n", ans);
      } else {
        assert(false);
      }
    }
    
  }
  return 0;
}
0