結果

問題 No.2361 Many String Compare Queries
ユーザー KudeKude
提出日時 2023-06-23 23:50:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,521 bytes
コンパイル時間 5,420 ms
コンパイル使用メモリ 245,988 KB
実行使用メモリ 15,724 KB
最終ジャッジ日時 2023-09-13 19:00:19
合計ジャッジ時間 7,397 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 66 ms
13,580 KB
testcase_12 AC 74 ms
13,584 KB
testcase_13 AC 70 ms
13,528 KB
testcase_14 AC 49 ms
15,648 KB
testcase_15 AC 56 ms
15,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
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 op(int x, int y) { return min(x, y); }
int e() { return 1001001001; }


template<class T, class Compare=less<T>>
pair<vector<array<int, 2>>, int> cartesian_tree(const vector<T>& a) {
  const int n = a.size();
  vector<array<int, 2>> g(n, {-1, -1});
  vector<int> st;
  constexpr Compare cmp;
  for (int i = 0; i < n; i++) {
    int last_popped = -1;
    while (!st.empty() && cmp(a[i], a[st.back()])) {
      last_popped = st.back(); st.pop_back();
    }
    g[i][0] = last_popped;
    if (!st.empty()) g[st.back()][1] = i;
    st.emplace_back(i);
  }
  return pair(move(g), st.empty() ? -1 : st[0]);
}

template<class T, class S, class Compare=less<T>>
vector<T> prefix_min_sums(const vector<S>& a) {
  int n = a.size();
  auto [to, root] = cartesian_tree<S, Compare>(a);
  vector<T> d(n + 1);
  auto dfs = [&](auto&& self, int u, int l) -> void {
    if (u == -1) return;
    d[l] += a[u];
    d[u + 1] -= a[u];
    self(self, to[u][0], l);
    self(self, to[u][1], u + 1);
  };
  dfs(dfs, root, 0);
  for (int i = 0; i < n; i++) d[i + 1] += d[i];
  d.resize(n);
  return d;
}


} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  VI sa = suffix_array(s);
  VI lcp = lcp_array(s, sa);
  segtree<int, op, e> seg(lcp);
  VI sa_inv(n);
  rep(i, n) sa_inv[sa[i]] = i;
  VL acc(n + 1);
  rep(i, n) acc[i + 1] = acc[i] + n - sa[i];
  VL d = prefix_min_sums<ll>(lcp);
  rep(_, q) {
    int l, r;
    cin >> l >> r;
    l--;
    int len = r - l;
    int i = sa_inv[l];
    int li = seg.min_left(i, [&](int x) {
      return x >= len;
    });
    int ri = seg.max_right(i, [&](int x) {
      return x >= len;
    });
    ll ans = ll(ri - li + 1) * (len - 1);
    ans += acc[li];
    ans += d[ri];
    cout << ans << '\n';
  }
}
0