結果

問題 No.2640 traO Stamps
ユーザー tobisatistobisatis
提出日時 2024-02-19 21:54:44
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 5,483 bytes
コンパイル時間 8,389 ms
コンパイル使用メモリ 169,668 KB
実行使用メモリ 239,140 KB
最終ジャッジ日時 2024-09-29 01:54:12
合計ジャッジ時間 17,793 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
30,464 KB
testcase_01 AC 51 ms
30,564 KB
testcase_02 AC 50 ms
30,592 KB
testcase_03 AC 50 ms
30,592 KB
testcase_04 AC 50 ms
30,592 KB
testcase_05 AC 52 ms
30,716 KB
testcase_06 AC 229 ms
72,056 KB
testcase_07 AC 234 ms
71,976 KB
testcase_08 AC 266 ms
74,456 KB
testcase_09 AC 222 ms
67,968 KB
testcase_10 AC 259 ms
73,792 KB
testcase_11 AC 217 ms
67,968 KB
testcase_12 AC 237 ms
74,696 KB
testcase_13 AC 231 ms
68,096 KB
testcase_14 AC 252 ms
76,612 KB
testcase_15 AC 230 ms
69,248 KB
testcase_16 AC 226 ms
73,700 KB
testcase_17 AC 244 ms
73,532 KB
testcase_18 AC 239 ms
68,840 KB
testcase_19 AC 239 ms
74,980 KB
testcase_20 AC 239 ms
74,056 KB
testcase_21 AC 225 ms
69,248 KB
testcase_22 AC 252 ms
74,840 KB
testcase_23 AC 217 ms
68,212 KB
testcase_24 AC 232 ms
68,224 KB
testcase_25 AC 238 ms
74,496 KB
testcase_26 AC 222 ms
75,516 KB
testcase_27 AC 224 ms
75,140 KB
testcase_28 AC 217 ms
75,228 KB
testcase_29 AC 232 ms
75,556 KB
testcase_30 AC 264 ms
74,772 KB
testcase_31 AC 271 ms
74,904 KB
testcase_32 AC 247 ms
73,788 KB
testcase_33 AC 250 ms
239,140 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (81 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

namespace AtCoder;

#nullable enable

using System.Numerics;

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}

abstract class SegmentTree<TMonoid>
{
    public interface IOperator
    {
        TMonoid IdentityElement { get; }
        TMonoid Operation(TMonoid x, TMonoid y);
    }

    public interface IBinarySearchOperator { bool F(TMonoid value); }

    public class Body<TOperator> where TOperator : struct, IOperator
    {
        public int BaseSize { get; init; }
        protected int Height { get; init; }
        protected TOperator Op { get; init; }
        protected TMonoid[] Values { get; init; }

        public Body(TMonoid[] initial, TOperator op = default)
        {
            Op = op;
            (BaseSize, Height) = (1, 0);
            while (BaseSize < initial.Length) (BaseSize, Height) = (BaseSize << 1, Height + 1);
            var size = BaseSize << 1;
            Values = new TMonoid[size];
            for (var i = 0; i < size; i++) Values[i] = op.IdentityElement;
            for (var i = 0; i < initial.Length; i++) Values[i + BaseSize] = initial[i];
            for (var i = BaseSize - 1; i > 0; i--) Update(i);
        }

        public TMonoid Product(int l, int r)
        {
            (l, r) = (l + BaseSize, r + BaseSize);
            var (resL, resR) = (Op.IdentityElement, Op.IdentityElement);
            while (l < r)
            {
                if ((l & 1) == 1) resL = Op.Operation(resL, Values[l++]);
                if ((r & 1) == 1) resR = Op.Operation(Values[--r], resR);
                (l, r) = (l >> 1, r >> 1);
            }
            return Op.Operation(resL, resR);
        }

        public void Set(int index, TMonoid value)
        {
            var j = index + BaseSize;
            Values[j] = value;
            for (var i = 1; i <= Height; i++) Update(j >> i);
        }

        // the result might be greater than n
        public int MinIndex<TMinIndexOperator>(TMinIndexOperator op = default) where TMinIndexOperator : struct, IBinarySearchOperator
        {
            if (!op.F(Values[1])) return BaseSize + 1;
            var product = Op.IdentityElement;
            var (res, w, i) = (0, BaseSize, 1);
            while (w > 0)
            {
                var p = Op.Operation(product, Values[i]);
                if (!op.F(p))
                {
                    res += w;
                    i++;
                    product = p;
                }
                w >>= 1;
                i <<= 1;
            }
            return res + 1;
        }

        protected void Update(int k) { Values[k] = Op.Operation(Values[k << 1], Values[(k << 1) + 1]); }
    }
}

struct Op : SegmentTree<long>.IOperator
{
    public long IdentityElement => 0;
    public long Operation(long x, long y) => x + y;
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var m = Int();
        var k = Int();
        var sz = (k + 1).Repeat(() => Int() - 1);
        var g = new long[n, n];
        var max = long.MaxValue / 8;
        for (var i = 0; i < n; i++) for (var j = 0; j < n; j++) if (i != j) g[i, j] = max;
        for (var i = 0; i < m; i++)
        {
            var a = Int() - 1;
            var b = Int() - 1;
            var c = Int();
            g[a, b] = c;
            g[b, a] = c;
        }
        for (var v = 0; v < n; v++) for (var i = 0; i < n; i++) for (var j = 0; j < n; j++)
        {
            g[i, j] = Math.Min(g[i, j], g[i, v] + g[v, j]);
        }
        var init = new long[k];
        for (var i = 0; i < k; i++) init[i] = g[sz[i], sz[i + 1]];
        var tree = new SegmentTree<long>.Body<Op>(init);
        var q = Int();
        var ans = new List<long>();
        for (var i = 0; i < q; i++)
        {
            var t = Int();
            var x = Int();
            var y = Int();
            if (t == 1)
            {
                y--;
                sz[x] = y;
                if (x > 0) tree.Set(x - 1, g[sz[x - 1], y]);
                if (x < k) tree.Set(x, g[y, sz[x + 1]]);
            }
            else
            {
                ans.Add(tree.Product(x, y));
            }
        }
        Out(ans);
        return null;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    #pragma warning disable IDE0051
    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
    #pragma warning restore IDE0051
}
0