Esempio n. 1
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[]          array = ReadInts();
            SegTree <long> lowCntTree
                = new SegTree <long>(n, 1, (i, j) => i + j, 0);

            int[] poses = new int[n];
            for (int i = 0; i < n; i++)
            {
                poses[array[i] - 1] = i;
            }
            long res = 0;

            for (int i = 0; i < n; i++)
            {
                long cnt = Min(lowCntTree.Run(0, poses[i]),
                               lowCntTree.Run(poses[i] + 1, n));
                res += cnt;
                lowCntTree.Update(poses[i], 0);
            }
            WriteLine(res);
        }
Esempio n. 2
0
        static void Method(string[] args)
        {
            int[] nl     = ReadInts();
            int   n      = nl[0];
            int   length = nl[1];

            int[][] lrcs = new int[n][];
            for (int i = 0; i < n; i++)
            {
                lrcs[i] = ReadInts();
            }
            Array.Sort(lrcs, (a, b) => a[0] - b[0]);
            SegTree <long> tree = new SegTree <long>(length,
                                                     long.MaxValue / 2, Min, long.MaxValue / 2);

            for (int i = 0; i < n; i++)
            {
                if (lrcs[i][0] == 0)
                {
                    if (lrcs[i][2] < tree.Look(lrcs[i][1] - 1))
                    {
                        tree.Update(lrcs[i][1] - 1, lrcs[i][2]);
                    }
                }
                else
                {
                    long val = lrcs[i][2]
                               + tree.Run(lrcs[i][0] - 1, lrcs[i][1] - 1);
                    if (val < tree.Look(lrcs[i][1] - 1))
                    {
                        tree.Update(lrcs[i][1] - 1, val);
                    }
                }
            }
            WriteLine(tree.Look(length - 1));
        }