結果
問題 | No.1675 Strange Minimum Query |
ユーザー | takytank |
提出日時 | 2021-09-10 22:02:26 |
言語 | C# (.NET 8.0.203) |
結果 |
AC
|
実行時間 | 353 ms / 2,000 ms |
コード長 | 36,736 bytes |
コンパイル時間 | 9,970 ms |
コンパイル使用メモリ | 167,684 KB |
実行使用メモリ | 212,808 KB |
最終ジャッジ日時 | 2024-06-12 00:08:02 |
合計ジャッジ時間 | 22,684 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
27,776 KB |
testcase_01 | AC | 47 ms
28,544 KB |
testcase_02 | AC | 50 ms
27,776 KB |
testcase_03 | AC | 197 ms
34,688 KB |
testcase_04 | AC | 194 ms
36,096 KB |
testcase_05 | AC | 104 ms
34,432 KB |
testcase_06 | AC | 216 ms
35,584 KB |
testcase_07 | AC | 237 ms
36,864 KB |
testcase_08 | AC | 52 ms
27,776 KB |
testcase_09 | AC | 53 ms
28,008 KB |
testcase_10 | AC | 114 ms
46,592 KB |
testcase_11 | AC | 100 ms
38,912 KB |
testcase_12 | AC | 122 ms
47,232 KB |
testcase_13 | AC | 251 ms
55,936 KB |
testcase_14 | AC | 224 ms
50,040 KB |
testcase_15 | AC | 254 ms
51,456 KB |
testcase_16 | AC | 46 ms
24,192 KB |
testcase_17 | AC | 105 ms
35,328 KB |
testcase_18 | AC | 121 ms
35,584 KB |
testcase_19 | AC | 150 ms
45,824 KB |
testcase_20 | AC | 243 ms
46,072 KB |
testcase_21 | AC | 227 ms
47,232 KB |
testcase_22 | AC | 287 ms
48,248 KB |
testcase_23 | AC | 222 ms
38,144 KB |
testcase_24 | AC | 213 ms
40,064 KB |
testcase_25 | AC | 165 ms
36,096 KB |
testcase_26 | AC | 120 ms
37,632 KB |
testcase_27 | AC | 211 ms
47,360 KB |
testcase_28 | AC | 142 ms
44,264 KB |
testcase_29 | AC | 124 ms
43,008 KB |
testcase_30 | AC | 119 ms
37,888 KB |
testcase_31 | AC | 271 ms
40,576 KB |
testcase_32 | AC | 353 ms
49,536 KB |
testcase_33 | AC | 350 ms
49,408 KB |
testcase_34 | AC | 353 ms
49,408 KB |
testcase_35 | AC | 263 ms
50,176 KB |
testcase_36 | AC | 266 ms
212,808 KB |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (110 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(); var (n, q) = cin.Int2(); var query = new (int l, int r, int b)[q]; for (int i = 0; i < q; i++) { var (l, r) = cin.Int2(); --l; int b = cin.Int(); query[i] = (l, r, b); } var seg1 = new DualSegmentTree<int>( n, 1, (x, y) => Math.Max(x, y)); for (int i = 0; i < q; i++) { var (l, r, b) = query[i]; seg1.Update(l, r, b); } var a = new int[n]; for (int i = 0; i < n; i++) { a[i] = seg1[i]; } var seg2 = new SegmentTree<int>(a, int.MaxValue, (x, y) => Math.Min(x, y)); for (int i = 0; i < q; i++) { var (l, r, b) = query[i]; int min = seg2.Query(l, r); if (min != b) { Console.WriteLine(-1); return; } } Console.WriteLine(a.Join(" ")); } } [DebuggerTypeProxy(typeof(SegmentTree<>.DebugView))] public class SegmentTree<T> : IEnumerable<T> { private readonly int n_; private readonly T unit_; private readonly T[] tree_; private readonly Func<T, T, T> operate_; public int Count { get; } public T Top => tree_[1]; public T this[int index] { get => tree_[index + n_]; set => Update(index, value); } public SegmentTree(int count, T unit, Func<T, T, T> operate) { operate_ = operate; unit_ = unit; Count = count; n_ = 1; while (n_ < count) { n_ <<= 1; } tree_ = new T[n_ << 1]; for (int i = 0; i < tree_.Length; i++) { tree_[i] = unit; } } public SegmentTree(T[] src, T unit, Func<T, T, T> operate) : this(src.Length, unit, operate) { for (int i = 0; i < src.Length; ++i) { tree_[i + n_] = src[i]; } for (int i = n_ - 1; i > 0; --i) { tree_[i] = operate_(tree_[i << 1], tree_[(i << 1) + 1]); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int index, T value) { if (index >= Count) { return; } index += n_; tree_[index] = value; index >>= 1; while (index != 0) { tree_[index] = operate_(tree_[index << 1], tree_[(index << 1) + 1]); index >>= 1; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query(Range range) => Query(range.Start.Value, range.End.Value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query(int left, int right) { if (left > right || right < 0 || left >= Count) { return unit_; } int l = left + n_; int r = right + n_; T valL = unit_; T valR = unit_; while (l < r) { if ((l & 1) != 0) { valL = operate_(valL, tree_[l]); ++l; } if ((r & 1) != 0) { --r; valR = operate_(tree_[r], valR); } l >>= 1; r >>= 1; } return operate_(valL, valR); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindLeftest(Range range, Func<T, bool> check) => FindLeftest(range.Start.Value, range.End.Value, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindLeftest(int left, int right, Func<T, bool> check) => FindLeftestCore(left, right, 1, 0, n_, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] private int FindLeftestCore(int left, int right, int v, int l, int r, Func<T, bool> check) { if (check(tree_[v]) == false || r <= left || right <= l || Count <= left) { return right; } else if (v >= n_) { return v - n_; } else { int lc = v << 1; int rc = (v << 1) + 1; int mid = (l + r) >> 1; int vl = FindLeftestCore(left, right, lc, l, mid, check); if (vl != right) { return vl; } else { return FindLeftestCore(left, right, rc, mid, r, check); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindRightest(Range range, Func<T, bool> check) => FindRightest(range.Start.Value, range.End.Value, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindRightest(int left, int right, Func<T, bool> check) => FindRightestCore(left, right, 1, 0, n_, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] private int FindRightestCore(int left, int right, int v, int l, int r, Func<T, bool> check) { if (check(tree_[v]) == false || r <= left || right <= l || Count <= left) { return left - 1; } else if (v >= n_) { return v - n_; } else { int lc = v << 1; int rc = (v << 1) + 1; int mid = (l + r) >> 1; int vr = FindRightestCore(left, right, rc, mid, r, check); if (vr != left - 1) { return vr; } else { return FindRightestCore(left, right, lc, l, mid, check); } } } public IEnumerator<T> GetEnumerator() { for (int i = 0; i < Count; i++) { yield return this[i]; } } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); [DebuggerDisplay("data= {" + nameof(data_) + "}", Name = "{" + nameof(key_) + ",nq}")] private struct DebugItem { [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly string key_; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly T data_; public DebugItem(int l, int r, T data) { if (r - l == 1) { key_ = $"[{l}]"; } else { key_ = $"[{l}-{r})"; } data_ = data; } } private class DebugView { private readonly SegmentTree<T> tree_; public DebugView(SegmentTree<T> tree) { tree_ = tree; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public DebugItem[] Items { get { var items = new List<DebugItem>(tree_.Count); int length = tree_.n_; while (length > 0) { int unit = tree_.n_ / length; for (int i = 0; i < length; i++) { int l = i * unit; int r = l + unit; if (l < tree_.Count) { int dataIndex = i + length; items.Add(new DebugItem( l, r, tree_.tree_[dataIndex])); } } length >>= 1; } return items.ToArray(); } } } } [DebuggerTypeProxy(typeof(DualSegmentTree<>.DebugView))] public class DualSegmentTree<T> : IEnumerable<T> { private readonly int n_; private readonly int height_; private readonly T unit_; private readonly T[] tree_; private readonly bool[] should_; private readonly Func<T, T, T> operate_; public int Count { get; } public T this[int i] => Query(i); public DualSegmentTree(int count, T unit, Func<T, T, T> operate) { operate_ = operate; unit_ = unit; Count = count; n_ = 1; height_ = 0; while (n_ < count) { n_ <<= 1; ++height_; } tree_ = new T[n_ << 1]; should_ = new bool[n_ << 1]; for (int i = 0; i < tree_.Length; i++) { tree_[i] = unit; } } public DualSegmentTree(IReadOnlyList<T> src, T unit, Func<T, T, T> operate) : this(src.Count, unit, operate) { for (int i = 0; i < src.Count; i++) { tree_[i + n_] = src[i]; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public T Query(int i) { int l = 0; int r = n_; int k = 1; while (r - l > 1) { Propagate(k); int m = (l + r) / 2; if (i < m) { r = m; k <<= 1; } else { l = m; k = (k << 1) + 1; } } return tree_[k]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(Range range, T value) => Update(range.Start.Value, range.End.Value, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Update(int left, int right, T value) { if (left > right || right < 0 || left >= Count) { return; } int l = left + n_; int r = right + n_; PropagateTopDown(l); PropagateTopDown(r - 1); while (l < r) { if ((l & 1) != 0) { tree_[l] = operate_(value, tree_[l]); should_[l] = true; ++l; } if ((r & 1) != 0) { --r; tree_[r] = operate_(value, tree_[r]); should_[r] = true; } l >>= 1; r >>= 1; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindLeftest(Range range, Func<T, bool> check) => FindLeftest(range.Start.Value, range.End.Value, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindLeftest(int left, int right, Func<T, bool> check) => FindLeftestCore(left, right, 1, 0, n_, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] private int FindLeftestCore(int left, int right, int v, int l, int r, Func<T, bool> check) { Propagate(v); if (check(tree_[v]) == false || r <= left || right <= l || Count <= left) { return right; } else if (v >= n_) { return v - n_; } else { int lc = v << 1; int rc = (v << 1) + 1; int mid = (l + r) >> 1; int vl = FindLeftestCore(left, right, lc, l, mid, check); if (vl != right) { return vl; } else { return FindLeftestCore(left, right, rc, mid, r, check); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindRightest(Range range, Func<T, bool> check) => FindRightest(range.Start.Value, range.End.Value, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] public int FindRightest(int left, int right, Func<T, bool> check) => FindRightestCore(left, right, 1, 0, n_, check); [MethodImpl(MethodImplOptions.AggressiveInlining)] private int FindRightestCore(int left, int right, int v, int l, int r, Func<T, bool> check) { Propagate(v); if (check(tree_[v]) == false || r <= left || right <= l || Count <= left) { return left - 1; } else if (v >= n_) { return v - n_; } else { int lc = v << 1; int rc = (v << 1) + 1; int mid = (l + r) >> 1; int vr = FindRightestCore(left, right, rc, mid, r, check); if (vr != left - 1) { return vr; } else { return FindRightestCore(left, right, lc, l, mid, check); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void PropagateTopDown(int v) { for (int i = height_; i > 0; i--) { Propagate(v >> i); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void Propagate(int v) { if (should_[v]) { if (v < n_) { int lc = v << 1; int rc = (v << 1) + 1; tree_[lc] = operate_(tree_[v], tree_[lc]); should_[lc] = true; tree_[rc] = operate_(tree_[v], tree_[rc]); should_[rc] = true; tree_[v] = unit_; } should_[v] = false; } } public IEnumerator<T> GetEnumerator() { for (int i = 0; i < Count; i++) { yield return this[i]; } } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); [DebuggerDisplay("data= {" + nameof(data_) + "}", Name = "{" + nameof(key_) + ",nq}")] private struct DebugItem { [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly string key_; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly T data_; public DebugItem(int l, int r, T data) { if (r - l == 1) { key_ = $"[{l}]"; } else { key_ = $"[{l}-{r})"; } data_ = data; } } private class DebugView { private readonly DualSegmentTree<T> tree_; public DebugView(DualSegmentTree<T> tree) { tree_ = tree; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public DebugItem[] Items { get { var items = new List<DebugItem>(tree_.Count); int length = tree_.n_; while (length > 0) { int unit = tree_.n_ / length; for (int i = 0; i < length; i++) { int l = i * unit; int r = l + unit; if (l < tree_.Count) { int dataIndex = i + length; items.Add(new DebugItem( l, r, tree_.tree_[dataIndex])); } } length >>= 1; } return items.ToArray(); } } } } 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 = 2; 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); } } }