Advertisement
999ms

Untitled

Feb 20th, 2020
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) begin(x),end(x)
  3. #define make_unique(x) if (!is_sorted(all(x))) sort(all(x)); x.erase(unique(all(x)), x.end());
  4. #define remax(x,y) (x > y ? x = y, true : false)
  5. #define remin(x,y) (x < y ? x = y, true : false)
  6. #define size(x) int(x.size())
  7.  
  8.  
  9. using namespace std;
  10. using ll = long long;
  11.  
  12. struct SuffixArray {
  13.   const string s;
  14.   const int n;
  15.   vector<int> arr;
  16.   vector<int> lcp;
  17.   #define modulo(x) (x >= n ? x - n : x)
  18.   SuffixArray(const string& str)
  19.     : s(str + char(0))
  20.     , n(size(s))
  21.     , arr(n)
  22.     , lcp(n - 1)
  23.   {
  24.     vector<int> c(n), buff(n), ind(n), cnt(n);
  25.     iota(all(arr), 0);
  26.     vector<int> mp(256);
  27.     for (int i = 0; i < n; i++) { mp[s[i]]++; }
  28.     for (int i = 1; i < 256; i++) { mp[i] += mp[i - 1]; }
  29.     for (int i = n - 1; i >= 0; i--) { arr[--mp[s[i]]] = i; }
  30.     c[arr[0]] = 0;
  31.     for (int i = 1; i < n; i++) {
  32.       c[arr[i]] = c[arr[i - 1]];
  33.       if (s[arr[i]] != s[arr[i - 1]]) {
  34.         c[arr[i]]++;
  35.       }
  36.     }
  37.     for (int deg = 1; (1 << (deg - 1)) < n; deg++) {
  38.       const int len = 1 << (deg - 1);
  39.       fill(all(cnt), 0);
  40.       ind[0] = 0;
  41.       for (int i = 0; i < n; i++) { arr[i] = modulo(arr[i] - len + n); }
  42.       for (int i = 0; i < n; i++) { cnt[c[arr[i]]]++; }
  43.       for (int i = 1; i < n; i++) { ind[i] = ind[i - 1] + cnt[i - 1]; }
  44.       for (int i = 0; i < n; i++) { buff[ind[c[arr[i]]]++] = arr[i]; }
  45.       copy(all(buff), begin(arr));
  46.       buff[arr[0]] = 0;
  47.       for (int i = 1; i < n; i++) {
  48.         buff[arr[i]] = buff[arr[i - 1]];
  49.         if (c[arr[i]] != c[arr[i - 1]] || c[modulo(arr[i] + len)] != c[modulo(arr[i - 1] + len)]) {
  50.           buff[arr[i]]++;
  51.         }
  52.       }
  53.       copy(all(buff), begin(c));
  54.     }
  55.     vector<int> pos(n);
  56.     for (int i = 0; i < n; i++) {
  57.       pos[arr[i]] = i;
  58.     }
  59.     int pre = 0;
  60.     for (int i = 0; i < n - 1; i++) {
  61.       int p = pos[i];
  62.       int len = min(n - 1 - i, n - 1 - arr[p - 1]);
  63.       int k = max(0, pre - 1);
  64.       lcp[p - 1] = k;
  65.       while (k < len && s[i + k] == s[arr[p - 1] + k]) { lcp[p - 1] = ++k; }
  66.       pre = k;
  67.     }
  68.   }
  69.   #undef modulo
  70. };
  71.  
  72. template<typename T = int, T def = 0>
  73. struct SparseTable {
  74.   int n, p;
  75.   vector<vector<T>> t;
  76.   vector<int> lg2;
  77.   vector<int> buff;
  78.   inline T Func(const T& __restrict a, const T& __restrict b) const {
  79.     if (b > n || buff[a] <= buff[b])
  80.       return a;
  81.     return b;
  82.   }
  83.   SparseTable(const vector<T>& arr)
  84.     : n(size(arr))
  85.     , p(log2(n + 1) + 1)
  86.     , t(p, vector<T>(n, def))
  87.     , lg2(n + 1, -1)
  88.     , buff(arr)
  89.   {
  90.     iota(all(t[0]), 0);
  91.     for (int d = 1; d < p; d++) {
  92.       const int dlt = 1 << (d - 1);
  93.       for (int i = 0; i + dlt < n; i++) {
  94.         t[d][i] = Func(t[d - 1][i], t[d - 1][i + dlt]);
  95.       }
  96.     }
  97.     for (int i = 1; i <= n; i++) { lg2[i] = lg2[i >> 1] + 1; }
  98.   }
  99.   T operator () (int l, int r) const {
  100.     if (l > r) return def;
  101.     int d = lg2[r - l + 1];
  102.     return Func(t[d][l], t[d][r - (1 << d) + 1]);
  103.   }
  104. };
  105.  
  106. ll Solve(const SparseTable<int, 1 << 25>& __restrict t, const vector<int>& __restrict arr, int l, int r, int val) {
  107.   vector<tuple<int,int,int>> queries;
  108.   queries.push_back({l, r, val});
  109.   ll ans = 0;
  110.   for (int i = 0; i < size(queries); i++) {
  111.     const auto& [cl, cr, cval] = queries[i];
  112.     l = cl;
  113.     r = cr;
  114.     val = cval;
  115.     while (l <= r) {
  116.       while (l <= r && arr[l] < val) { l++; }
  117.       if (l > r) break;
  118.       int cur = t(l, r);
  119.       if (arr[cur] < val) {
  120.         cur--;
  121.       } else {
  122.         cur = r;
  123.       }
  124.       int len = cur - l + 1;
  125.       ans += 1ll * len * (len + 1) / 2;
  126.       queries.push_back({l, cur, val + 1});
  127.       l = cur + 1;
  128.     }
  129.   }
  130.   return ans;
  131. }
  132.  
  133. int main() {
  134.   ios_base::sync_with_stdio(false);
  135.   cin.tie(nullptr);
  136.   cout.tie(nullptr);
  137.   string s;
  138.   cin >> s;
  139.   SuffixArray suff(s);
  140.   const int n = size(s);
  141.   const auto& arr = suff.lcp;
  142.   SparseTable<int, 1 << 25> t(arr);
  143.   cout << Solve(t, arr, 0, n - 1, 0) << '\n';
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement