結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー eitahoeitaho
提出日時 2015-06-14 15:58:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,243 ms / 5,000 ms
コード長 3,311 bytes
コンパイル時間 4,546 ms
コンパイル使用メモリ 111,076 KB
実行使用メモリ 110,648 KB
最終ジャッジ日時 2023-09-11 04:04:52
合計ジャッジ時間 12,850 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
26,904 KB
testcase_01 AC 160 ms
30,412 KB
testcase_02 AC 81 ms
22,568 KB
testcase_03 AC 868 ms
99,412 KB
testcase_04 AC 2,243 ms
110,648 KB
testcase_05 AC 874 ms
99,440 KB
testcase_06 AC 779 ms
100,148 KB
testcase_07 AC 828 ms
100,340 KB
testcase_08 AC 924 ms
101,340 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 sorted = A.Distinct().OrderBy(a => a).ToArray();
        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