結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー eitahoeitaho
提出日時 2015-06-14 16:02:20
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,878 ms / 5,000 ms
コード長 3,467 bytes
コンパイル時間 2,637 ms
コンパイル使用メモリ 109,688 KB
実行使用メモリ 112,680 KB
最終ジャッジ日時 2023-09-11 04:05:26
合計ジャッジ時間 10,431 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
27,380 KB
testcase_01 AC 146 ms
30,920 KB
testcase_02 AC 81 ms
23,272 KB
testcase_03 AC 842 ms
101,992 KB
testcase_04 AC 1,878 ms
112,680 KB
testcase_05 AC 845 ms
100,180 KB
testcase_06 AC 763 ms
101,060 KB
testcase_07 AC 832 ms
100,616 KB
testcase_08 AC 896 ms
99,896 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.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    public void Solve()
    {
        int N = Reader.Int();
        var A = Reader.IntLine();
        var set = new HashSet<int>();
        foreach (var a in A) set.Add(a);
        var sorted = new int[set.Count];
        int si = 0;
        foreach (var a in set) sorted[si++] = a;
        Array.Sort(sorted);
        for (int i = 0; i < N; i++)
            A[i] = Array.BinarySearch(sorted, A[i]);
        var L = new BinaryIndexedTree(N);
        var R = new BinaryIndexedTree(N);
        foreach (var a in A) R.Add(a, 1);
        long ans = 0;

        foreach (var a in A)
        {
            ans += (long)L.Sum(a - 1) * L.Sum(a + 1, N + 1);
            ans += (long)R.Sum(a - 1) * R.Sum(a + 1, N + 1);
            L.Add(a, 1);
            R.Add(a, -1);
        }

        Console.WriteLine(ans);
    }

    class BinaryIndexedTree
    {
        private readonly int N;
        private readonly int[] A;

        public BinaryIndexedTree(int N)
        {
            this.N = N + 1;
            A = new int[N + 2];
        }
        public void Add(int i, int val)
        {
            for (++i; i <= N; i += i & -i) A[i] += val;
        }
        public void Update(int i, int val)
        {
            Add(i, val - (Sum(i) - Sum(i - 1)));
        }
        public int Sum(int i)
        {
            int sum = 0;
            for (++i; i > 0; i &= i - 1) sum += A[i];
            return sum;
        }
        public int Sum(int L, int R) // [L, R)
        {
            return Sum(R - 1) - Sum(L - 1);
        }
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Line()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0