結果

問題 No.1300 Sum of Inversions
ユーザー keymoonkeymoon
提出日時 2020-11-27 21:47:17
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,051 ms / 2,000 ms
コード長 6,197 bytes
コンパイル時間 3,142 ms
コンパイル使用メモリ 112,048 KB
実行使用メモリ 65,764 KB
最終ジャッジ日時 2023-10-01 05:17:25
合計ジャッジ時間 27,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
21,928 KB
testcase_01 AC 72 ms
23,868 KB
testcase_02 AC 72 ms
21,924 KB
testcase_03 AC 746 ms
53,676 KB
testcase_04 AC 793 ms
53,236 KB
testcase_05 AC 657 ms
48,904 KB
testcase_06 AC 827 ms
63,188 KB
testcase_07 AC 783 ms
53,732 KB
testcase_08 AC 871 ms
62,544 KB
testcase_09 AC 869 ms
64,440 KB
testcase_10 AC 482 ms
45,908 KB
testcase_11 AC 503 ms
44,960 KB
testcase_12 AC 773 ms
51,280 KB
testcase_13 AC 709 ms
52,908 KB
testcase_14 AC 1,051 ms
54,696 KB
testcase_15 AC 914 ms
61,524 KB
testcase_16 AC 735 ms
55,336 KB
testcase_17 AC 474 ms
49,628 KB
testcase_18 AC 547 ms
48,040 KB
testcase_19 AC 635 ms
53,336 KB
testcase_20 AC 663 ms
52,900 KB
testcase_21 AC 667 ms
55,468 KB
testcase_22 AC 593 ms
46,800 KB
testcase_23 AC 839 ms
60,960 KB
testcase_24 AC 668 ms
47,636 KB
testcase_25 AC 546 ms
45,836 KB
testcase_26 AC 541 ms
45,536 KB
testcase_27 AC 570 ms
46,460 KB
testcase_28 AC 927 ms
54,204 KB
testcase_29 AC 645 ms
52,728 KB
testcase_30 AC 879 ms
65,764 KB
testcase_31 AC 576 ms
48,860 KB
testcase_32 AC 614 ms
49,600 KB
testcase_33 AC 202 ms
42,704 KB
testcase_34 AC 261 ms
47,720 KB
testcase_35 AC 729 ms
57,084 KB
testcase_36 AC 744 ms
55,460 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;

public static class P
{
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var ordered = a.Distinct().OrderBy(x => x).ToArray();
        var dic = ordered.Select((elem, ind) => (elem, ind)).ToDictionary(x => x.elem, x => x.ind);
        a = a.Select(x => dic[x]).ToArray();
        (ModInt val, ModInt cnt)[] lRes = new (ModInt val, ModInt cnt)[n];
        {
            var segTree = new SegmentTree<(ModInt, ModInt)>(ordered.Length, (0, 0), (x, y) => (x.Item1 + y.Item1, x.Item2 + y.Item2));
            for (int i = 0; i < lRes.Length; i++)
            {
                lRes[i] = segTree.Query(a[i] + 1, ordered.Length - 1);
                segTree.Operate(a[i], (ordered[a[i]], 1));
            }
        }
        (ModInt val, ModInt cnt)[] rRes = new (ModInt val, ModInt cnt)[n];
        {
            var segTree = new SegmentTree<(ModInt, ModInt)>(ordered.Length, (0, 0), (x, y) => (x.Item1 + y.Item1, x.Item2 + y.Item2));
            for (int i = rRes.Length - 1; i >= 0; i--)
            {
                rRes[i] = segTree.Query(0, a[i] - 1);
                segTree.Operate(a[i], (ordered[a[i]], 1));
            }
        }
        ModInt res = 0;
        for (int i = 0; i < n; i++)
        {
            var (lVal, lCnt) = lRes[i];
            var (rVal, rCnt) = rRes[i];
            var sum = lVal * rCnt + rVal * lCnt + ordered[a[i]] * lCnt * rCnt;
            res += sum;
        }
        Console.WriteLine(res);
    }
}

