結果

問題 No.1300 Sum of Inversions
ユーザー keymoonkeymoon
提出日時 2020-11-27 21:45:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 6,186 bytes
コンパイル時間 4,238 ms
コンパイル使用メモリ 112,808 KB
実行使用メモリ 64,096 KB
最終ジャッジ日時 2023-10-01 05:15:12
合計ジャッジ時間 26,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
21,944 KB
testcase_01 AC 73 ms
23,852 KB
testcase_02 AC 72 ms
23,864 KB
testcase_03 AC 673 ms
53,944 KB
testcase_04 AC 638 ms
55,272 KB
testcase_05 AC 535 ms
48,904 KB
testcase_06 AC 740 ms
63,984 KB
testcase_07 AC 779 ms
55,780 KB
testcase_08 AC 851 ms
61,532 KB
testcase_09 AC 784 ms
63,404 KB
testcase_10 AC 439 ms
45,092 KB
testcase_11 AC 456 ms
47,120 KB
testcase_12 AC 647 ms
53,720 KB
testcase_13 AC 658 ms
55,188 KB
testcase_14 AC 832 ms
56,284 KB
testcase_15 AC 787 ms
62,588 KB
testcase_16 AC 679 ms
54,624 KB
testcase_17 AC 433 ms
46,780 KB
testcase_18 AC 514 ms
46,100 KB
testcase_19 AC 592 ms
56,740 KB
testcase_20 AC 603 ms
54,468 KB
testcase_21 AC 595 ms
54,852 KB
testcase_22 AC 566 ms
49,092 KB
testcase_23 AC 811 ms
64,096 KB
testcase_24 AC 595 ms
47,220 KB
testcase_25 AC 480 ms
45,840 KB
testcase_26 AC 501 ms
47,672 KB
testcase_27 AC 529 ms
44,556 KB
testcase_28 AC 822 ms
58,220 KB
testcase_29 AC 584 ms
54,608 KB
testcase_30 AC 802 ms
62,604 KB
testcase_31 AC 558 ms
47,056 KB
testcase_32 AC 611 ms
50,092 KB
testcase_33 AC 197 ms
44,776 KB
testcase_34 AC 247 ms
51,716 KB
testcase_35 AC 660 ms
57,700 KB
testcase_36 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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, long cnt)[] lRes = new (ModInt val, long cnt)[n];
        {
            var segTree = new SegmentTree<(ModInt, long)>(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, long cnt)[] rRes = new (ModInt val, long cnt)[n];
        {
            var segTree = new SegmentTree<(ModInt, long)>(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 + lCnt * rCnt * ordered[a[i]];
            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