#6031. 「雅礼集训 2017 Day1」字符串 [SAM,根号分治]

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>

#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)

using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;

using pii = pair<int, int>;

#define fir first
#define sec second

template <class T>

void cmax(T& x, const T& y) {
  if (x < y) x = y;
}

template <class T>

void cmin(T& x, const T& y) {
  if (x > y) x = y;
}

#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back

template <class T>

void sort(vector<T>& v) {
  sort(all(v));
}

template <class T>

void reverse(vector<T>& v) {
  reverse(all(v));
}

template <class T>

void unique(vector<T>& v) {
  sort(all(v)), v.erase(unique(all(v)), v.end());
}

void reverse(string& s) { reverse(s.begin(), s.end()); }

const int io_size = 1 << 23 | 233;
const int io_limit = 1 << 22;
struct io_in {
  char ch;
#ifndef __WIN64
  char getchar() {
    static char buf[io_size], *p1 = buf, *p2 = buf;

    return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, io_size, stdin), p1 == p2) ? EOF : *p1++;
  }
#endif
  io_in& operator>>(char& c) {
    for (c = getchar(); isspace(c); c = getchar())
      ;

    return *this;
  }
  io_in& operator>>(string& s) {
    for (s.clear(); isspace(ch = getchar());)
      ;

    if (!~ch) return *this;

    for (s = ch; !isspace(ch = getchar()) && ~ch; s += ch)
      ;

    return *this;
  }

  io_in& operator>>(char* str) {
    char* cur = str;
    while (*cur) *cur++ = 0;

    for (cur = str; isspace(ch = getchar());)
      ;
    if (!~ch) return *this;

    for (*cur = ch; !isspace(ch = getchar()) && ~ch; *++cur = ch)
      ;

    return *++cur = 0, *this;
  }

  template <class T>

  void read(T& x) {
    bool f = 0;
    while ((ch = getchar()) < 48 && ~ch) f ^= (ch == 45);

    x = ~ch ? (ch ^ 48) : 0;
    while ((ch = getchar()) > 47) x = x * 10 + (ch ^ 48);
    x = f ? -x : x;
  }

  io_in& operator>>(int& x) { return read(x), *this; }

  io_in& operator>>(ll& x) { return read(x), *this; }

  io_in& operator>>(uint& x) { return read(x), *this; }

  io_in& operator>>(ull& x) { return read(x), *this; }

  io_in& operator>>(db& x) {
    read(x);
    bool f = x < 0;
    x = f ? -x : x;
    if (ch ^ '.') return *this;

    double d = 0.1;
    while ((ch = getchar()) > 47) x += d * (ch ^ 48), d *= .1;
    return x = f ? -x : x, *this;
  }
} in;

struct io_out {
  char buf[io_size], *s = buf;
  int pw[233], st[233];

  io_out() {
    set(7);
    rep(i, pw[0] = 1, 9) pw[i] = pw[i - 1] * 10;
  }

  ~io_out() { flush(); }

  void io_chk() {
    if (s - buf > io_limit) flush();
  }

  void flush() { fwrite(buf, 1, s - buf, stdout), fflush(stdout), s = buf; }

  io_out& operator<<(char c) { return *s++ = c, *this; }

  io_out& operator<<(string str) {
    for (char c : str) *s++ = c;
    return io_chk(), *this;
  }

  io_out& operator<<(char* str) {
    char* cur = str;
    while (*cur) *s++ = *cur++;
    return io_chk(), *this;
  }

  template <class T>

  void write(T x) {
    if (x < 0) *s++ = '-', x = -x;

    do {
      st[++st[0]] = x % 10, x /= 10;
    } while (x);

    while (st[0]) *s++ = st[st[0]--] ^ 48;
  }

  io_out& operator<<(int x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ll x) { return write(x), io_chk(), *this; }

  io_out& operator<<(uint x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ull x) { return write(x), io_chk(), *this; }

  int len, lft, rig;

  void set(int _length) { len = _length; }

  io_out& operator<<(db x) {
    bool f = x < 0;
    x = f ? -x : x, lft = x, rig = 1. * (x - lft) * pw[len];
    return write(f ? -lft : lft), *s++ = '.', write(rig), io_chk(), *this;
  }
} out;

template <int sz, int mod>

struct math_t {
  math_t() {
    fac.resize(sz + 1), ifac.resize(sz + 1);
    rep(i, fac[0] = 1, sz) fac[i] = fac[i - 1] * i % mod;

    ifac[sz] = inv(fac[sz]);
    Rep(i, sz - 1, 0) ifac[i] = ifac[i + 1] * (i + 1) % mod;
  }

  vector<int> fac, ifac;

  int qpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = x * x % mod)
      if (y & 1) ans = ans * x % mod;
    return ans;
  }

  int inv(int x) { return qpow(x, mod - 2); }

  int C(int n, int m) {
    if (n < 0 || m < 0 || n < m) return 0;
    return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
  }
};

int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int n, m, q, k;
const int maxn = 2e5 + 52;
vector<int> g[maxn];
int sz[maxn], len[maxn];

