結果

問題 No.1300 Sum of Inversions
ユーザー keymoonkeymoon
提出日時 2020-11-27 21:45:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 6,186 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 120,900 KB
実行使用メモリ 68,840 KB
最終ジャッジ日時 2024-07-26 12:09:12
合計ジャッジ時間 23,340 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
25,412 KB
testcase_01 AC 40 ms
23,860 KB
testcase_02 AC 41 ms
25,504 KB
testcase_03 AC 703 ms
57,736 KB
testcase_04 AC 670 ms
57,396 KB
testcase_05 AC 552 ms
53,204 KB
testcase_06 AC 770 ms
65,952 KB
testcase_07 AC 762 ms
58,756 KB
testcase_08 AC 837 ms
68,840 KB
testcase_09 AC 806 ms
65,260 KB
testcase_10 AC 443 ms
49,136 KB
testcase_11 AC 445 ms
50,972 KB
testcase_12 AC 659 ms
57,872 KB
testcase_13 AC 669 ms
57,032 KB
testcase_14 AC 874 ms
61,216 KB
testcase_15 AC 809 ms
64,516 KB
testcase_16 AC 697 ms
59,812 KB
testcase_17 AC 426 ms
48,580 KB
testcase_18 AC 504 ms
52,212 KB
testcase_19 AC 589 ms
55,484 KB
testcase_20 AC 598 ms
55,000 KB
testcase_21 AC 597 ms
58,876 KB
testcase_22 AC 533 ms
50,840 KB
testcase_23 AC 761 ms
64,948 KB
testcase_24 AC 563 ms
51,992 KB
testcase_25 AC 454 ms
51,816 KB
testcase_26 AC 467 ms
54,020 KB
testcase_27 AC 509 ms
52,720 KB
testcase_28 AC 802 ms
61,680 KB
testcase_29 AC 575 ms
58,432 KB
testcase_30 AC 814 ms
67,452 KB
testcase_31 AC 526 ms
51,128 KB
testcase_32 AC 561 ms
55,736 KB
testcase_33 AC 157 ms
46,628 KB
testcase_34 AC 206 ms
55,672 KB
testcase_35 AC 602 ms
62,620 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