Ejemplo n.º 1
0
        //check if tree is sumtree or not
        //public static TreeNode<int> ConstructTreeGivenInOrderAndPreOrder(int[] inorder,int[] preorder)
        //{
        //    wh
        //}

        //summify tree
        //public static TreeNode<int> ConstructTreeGivenInOrderAndPreOrder(int[] inorder,int[] preorder)
        //{
        //    wh
        //}

        //Insertion in tree
        //public static TreeNode<int> ConstructTreeGivenInOrderAndPreOrder(int[] inorder,int[] preorder)
        //{
        //    wh
        //}

        //Deletion in tree
        //public static TreeNode<int> ConstructTreeGivenInOrderAndPreOrder(int[] inorder,int[] preorder)
        //{
        //    wh
        //}

        //Find Binary tree max path sum
        //http://www.geeksforgeeks.org/find-maximum-path-sum-in-a-binary-tree/
        public static int MaxPathSum(TreeNode <int> node)
        {
            MaxInt max = new MaxInt();

            MaxPathSumInternal(node, max);
            return(max.Val);
        }
Ejemplo n.º 2
0
        public override void Solve(IOManager io)
        {
            var n       = io.ReadInt();
            var k       = io.ReadInt();
            var a       = new int[n];
            var segTree = new SegmentTree <MaxInt>(Enumerable.Repeat(new MaxInt(0), 300001).ToArray());

            for (int i = 0; i < a.Length; i++)
            {
                var ai    = io.ReadInt();
                var left  = System.Math.Max(0, ai - k);
                var right = System.Math.Min(segTree.Length, ai + k + 1);
                segTree[ai] = new MaxInt(segTree.Query(left, right).Value + 1);
            }

            io.WriteLine(segTree.Query(0, segTree.Length).Value);
        }
Ejemplo n.º 3
0
        private static int MaxPathSumInternal(TreeNode <int> node, MaxInt max)
        {
            if (node == null)
            {
                max.Val = 0;
                return(0);
            }

            int left                = MaxPathSumInternal(node.left, max);
            int right               = MaxPathSumInternal(node.right, max);
            int biggerChild         = Math.Max(left, right);
            int biggerIncludingRoot = Math.Max(biggerChild + node.data, node.data);
            int leftRightRoot       = left + right + node.data;

            int bigger = Math.Max(biggerIncludingRoot, leftRightRoot);

            if (max.Val < bigger)
            {
                max.Val = bigger;
            }
            return(biggerIncludingRoot);
        }
Ejemplo n.º 4
0
        public override void Solve(IOManager io)
        {
            var n       = io.ReadInt();
            var queries = io.ReadInt();
            var a       = new MaxInt[n];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = new MaxInt(io.ReadInt());
            }

            var segtree = new LazySegmentTree <MaxInt, Nothing>(a);

            for (int q = 0; q < queries; q++)
            {
                var type = io.ReadInt();

                if (type == 1)
                {
                    var i = io.ReadInt() - 1;
                    var v = io.ReadInt();
                    segtree[i] = new MaxInt(v);
                }
                else if (type == 2)
                {
                    var l = io.ReadInt() - 1;
                    var r = io.ReadInt();
                    io.WriteLine(segtree.Query(l, r).Value);
                }
                else
                {
                    var l = io.ReadInt() - 1;
                    var v = io.ReadInt();
                    io.WriteLine(Math.Max(l, segtree.FindMaxRight(l, ai => ai.Value < v)) + 1);
                }
            }
        }