結果

問題 No.1641 Tree Xor Query
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-27 23:38:31
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 451 ms / 5,000 ms
コード長 4,423 bytes
コンパイル時間 13,563 ms
コンパイル使用メモリ 158,548 KB
実行使用メモリ 218,472 KB
最終ジャッジ日時 2023-12-27 23:38:51
合計ジャッジ時間 18,764 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
33,060 KB
testcase_01 AC 85 ms
33,060 KB
testcase_02 AC 85 ms
33,060 KB
testcase_03 AC 86 ms
33,060 KB
testcase_04 AC 86 ms
33,060 KB
testcase_05 AC 85 ms
33,060 KB
testcase_06 AC 85 ms
33,060 KB
testcase_07 AC 86 ms
33,060 KB
testcase_08 AC 86 ms
33,060 KB
testcase_09 AC 85 ms
33,060 KB
testcase_10 AC 89 ms
33,060 KB
testcase_11 AC 85 ms
33,060 KB
testcase_12 AC 84 ms
33,060 KB
testcase_13 AC 451 ms
88,096 KB
testcase_14 AC 432 ms
88,092 KB
testcase_15 AC 90 ms
34,980 KB
testcase_16 AC 104 ms
39,076 KB
testcase_17 AC 97 ms
36,900 KB
testcase_18 AC 96 ms
36,644 KB
testcase_19 AC 93 ms
35,364 KB
testcase_20 AC 385 ms
218,472 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (103 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 #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        c = NList;
        var map = NArr(n - 1);
        var query = NArr(q);
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        foreach (var edge in map)
        {
            tree[edge[0] - 1].Add(edge[1] - 1);
            tree[edge[1] - 1].Add(edge[0] - 1);
        }
        var left = new int[n];
        var right = new int[n];
        var pos = 0;
        DFS(-1, 0, ref pos, tree, left, right);
        var seg = new XorSegmentTree(n * 2);
        for (var i = 0; i < n; ++i) seg.Update(left[i], c[i]);
        var ans = new List<int>();
        foreach (var que in query)
        {
            if (que[0] == 1)
            {
                seg.Update(left[que[1] - 1], seg.Find(left[que[1] - 1], left[que[1] - 1] + 1) ^ que[2]);
            }
            else
            {
                ans.Add(seg.Find(left[que[1] - 1], right[que[1] - 1]));
            }
        }
        WriteLine(string.Join("\n", ans));
    }
    static void DFS(int prev, int cur, ref int pos, List<int>[] tree, int[] left, int[] right)
    {
        left[cur] = pos++;
        foreach (var next in tree[cur])
        {
            if (prev == next) continue;
            DFS(cur, next, ref pos, tree, left, right);
        }
        right[cur] = pos++;
    }
    class XorSegmentTree
    {
        public int[] tree { get; private set; }
        int INF = 0;
        public XorSegmentTree(int _n)
        {
            var x = 1;
            while (_n > x) x *= 2;
            var n = x * 2;
            tree = Enumerable.Repeat(INF, n).ToArray();
        }
        public XorSegmentTree(int[] valueList)
        {
            var _n = valueList.Length;
            var x = 1;
            while (_n > x) x *= 2;
            var n = x * 2;
            tree = Enumerable.Repeat(INF, n).ToArray();
            for (var i = 0; i < valueList.Length; ++i) tree[i + tree.Length / 2] = valueList[i];
            for (var i = tree.Length / 2 - 1; i > 0; --i) tree[i] = tree[i * 2] ^ tree[i * 2 + 1];
        }
        public void Update(int index, int value)
        {
            var idx = index + tree.Length / 2;
            tree[idx] = value;
            while (idx > 1)
            {
                var parent = idx / 2;
                var siblings = idx + 1 - (idx % 2) * 2;
                tree[parent] = tree[idx] ^ tree[siblings];
                idx = parent;
            }
        }
        public int Find(int a, int b)
        {
            return Find(a, b, 1, 0, tree.Length / 2);
        }
        int Find(int a, int b, int k, int l, int r)
        {
            if (r <= a || b <= l) return INF;
            if (a <= l && r <= b) return tree[k];
            return Find(a, b, k * 2, l, (l + r) / 2) ^ Find(a, b, k * 2 + 1, (l + r) / 2, r);
        }
        /// <summary>aおよびその右側から、val以上の値を持つ一番左のインデックスを取得する</summary>
        public int FindLeft(int a, int val, int b = -1, int k = 1, int l = 0, int r = -1)
        {
            if (b < 0) b = tree.Length / 2;
            if (r < 0) r = tree.Length / 2;
            if (r <= a || b <= l) return tree.Length;
            if (a <= l && r <= b)
            {
                if (tree[k] >= val)
                {
                    if (r - l > 1)
                    {
                        var idx = FindLeft(a, val, b, k * 2, l, (l + r) / 2);
                        if (idx < tree.Length) return idx;
                        return FindLeft(a, val, b, k * 2 + 1, (l + r) / 2, r);
                    }
                    else return l;
                }
                else return tree.Length;
            }
            return FindLeft(a, val, b, k * 2, l, (l + r) / 2) ^ FindLeft(a, val, b, k * 2 + 1, (l + r) / 2, r);
        }
    }
}
0