Exemple #1
0
    public int GetLca(int u, int v)
    {
        if (u == v)
        {
            return(u);
        }
        if (ordersMap[u][0] > ordersMap[v][0])
        {
            var t = u; u = v; v = t;
        }
        if (ordersMap[u].Last() > ordersMap[v][0])
        {
            return(u);
        }

        var minDepth = depthST.Get(ordersMap[u].Last(), ordersMap[v][0]);
        var lcaOrder = depthST.Aggregate(ordersMap[u].Last(), ordersMap[v][0], -1, (p, n, l) =>
        {
            if (p != -1 || depthST[n] != minDepth)
            {
                return(p);
            }
            while (l > 1)
            {
                n   = depthST[n.Child0] == minDepth ? n.Child0 : n.Child1;
                l >>= 1;
            }
            return(depthST.Original(n));
        });

        return(tour[lcaOrder]);
    }
Exemple #2
0
    static void Main()
    {
        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        var h = Read();
        var n = h[0];
        var a = Read();

        var st = new ST1 <int>(n, Math.Max, 0, a);

        for (int k = 0; k < h[1]; k++)
        {
            var q = Read();
            if (q[0] == 1)
            {
                st.Set(q[1] - 1, q[2]);
            }
            else if (q[0] == 2)
            {
                Console.WriteLine(st.Get(q[1] - 1, q[2]));
            }
            else
            {
                Console.WriteLine(1 + st.Aggregate(q[1] - 1, n, n, (p, node, l) =>
                {
                    if (p < n || st[node] < q[2])
                    {
                        return(p);
                    }
                    while (node.i < st.n2 >> 1)
                    {
                        node = q[2] <= st[node.Child0] ? node.Child0 : node.Child1;
                    }
                    return(st.Original(node));
                }));
            }
        }
        Console.Out.Flush();
    }