結果

問題 No.515 典型LCP
ユーザー nebukuro09nebukuro09
提出日時 2017-05-11 09:54:35
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,305 bytes
コンパイル時間 756 ms
コンパイル使用メモリ 119,480 KB
実行使用メモリ 234,272 KB
最終ジャッジ日時 2023-09-03 13:16:18
合計ジャッジ時間 5,495 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(i => tuple(readln.chomp, i)).array;
    auto ttt = readln.split.map!(to!long);
    auto M = ttt[0].to!int;
    auto x = ttt[1];
    auto d = ttt[2];

    auto I = new int[](M);
    auto J = new int[](M);
    foreach (k; 0..M) {
        I[k] = ((x / (N - 1)) + 1).to!int;
        J[k] = ((x % (N - 1)) + 1).to!int;
        if (I[k] > J[k]) swap(I[k], J[k]);
        else J[k] += 1;
        x = (x + d) % (N.to!long * (N.to!long - 1));
    }

    S.sort();
    auto rev = new int[](N);
    foreach (i; 0..N) {
        rev[S[i][1]] = i;
    }


    auto T = new string[](N);
    foreach (i; 0..N) T[i] = S[i][0];
    auto D = new int[](N-1);
    foreach (i; 0..N-1) {
        foreach (j; 0..min(T[i].length, T[i+1].length)) {
            if (T[i][j] == T[i+1][j]) D[i]++;
            else break;
        }
    }

    auto st = new SparseTable(D);

    long[int][int] mem;
    long ans = 0;
    foreach (k; 0..M.to!int) {
        int i = rev[I[k] - 1];
        int j = rev[J[k] - 1];
        if (i > j) swap(i, j);
        if (i in mem && j in mem[i]) ans += mem[i][j];
        else ans += mem[i][j] = st.getmin(i, j-1);
    }

    ans.writeln;
}

class SparseTable {
    int LOGMAXN = 25;
    int N;
    int[] A;
    int[][] M;
    
    this(int[] A) {
        this.A = A.dup;
        N = A.length.to!int;
        M = new int[][](N, LOGMAXN);
   
        //initialize M for the intervals with length 1
        for (int i = 0; i < N; i++)
            M[i][0] = i;
        //compute values from smaller to bigger intervals
        for (int j = 1; 1 << j <= N; j++)
            for (int i = 0; i + (1 << j) - 1 < N; i++)
                if (A[M[i][j - 1]] < A[M[i + (1 << (j - 1))][j - 1]])
                    M[i][j] = M[i][j - 1];
                else
                    M[i][j] = M[i + (1 << (j - 1))][j - 1];
    }

    int getmin(int i, int j) {
        //int m = j - i + 1;
        int k = bsr(j - i + 1);
        //while (m >>= 1) k += 1;
        int h = A[M[i][k]] <= A[M[j-2^^k+1][k]] ? M[i][k] : M[j-2^^k+1][k];
        return A[h];
    }    
}

0