結果
| 問題 |
No.1619 Coccinellidae
|
| コンテスト | |
| ユーザー |
takytank
|
| 提出日時 | 2021-07-22 22:29:11 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 43,487 bytes |
| コンパイル時間 | 18,820 ms |
| コンパイル使用メモリ | 169,660 KB |
| 実行使用メモリ | 52,896 KB |
| 最終ジャッジ日時 | 2024-07-17 18:39:47 |
| 合計ジャッジ時間 | 22,864 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 1 TLE * 1 -- * 14 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (95 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace YukiCoder
{
class Program
{
static void Main()
{
using var cin = new Scanner();
int n = cin.Int();
var (m, k) = cin.Long2();
var nums = new long[n];
long mm = m;
for (int i = 0; i < n; i++) {
nums[i] = i;
mm -= i;
}
long div = mm / n;
long rem = mm % n;
for (int i = 0; i < n; i++) {
nums[i] += div;
if (i >= n - rem) {
nums[i] += 1;
}
}
var set = new RedBlackTree<long>();
for (int i = 0; i < n; i++) {
set.Add(nums[i]);
}
var ans = new long[n];
for (int i = n - 1; i >= 0; i--) {
if (k > 0 && k >= i) {
ans[i] = set[n - 1 - i];
set.Remove(ans[i]);
k -= i;
} else {
ans[i] = set.Max();
set.RemoveAt(set.Count - 1);
}
}
Console.WriteLine(ans.JoinNL());
}
}
public class RedBlackTree<T> : ICollection<T>, IReadOnlyCollection<T>
where T : IComparable<T>
{
private readonly bool isMulti_;
private readonly Stack<Node> pool_ = new Stack<Node>();
private Node root_;
public RedBlackTree(bool isMulti = false)
{
isMulti_ = isMulti;
}
public RedBlackTree(IEnumerable<T> collection, bool isMulti = false)
{
isMulti_ = isMulti;
var arr = InitializeArray(collection);
root_ = ConstructRootFromSortedArray(arr, 0, arr.Length - 1, null);
}
public T this[int index] => Find(index);
public int Count => CountOf(root_);
public T Min
{
get
{
if (root_ is null) {
return default;
}
var cur = root_;
while (cur.Left is null == false) {
cur = cur.Left;
}
return cur.Item;
}
}
public T Max
{
get
{
if (root_ is null) {
return default;
}
var cur = root_;
while (cur.Right is null == false) {
cur = cur.Right;
}
return cur.Item;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(T item) => FindNode(item) is null == false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int IndexOf(T item) => Index(FindNode(item));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Find(int index)
{
var node = FindNodeByIndex(index);
return node is null == false ? node.Item : default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
{
if (root_ is null) {
root_ = NewNode(item, false);
return;
}
Node current = root_;
Node parent = null;
Node grandParent = null;
Node greatGrandParent = null;
int order = 0;
while (current is null == false) {
order = item.CompareTo(current.Item);
if (order == 0 && isMulti_ == false) {
root_.IsRed = false;
return;
}
if (current.Is4Node()) {
current.Split4Node();
if (Node.IsNonNullRed(parent) == true) {
InsertionBalance(current, ref parent, grandParent, greatGrandParent);
}
}
greatGrandParent = grandParent;
grandParent = parent;
parent = current;
current = (order < 0) ? current.Left : current.Right;
}
Node node = NewNode(item, true);
if (order >= 0) {
parent.Right = node;
} else {
parent.Left = node;
}
if (parent.IsRed) {
InsertionBalance(node, ref parent, grandParent, greatGrandParent);
}
root_.IsRed = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAt(int index) => Remove(FindNodeByIndex(index).Item);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Remove(T item)
{
if (root_ is null) {
return false;
}
Node current = root_;
Node parent = null;
Node grandParent = null;
Node match = null;
Node parentOfMatch = null;
bool foundMatch = false;
while (current is null == false) {
if (current.Is2Node()) {
if (parent is null) {
current.IsRed = true;
} else {
Node sibling = parent.GetSibling(current);
if (sibling.IsRed) {
if (parent.Right == sibling) {
parent.RotateLeft();
} else {
parent.RotateRight();
}
parent.IsRed = true;
sibling.IsRed = false;
ReplaceChildOrRoot(grandParent, parent, sibling);
grandParent = sibling;
if (parent == match) {
parentOfMatch = sibling;
}
sibling = (parent.Left == current) ? parent.Right : parent.Left;
}
if (sibling.Is2Node()) {
parent.Merge2Nodes();
} else {
TreeRotation rotation = parent.GetRotation(current, sibling);
Node newGrandParent = parent.Rotate(rotation);
newGrandParent.IsRed = parent.IsRed;
parent.IsRed = false;
current.IsRed = true;
ReplaceChildOrRoot(grandParent, parent, newGrandParent);
if (parent == match) {
parentOfMatch = newGrandParent;
}
}
}
}
int order = foundMatch ? -1 : item.CompareTo(current.Item);
if (order == 0) {
foundMatch = true;
match = current;
parentOfMatch = parent;
}
grandParent = parent;
parent = current;
current = order < 0 ? current.Left : current.Right;
}
if (match is null == false) {
ReplaceNode(match, parentOfMatch, parent, grandParent);
match.Clear();
pool_.Push(match);
}
if (root_ is null == false) {
root_.IsRed = false;
}
return foundMatch;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
root_ = null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(T[] array, int arrayIndex)
{
foreach (var item in this) {
array[arrayIndex++] = item;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int index, T value) Prev((int index, T value) node)
=> (node.index - 1, Find(node.index - 1));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int index, T value) Next((int index, T value) node)
=> (node.index + 1, Find(node.index + 1));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int index, T value) LowerBound(T item) => BinarySearch(item, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int index, T value) UpperBound(T item) => BinarySearch(item, false);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int index, T value) BinarySearch(T item, bool isLowerBound)
{
Node right = null;
int ri = -1;
Node current = root_;
if (current is null) {
return (ri, default);
}
ri = 0;
while (current is null == false) {
var order = item.CompareTo(current.Item);
if (order < 0 || (isLowerBound && order == 0)) {
right = current;
current = current.Left;
} else {
ri += CountOf(current.Left) + 1;
current = current.Right;
}
}
return right is null == false ? (ri, right.Item) : (ri, default);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CountOf(Node node) => node is null ? 0 : node.Size;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Node NewNode(T item, bool isRed)
{
if (pool_.Count > 0) {
var node = pool_.Pop();
node.Item = item;
node.IsRed = isRed;
return node;
} else {
return new Node(item, isRed);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Node FindNode(T item)
{
Node current = root_;
while (current is null == false) {
int cmp = item.CompareTo(current.Item);
if (cmp == 0) {
return current;
}
current = cmp < 0 ? current.Left : current.Right;
}
return null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int Index(Node node)
{
if (node is null) {
return -1;
}
var ret = CountOf(node.Left);
Node prev = node;
node = node.Parent;
while (prev != root_) {
if (node.Left != prev) {
ret += CountOf(node.Left) + 1;
}
prev = node;
node = node.Parent;
}
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Node FindNodeByIndex(int index)
{
var current = root_;
var currentIndex = current.Size - CountOf(current.Right) - 1;
while (currentIndex != index) {
if (currentIndex > index) {
current = current.Left;
if (current is null) {
break;
}
currentIndex -= CountOf(current.Right) + 1;
} else {
current = current.Right;
if (current is null) {
break;
}
currentIndex += CountOf(current.Left) + 1;
}
}
return current;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T[] InitializeArray(IEnumerable<T> collection)
{
T[] array;
if (isMulti_) {
array = collection.ToArray();
Array.Sort(array, (x, y) => x.CompareTo(y));
} else {
var list = new List<T>(collection);
list.Sort((x, y) => x.CompareTo(y));
for (int i = list.Count - 1; i > 0; i--) {
if (list[i - 1].CompareTo(list[i]) == 0) {
list.RemoveAt(i);
}
}
array = list.ToArray();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Node ConstructRootFromSortedArray(T[] arr, int startIndex, int endIndex, Node redNode)
{
int size = endIndex - startIndex + 1;
Node root;
switch (size) {
case 0:
return null;
case 1:
root = NewNode(arr[startIndex], false);
if (redNode is null == false) {
root.Left = redNode;
}
break;
case 2:
root = NewNode(arr[startIndex], false);
root.Right = NewNode(arr[endIndex], true);
if (redNode is null == false) {
root.Left = redNode;
}
break;
case 3:
root = NewNode(arr[startIndex + 1], false);
root.Left = NewNode(arr[startIndex], false);
root.Right = NewNode(arr[endIndex], false);
if (redNode is null == false) {
root.Left.Left = redNode;
}
break;
default:
int midpt = ((startIndex + endIndex) / 2);
root = NewNode(arr[midpt], false);
root.Left = ConstructRootFromSortedArray(arr, startIndex, midpt - 1, redNode);
root.Right = size % 2 == 0
? ConstructRootFromSortedArray(arr, midpt + 2, endIndex, NewNode(arr[midpt + 1], true))
: ConstructRootFromSortedArray(arr, midpt + 1, endIndex, null);
break;
}
return root;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ReplaceNode(Node match, Node parentOfMatch, Node succesor, Node parentOfSuccesor)
{
if (succesor == match) {
succesor = match.Left;
} else {
if (succesor.Right is null == false) {
succesor.Right.IsRed = false;
}
if (parentOfSuccesor != match) {
parentOfSuccesor.Left = succesor.Right;
succesor.Right = match.Right;
}
succesor.Left = match.Left;
}
if (succesor is null == false) {
succesor.IsRed = match.IsRed;
}
ReplaceChildOrRoot(parentOfMatch, match, succesor);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void InsertionBalance(Node current, ref Node parent, Node grandParent, Node greatGrandParent)
{
bool parentIsOnRight = grandParent.Right == parent;
bool currentIsOnRight = parent.Right == current;
Node newChildOfGreatGrandParent;
if (parentIsOnRight == currentIsOnRight) {
newChildOfGreatGrandParent = currentIsOnRight
? grandParent.RotateLeft()
: grandParent.RotateRight();
} else {
newChildOfGreatGrandParent = currentIsOnRight
? grandParent.RotateLeftRight()
: grandParent.RotateRightLeft();
parent = greatGrandParent;
}
grandParent.IsRed = true;
newChildOfGreatGrandParent.IsRed = false;
ReplaceChildOrRoot(greatGrandParent, grandParent, newChildOfGreatGrandParent);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ReplaceChildOrRoot(Node parent, Node child, Node newChild)
{
if (parent is null == false) {
if (parent.Left == child) {
parent.Left = newChild;
} else {
parent.Right = newChild;
}
} else {
root_ = newChild;
}
}
bool ICollection<T>.IsReadOnly => false;
public IEnumerable<T> Reverse()
{
var e = new Enumerator(this, true);
while (e.MoveNext()) {
yield return e.Current;
}
}
public Enumerator GetEnumerator() => new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => new Enumerator(this);
private class Node
{
public bool IsRed { get; set; }
public T Item { get; set; }
public Node Parent { get; private set; }
private Node _Left;
public Node Left
{
get => _Left;
set
{
_Left = value;
Update(value);
}
}
private Node _Right;
public Node Right
{
get => _Right;
set
{
_Right = value;
Update(value);
}
}
public int Size { get; private set; } = 1;
public Node(T item, bool isRed)
{
Item = item;
IsRed = isRed;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
Parent = null;
_Left = null;
_Right = null;
Size = 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Update(Node child)
{
if (child is null == false) {
child.Parent = this;
}
for (var cur = this; cur is null == false; cur = cur.Parent) {
if (!cur.UpdateSize()) {
break;
}
if (cur.Parent is null == false
&& cur.Parent.Left != cur
&& cur.Parent.Right != cur) {
cur.Parent = null;
return;
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool UpdateSize()
{
var oldsize = Size;
var size = 1;
if (Left is null == false) {
size += Left.Size;
}
if (Right is null == false) {
size += Right.Size;
}
Size = size;
return oldsize != size;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNonNullRed(Node node) => node is null == false && node.IsRed;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNonNullBlack(Node node) => node is null == false && node.IsRed == false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrBlack(Node node) => node is null || node.IsRed == false;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Is2Node() => IsRed == false && IsNullOrBlack(Left) && IsNullOrBlack(Right);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Is4Node() => IsNonNullRed(Left) && IsNonNullRed(Right);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Merge2Nodes()
{
IsRed = false;
Left.IsRed = true;
Right.IsRed = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Split4Node()
{
IsRed = true;
Left.IsRed = false;
Right.IsRed = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TreeRotation GetRotation(Node current, Node sibling)
{
if (IsNonNullRed(sibling.Left)) {
if (Left == current) {
return TreeRotation.RightLeft;
}
return TreeRotation.Right;
} else {
if (Left == current) {
return TreeRotation.Left;
}
return TreeRotation.LeftRight;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node GetSibling(Node node)
{
return Left == node ? Right : Left;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node Rotate(TreeRotation rotation)
{
switch (rotation) {
default:
case TreeRotation.Right:
Left.Left.IsRed = false;
return RotateRight();
case TreeRotation.Left:
Right.Right.IsRed = false;
return RotateLeft();
case TreeRotation.RightLeft:
return RotateRightLeft();
case TreeRotation.LeftRight:
return RotateLeftRight();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node RotateLeft()
{
Node child = Right;
Right = child.Left;
child.Left = this;
return child;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node RotateLeftRight()
{
Node child = Left;
Node grandChild = child.Right;
Left = grandChild.Right;
grandChild.Right = this;
child.Right = grandChild.Left;
grandChild.Left = child;
return grandChild;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node RotateRight()
{
Node child = Left;
Left = child.Right;
child.Right = this;
return child;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Node RotateRightLeft()
{
Node child = Right;
Node grandChild = child.Left;
Right = grandChild.Left;
grandChild.Left = this;
child.Left = grandChild.Right;
grandChild.Right = child;
return grandChild;
}
public override string ToString() => $"size = {Size}, item = {Item}";
}
public enum TreeRotation : byte
{
Left = 1,
Right = 2,
RightLeft = 3,
LeftRight = 4,
}
public struct Enumerator : IEnumerator<T>
{
private readonly RedBlackTree<T> tree_;
private readonly Stack<Node> stack_;
private readonly bool reverse_;
private Node current_;
internal Enumerator(RedBlackTree<T> tree, bool reverse = false)
{
tree_ = tree;
stack_ = new Stack<Node>(2 * Log2(tree_.Count + 1));
current_ = null;
reverse_ = reverse;
Intialize(tree_.root_);
}
private void Intialize(Node startNode)
{
current_ = null;
Node node = startNode;
Node next;
while (node != null) {
next = (reverse_ ? node.Right : node.Left);
stack_.Push(node);
node = next;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Log2(int num) => num == 0 ? 0 : BitOperations.Log2((uint)num) + 1;
object System.Collections.IEnumerator.Current => Current;
public T Current => current_ is null ? default : current_.Item;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
if (stack_.Count == 0) {
current_ = null;
return false;
}
current_ = stack_.Pop();
var node = reverse_ ? current_.Left : current_.Right;
Node next;
while (node is null == false) {
next = reverse_ ? node.Right : node.Left;
stack_.Push(node);
node = next;
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset() => throw new NotSupportedException();
public void Dispose() { }
}
}
public struct BitFlag
{
public static BitFlag Begin() => 0;
public static BitFlag End(int bitCount) => 1 << bitCount;
public static BitFlag FromBit(int bitNumber) => 1 << bitNumber;
public static BitFlag Fill(int count) => (1 << count) - 1;
private readonly int flags_;
public int Flag => flags_;
public bool this[int bitNumber] => (flags_ & (1 << bitNumber)) != 0;
public BitFlag(int flags) { flags_ = flags; }
public bool Has(BitFlag target) => (flags_ & target.flags_) == target.flags_;
public bool Has(int target) => (flags_ & target) == target;
public bool HasBit(int bitNumber) => (flags_ & (1 << bitNumber)) != 0;
public BitFlag OrBit(int bitNumber) => (flags_ | (1 << bitNumber));
public BitFlag AndBit(int bitNumber) => (flags_ & (1 << bitNumber));
public BitFlag XorBit(int bitNumber) => (flags_ ^ (1 << bitNumber));
public BitFlag ComplementOf(BitFlag sub) => flags_ ^ sub.flags_;
public int PopCount() => BitOperations.PopCount((uint)flags_);
public static BitFlag operator ++(BitFlag src) => new BitFlag(src.flags_ + 1);
public static BitFlag operator --(BitFlag src) => new BitFlag(src.flags_ - 1);
public static BitFlag operator |(BitFlag lhs, BitFlag rhs)
=> new BitFlag(lhs.flags_ | rhs.flags_);
public static BitFlag operator |(BitFlag lhs, int rhs)
=> new BitFlag(lhs.flags_ | rhs);
public static BitFlag operator |(int lhs, BitFlag rhs)
=> new BitFlag(lhs | rhs.flags_);
public static BitFlag operator &(BitFlag lhs, BitFlag rhs)
=> new BitFlag(lhs.flags_ & rhs.flags_);
public static BitFlag operator &(BitFlag lhs, int rhs)
=> new BitFlag(lhs.flags_ & rhs);
public static BitFlag operator &(int lhs, BitFlag rhs)
=> new BitFlag(lhs & rhs.flags_);
public static bool operator <(BitFlag lhs, BitFlag rhs) => lhs.flags_ < rhs.flags_;
public static bool operator <(BitFlag lhs, int rhs) => lhs.flags_ < rhs;
public static bool operator <(int lhs, BitFlag rhs) => lhs < rhs.flags_;
public static bool operator >(BitFlag lhs, BitFlag rhs) => lhs.flags_ > rhs.flags_;
public static bool operator >(BitFlag lhs, int rhs) => lhs.flags_ > rhs;
public static bool operator >(int lhs, BitFlag rhs) => lhs > rhs.flags_;
public static bool operator <=(BitFlag lhs, BitFlag rhs) => lhs.flags_ <= rhs.flags_;
public static bool operator <=(BitFlag lhs, int rhs) => lhs.flags_ <= rhs;
public static bool operator <=(int lhs, BitFlag rhs) => lhs <= rhs.flags_;
public static bool operator >=(BitFlag lhs, BitFlag rhs) => lhs.flags_ >= rhs.flags_;
public static bool operator >=(BitFlag lhs, int rhs) => lhs.flags_ >= rhs;
public static bool operator >=(int lhs, BitFlag rhs) => lhs >= rhs.flags_;
public static implicit operator BitFlag(int t) => new BitFlag(t);
public static implicit operator int(BitFlag t) => t.flags_;
public override string ToString() => $"{Convert.ToString(flags_, 2).PadLeft(32, '0')} ({flags_})";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ForEachSubBits(Action<BitFlag> action)
{
for (BitFlag sub = (flags_ - 1) & flags_; sub > 0; sub = --sub & flags_) {
action(sub);
}
}
public SubBitsEnumerator SubBits => new SubBitsEnumerator(flags_);
public struct SubBitsEnumerator : IEnumerable<BitFlag>
{
private readonly int flags_;
public SubBitsEnumerator(int flags)
{
flags_ = flags;
}
IEnumerator<BitFlag> IEnumerable<BitFlag>.GetEnumerator() => new Enumerator(flags_);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(flags_);
public Enumerator GetEnumerator() => new Enumerator(flags_);
public struct Enumerator : IEnumerator<BitFlag>
{
private readonly int src_;
public BitFlag Current { get; private set; }
object IEnumerator.Current => Current;
public Enumerator(int flags)
{
src_ = flags;
Current = flags;
}
public void Dispose() { }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => (Current = --Current & src_) > 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset() => Current = src_;
}
}
}
public class HashMap<TKey, TValue> : Dictionary<TKey, TValue>
{
private readonly Func<TKey, TValue> initialzier_;
public HashMap(Func<TKey, TValue> initialzier)
: base()
{
initialzier_ = initialzier;
}
public HashMap(Func<TKey, TValue> initialzier, int capacity)
: base(capacity)
{
initialzier_ = initialzier;
}
new public TValue this[TKey key]
{
get
{
if (TryGetValue(key, out TValue value)) {
return value;
} else {
var init = initialzier_(key);
base[key] = init;
return init;
}
}
set { base[key] = value; }
}
public HashMap<TKey, TValue> Merge(
HashMap<TKey, TValue> src,
Func<TValue, TValue, TValue> mergeValues)
{
foreach (var key in src.Keys) {
this[key] = mergeValues(this[key], src[key]);
}
return this;
}
}
public class JagList2<T> where T : struct
{
private readonly int n_;
private readonly List<T>[] tempValues_;
private T[][] values_;
public int Count => n_;
public List<T>[] Raw => tempValues_;
public T[][] Values => values_;
public T[] this[int index] => values_[index];
public JagList2(int n)
{
n_ = n;
tempValues_ = new List<T>[n];
for (int i = 0; i < n; ++i) {
tempValues_[i] = new List<T>();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(int i, T value) => tempValues_[i].Add(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Build()
{
values_ = new T[n_][];
for (int i = 0; i < values_.Length; ++i) {
values_[i] = tempValues_[i].ToArray();
}
}
}
public class DijkstraQ
{
private int count_ = 0;
private long[] distanceHeap_;
private int[] vertexHeap_;
public int Count => count_;
public DijkstraQ()
{
distanceHeap_ = new long[8];
vertexHeap_ = new int[8];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(long distance, int v)
{
if (distanceHeap_.Length == count_) {
var newDistanceHeap = new long[distanceHeap_.Length << 1];
var newVertexHeap = new int[vertexHeap_.Length << 1];
Unsafe.CopyBlock(
ref Unsafe.As<long, byte>(ref newDistanceHeap[0]),
ref Unsafe.As<long, byte>(ref distanceHeap_[0]),
(uint)(8 * count_));
Unsafe.CopyBlock(
ref Unsafe.As<int, byte>(ref newVertexHeap[0]),
ref Unsafe.As<int, byte>(ref vertexHeap_[0]),
(uint)(4 * count_));
distanceHeap_ = newDistanceHeap;
vertexHeap_ = newVertexHeap;
}
ref var dRef = ref distanceHeap_[0];
ref var vRef = ref vertexHeap_[0];
Unsafe.Add(ref dRef, count_) = distance;
Unsafe.Add(ref vRef, count_) = v;
++count_;
int c = count_ - 1;
while (c > 0) {
int p = (c - 1) >> 1;
var tempD = Unsafe.Add(ref dRef, p);
if (tempD <= distance) {
break;
} else {
Unsafe.Add(ref dRef, c) = tempD;
Unsafe.Add(ref vRef, c) = Unsafe.Add(ref vRef, p);
c = p;
}
}
Unsafe.Add(ref dRef, c) = distance;
Unsafe.Add(ref vRef, c) = v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (long distance, int v) Dequeue()
{
ref var dRef = ref distanceHeap_[0];
ref var vRef = ref vertexHeap_[0];
(long distance, int v) ret = (dRef, vRef);
int n = count_ - 1;
var distance = Unsafe.Add(ref dRef, n);
var vertex = Unsafe.Add(ref vRef, n);
int p = 0;
int c = (p << 1) + 1;
while (c < n) {
if (c != n - 1 && Unsafe.Add(ref dRef, c + 1) < Unsafe.Add(ref dRef, c)) {
++c;
}
var tempD = Unsafe.Add(ref dRef, c);
if (distance > tempD) {
Unsafe.Add(ref dRef, p) = tempD;
Unsafe.Add(ref vRef, p) = Unsafe.Add(ref vRef, c);
p = c;
c = (p << 1) + 1;
} else {
break;
}
}
Unsafe.Add(ref dRef, p) = distance;
Unsafe.Add(ref vRef, p) = vertex;
--count_;
return ret;
}
}
public struct ModInt
{
//public const long P = 1000000007;
public const long P = 998244353;
//public const long P = 10;
public const long ROOT = 3;
// (924844033, 5)
// (998244353, 3)
// (1012924417, 5)
// (167772161, 3)
// (469762049, 3)
// (1224736769, 3)
private long value_;
public static ModInt New(long value, bool mods) => new ModInt(value, mods);
public ModInt(long value) => value_ = value;
public ModInt(long value, bool mods)
{
if (mods) {
value %= P;
if (value < 0) {
value += P;
}
}
value_ = value;
}
public static ModInt operator +(ModInt lhs, ModInt rhs)
{
lhs.value_ = (lhs.value_ + rhs.value_) % P;
return lhs;
}
public static ModInt operator +(long lhs, ModInt rhs)
{
rhs.value_ = (lhs + rhs.value_) % P;
return rhs;
}
public static ModInt operator +(ModInt lhs, long rhs)
{
lhs.value_ = (lhs.value_ + rhs) % P;
return lhs;
}
public static ModInt operator -(ModInt lhs, ModInt rhs)
{
lhs.value_ = (P + lhs.value_ - rhs.value_) % P;
return lhs;
}
public static ModInt operator -(long lhs, ModInt rhs)
{
rhs.value_ = (P + lhs - rhs.value_) % P;
return rhs;
}
public static ModInt operator -(ModInt lhs, long rhs)
{
lhs.value_ = (P + lhs.value_ - rhs) % P;
return lhs;
}
public static ModInt operator *(ModInt lhs, ModInt rhs)
{
lhs.value_ = lhs.value_ * rhs.value_ % P;
return lhs;
}
public static ModInt operator *(long lhs, ModInt rhs)
{
rhs.value_ = lhs * rhs.value_ % P;
return rhs;
}
public static ModInt operator *(ModInt lhs, long rhs)
{
lhs.value_ = lhs.value_ * rhs % P;
return lhs;
}
public static ModInt operator /(ModInt lhs, ModInt rhs)
=> lhs * Inverse(rhs);
public static implicit operator ModInt(long n) => new ModInt(n, true);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ModInt Inverse(ModInt value) => Pow(value, P - 2);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ModInt Pow(ModInt value, long k) => Pow(value.value_, k);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ModInt Pow(long value, long k)
{
long ret = 1;
while (k > 0) {
if ((k & 1) != 0) {
ret = ret * value % P;
}
value = value * value % P;
k >>= 1;
}
return new ModInt(ret);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long ToLong() => value_;
public override string ToString() => value_.ToString();
}
public static class Helper
{
public static long INF => 1L << 60;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Clamp<T>(this T value, T min, T max) where T : struct, IComparable<T>
{
if (value.CompareTo(min) <= 0) {
return min;
}
if (value.CompareTo(max) >= 0) {
return max;
}
return value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMin<T>(this ref T target, T value) where T : struct, IComparable<T>
=> target = target.CompareTo(value) > 0 ? value : target;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMin<T>(this ref T target, T value, Action<T> onUpdated)
where T : struct, IComparable<T>
{
if (target.CompareTo(value) > 0) {
target = value;
onUpdated(value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMax<T>(this ref T target, T value) where T : struct, IComparable<T>
=> target = target.CompareTo(value) < 0 ? value : target;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateMax<T>(this ref T target, T value, Action<T> onUpdated)
where T : struct, IComparable<T>
{
if (target.CompareTo(value) < 0) {
target = value;
onUpdated(value);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Array1<T>(int n, T initialValue) where T : struct
=> new T[n].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Array1<T>(int n, Func<int, T> initializer)
=> Enumerable.Range(0, n).Select(x => initializer(x)).ToArray();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Fill<T>(this T[] array, T value)
where T : struct
{
array.AsSpan().Fill(value);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Array2<T>(int n, int m, T initialValule) where T : struct
=> new T[n, m].Fill(initialValule);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Array2<T>(int n, int m, Func<int, int, T> initializer)
{
var array = new T[n, m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
array[i, j] = initializer(i, j);
}
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,] Fill<T>(this T[,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan<T>(ref array[0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(this T[,] array, int i)
=> MemoryMarshal.CreateSpan<T>(ref array[i, 0], array.GetLength(1));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,] Array3<T>(int n1, int n2, int n3, T initialValue)
where T : struct
=> new T[n1, n2, n3].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,] Fill<T>(this T[,,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan<T>(ref array[0, 0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(this T[,,] array, int i, int j)
=> MemoryMarshal.CreateSpan<T>(ref array[i, j, 0], array.GetLength(2));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,,] Array4<T>(int n1, int n2, int n3, int n4, T initialValue)
where T : struct
=> new T[n1, n2, n3, n4].Fill(initialValue);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[,,,] Fill<T>(this T[,,,] array, T initialValue)
where T : struct
{
MemoryMarshal.CreateSpan<T>(ref array[0, 0, 0, 0], array.Length).Fill(initialValue);
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(this T[,,,] array, int i, int j, int k)
=> MemoryMarshal.CreateSpan<T>(ref array[i, j, k, 0], array.GetLength(3));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Merge<T>(ReadOnlySpan<T> first, ReadOnlySpan<T> second) where T : IComparable<T>
{
var ret = new T[first.Length + second.Length];
int p = 0;
int q = 0;
while (p < first.Length || q < second.Length) {
if (p == first.Length) {
ret[p + q] = second[q];
q++;
continue;
}
if (q == second.Length) {
ret[p + q] = first[p];
p++;
continue;
}
if (first[p].CompareTo(second[q]) < 0) {
ret[p + q] = first[p];
p++;
} else {
ret[p + q] = second[q];
q++;
}
}
return ret;
}
private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DoIn4(int i, int j, int imax, int jmax, Action<int, int> action)
{
for (int dn = 0; dn < 4; ++dn) {
int d4i = i + delta4_[dn];
int d4j = j + delta4_[dn + 1];
if ((uint)d4i < (uint)imax && (uint)d4j < (uint)jmax) {
action(d4i, d4j);
}
}
}
private static readonly int[] delta8_ = { 1, 0, -1, 0, 1, 1, -1, -1, 1 };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void DoIn8(int i, int j, int imax, int jmax, Action<int, int> action)
{
for (int dn = 0; dn < 8; ++dn) {
int d8i = i + delta8_[dn];
int d8j = j + delta8_[dn + 1];
if ((uint)d8i < (uint)imax && (uint)d8j < (uint)jmax) {
action(d8i, d8j);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ForEachSubBits(int bit, Action<int> action)
{
for (int sub = bit; sub >= 0; --sub) {
sub &= bit;
action(sub);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Reverse(string src)
{
var chars = src.ToCharArray();
for (int i = 0, j = chars.Length - 1; i < j; ++i, --j) {
var tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
return new string(chars);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Swap(this string str, int i, int j)
{
var span = str.AsWriteableSpan();
(span[i], span[j]) = (span[j], span[i]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static char Replace(this string str, int index, char c)
{
var span = str.AsWriteableSpan();
char old = span[index];
span[index] = c;
return old;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<char> AsWriteableSpan(this string str)
{
var span = str.AsSpan();
return MemoryMarshal.CreateSpan(ref MemoryMarshal.GetReference(span), span.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Join<T>(this IEnumerable<T> values, string separator = "")
=> string.Join(separator, values);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string JoinNL<T>(this IEnumerable<T> values)
=> string.Join(Environment.NewLine, values);
}
public static class Extensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(this List<T> list)
{
return Unsafe.As<FakeList<T>>(list).Array.AsSpan(0, list.Count);
}
private class FakeList<T>
{
public T[] Array = null;
}
}
public class Scanner : IDisposable
{
private const int BUFFER_SIZE = 1024;
private const int ASCII_CHAR_BEGIN = 33;
private const int ASCII_CHAR_END = 126;
private readonly string filePath_;
private readonly Stream stream_;
private readonly byte[] buf_ = new byte[BUFFER_SIZE];
private int length_ = 0;
private int index_ = 0;
private bool isEof_ = false;
public Scanner(string file = "")
{
if (string.IsNullOrWhiteSpace(file)) {
stream_ = Console.OpenStandardInput();
} else {
filePath_ = file;
stream_ = new FileStream(file, FileMode.Open);
}
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {
AutoFlush = false
});
}
public void Dispose()
{
Console.Out.Flush();
stream_.Dispose();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public char Char()
{
byte b;
do {
b = Read();
} while (b < ASCII_CHAR_BEGIN || ASCII_CHAR_END < b);
return (char)b;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string Next()
{
var sb = new StringBuilder();
for (var b = Char(); b >= ASCII_CHAR_BEGIN && b <= ASCII_CHAR_END; b = (char)Read()) {
sb.Append(b);
}
return sb.ToString();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string[] ArrayString(int length)
{
var array = new string[length];
for (int i = 0; i < length; ++i) {
array[i] = Next();
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Int() => (int)Long();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Int(int offset) => Int() + offset;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int, int) Int2(int offset = 0)
=> (Int(offset), Int(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int, int, int) Int3(int offset = 0)
=> (Int(offset), Int(offset), Int(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (int, int, int, int) Int4(int offset = 0)
=> (Int(offset), Int(offset), Int(offset), Int(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int[] ArrayInt(int length, int offset = 0)
{
var array = new int[length];
for (int i = 0; i < length; ++i) {
array[i] = Int(offset);
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Long()
{
long ret = 0;
byte b;
bool ng = false;
do {
b = Read();
} while (b != '-' && (b < '0' || '9' < b));
if (b == '-') {
ng = true;
b = Read();
}
for (; true; b = Read()) {
if (b < '0' || '9' < b) {
return ng ? -ret : ret;
} else {
ret = ret * 10 + b - '0';
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Long(long offset) => Long() + offset;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (long, long) Long2(long offset = 0)
=> (Long(offset), Long(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (long, long, long) Long3(long offset = 0)
=> (Long(offset), Long(offset), Long(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (long, long, long, long) Long4(long offset = 0)
=> (Long(offset), Long(offset), Long(offset), Long(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long[] ArrayLong(int length, long offset = 0)
{
var array = new long[length];
for (int i = 0; i < length; ++i) {
array[i] = Long(offset);
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BigInteger Big() => new BigInteger(Long());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BigInteger Big(long offset) => Big() + offset;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (BigInteger, BigInteger) Big2(long offset = 0)
=> (Big(offset), Big(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (BigInteger, BigInteger, BigInteger) Big3(long offset = 0)
=> (Big(offset), Big(offset), Big(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (BigInteger, BigInteger, BigInteger, BigInteger) Big4(long offset = 0)
=> (Big(offset), Big(offset), Big(offset), Big(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public BigInteger[] ArrayBig(int length, long offset = 0)
{
var array = new BigInteger[length];
for (int i = 0; i < length; ++i) {
array[i] = Big(offset);
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Double() => double.Parse(Next(), CultureInfo.InvariantCulture);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Double(double offset) => Double() + offset;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (double, double) Double2(double offset = 0)
=> (Double(offset), Double(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (double, double, double) Double3(double offset = 0)
=> (Double(offset), Double(offset), Double(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (double, double, double, double) Double4(double offset = 0)
=> (Double(offset), Double(offset), Double(offset), Double(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double[] ArrayDouble(int length, double offset = 0)
{
var array = new double[length];
for (int i = 0; i < length; ++i) {
array[i] = Double(offset);
}
return array;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public decimal Decimal() => decimal.Parse(Next(), CultureInfo.InvariantCulture);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public decimal Decimal(decimal offset) => Decimal() + offset;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (decimal, decimal) Decimal2(decimal offset = 0)
=> (Decimal(offset), Decimal(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (decimal, decimal, decimal) Decimal3(decimal offset = 0)
=> (Decimal(offset), Decimal(offset), Decimal(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (decimal, decimal, decimal, decimal) Decimal4(decimal offset = 0)
=> (Decimal(offset), Decimal(offset), Decimal(offset), Decimal(offset));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public decimal[] ArrayDecimal(int length, decimal offset = 0)
{
var array = new decimal[length];
for (int i = 0; i < length; ++i) {
array[i] = Decimal(offset);
}
return array;
}
private byte Read()
{
if (isEof_) {
throw new EndOfStreamException();
}
if (index_ >= length_) {
index_ = 0;
if ((length_ = stream_.Read(buf_, 0, BUFFER_SIZE)) <= 0) {
isEof_ = true;
return 0;
}
}
return buf_[index_++];
}
public void Save(string text)
{
if (string.IsNullOrWhiteSpace(filePath_)) {
return;
}
File.WriteAllText(filePath_ + "_output.txt", text);
}
}
}
takytank