結果

問題 No.515 典型LCP
ユーザー くれちーくれちー
提出日時 2017-07-27 02:09:29
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 4,368 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 119,192 KB
実行使用メモリ 64,916 KB
最終ジャッジ日時 2024-04-18 02:08:21
合計ジャッジ時間 5,534 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Extentions;

class IO
{
    int idx;
    string[] input;

    public IO(TextReader reader)
    {
        input = reader.ReadToEnd().Split(new[] { " ", "\n", "\r" },
            StringSplitOptions.RemoveEmptyEntries);
    }

    T Get<T>(Func<string, T> parser) => parser(input[idx++]);

    public string S => Get(s => s);
    public char C => Get(char.Parse);
    public int I => Get(int.Parse);
    public long L => Get(long.Parse);
    public double F => Get(double.Parse);
    public decimal D => Get(decimal.Parse);
    public BigInteger B => Get(BigInteger.Parse);

    T[] Gets<T>(int n, Func<string, T> parser)
        => input.Skip((idx += n) - n).Take(n).Select(parser).ToArray();

    public string[] Ss(int n) => Gets(n, s => s);
    public char[] Cs(int n) => Gets(n, char.Parse);
    public int[] Is(int n) => Gets(n, int.Parse);
    public long[] Ls(int n) => Gets(n, long.Parse);
    public double[] Fs(int n) => Gets(n, double.Parse);
    public decimal[] Ds(int n) => Gets(n, decimal.Parse);
    public BigInteger[] Bs(int n) => Gets(n, BigInteger.Parse);

    public void Write<T>(params T[] xs) => WriteLine(string.Join(" ", xs));
    public void Write(params object[] xs) => WriteLine(string.Join(" ", xs));
}

static class Pair
{
    public static Pair<T1, T2> Create<T1, T2>(T1 first, T2 second)
        => new Pair<T1, T2>(first, second);
}

struct Pair<T1, T2>
{
    public T1 First;
    public T2 Second;

    public Pair(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

class SegmentTree
{
    int n;
    int[] tree;

    public SegmentTree(int n)
    {
        this.n = n;
        tree = Enumerable.Repeat(int.MaxValue, n * 2 - 1).ToArray();
    }

    public void Update(int i, int x)
    {
        i += n - 1;
        tree[i] = x;

        while (i > 0)
        {
            i = (i - 1) / 2;
            tree[i] = Min(tree[i * 2 + 1], tree[i * 2 + 2]);
        }
    }

    public int Query(int i, int j) => Query(i, j, 0, 0, n);

    int Query(int i, int j, int k, int l, int r)
    {
        if (r <= i || j <= l) return int.MaxValue;
        if (i <= l && r <= j) return tree[k];
        var vl = Query(i, j, k * 2 + 1, l, (l + r) / 2);
        var vr = Query(i, j, k * 2 + 2, (l + r) / 2, r);
        return Min(vl, vr);
    }
}

static class Extentions
{
    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x;
        x = y;
        y = tmp;
    }
}

static class Program
{
    public static void Main()
    {
        var sw = new StreamWriter(OpenStandardOutput()) { NewLine = "\n" };
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        SetOut(sw);
        Solve(new IO(In));
        Out.Flush();
    }

    static IEnumerable<Pair<int, int>> Queries(long n, long m, long x, long d)
    {
        for (var k = 0; k < m; k++)
        {
            var i = x / (n - 1) + 1;
            var j = x % (n - 1) + 1;
            if (i > j) Swap(ref i, ref j);
            else j = j + 1;
            yield return Pair.Create((int)i, (int)j);
            x = (x + d) % (n * (n - 1));
        }
    }

    static int Lcp(string a, string b)
        => a.Zip(b, (x, y) => x == y).TakeWhile(x => x).Count();

    static void Solve(IO io)
    {
        var n = io.I;
        var s = io.Ss(n);
        var m = io.I;
        var x = io.L;
        var d = io.L;

        var ss = s.Select(Pair.Create).OrderBy(a => a.First).ToArray();
        var si = ss.Select((a, i) => Pair.Create(a.Second, i))
            .OrderBy(a => a.First).Select(a => a.Second).ToArray();
        var segtree = new SegmentTree(131072);
        var lcplen = new int[n - 1];

        for (var i = 0; i < n - 1; i++)
        {
            lcplen[i] = Lcp(ss[i].First, ss[i + 1].First);
            segtree.Update(i, lcplen[i]);
        }

        var ans = 0L;

        foreach (var query in Queries(n, m, x, d))
        {
            var p = si[query.First - 1];
            var q = si[query.Second - 1];
            if (p > q) Swap(ref p, ref q);
            ans += segtree.Query(p, q);
        }

        io.Write(ans);
    }
}
0