Ejemplo n.º 1
0
    public void Solve()
    {
        int N = Reader.Int();

        int[] X = new int[N], V = new int[N];
        for (int i = 0; i < N; i++)
        {
            X[i] = Reader.Int(); V[i] = Reader.Int();
        }
        V = Compress(V);
        Array.Sort(X, V);
        int[] L = new int[N], R = new int[N];

        for (int i = 0, max = -1; i < N; i++)
        {
            R[i] = max = Math.Max(max, V[i] + 1);
        }
        for (int i = N - 1, min = int.MaxValue; i >= 0; i--)
        {
            L[i] = min = Math.Min(min, V[i]);
        }

        var BIT = new BinaryIndexedTree(N + 1, Mod);

        BIT.Add(0, 1);
        for (int i = 0; i < N; i++)
        {
            BIT.Add(R[i], BIT.Sum(L[i], R[i] + 1));
        }

        Console.WriteLine(BIT.Sum(N, N + 1));
    }
Ejemplo n.º 2
0
    string Solve(int N, int[] who, int[] index)
    {
        Array.Sort(index, who);
        var ans = new int[N * N];
        int L   = 0;
        var bit = new BinaryIndexedTree(N * N);

        for (int i = 0; i < N * N; i++)
        {
            bit.Add(i, 1);
        }

        for (int i = 0; i < N; i++)
        {
            if (bit.Sum(index[i]) < who[i])
            {
                return(No);
            }
            ans[index[i]] = who[i];
            bit.Add(index[i], -1);

            int num = who[i] - 1;
            for (; L < ans.Length && num > 0; L++)
            {
                if (ans[L] == 0)
                {
                    num--;
                    bit.Add(L, -1);
                    ans[L] = who[i];
                }
            }
            if (num > 0)
            {
                return(No);
            }
        }
        for (int i = 0; i < N; i++)
        {
            for (int num = N - who[i]; L < ans.Length && num > 0; L++)
            {
                if (ans[L] == 0)
                {
                    if (L < index[i])
                    {
                        return(No);
                    }
                    ans[L] = who[i];
                    num--;
                }
            }
        }

        return(Yes + "\n" + string.Join(" ", ans));
    }
Ejemplo n.º 3
0
    void Solve()
    {
        var S = Console.ReadLine();
        var N = S.Length;
        var s = S.Select(c => c - '0').ToArray();
        var bit = new BinaryIndexedTree(s);
        int k = 0, l = 0, r = N;

        while (true)
        {
            var t = bit.Sum(l, r);
            if (t == 0 || t == r - l)
            {
                break;
            }
            bit.Add(l, -s[l]);
            bit.Add(r - 1, -s[r - 1]);
            l++; r--; k++;
        }
        Console.WriteLine(N - k);
    }
Ejemplo n.º 4
0
    void Solve()
    {
        var N   = F();
        var B   = G();
        var rev = new int[N];
        var bit = new BinaryIndexedTree(N);

        for (var i = 0; i < N; i++)
        {
            rev[--B[i]] = i;
            bit.Add(i, 1);
        }
        var s = 0L;

        for (var i = 0; i < N; i++)
        {
            s += Min(bit.Sum(0, rev[i]), bit.Sum(rev[i] + 1, N));
            bit.Add(rev[i], -1);
        }
        WriteLine(s);
    }
Ejemplo n.º 5
0
        public void TestMethod1()
        {
            int[] A = { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };

            BinaryIndexedTree biTree = new BinaryIndexedTree(A);
            var sum = biTree.GetSum(5);

            Assert.AreEqual(12, sum);

            biTree.Add(3, 6);
            sum = biTree.GetSum(5);
            Assert.AreEqual(18, sum);
        }
Ejemplo n.º 6
0
 public long countColoring(int n, int xzero, int xmul, int xadd, int xmod, int yzero, int ymul, int yadd, int ymod)
 {
     int[] xs = new int[n], ys = new int[n];
     Point[] points = new Point[n];
     xs[0] = xzero;
     ys[0] = yzero;
     points[0] = new Point(xs[0], ys[0]);
     for (int i = 1; i < n; ++i) {
         xs[i] = (int)(((long)points[i - 1].X * xmul + xadd) % xmod);
         ys[i] = (int)(((long)points[i - 1].Y * ymul + yadd) % ymod);
         points[i] = new Point(xs[i], ys[i]);
     }
     Array.Sort(xs);
     Array.Sort(ys);
     for (int i = 0; i < n; ++i) {
         points[i].X = Array.BinarySearch(xs, points[i].X) + 1;
         points[i].Y = Array.BinarySearch(ys, points[i].Y) + 1;
     }
     Array.Sort(points);
     long count = 0;
     BinaryIndexedTree tree;
     tree = new BinaryIndexedTree(n);
     int[] countL = new int[n], countR = new int[n];
     for (int i = 0; i < n; ++i) {
         countL[i] = tree.Sum(points[i].Y);
         tree.Add(points[i].Y, 1);
     }
     tree = new BinaryIndexedTree(n);
     for (int i = n - 1; i >= 0; --i) {
         countR[i] = tree.Sum(n) - tree.Sum(points[i].Y);
         count += (long)countR[i] * (countR[i] - 1) / 2;
         count -= (long)countL[i] * countR[i];
         tree.Add(points[i].Y, 1);
     }
     return count;
 }
Ejemplo n.º 7
0
    void Solve()
    {
        var N   = F();
        var A   = G();
        var B   = G();
        var rev = new int[N];

        for (var i = 0; i < N; i++)
        {
            rev[--A[i]] = i;
        }
        for (var i = 0; i < N; i++)
        {
            B[i] = rev[B[i] - 1];
        }
        var inv = new int[N];
        var s   = 0L;
        var bit = new BinaryIndexedTree(N);

        for (var i = 0; i < N; i++)
        {
            s += inv[B[i]] = bit.Sum(B[i], N);
            bit.Add(B[i], 1);
        }
        if (s % 2 == 1)
        {
            Console.WriteLine(-1);
        }
        else
        {
            if (s > 0)
            {
                s /= 2;
                var done = 0;
                while (s >= inv[done])
                {
                    s -= inv[done++];
                }
                var k = N - 1;
                for (var j = N - 1; j >= done; j--)
                {
                    while (B[k] < done)
                    {
                        k--;
                    }
                    B[j] = B[k--];
                }
                for (var j = 0; j < done; j++)
                {
                    B[j] = j;
                }
                for (var j = done; s > 0 && j < N; j++)
                {
                    for (k = N - 1; s > 0 && k > j; k--)
                    {
                        if (B[k - 1] > B[k])
                        {
                            var t = B[k];
                            B[k]     = B[k - 1];
                            B[k - 1] = t;
                            s--;
                        }
                    }
                }
            }
            for (var i = 0; i < N; i++)
            {
                B[i] = A[B[i]] + 1;
            }
            Console.WriteLine(string.Join(" ", B));
        }
    }