結果

問題 No.962 LCPs
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-12-24 23:19:59
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 2,800 bytes
コンパイル時間 759 ms
コンパイル使用メモリ 109,708 KB
実行使用メモリ 32,976 KB
最終ジャッジ日時 2023-09-04 04:27:51
合計ジャッジ時間 5,605 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 169 ms
32,976 KB
testcase_18 AC 84 ms
22,896 KB
testcase_19 AC 84 ms
24,144 KB
testcase_20 AC 57 ms
17,116 KB
testcase_21 AC 57 ms
17,716 KB
testcase_22 AC 44 ms
12,432 KB
testcase_23 AC 44 ms
12,728 KB
testcase_24 AC 37 ms
10,476 KB
testcase_25 AC 37 ms
11,660 KB
testcase_26 AC 30 ms
8,528 KB
testcase_27 AC 46 ms
13,704 KB
testcase_28 AC 31 ms
7,000 KB
testcase_29 AC 27 ms
7,808 KB
testcase_30 AC 43 ms
11,484 KB
testcase_31 AC 54 ms
14,192 KB
testcase_32 AC 25 ms
5,932 KB
testcase_33 AC 79 ms
22,552 KB
testcase_34 AC 32 ms
7,492 KB
testcase_35 AC 29 ms
7,244 KB
testcase_36 AC 37 ms
8,852 KB
testcase_37 AC 17 ms
4,660 KB
testcase_38 AC 17 ms
4,672 KB
testcase_39 AC 17 ms
6,196 KB
testcase_40 AC 17 ms
4,676 KB
testcase_41 AC 17 ms
4,692 KB
testcase_42 AC 17 ms
4,608 KB
testcase_43 AC 17 ms
4,636 KB
testcase_44 AC 17 ms
4,616 KB
testcase_45 AC 17 ms
4,632 KB
testcase_46 AC 17 ms
4,692 KB
testcase_47 AC 3 ms
4,384 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,376 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 3 ms
4,376 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 4 ms
4,380 KB
testcase_57 AC 4 ms
4,376 KB
testcase_58 AC 4 ms
4,376 KB
testcase_59 AC 5 ms
4,380 KB
testcase_60 AC 5 ms
4,380 KB
testcase_61 AC 5 ms
4,380 KB
testcase_62 AC 5 ms
4,380 KB
testcase_63 AC 103 ms
27,252 KB
testcase_64 AC 4 ms
4,376 KB
testcase_65 AC 109 ms
25,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum INF = 10^^9;

int N;
string[] S;
int[] L, C;

int segN;
Tuple!(int, int)[] seg;

Tuple!(int, int) segRangeMin(int a, int b) {
  auto ret = tuple(INF, -1);
  for (a += segN, b += segN; a < b; a >>= 1, b >>= 1) {
    if (a & 1) chmin(ret, seg[a++]);
    if (b & 1) chmin(ret, seg[--b]);
  }
  return ret;
}

long solve(int l, int r) {
  if (l == r) {
    return L[l];
  } else {
    const res = segRangeMin(l, r);
    const c = res[0];
    const mid = res[1];
    long ret;
    ret += solve(l, mid);
    ret += solve(mid + 1, r);
    const long a = mid - l + 1, b = r - mid;
    ret += c * (a * (a + 1) / 2) * b;
    ret += c * a * (b * (b + 1) / 2);
    debug {
      writeln("solve ", l, " ", r, ": ", mid, "; ", ret);
    }
    return ret;
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt();
      S = new string[N];
      foreach (i; 0 .. N) {
        S[i] = readToken();
      }
      
      L = new int[N];
      foreach (i; 0 .. N) {
        L[i] = cast(int)(S[i].length);
      }
      C = new int[N - 1];
      foreach (i; 0 .. N - 1) {
        int x;
        for (x = 0; x < L[i] && x < L[i + 1] && S[i][x] == S[i + 1][x]; ++x) {}
        C[i] = x;
      }
      debug {
        writeln("L = ", L);
        writeln("C = ", C);
      }
      
      for (segN = 1; segN < N; segN <<= 1) {}
      seg.length = segN << 1;
      foreach (i; 0 .. N - 1) {
        seg[segN + i] = tuple(C[i], i);
      }
      foreach_reverse (a; 1 .. segN) {
        seg[a] = min(seg[a << 1], seg[a << 1 | 1]);
      }
      
      const res = solve(0, N - 1);
      writeln(res);
    }
  } catch (EOFException e) {
  }
}
0