結果
問題 | No.1997 X Lighting |
ユーザー |
|
提出日時 | 2023-10-01 22:07:47 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 446 ms / 2,000 ms |
コード長 | 4,499 bytes |
コンパイル時間 | 4,214 ms |
コンパイル使用メモリ | 109,568 KB |
実行使用メモリ | 60,644 KB |
最終ジャッジ日時 | 2024-07-26 13:32:58 |
合計ジャッジ時間 | 10,580 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;using static System.Console;using System.Linq;using System.Collections.Generic;class Program{static long NN => long.Parse(ReadLine());static long[] NList => ReadLine().Split().Select(long.Parse).ToArray();static long[] NMi => ReadLine().Split().Select(c => long.Parse(c) - 1).ToArray();static long[][] NMap(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NMi).ToArray();public static void Main(){Solve();}static void Solve(){var c = NList;var (n, m) = (c[0], c[1]);var map = NMap(m);var pset = new HashSet<long>();var mset = new HashSet<long>();var opset = new HashSet<long>();var omset = new HashSet<long>();var ecset = new HashSet<long>();var ocset = new HashSet<long>();ecset.Add(0);ocset.Add(0);foreach (var mi in map){pset.Add(mi[0] + mi[1]);mset.Add(mi[0] - mi[1]);if ((mi[0] + mi[1]) % 2 == 0){ecset.Add(mi[0] + mi[1]);ecset.Add(Math.Abs(mi[0] - mi[1]));ecset.Add(2 * n - 2 - Math.Abs(mi[0] - mi[1]));}else{ocset.Add(mi[0] + mi[1]);ocset.Add(Math.Abs(mi[0] - mi[1]));ocset.Add(2 * n - 2 - Math.Abs(mi[0] - mi[1]));}}var eclist = new List<long>(ecset);eclist.Sort();var edic = new Dictionary<long, int>();for (var i = 0; i < eclist.Count; ++i) edic[eclist[i]] = i;var oclist = new List<long>(ocset);oclist.Sort();var odic = new Dictionary<long, int>();for (var i = 0; i < oclist.Count; ++i) odic[oclist[i]] = i;var eft = new FenwickTree(eclist.Count);var oft = new FenwickTree(oclist.Count);var ans = 0L;foreach (var plus in pset){if (plus % 2 == 0){ans += Math.Min(plus + 1, 2 * n - 1 - plus);// WriteLine($"/: {Math.Min(plus + 1, 2 * n - 1 - plus)}");eft.Add(edic[plus], 1);}else{ans += Math.Min(plus + 1, 2 * n - 1 - plus);// WriteLine($"/: {Math.Min(plus + 1, 2 * n - 1 - plus)}");oft.Add(odic[plus], 1);}}foreach (var mi in mset){var left = Math.Abs(mi);var right = 2 * n - 2 - Math.Abs(mi);if (mi % 2 == 0){ans += (right - left) / 2 + 1 - eft.Sum(edic[right]) + eft.Sum(edic[left] - 1);// WriteLine($"({right} - {left}) / 2 + 1 - {eft.Sum(edic[right])} + {eft.Sum(edic[left] - 1)}");}else{ans += (right - left) / 2 + 1 - oft.Sum(odic[right]) + oft.Sum(odic[left] - 1);// WriteLine($"({right} - {left}) / 2 + 1 - {oft.Sum(odic[right])} + {oft.Sum(odic[left] - 1)}");}}WriteLine(ans);}class FenwickTree{int size;long[] tree;public FenwickTree(int size){this.size = size;tree = new long[size + 2];}public void Add(int index, int value){++index;for (var x = index; x <= size; x += (x & -x)) tree[x] += value;}/// <summary>先頭からindexまでの和(include index)</summary>public long Sum(int index){if (index < 0) return 0;++index;var sum = 0L;for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];return sum;}/// <summary>Sum(x) >= value となる最小のxを求める</summary>// 各要素は非負であることpublic int LowerBound(long value){if (value < 0) return -1;var x = 0;var b = 1;while (b * 2 <= size) b <<= 1;for (var k = b; k > 0; k >>= 1){if (x + k <= size && tree[x + k] < value){value -= tree[x + k];x += k;}}return x;}}}