結果

問題 No.2020 Sum of Common Prefix Length
ユーザー tnakao0123tnakao0123
提出日時 2022-07-24 02:09:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 3,335 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 65,440 KB
実行使用メモリ 61,188 KB
最終ジャッジ日時 2023-09-19 05:25:05
合計ジャッジ時間 11,582 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
18,664 KB
testcase_01 AC 7 ms
18,584 KB
testcase_02 AC 6 ms
18,576 KB
testcase_03 AC 7 ms
20,744 KB
testcase_04 AC 7 ms
20,756 KB
testcase_05 AC 7 ms
20,804 KB
testcase_06 AC 7 ms
20,796 KB
testcase_07 AC 65 ms
20,672 KB
testcase_08 AC 66 ms
20,620 KB
testcase_09 AC 65 ms
20,684 KB
testcase_10 AC 66 ms
20,676 KB
testcase_11 AC 66 ms
20,588 KB
testcase_12 AC 66 ms
20,624 KB
testcase_13 AC 66 ms
20,676 KB
testcase_14 AC 68 ms
20,656 KB
testcase_15 AC 77 ms
20,660 KB
testcase_16 AC 78 ms
20,588 KB
testcase_17 AC 115 ms
37,660 KB
testcase_18 AC 106 ms
33,288 KB
testcase_19 AC 105 ms
33,204 KB
testcase_20 AC 126 ms
60,952 KB
testcase_21 AC 152 ms
56,776 KB
testcase_22 AC 149 ms
52,716 KB
testcase_23 AC 145 ms
52,764 KB
testcase_24 AC 154 ms
60,988 KB
testcase_25 AC 155 ms
60,880 KB
testcase_26 AC 87 ms
41,984 KB
testcase_27 AC 6 ms
18,620 KB
testcase_28 AC 69 ms
20,596 KB
testcase_29 AC 69 ms
20,660 KB
testcase_30 AC 69 ms
20,604 KB
testcase_31 AC 110 ms
58,872 KB
testcase_32 AC 107 ms
61,188 KB
testcase_33 AC 91 ms
42,240 KB
testcase_34 AC 85 ms
27,892 KB
testcase_35 AC 82 ms
27,888 KB
testcase_36 AC 98 ms
41,952 KB
testcase_37 AC 88 ms
33,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2020.cc:  No.2020 Sum of Common Prefix Length - yukicoder
 */

#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_QN = 200000;
const int MAX_L = 200000;
const int MAX_M = MAX_L + MAX_QN;

/* typedef */

struct Query {
  int t, x, c;
  Query() {}

  void read() {
    scanf("%d%d", &t, &x), x--;
    if (t == 1) {
      char w[4];
      scanf("%s", w);
      c = w[0];
    }
    else
      c = -1;
  }
};

template <typename T>
struct SegTreeSum {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeSum() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};


/* global variables */

char s[MAX_L + 4];
string ss[MAX_N];
int sps[MAX_N], ses[MAX_N];
int cs[MAX_M][26], ps[MAX_M], vcs[MAX_M];
int cis[MAX_M], ls[MAX_M], rs[MAX_M];
Query qs[MAX_QN];
SegTreeSum<int> st;

/* subroutines */

void trie_init(int i, int pi = -1) {
  fill(cs[i], cs[i] + 26, -1);
  ps[i] = pi, vcs[i] = 0;
}

int trie_append(int u, int c, int &m) {
  int id = c - 'a';
  if (cs[u][id] < 0) {
    trie_init(m, u);
    cs[u][id] = m++;
  }
  return cs[u][id];
}

int trie_add(string &si, int &m) {
  int u = 0;
  for (auto ci: si) {
    int v = trie_append(u, ci, m);
    vcs[v]++;
    u = v;
  }
  return u;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  trie_init(0);
  int m = 1;

  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    ss[i] = string(s);
    sps[i] = ses[i] = trie_add(ss[i], m);
  }
  //printf("m=%d\n", m);

  int qn;
  scanf("%d", &qn);

  for (int i = 0; i < qn; i++) {
    qs[i].read();
    if (qs[i].t == 1) {
      int xi = qs[i].x;
      ses[xi] = trie_append(ses[xi], qs[i].c, m);
    }
  }
  //printf("m=%d\n", m);

  for (int u = 0, k = 0; u >= 0;) {
    int *cu = cs[u], up = ps[u];

    if (cis[u] == 0) ls[u] = k++;
    while (cis[u] < 26 && cu[cis[u]] < 0) cis[u]++;
    if (cis[u] < 26) {
      int v = cu[cis[u]++];
      u = v;
    }
    else {
      rs[u] = k++;
      u = up;
    }
  }

  st.init(m * 2, 0);
  for (int u = 0; u < m; u++) {
    st.seti(ls[u], vcs[u]);
    st.seti(rs[u], -vcs[u]);
  }
  st.setall();

  for (int i = 0; i < qn; i++) {
    Query &qi = qs[i];

    if (qi.t == 1) {
      int u = sps[qi.x] = cs[sps[qi.x]][qi.c - 'a'];
      vcs[u]++;
      st.set(ls[u], st.geti(ls[u]) + 1);
      st.set(rs[u], st.geti(rs[u]) - 1);
    }
    else {
      int e = st.sum_range(0, ls[sps[qi.x]] + 1);
      printf("%d\n", e);
    }
  }
  return 0;
}
0