結果

問題 No.962 LCPs
ユーザー SSRSSSRS
提出日時 2021-12-21 08:23:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,001 bytes
コンパイル時間 2,893 ms
コンパイル使用メモリ 211,252 KB
実行使用メモリ 14,940 KB
最終ジャッジ日時 2023-10-13 19:47:28
合計ジャッジ時間 7,732 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 83 ms
14,940 KB
testcase_18 AC 45 ms
9,068 KB
testcase_19 AC 45 ms
9,028 KB
testcase_20 AC 32 ms
7,756 KB
testcase_21 AC 32 ms
7,884 KB
testcase_22 AC 24 ms
5,972 KB
testcase_23 AC 24 ms
5,980 KB
testcase_24 AC 22 ms
5,920 KB
testcase_25 AC 22 ms
5,952 KB
testcase_26 AC 18 ms
5,516 KB
testcase_27 AC 25 ms
6,240 KB
testcase_28 AC 18 ms
5,328 KB
testcase_29 AC 16 ms
4,632 KB
testcase_30 AC 23 ms
5,952 KB
testcase_31 AC 30 ms
7,824 KB
testcase_32 AC 15 ms
4,584 KB
testcase_33 AC 41 ms
8,740 KB
testcase_34 AC 19 ms
5,384 KB
testcase_35 AC 18 ms
5,376 KB
testcase_36 AC 21 ms
5,588 KB
testcase_37 AC 13 ms
4,712 KB
testcase_38 AC 13 ms
4,692 KB
testcase_39 AC 13 ms
4,600 KB
testcase_40 AC 13 ms
4,644 KB
testcase_41 AC 13 ms
4,652 KB
testcase_42 AC 13 ms
4,740 KB
testcase_43 AC 13 ms
4,648 KB
testcase_44 AC 13 ms
4,716 KB
testcase_45 AC 14 ms
4,644 KB
testcase_46 AC 13 ms
4,724 KB
testcase_47 AC 4 ms
4,348 KB
testcase_48 AC 4 ms
4,348 KB
testcase_49 AC 5 ms
4,348 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 4 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 4 ms
4,348 KB
testcase_54 AC 4 ms
4,348 KB
testcase_55 AC 3 ms
4,348 KB
testcase_56 AC 5 ms
4,348 KB
testcase_57 AC 5 ms
4,352 KB
testcase_58 AC 5 ms
4,356 KB
testcase_59 AC 5 ms
4,348 KB
testcase_60 AC 6 ms
4,348 KB
testcase_61 AC 6 ms
4,352 KB
testcase_62 AC 5 ms
4,352 KB
testcase_63 AC 41 ms
10,064 KB
testcase_64 AC 5 ms
4,352 KB
testcase_65 AC 56 ms
10,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
struct segment_tree{
  int N;
  vector<pair<int, int>> ST;
  segment_tree(vector<int> &A){
    int N2 = A.size();
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<pair<int, int>>(N * 2 - 1, make_pair(INF, 0));
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i] = make_pair(A[i], i);
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  pair<int, int> range_min(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return make_pair(INF, 0);
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return min(range_min(L, R, i * 2 + 1, l, m), range_min(L, R, i * 2 + 2, m, r));
    }
  }
  pair<int, int> range_min(int L, int R){
    return range_min(L, R, 0, 0, N);
  }
};
int main(){
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++){
    cin >> S[i];
  }
  vector<int> L(N);
  for (int i = 0; i < N; i++){
    L[i] = S[i].size();
  }
  long long ans = 0;
  if (N > 1){
    vector<int> P(N - 1, 0);
    for (int i = 0; i < N - 1; i++){
      while (P[i] < L[i] && P[i] < L[i + 1]){
        if (S[i][P[i]] == S[i + 1][P[i]]){
          P[i]++;
        } else {
          break;
        }
      }
    }
    segment_tree ST(P);
    queue<pair<int, int>> Q;
    Q.push(make_pair(0, N - 1));
    while (!Q.empty()){
      int l = Q.front().first;
      int r = Q.front().second;
      Q.pop();
      pair<int, int> p = ST.range_min(l, r);
      int mn = p.first;
      int m = p.second;
      long long S1 = (long long) (m - l + 1) * (m - l + 2) / 2 * (r - m);
      long long S2 = (long long) (r - m) * (r - m + 1) / 2 * (m - l + 1);
      ans += (S1 + S2) * mn;
      if (l != m){
        Q.push(make_pair(l, m));
      }
      if (m + 1 != r){
        Q.push(make_pair(m + 1, r));
      }
    }
  }
  for (int i = 0; i < N; i++){
    ans += L[i];
  }
  cout << ans << endl;
}
0