結果
問題 | No.2361 Many String Compare Queries |
ユーザー |
👑 |
提出日時 | 2023-06-23 22:21:00 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 164 ms / 2,500 ms |
コード長 | 9,210 bytes |
コンパイル時間 | 2,314 ms |
コンパイル使用メモリ | 142,032 KB |
実行使用メモリ | 35,252 KB |
最終ジャッジ日時 | 2024-07-01 02:04:23 |
合計ジャッジ時間 | 4,732 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 14 |
ソースコード
#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 <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; }////////////////////////////////////////////////////////////////////////////////// SA-IS// String: string, vector<int>, vector<long long>// if sigma <= n, O(n) time, O(n) space// if sigma > n, O(n log n) time, O(n) spacetemplate <class String> vector<int> suffixArrayRec(const String &as) {const int n = as.size();if (n == 0) return {};const auto minmaxA = minmax_element(as.begin(), as.end());const auto minA = *minmaxA.first, maxA = *minmaxA.second;if (static_cast<unsigned long long>(maxA) - minA >=static_cast<unsigned long long>(n)) {vector<int> us(n);for (int u = 0; u < n; ++u) us[u] = u;std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {return (as[u] < as[v]);});int b = 0;vector<int> bs(n, 0);for (int i = 1; i < n; ++i) {if (as[us[i - 1]] != as[us[i]]) ++b;bs[us[i]] = b;}return suffixArrayRec(bs);}const int sigma = maxA - minA + 1;vector<int> pt(sigma + 1, 0), poss(sigma);for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];// cmp[u] := (as[u, n) < as[u + 1, n))vector<bool> cmp(n);cmp[n - 1] = false;for (int u = n - 1; --u >= 0; ) {cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];}// ><, nn - id (0 <= id < n)int nn = 0;vector<int> ids(n, 0);int last = n;vector<int> nxt(n, 0);// put ><, from the tail of each bucketvector<int> us(n, 0);memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {ids[u] = ++nn;nxt[u] = last;last = u;us[--poss[as[u] - minA]] = u;}// follow > backwards, from the head of each bucketmemcpy(poss.data(), pt.data(), sigma * sizeof(int));us[poss[as[n - 1] - minA]++] = n - 1;for (int i = 0; i < n; ++i) {const int u = us[i];if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;}// follow < backwards, from the tail of each bucketmemcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));for (int i = n; --i >= 0; ) {const int u = us[i];if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;}if (nn) {int vsLen = 0;vector<int> vs(nn);for (const int u : us) if (ids[u]) vs[vsLen++] = u;int b = 0;vector<int> bs(nn, 0);for (int j = 1; j < nn; ++j) {// as[v1, w1] (< or =) as[v0, w0]int v1 = vs[j - 1], v0 = vs[j];const int w1 = nxt[v1], w0 = nxt[v0];if (w1 - v1 != w0 - v0) {++b;} else {for (; ; ++v1, ++v0) {if (v1 == n) { ++b; break; }if (as[v1] != as[v0]) { ++b; break; }if (v1 == w1) break;}}bs[nn - ids[vs[j]]] = b;}for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;const auto sub = suffixArrayRec(bs);// put ><, from the tail of each bucketmemset(us.data(), 0, n * sizeof(int));memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));for (int j = nn; --j >= 0; ) {const int u = vs[sub[j]];us[--poss[as[u] - minA]] = u;}// follow > backwards, from the head of each bucketmemcpy(poss.data(), pt.data(), sigma * sizeof(int));us[poss[as[n - 1] - minA]++] = n - 1;for (int i = 0; i < n; ++i) {const int u = us[i];if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;}// follow < backwards, from the tail of each bucketmemcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));for (int i = n; --i >= 0; ) {const int u = us[i];if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;}}return us;}// us[i]: i-th suffix// su[u]: index of as[u, n)// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)struct SuffixArray {int n;bool rmq;vector<int> us, su, hs, bsr;SuffixArray() : n(0), rmq(false) {}SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }template <class String> void build(const String &as) {n = as.size();us = suffixArrayRec(as);su.resize(n + 1);for (int i = 0; i < n; ++i) su[us[i]] = i;su[n] = -1;hs.assign(n, 0);for (int h = 0, u = 0; u < n; ++u) if (su[u]) {for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}hs[su[u]] = h;if (h) --h;}if (rmq) {const int logN = n ? (31 - __builtin_clz(n)) : 0;hs.resize((logN + 1) * n - (1 << logN) + 1);for (int e = 0; e < logN; ++e) {int *hes = hs.data() + e * n;for (int i = 0; i <= n - (1 << (e + 1)); ++i) {hes[n + i] = min(hes[i], hes[i + (1 << e)]);}}bsr.resize(n + 1);bsr[0] = -1;for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1;}}// Returns longest common prefix of as[u, n) and as[v, n).// 0 <= u, v <= n// Assumes rmq.inline int lcp(int u, int v) const {if (u == v) return n - u;int i = su[u], j = su[v];if (i > j) swap(i, j);const int e = bsr[j - i];return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);}};////////////////////////////////////////////////////////////////////////////////// root: min (tie-break: left)struct MinCartesianTree {int n, rt;vector<int> par, lef, rig;template <class T> void build(int n_, T *as) {assert(n_ >= 1);n = n_;rt = 0;par.assign(n, -1);lef.assign(n, -1);rig.assign(n, -1);int top = 0;vector<int> stack(n, 0);for (int u = 1; u < n; ++u) {if (as[stack[top]] > as[u]) { // >for (; top >= 1 && as[stack[top - 1]] > as[u]; --top) {} // >if (top == 0) {rt = par[lef[u] = stack[top]] = u;} else {par[lef[u] = stack[top]] = u;rig[par[u] = stack[top - 1]] = u;}stack[top] = u;} else {rig[par[u] = stack[top]] = u;stack[++top] = u;}}}template <class T> void build(const T &as) {build(as.size(), as.data());}};int N, Q;char S[200'010];vector<int> L, R;vector<Int> ans;SuffixArray sa;MinCartesianTree ct;vector<vector<pair<int, int>>> qss(N);Int now;void dfs(int u, int l, int r, int d) {const int dd = (~u) ? sa.hs[u] : (N - sa.us[l]);// cerr<<"dfs "<<u<<" ["<<l<<", "<<r<<") "<<d<<" -> "<<dd<<endl;for (; !qss[l].empty() && qss[l].back().first <= dd; ) {const int len = qss[l].back().first;const int q = qss[l].back().second;qss[l].pop_back();// cerr<<" len="<<len<<", q="<<q<<endl;assert(d < len); assert(len <= dd);ans[q] = 0;ans[q] += now;ans[q] += (r - l) * (len - d - 1);}now += (r - l) * (dd - d);if (~u) {dfs(ct.lef[u], l, u, dd);dfs(ct.rig[u], u, r, dd);}}int main() {for (; ~scanf("%d%d", &N, &Q); ) {scanf("%s", S);L.resize(Q);R.resize(Q);for (int q = 0; q < Q; ++q) {scanf("%d%d", &L[q], &R[q]);--L[q];}ans.assign(Q, -1);sa.rmq = true;sa.build(string(S));ct.build(vector<int>(sa.hs.begin(), sa.hs.begin() + N));// cerr<<"sa.hs = "<<sa.hs<<endl;// for(int i=0;i<N;++i)cerr<<S+sa.us[i]<<endl;qss.assign(N, {});for (int q = 0; q < Q; ++q) {const int len = R[q] - L[q];int lo = -1, hi = sa.su[L[q]];for (; lo + 1 < hi; ) {const int mid = (lo + hi) / 2;((sa.lcp(sa.us[mid], L[q]) >= len) ? hi : lo) = mid;}qss[hi].emplace_back(len, q);}for (int i = 0; i < N; ++i) {sort(qss[i].begin(), qss[i].end(), greater<pair<int, int>>{});}// cerr<<"qss = "<<qss<<endl;now = 0;dfs(ct.rig[ct.rt], 0, N, 0);// cerr<<"now = "<<now<<endl;for (int q = 0; q < Q; ++q) {printf("%lld\n", ans[q]);}}return 0;}