結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー りあんりあん
提出日時 2018-05-31 17:46:53
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 4,962 ms / 5,000 ms
コード長 2,719 bytes
コンパイル時間 4,370 ms
コンパイル使用メモリ 112,680 KB
実行使用メモリ 115,268 KB
最終ジャッジ日時 2023-09-12 21:02:34
合計ジャッジ時間 20,020 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 208 ms
34,000 KB
testcase_01 AC 287 ms
41,536 KB
testcase_02 AC 89 ms
24,288 KB
testcase_03 AC 1,858 ms
105,396 KB
testcase_04 AC 4,962 ms
115,268 KB
testcase_05 AC 1,869 ms
109,824 KB
testcase_06 AC 692 ms
107,428 KB
testcase_07 AC 1,007 ms
105,224 KB
testcase_08 AC 1,933 ms
107,436 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.Generic;
using System.Linq;
using System.IO;
using System.Text;

class Program
{
    static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        var sc = new Scan();
        int n = sc.Int;
        var a = sc.IntArr;
        var b = new int[n];
        var ar = a.ToList().Distinct().ToArray();
        Array.Sort(ar);
        int lim = ar.Length;
        var zip = new SortedDictionary<int, int>();
        for (int i = 0; i < lim; i++)
            zip.Add(ar[i], i);

        var sg = new Segtree<long>(lim, (i, j) => i + j, 0);
        for (int i = 0; i < n; i++)
        {
            b[i] = zip[a[i]];
        }
        long ans = 0;
        for (int i = 0; i < n; i++)
        {
            ans += sg.run(0, b[i]) * sg.run(b[i] + 1, lim);
            sg.update(b[i], sg.look(b[i]) + 1);
        }
        sg = new Segtree<long>(lim, (i, j) => i + j, 0);
        for (int i = n - 1; i >= 0; i--)
        {
            ans += sg.run(0, b[i]) * sg.run(b[i] + 1, lim);
            sg.update(b[i], sg.look(b[i]) + 1);
        }
        sw.WriteLine(ans);
        sw.Flush();
    }
}
class Scan
{
    public int Int { get { return int.Parse(Str); } }
    public long Long { get { return long.Parse(Str); } }
    public string Str { get { return Console.ReadLine().Trim(); } }
    public int[] IntArr { get { return Str.Split().Select(int.Parse).ToArray(); } }
}
class Segtree<T>
{
    int n;
    T[] tree;
    Func<T, T, T> f;
    T exnum;
    public Segtree(int m, Func<T, T, T> f, T ex)
    {
        this.f = f;
        this.exnum = ex;
        n = 1;
        while (n < m) n <<= 1;

        tree = new T[(n << 1) - 1];
        for (int i = 0; i < tree.Length; i++) tree[i] = ex;
    }
    public Segtree(int m, T ini, Func<T, T, T> f, T ex)
    {
        this.f = f;
        this.exnum = ex;
        n = 1;
        while (n < m) n <<= 1;

        tree = new T[(n << 1) - 1];
        for (int i = 0; i < tree.Length; i++) tree[i] = ini;
        for (int i = 0; i < m; ++i) update(i, ini);
    }
    public void update(int j, T x)
    {
        int i = j + n - 1;
        tree[i] = x;
        while (i > 0)
        {
            i = (i - 1) >> 1;
            tree[i] = f(tree[(i << 1) + 1], tree[(i << 1) + 2]);
        }
    }
    public T look(int i) { return tree[i + n - 1]; }

    // [s, t)
    public T run(int s, int t) { return query(s, t, 0, 0, n); }
    T query(int s, int t, int k, int l, int r)
    {
        if (r <= s || t <= l) return exnum;
        if (s <= l && r <= t) return tree[k];

        return f(query(s, t, (k << 1) + 1, l, (l + r) >> 1), query(s, t, (k + 1) << 1, (l + r) >> 1, r));
    }
}
0