Beispiel #1
0
    static int[] Solve(int n, long[] a)
    {
        var        res = new int[n];
        BIT <long> bit = new BIT <long>(n + 1, 0, (x, y) => x + y);

        for (int i = 0; i < bit.Size; i++)
        {
            bit.Operate(i, i);
        }
        for (int i = a.Length - 1; i >= 1; i--)
        {
            int valid   = n + 1;
            int invalid = -1;
            while (valid - invalid > 1)
            {
                var mid = (valid + invalid) / 2;
                if (a[i] < bit.Query(mid))
                {
                    valid = mid;
                }
                else
                {
                    invalid = mid;
                }
            }
            res[i] = valid;
            bit.Operate(res[i], -res[i]);
        }
        res[0] = (int)bit.Query(bit.Size - 1);
        return(res);
    }
Beispiel #2
0
    public static void Main()
    {
        var n        = NextInt;
        var k        = NextInt;
        var reducedA = new int[n];

        for (int i = 0; i < n; i++)
        {
            reducedA[i] = NextInt - k;
        }
        var accum = new long[n + 1];

        for (int i = 0; i < reducedA.Length; i++)
        {
            accum[i + 1] = accum[i] + reducedA[i];
        }
        //var compressDict = accum.Distinct().OrderBy(x => x).Select((elem, ind) => new Tuple { Item1 = elem, Item2 = ind }).ToDictionary(x => x.Item1, x => x.Item2);
        var distincted = accum.Distinct().ToArray();

        Array.Sort(distincted);
        long      res = 0;
        BIT <int> bit = new BIT <int>(distincted.Length, 0, (x, y) => x + y);

        for (int i = 0; i < accum.Length; i++)
        {
            var ind = Array.BinarySearch(distincted, accum[i]);
            res += bit.Query(ind);
            bit.Operate(ind, 1);
        }
        Console.WriteLine(res);
    }
Beispiel #3
0
    static void Main()
    {
        BIT <int>     bit     = new BIT <int>(NextInt + 1, 0, (x, y) => x + y);
        int           q       = NextInt;
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < q; i++)
        {
            if (NextInt == 0)
            {
                var l = NextInt;
                var r = NextInt;
                var x = NextInt;
                bit.Operate(l, x);
                bit.Operate(r + 1, -x);
            }
            else
            {
                builder.AppendLine(bit.Query(NextInt).ToString());
            }
        }
        Console.Write(builder.ToString());
    }
Beispiel #4
0
    public static void Main()
    {
        var       n   = NextInt;
        BIT <int> bit = new BIT <int>(100001, 0, Max);

        foreach (var boxes in Enumerable.Repeat(0, n).Select(_ => new Box()
        {
            X = NextInt, Y = NextInt
        }).GroupBy(x => x.X).OrderBy(x => x.Key))
        {
            foreach (var item in boxes.Select(x => new { Pos = x.Y, Res = bit.Query(x.Y) }).ToArray())
            {
                bit.Operate(item.Pos, item.Res + 1);
            }
        }
        var res = bit.Query(bit.Size);

        Console.WriteLine(res);
    }
Beispiel #5
0
    static void Main()
    {
        int           n       = NextInt;
        var           q       = NextInt;
        BIT <int>     bit     = new BIT <int>(n + 1, 0, (x, y) => x + y);
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < q; i++)
        {
            if (NextInt == 0)
            {
                bit.Operate(NextInt, NextInt);
            }
            else
            {
                builder.AppendLine((-bit.Query(NextInt - 1) + bit.Query(NextInt)).ToString());
            }
        }
        Console.Write(builder.ToString());
    }
Beispiel #6
0
    public static void Main()
    {
        int n        = int.Parse(Console.ReadLine());
        var a        = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var compress = a.Distinct().OrderByDescending(x => x).Select((Elem, Count) => new { Elem, Count }).ToDictionary(x => x.Elem, x => x.Count);

        long res = 0;

        for (int i = 0; i < n; i++)
        {
            //一点加算/区間和が可能なBinary Indexed Tree(BIT)
            BIT <int> bit = new BIT <int>(compress.Count, 0, (x, y) => x + y);
            for (int k = i + 1; k < n; k++)
            {
                //max(a_i, a_k)より大きい要素の数の和(座標圧縮先のindexに直してからquery)
                res += bit.Query(compress[Max(a[i], a[k])]);
                //今の点についてBITに追加
                bit.Operate(compress[a[k]], 1);
            }
        }
        Console.WriteLine(res);
    }
Beispiel #7
0
    static void Validate(int n)
    {
        Random     rng = new Random();
        var        res = Enumerable.Range(1, n).OrderBy(x => rng.Next()).ToArray();
        BIT <long> bit = new BIT <long>(n + 1, 0, (x, y) => x + y);

        long[] a = new long[n];
        for (int i = 0; i < res.Length; i++)
        {
            a[i] = bit.Query(res[i]);
            bit.Operate(res[i], res[i]);
        }
        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();
        var ans = Solve(n, a);

        Console.WriteLine(watch.ElapsedMilliseconds);
        if (res.Zip(ans, (x, y) => x != y).Any(x => x))
        {
            Console.WriteLine(string.Join(" ", res));
            Console.WriteLine(string.Join(" ", Solve(n, a)));
            Console.WriteLine(string.Join(" ", a));
        }
    }