結果
| 問題 |
No.121 傾向と対策:門松列(その2)
|
| コンテスト | |
| ユーザー |
eitaho
|
| 提出日時 | 2015-06-14 16:02:20 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 1,533 ms / 5,000 ms |
| コード長 | 3,467 bytes |
| コンパイル時間 | 1,029 ms |
| コンパイル使用メモリ | 109,568 KB |
| 実行使用メモリ | 108,480 KB |
| 最終ジャッジ日時 | 2024-06-28 18:36:27 |
| 合計ジャッジ時間 | 8,214 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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;
}
}
eitaho