Exemple #1
0
    static void Main()
    {
        var  h = ReadL();
        int  n = (int)h[0], k = (int)h[1];
        long l = h[2], r = h[3];
        var  a = ReadL();

        var n1 = n / 2;
        var n2 = n - n1;

        var gs1 = BruteForceHelper.CreateAllSumsForCount(a.Take(n1).ToArray());
        var gs2 = BruteForceHelper.CreateAllSumsForCount(a.Skip(n1).ToArray());

        var c = 0L;

        for (int i1 = 0; i1 <= n1; i1++)
        {
            var i2 = k - i1;
            if (i2 < 0 || n2 < i2)
            {
                continue;
            }

            var g1 = gs1[i1];
            var g2 = gs2[i2];
            Array.Sort(g1);
            Array.Reverse(g1);
            Array.Sort(g2);

            var ls = new int[g1.Length];
            var rs = new int[g1.Length];

            foreach (var ij in TwoPointers(g1.Length, g2.Length + 1, (i, j) => j == g2.Length || g1[i] + g2[j] >= l))
            {
                ls[ij.i] = ij.j;
            }
            foreach (var ij in TwoPointers(g1.Length, g2.Length + 1, (i, j) => j == g2.Length || g1[i] + g2[j] > r))
            {
                rs[ij.i] = ij.j;
            }

            for (int i = 0; i < g1.Length; i++)
            {
                c += rs[i] - ls[i];
            }
        }
        Console.WriteLine(c);
    }
Exemple #2
0
    static void Main()
    {
        var  h = ReadL();
        int  n = (int)h[0], k = (int)h[1];
        long l = h[2], r = h[3];
        var  a = ReadL();

        var n1 = n / 2;
        var n2 = n - n1;

        var gs1 = BruteForceHelper.CreateAllSumsForCount(a.Take(n1).ToArray());
        var gs2 = BruteForceHelper.CreateAllSumsForCount(a.Skip(n1).ToArray());

        var c = 0L;

        for (int i1 = 0; i1 <= n1; i1++)
        {
            var i2 = k - i1;
            if (i2 < 0 || n2 < i2)
            {
                continue;
            }

            var s1 = gs1[i1];
            var s2 = gs2[i2];
            Array.Sort(s2);

            foreach (var x1 in s1)
            {
                var li = First(0, s2.Length, si => x1 + s2[si] >= l);
                var ri = First(0, s2.Length, si => x1 + s2[si] > r);
                c += ri - li;
            }
        }
        Console.WriteLine(c);
    }