class SegmentTree<T>
{
    public int Size { get; private set; }
    T Identity;
    T[] Data;
    Func<T, T, T> Merge;
    int LeafCount;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public SegmentTree(int size, T identity, Func<T, T, T> merge)
    {
        Init(size, identity, merge);
        for (int i = 1; i < Data.Length; i++) Data[i] = identity;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public SegmentTree(T[] elems, T identity, Func<T, T, T> merge)
    {
        Init(elems.Length, identity, merge);
        elems.CopyTo(Data, LeafCount);
        for (int i = elems.Length + LeafCount; i < Data.Length; i++) Data[i] = identity;
        for (int i = LeafCount - 1; i >= 1; i--) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]);
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private void Init(int size, T identity, Func<T, T, T> merge)
    {
        Size = size;
        Identity = identity;
        Merge = merge;
        LeafCount = 1;
        while (LeafCount < size) LeafCount <<= 1;
        Data = new T[LeafCount << 1];
    }

    public T this[int index]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get { return Data[LeafCount + index]; }
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        set { Update(index, value); }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Update(int i, T x) { Data[i += LeafCount] = x; while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]); }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public void Operate(int i, T x) { i += LeafCount; Data[i] = Merge(Data[i], x); while (0 < (i >>= 1)) Data[i] = Merge(Data[i << 1], Data[(i << 1) | 1]); }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Query(int l, int r)
    {
        T lRes = Identity, rRes = Identity;
        for (l += LeafCount, r += LeafCount; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1)
        {
            if ((l & 1) == 1) lRes = Merge(lRes, Data[l]); if ((r & 1) == 0) rRes = Merge(Data[r], rRes);
        }
        return Merge(lRes, rRes);
    }
}


struct ModInt
{
    public const int Mod = 998244353;
    const long POSITIVIZER = ((long)Mod) << 31;
    long Data;
    public ModInt(long data) { if ((Data = data % Mod) < 0) Data += Mod; }
    public static implicit operator long(ModInt modInt) => modInt.Data;
    public static implicit operator ModInt(long val) => new ModInt(val);
    public static ModInt operator +(ModInt a, int b) => new ModInt() { Data = (a.Data + b + POSITIVIZER) % Mod };
    public static ModInt operator +(ModInt a, long b) => new ModInt(a.Data + b);
    public static ModInt operator +(ModInt a, ModInt b) { long res = a.Data + b.Data; return new ModInt() { Data = res >= Mod ? res - Mod : res }; }
    public static ModInt operator -(ModInt a, int b) => new ModInt() { Data = (a.Data - b + POSITIVIZER) % Mod };
    public static ModInt operator -(ModInt a, long b) => new ModInt(a.Data - b);
    public static ModInt operator -(ModInt a, ModInt b) { long res = a.Data - b.Data; return new ModInt() { Data = res < 0 ? res + Mod : res }; }
    public static ModInt operator *(ModInt a, int b) => new ModInt(a.Data * b);
    public static ModInt operator *(ModInt a, long b) => a * new ModInt(b);
    public static ModInt operator *(ModInt a, ModInt b) => new ModInt() { Data = a.Data * b.Data % Mod };
    public static ModInt operator /(ModInt a, ModInt b) => new ModInt() { Data = a.Data * GetInverse(b) % Mod };
    public static bool operator ==(ModInt a, ModInt b) => a.Data == b.Data;
    public static bool operator !=(ModInt a, ModInt b) => a.Data != b.Data;
    public override string ToString() => Data.ToString();
    public override bool Equals(object obj) => (ModInt)obj == this;
    public override int GetHashCode() => (int)Data;
    static long GetInverse(long a)
    {
        long div, p = Mod, x1 = 1, y1 = 0, x2 = 0, y2 = 1;
        while (true)
        {
            if (p == 1) return x2 + Mod; div = a / p; x1 -= x2 * div; y1 -= y2 * div; a %= p;
            if (a == 1) return x1 + Mod; div = p / a; x2 -= x1 * div; y2 -= y1 * div; p %= a;
        }
    }
}
0