結果

問題 No.2020 Sum of Common Prefix Length
ユーザー KudeKude
提出日時 2022-07-22 23:33:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,249 bytes
コンパイル時間 3,465 ms
コンパイル使用メモリ 252,976 KB
実行使用メモリ 109,444 KB
最終ジャッジ日時 2023-09-17 12:44:51
合計ジャッジ時間 12,025 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 150 ms
61,416 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 92 ms
35,232 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 148 ms
61,476 KB
testcase_32 AC 149 ms
61,668 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> s(n);
  VI len(n);
  rep(i, n) cin >> s[i], len[i] = s[i].size();
  int q;
  cin >> q;
  vector<P> qs(q);
  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      char c;
      cin >> x >> c;
      x--;
      s[x] += c;
      qs[i] = {t, x};
    } else {
      int x;
      cin >> x;
      x--;
      qs[i] = {t, x};
    }
  }
  vector<int> s_tot;
  int sep = 26;
  VI spos(n);
  rep(i, n) {
    spos[i] = s_tot.size();
    for(char c: s[i]) s_tot.emplace_back(c - 'a');
    s_tot.emplace_back(sep++);
  }
  VI sa = suffix_array(s_tot);
  auto lcp = lcp_array(s_tot, sa);
  lcp.insert(lcp.begin(), 1, -1);
  lcp.emplace_back(-1);

  int sz = sa.size();
  VI sa_inv(sz);
  rep(i, sz) sa_inv[sa[i]] = i;
  vector<char> is_front(sz);
  for(int i: spos) is_front[sa_inv[i]] = true;
  VI occ_acc(sz + 1);
  rep(i, sz) occ_acc[i + 1] = occ_acc[i] + is_front[i];
  vector<vector<int>> to1(19, vector<int>(sz + 1)), to2 = to1;
  {
    VI st;
    st.emplace_back(0);
    rep(i, sz) {
      while (lcp[st.back()] > lcp[i + 1]) st.pop_back();
      to1[0][i + 1] = st.back();
      st.emplace_back(i + 1);
    }
    rep(k, 18) rep(i, sz + 1) to1[k+1][i] = to1[k][to1[k][i]];
  }
  {
    VI st;
    st.emplace_back(sz);
    to2[0][sz] = sz;
    rrep(i, sz) {
      while (lcp[st.back()] > lcp[i]) st.pop_back();
      to2[0][i] = st.back();
      st.emplace_back(i);
    }
    rep(k, 18) rep(i, sz + 1) to2[k+1][i] = to2[k][to2[k][i]];
  }
  VL acc_f(sz + 1), acc_b(sz + 1);
  for (int i = 1; i <= sz; i++) {
    int j = to1[0][i];
    acc_f[i] = acc_f[j] + (ll)(occ_acc[i] - occ_acc[j]) * lcp[i];
  }
  for (int i = sz - 1; i >= 0; i--) {
    int j = to2[0][i];
    acc_b[i] = acc_b[j] + (ll)(occ_acc[j] - occ_acc[i]) * lcp[i];
  }
  for(auto [t, x]: qs) {
    if (t == 1) {
      len[x]++;
    } else {
      int l = len[x];
      ll ans = l;
      int i = sa_inv[spos[x]];
      int j = i;
      if (lcp[j] > l) {
        rrep(k, 19) {
          int nj = to1[k][j];
          if (lcp[nj] > l) j = nj;
        }
        j = to1[0][j];
        ans += (ll)l * (occ_acc[i] - occ_acc[j]);
      }
      ans += acc_f[j];
      i++;
      j = i;
      if (lcp[j] > l) {
        rrep(k, 19) {
          int nj = to2[k][j];
          if (lcp[nj] >= l) j = nj;
        }
        j = to2[0][j];
        ans += (ll)l * (occ_acc[j] - occ_acc[i]);
      }
      ans += acc_b[j];
      cout << ans << '\n';
    }
  }
}
0