struct suffix_auto_maton {
  int ch[maxn][26], fa[maxn];
  int las, cnt;

  suffix_auto_maton() { las = cnt = 1; }

  void ins(int c) {
    int p = las, np = las = ++cnt;
    len[np] = len[p] + 1, sz[np] = 1;
    for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!p) {
      fa[np] = 1;
    } else {
      int q = ch[p][c];
      if (len[q] == len[p] + 1) {
        fa[np] = q;
      } else {
        int nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;
        for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
  }

  void ins(char* s) {
    char* cur = s;
    while (*cur) {
      ins((*cur++) - 'a');
    }
  }

  void build() { rep(i, 2, cnt) g[fa[i]].pb(i); }
} sam;

struct Mt {
  int f[maxn][20];
  int dep[maxn];

  void dfs(int u) {
    for (int v : g[u]) {
      dep[v] = dep[u] + 1;
      dfs(v);
      sz[u] += sz[v];
    }
  }

  void solve() {
    dfs(1);
    rep(i, 1, sam.cnt) f[i][0] = sam.fa[i];
    rep(j, 1, 18) rep(i, 1, sam.cnt) f[i][j] = f[f[i][j - 1]][j - 1];
  }

  int calc(int x, int k) {
    for (int i = 18; ~i; i--)
      if (len[f[x][i]] >= k) x = f[x][i];
    return sz[x];
  }
};

Mt mt;
int S, bl[maxn];
char s[maxn];
struct Que {
  int l, r, id;
} que[maxn];

struct node {
  string s;
  int l, r, id;
} d[maxn];

int num[455][455];
int rt[maxn], Len[maxn];
ll ans[maxn];

signed main() {
  // code begin.
  in >> n >> m >> q >> k;
  S = sqrt(m);
  rep(i, 1, m) { bl[i] = (i - 1) / S + 1; }
  int bas = sqrt(q * k);
  in >> s;
  sam.ins(s);
  sam.build();
  mt.solve();
  rep(i, 1, m) {
    in >> que[i].l >> que[i].r;
    que[i].l++;
    que[i].r++;
    que[i].id = i;
  }
  rep(i, 1, q) {
    in >> d[i].s >> d[i].l >> d[i].r;
    d[i].l++;
    d[i].r++;
    d[i].id = i;
  }
  if (k <= bas) {
    sort(d + 1, d + q + 1, [](node x, node y) {
      if (bl[x.l] == bl[y.l]) return x.r < y.r;
      return bl[x.l] < bl[y.l];
    });
    int l = 1, r = 0;
    rep(i, 1, q) {
      while (r < d[i].r) {
        r++;
        num[que[r].l][que[r].r]++;
      }
      while (r > d[i].r) {
        num[que[r].l][que[r].r]--;
        r--;
      }
      while (l > d[i].l) {
        l--;
        num[que[l].l][que[l].r]++;
      }
      while (l < d[i].l) {
        num[que[l].l][que[l].r]--;
        l++;
      }
      int p = 1, l = 0;
      for (int j = 0; j < k; j++) {
        int c = d[i].s[j] - 'a';
        if (sam.ch[p][c]) {
          p = sam.ch[p][c], ++l;
        } else {
          while (p && !sam.ch[p][c]) p = sam.fa[p], l = len[p];
          if (!p) {
            p = 1, l = 0;
          } else {
            p = sam.ch[p][c], l++;
          }
        }
        int pp = p, ll = l;
        while (pp ^ 1) {
          for (int w = j - len[sam.fa[pp]]; w >= j - ll + 1; w--) {
            ans[d[i].id] += 1ll * num[w + 1][j + 1] * sz[pp];
          }
          pp = sam.fa[pp];
          ll = len[pp];
        }
      }
    }
    rep(i, 1, q) out << ans[i] << '\n';
  } else {
    sort(que + 1, que + m + 1, [](Que x, Que y) { return x.r < y.r; });
    int tot = 0;
    rep(i, 1, q) rt[i] = 1, Len[i] = 0;
    rep(i, 1, m) {
      while (tot < k && tot < que[i].r) {
        rep(j, 1, q) {
          int c = d[j].s[tot] - 'a', p = rt[j], l = Len[j];
          if (sam.ch[p][c]) {
            p = sam.ch[p][c], ++l;
          } else {
            while (p && !sam.ch[p][c]) p = sam.fa[p], l = len[p];
            if (!p) {
              p = 1, l = 0;
            } else {
              p = sam.ch[p][c], l++;
            }
          }
          rt[j] = p, Len[j] = l;
        }
        tot++;
      }
      rep(j, 1, q) {
        if (d[j].l <= que[i].id && que[i].id <= d[j].r) {
          if (Len[j] < que[i].r - que[i].l + 1) continue;
          ans[j] += mt.calc(rt[j], que[i].r - que[i].l + 1);
        }
      }
    }
    rep(i, 1, q) out << ans[i] << '\n';
  }
  return 0;
  // code end.
}
上一篇:Windows账号克隆


下一篇:JavaScript-模拟弹框