結果

問題 No.2327 Inversion Sum
ユーザー 👑 kakel-sankakel-san
提出日時 2023-10-14 23:47:56
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 4,057 bytes
コンパイル時間 1,107 ms
コンパイル使用メモリ 64,200 KB
実行使用メモリ 33,944 KB
最終ジャッジ日時 2023-10-14 23:48:02
合計ジャッジ時間 5,902 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
25,640 KB
testcase_01 AC 138 ms
29,556 KB
testcase_02 AC 114 ms
26,736 KB
testcase_03 AC 74 ms
25,272 KB
testcase_04 AC 169 ms
33,944 KB
testcase_05 AC 71 ms
22,252 KB
testcase_06 AC 121 ms
28,756 KB
testcase_07 AC 95 ms
27,116 KB
testcase_08 AC 73 ms
23,896 KB
testcase_09 AC 129 ms
29,332 KB
testcase_10 AC 80 ms
23,912 KB
testcase_11 AC 66 ms
22,484 KB
testcase_12 AC 65 ms
24,092 KB
testcase_13 AC 61 ms
19,892 KB
testcase_14 AC 104 ms
27,548 KB
testcase_15 AC 138 ms
29,620 KB
testcase_16 AC 87 ms
25,436 KB
testcase_17 AC 71 ms
22,868 KB
testcase_18 AC 71 ms
21,852 KB
testcase_19 AC 82 ms
25,396 KB
testcase_20 AC 60 ms
21,756 KB
testcase_21 AC 58 ms
21,852 KB
testcase_22 AC 60 ms
23,892 KB
testcase_23 AC 61 ms
21,840 KB
testcase_24 AC 60 ms
21,968 KB
testcase_25 AC 59 ms
21,852 KB
testcase_26 AC 62 ms
21,808 KB
testcase_27 AC 65 ms
23,900 KB
testcase_28 AC 63 ms
23,972 KB
testcase_29 AC 60 ms
21,792 KB
testcase_30 AC 59 ms
23,916 KB
testcase_31 AC 59 ms
21,736 KB
testcase_32 AC 61 ms
21,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NArr(m);
        WriteLine(Perm(n, m, map));
    }
    static long Perm(int n, int m, int[][] map)
    {
        Array.Sort(map, (l, r) => l[0].CompareTo(r[0]));
        var p = Enumerable.Repeat(-1, n).ToArray();
        var pos = Enumerable.Repeat(-1, n).ToArray();
        var ans = 0L;
        var ft = new FenwickTree(n + 1);
        foreach (var info in map)
        {
            p[info[0] - 1] = info[1] - 1;
            pos[info[1] - 1] = info[0] - 1;
            ans += ft.Sum(n) - ft.Sum(info[1] - 1);
            ft.Add(info[1] - 1, 1);
        }
        var mod = 998_244_353;
        ans %= mod;
        if (n > m)
        {
            var all = 1L;
            var rest1 = 1L;
            var inv2 = 499_122_177;
            for (var i = 0; i < n - m; ++i)
            {
                all = all * (i + 1) % mod;
                if (i + 1 < n - m) rest1 = rest1 * (i + 1) % mod;
            }
            ans = (ans + (long)(n - m) * (n - m - 1) % mod * inv2 % mod * inv2 % mod) % mod * all % mod;
            var sub = 0L;
            var count = new int[n];
            for (var i = n - 1; i >= 0; --i)
            {
                if (p[i] < 0) ++count[i];
                if (i + 1 < n) count[i] += count[i + 1];
            }
            var add = 0L;
            for (var i = n - 1; i >= 0; --i)
            {
                if (pos[i] >= 0) add += count[pos[i]];
                else sub = (sub + add % mod) % mod;
            }
            count = new int[n];
            for (var i = 0; i < n; ++i)
            {
                if (p[i] < 0) ++count[i];
                if (i > 0) count[i] += count[i - 1];
            }
            add = 0L;
            for (var i = 0; i < n; ++i)
            {
                if (pos[i] >= 0) add += count[pos[i]];
                else sub = (sub + add % mod) % mod;
            }
            ans = (ans + sub * rest1) % mod;
        }
        return ans;
    }
    static long Exp(long n, long k, int mod)
    {
        if (k == 0) return 1;
        if (k == 1) return n % mod;
        var half = Exp(n, k / 2, mod);
        var result = (half * half) % mod;
        return ((k % 2) == 0) ? result : ((result * n) % mod);
    }
    class FenwickTree
    {
        int size;
        long[] tree;
        public FenwickTree(int size)
        {
            this.size = size;
            tree = new long[size + 2];
        }
        public void Add(int index, int value)
        {
            ++index;
            for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
        }
        /// <summary>先頭からindexまでの和(include index)</summary>
        public long Sum(int index)
        {
            if (index < 0) return 0;
            ++index;
            var sum = 0L;
            for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
            return sum;
        }
        /// <summary>Sum(x) >= value となる最小のxを求める</summary>
        // 各要素は非負であること
        public int LowerBound(long value)
        {
            if (value < 0) return -1;
            var x = 0;
            var b = 1;
            while (b * 2 <= size) b <<= 1;
            for (var k = b; k > 0; k >>= 1)
            {
                if (x + k <= size && tree[x + k] < value)
                {
                    value -= tree[x + k];
                    x += k;
                }
            }
            return x;
        }
    }
}
0