結果

問題 No.2873 Kendall's Tau
ユーザー tobisatistobisatis
提出日時 2024-09-06 22:04:43
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 2,225 ms / 4,500 ms
コード長 5,895 bytes
コンパイル時間 7,376 ms
コンパイル使用メモリ 166,372 KB
実行使用メモリ 273,908 KB
最終ジャッジ日時 2024-09-06 22:05:32
合計ジャッジ時間 42,586 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
31,616 KB
testcase_01 AC 69 ms
31,744 KB
testcase_02 AC 70 ms
31,616 KB
testcase_03 AC 67 ms
31,356 KB
testcase_04 AC 65 ms
31,744 KB
testcase_05 AC 67 ms
31,744 KB
testcase_06 AC 68 ms
31,576 KB
testcase_07 AC 2,217 ms
158,840 KB
testcase_08 AC 2,126 ms
161,128 KB
testcase_09 AC 2,225 ms
160,792 KB
testcase_10 AC 2,148 ms
160,904 KB
testcase_11 AC 2,213 ms
158,888 KB
testcase_12 AC 2,056 ms
157,224 KB
testcase_13 AC 703 ms
75,352 KB
testcase_14 AC 1,577 ms
138,688 KB
testcase_15 AC 348 ms
58,660 KB
testcase_16 AC 405 ms
59,884 KB
testcase_17 AC 1,755 ms
130,376 KB
testcase_18 AC 1,100 ms
107,380 KB
testcase_19 AC 1,653 ms
128,088 KB
testcase_20 AC 360 ms
59,948 KB
testcase_21 AC 1,320 ms
115,280 KB
testcase_22 AC 479 ms
65,132 KB
testcase_23 AC 1,319 ms
115,740 KB
testcase_24 AC 228 ms
46,448 KB
testcase_25 AC 362 ms
57,624 KB
testcase_26 AC 1,735 ms
130,540 KB
testcase_27 AC 1,024 ms
94,992 KB
testcase_28 AC 1,912 ms
142,172 KB
testcase_29 AC 1,753 ms
147,372 KB
testcase_30 AC 287 ms
52,956 KB
testcase_31 AC 525 ms
69,356 KB
testcase_32 AC 1,354 ms
273,908 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (89 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

namespace AtCoder;

#nullable enable

using System.Numerics;

class WaveletMatrix<T> where T : IAdditionOperators<T, T, T>, ISubtractionOperators<T, T, T>, IAdditiveIdentity<T, T>
{
    const int d = 32;
    uint[,] Rank0 { get; init; }
    T[,] Sum { get; init; }
    int Length => Rank0.GetLength(1) - 1;

    public WaveletMatrix((uint, T)[] array)
    {
        var n = array.Length;
        var ranks = new uint[d, n + 1];
        var sum = new T[d, n + 1];
        var a = array.AsSpan();
        var b = new (uint, T)[n].AsSpan();
        var a0 = new (uint, T)[n].AsSpan();
        var a1 = new (uint, T)[n].AsSpan();
        for (var i = d - 1; i >= 0; i--)
        {
            var (i0, i1) = (0, 0);
            for (var j = 0; j < n; j++)
            {
                ranks[i, j + 1] += ranks[i, j];
                var v = a[j];
                if ((v.Item1 & (1U << i)) > 0)
                {
                    ranks[i, j + 1]++;
                    a1[i1++] = v;
                }
                else a0[i0++] = v;
            }
            for (var j = 0; j < i0; j++) b[j] = a0[j];
            for (var j = 0; j < i1; j++) b[j + i0] = a1[j];
            sum[i, 0] = T.AdditiveIdentity;
            for (var j = 0; j < n; j++) sum[i, j + 1] = sum[i, j] + b[j].Item2;
            var tmp = a; a = b; b = tmp;
        }
        for (var i = 0; i < d; i++) for (var j = 1U; j <= n; j++) ranks[i, j] = j - ranks[i, j];
        (Rank0, Sum) = (ranks, sum);
    }

    public T Frequency(uint l, uint r, uint upper) // [l, r), [0, upper)
    {
        var (n, rank0, sum) = (Length, Rank0, Sum);
        var res = T.AdditiveIdentity;
        for (var i = d - 1; i >= 0; i--)
        {
            if (l >= r) break;
            var (l0, r0) = (rank0[i, l], rank0[i, r]);
            if ((upper & (1U << i)) > 0)
            {
                res += sum[i, r0] - sum[i, l0];
                var zeros = rank0[i, n];
                l += zeros - l0;
                r += zeros - r0;
            }
            else (l, r) = (l0, r0);
        }
        return res;
    }
}

class PointMap
{
    readonly WaveletMatrix<long> matrix;
    readonly uint[] xs;

    public PointMap((uint, uint, long)[] points)
    {
        var sorted = points.OrderBy(e => e.Item1);
        var xs = sorted.Select(e => e.Item1);
        var ys = sorted.Select(e => (e.Item2, e.Item3));
        matrix = new WaveletMatrix<long>(ys.ToArray());
        this.xs = xs.ToArray();
    }

    public long Sum((uint, uint) lowerLeft, (uint, uint) upperRight) // [lx, rx), [ly, ry)
    {
        var (lx, ly) = lowerLeft;
        var (rx, ry) = upperRight;
        if (lx > rx || ly > ry) return 0;
        return Sum(lx, ly, rx - lx, ry - ly);
    }

    public long Sum(uint x, uint y, uint dx, uint dy) // [x, x + dx), [y, y + dy)
    {
        var ux = uint.MaxValue;
        if (x <= ux - dx) ux = x + dx;
        var uy = uint.MaxValue;
        if (y <= uy - dy) uy = y + dy;
        var l = MinIndex(x);
        var r = MinIndex(ux);
        return matrix.Frequency(l, r, uy) - matrix.Frequency(l, r, y);
    }

    uint MinIndex(uint x)
    {
        var xs = this.xs;
        var (l, r) = (-1, xs.Length);
        while (r - l > 1)
        {
            var m = (l + r) >> 1;
            if (xs[m] < x) l = m; else r = m;
        }
        return (uint)r;
    }
}

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var xy = new Dictionary<uint, int>();
        var yx = new Dictionary<uint, int>();
        var points = new (uint, uint, long)[n];
        PointMap map;
        {
            for (var i = 0; i < n; i++)
            {
                var x = (uint)(Input<long>() + int.MaxValue - 1);
                var y = (uint)(Input<long>() + int.MaxValue - 1);
                points[i] = (x, y, 1);
                if (xy.TryGetValue(x, out var vx)) xy[x] = vx + 1;
                else xy[x] = 1;
                if (yx.TryGetValue(y, out var vy)) yx[y] = vy + 1;
                else yx[y] = 1;
            }
            map = new(points);
        }
        var p = 0L;
        var q = 0L;
        var r = (long)n * (n - 1) / 2;
        var s = r;
        var u = uint.MaxValue;
        foreach (var (x, y, _) in points)
        {
            p += map.Sum(0, 0, x, y) + map.Sum(x + 1, y + 1, u, u);
            q += map.Sum(x + 1, 0, u, y) + map.Sum(0, y + 1, x, u);
        }
        foreach (var (_, v) in xy) r -= (long)v * (v - 1) / 2;
        foreach (var (_, v) in yx) s -= (long)v * (v - 1) / 2;
        var ans = (p - q) / (Math.Sqrt(r) * Math.Sqrt(s));
        return ans / 2;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
}
0