結果

問題 No.2361 Many String Compare Queries
ユーザー 👑 hitonanodehitonanode
提出日時 2023-06-23 23:43:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,500 ms
コード長 2,122 bytes
コンパイル時間 2,181 ms
コンパイル使用メモリ 144,772 KB
実行使用メモリ 17,856 KB
最終ジャッジ日時 2023-09-13 18:53:45
合計ジャッジ時間 4,451 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 148 ms
17,824 KB
testcase_09 AC 155 ms
17,752 KB
testcase_10 AC 153 ms
17,836 KB
testcase_11 AC 133 ms
16,560 KB
testcase_12 AC 156 ms
17,856 KB
testcase_13 AC 149 ms
17,752 KB
testcase_14 AC 43 ms
12,816 KB
testcase_15 AC 75 ms
13,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)


#include <atcoder/string>
#include <atcoder/segtree>

int op1(int l, int r) { return min(l, r); }
int e1() { return 1 << 30; }


int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, Q;
    string S;
    cin >> N >> Q >> S;

    auto sa = atcoder::suffix_array(S);
    auto lcp = atcoder::lcp_array(S, sa);
    lcp.push_back(0);

    vector<lint> rcs(N);
    vector<pint> st;
    st.emplace_back(N - 1, 0);
    IREP(i, N - 1) {
        while (st.back().second > lcp.at(i)) st.pop_back();
        rcs.at(i) = rcs.at(st.back().first) + lint(st.back().first - i) * lcp.at(i);
        st.emplace_back(i, lcp.at(i));
    }

    vector<int> sainv(N, -1);
    REP(i, N) sainv.at(sa.at(i)) = i;

    vector<lint> cs(N + 1);
    REP(i, N) cs.at(i + 1) = cs.at(i) + (N - sa.at(i));

    atcoder::segtree<int, op1, e1> lcptree(lcp);

    map<pint, lint> memo;

    while (Q--) {
        int l, r;
        cin >> l >> r;
        --l;
        if (memo.count(pint(l, r))) {
            cout << memo.at(pint(l, r)) << '\n';
            continue;
        }
        int len = r - l;
        int x = sainv.at(l);
        int b = lcptree.max_right(x, [&](int v) { return v >= len; });
        int a = lcptree.min_left(x, [&](int v) { return v >= len; });
        lint ret = cs.at(a) + lint(b - a + 1) * (len - 1);

        int h = len - 1;
        while (b < N and h) {
            int bnxt = lcptree.max_right(b, [&](int v) { return v >= h; });
            ret += lint(h) * (bnxt - b);
            b = bnxt;
            if (b < N) {
                ret += rcs.at(b);
                break;
            }
        }
        memo[pint(l, r)] = ret;
        cout << ret << '\n';
    }
}
0