結果

問題 No.2640 traO Stamps
ユーザー tobisatistobisatis
提出日時 2024-02-19 21:54:44
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 5,483 bytes
コンパイル時間 8,525 ms
コンパイル使用メモリ 158,404 KB
実行使用メモリ 74,980 KB
最終ジャッジ日時 2024-02-19 21:55:07
合計ジャッジ時間 19,772 ms
ジャッジサーバーID
(参考情報)
judge13 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
33,060 KB
testcase_01 AC 79 ms
33,060 KB
testcase_02 AC 78 ms
33,060 KB
testcase_03 AC 79 ms
33,060 KB
testcase_04 AC 80 ms
33,060 KB
testcase_05 AC 77 ms
33,060 KB
testcase_06 AC 264 ms
72,680 KB
testcase_07 AC 265 ms
72,820 KB
testcase_08 AC 281 ms
74,168 KB
testcase_09 AC 270 ms
66,340 KB
testcase_10 AC 297 ms
73,504 KB
testcase_11 AC 233 ms
57,992 KB
testcase_12 AC 266 ms
74,248 KB
testcase_13 AC 248 ms
67,052 KB
testcase_14 AC 290 ms
74,264 KB
testcase_15 AC 270 ms
74,644 KB
testcase_16 AC 253 ms
73,476 KB
testcase_17 AC 271 ms
73,432 KB
testcase_18 AC 264 ms
74,576 KB
testcase_19 AC 280 ms
74,616 KB
testcase_20 AC 276 ms
73,836 KB
testcase_21 AC 247 ms
70,152 KB
testcase_22 AC 284 ms
74,360 KB
testcase_23 AC 235 ms
56,256 KB
testcase_24 AC 256 ms
56,980 KB
testcase_25 AC 282 ms
74,196 KB
testcase_26 AC 251 ms
74,980 KB
testcase_27 AC 250 ms
74,864 KB
testcase_28 AC 257 ms
74,952 KB
testcase_29 AC 261 ms
74,888 KB
testcase_30 AC 297 ms
74,632 KB
testcase_31 AC 301 ms
74,620 KB
testcase_32 AC 288 ms
73,608 KB
testcase_33 AC 301 ms
73,892 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (99 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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