/** * */ // header {{{ #include using namespace std; using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; #ifdef __SIZEOF_INT128__ using i128 = __int128; using u128 = unsigned __int128; #endif using f32 = float; using f64 = double; using f80 = __float80; using f128 = __float128; // }}} template constexpr T PROCON_INF(); template<> constexpr i64 PROCON_INF() { return 1'010'000'000'000'000'000LL; } template<> constexpr f64 PROCON_INF() { return 1e100; } constexpr i64 INF = PROCON_INF(); constexpr f64 FINF = PROCON_INF(); constexpr i64 MOD = 1'000'000'007LL; constexpr f64 EPS = 1e-12; constexpr f64 PI = 3.14159265358979323846; // util {{{ #define FOR(i, start, end) for(i64 i = (start), i##_end=(end); i < i##_end; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c)) #define SLICE(f,c,l,r,...) (([&](decltype((c)) cccc, decltype((l)) llll, decltype((r)) rrrr) {\ auto iiii = llll <= rrrr ? std::begin(cccc)+llll : std::end(cccc);\ auto jjjj = llll <= rrrr ? std::begin(cccc)+rrrr : std::end(cccc);\ return (f)(iiii, jjjj, ## __VA_ARGS__);\ })(c,l,r)) #define GENERIC(f) ([](auto&&... args) -> decltype(auto) { return (f)(std::forward(args)...); }) template class FixPoint { public: explicit constexpr FixPoint(F&& f) : f_(forward(f)) {} template constexpr decltype(auto) operator()(Args&&... args) const { return f_(*this, forward(args)...); } private: const F f_; }; template decltype(auto) FIX(F&& f) { return FixPoint(forward(f)); } template i64 SIZE(const C& c) { return static_cast(c.size()); } template i64 SIZE(const T (&)[N]) { return static_cast(N); } bool is_odd (i64 x) { return x % 2 != 0; } bool is_even(i64 x) { return x % 2 == 0; } template i64 cmp(T x, T y) { return (y i64 sgn(T x) { return cmp(x, T(0)); } i64 ipow(i64 x, i64 e) { assert(e >= 0); i64 res = 1; REP(_, e) { res *= x; } return res; } // Haskell の divMod と同じ pair divmod(i64 a, i64 b) { i64 q = a / b; i64 r = a % b; if((b>0 && r<0) || (b<0 && r>0)) { --q; r += b; } return {q,r}; } i64 div_ceil(i64 a, i64 b) { i64 q = a / b; i64 r = a % b; if((b>0 && r>0) || (b<0 && r<0)) ++q; return q; } i64 div_floor(i64 a, i64 b) { return divmod(a,b).first; } i64 modulo(i64 a, i64 b) { return divmod(a,b).second; } bool feq(f64 x, f64 y, f64 eps=EPS) { return fabs(x-y) < eps; } template bool chmax(T& xmax, const U& x) { if(xmax < x) { xmax = x; return true; } return false; } template bool chmin(T& xmin, const U& x) { if(x < xmin) { xmin = x; return true; } return false; } template> ForwardIt bsearch_find(ForwardIt first, ForwardIt last, const T& x, Comp comp={}) { auto it = lower_bound(first, last, x, comp); if(it == last || comp(x,*it)) return last; return it; } // x 未満の最後の要素 template> BidiIt bsearch_lt(BidiIt first, BidiIt last, const T& x, Comp comp={}) { auto it = lower_bound(first, last, x, comp); if(it == first) return last; return prev(it); } // x 以下の最後の要素 template> BidiIt bsearch_le(BidiIt first, BidiIt last, const T& x, Comp comp={}) { auto it = upper_bound(first, last, x, comp); if(it == first) return last; return prev(it); } // x より大きい最初の要素 template> BidiIt bsearch_gt(BidiIt first, BidiIt last, const T& x, Comp comp={}) { return upper_bound(first, last, x, comp); } // x 以上の最初の要素 template> BidiIt bsearch_ge(BidiIt first, BidiIt last, const T& x, Comp comp={}) { return lower_bound(first, last, x, comp); } template auto SUM(InputIt first, InputIt last) { using T = typename iterator_traits::value_type; return accumulate(first, last, T()); } template ForwardIt transform_self(ForwardIt first, ForwardIt last, UnaryOperation op) { return transform(first, last, first, op); } template void UNIQ(C& c) { c.erase(ALL(unique,c), end(c)); } template enable_if_t::value==0> ARRAY_FOREACH(T& e, F f) { f(e); } template enable_if_t::value!=0> ARRAY_FOREACH(Array& ary, F f) { for(auto& e : ary) ARRAY_FOREACH(e, f); } template enable_if_t::value!=0> ARRAY_FILL(Array& ary, const U& v) { ARRAY_FOREACH(ary, [&v](auto& e) { e = v; }); } template auto ON(BinaryFunc bf, UnaryFunc uf) { return [bf,uf](const auto& x, const auto& y) { return bf(uf(x), uf(y)); }; } template auto LT_ON(F f) { return ON(less<>(), f); } template auto GT_ON(F f) { return ON(greater<>(), f); } struct IDENTITY { template constexpr T&& operator()(T&& x) const noexcept { return forward(x); } }; char digit_chr(i64 n) { return static_cast('0' + n); } i64 digit_ord(char c) { return c - '0'; } char lower_chr(i64 n) { return static_cast('a' + n); } i64 lower_ord(char c) { return c - 'a'; } char upper_chr(i64 n) { return static_cast('A' + n); } i64 upper_ord(char c) { return c - 'A'; } // 出力は operator<< を直接使わず、このテンプレート経由で行う // 提出用出力とデバッグ用出力を分けるため template struct Formatter { static ostream& write_str(ostream& out, const T& x) { return out << x; } static ostream& write_repr(ostream& out, const T& x) { return out << x; } }; template ostream& WRITE_STR(ostream& out, const T& x) { return Formatter::write_str(out, x); } template ostream& WRITE_REPR(ostream& out, const T& x) { return Formatter::write_repr(out, x); } template ostream& WRITE_JOIN_STR(ostream& out, InputIt first, InputIt last, const string& sep) { while(first != last) { WRITE_STR(out, *first++); if(first != last) out << sep; } return out; } template ostream& WRITE_JOIN_REPR(ostream& out, InputIt first, InputIt last, const string& sep) { while(first != last) { WRITE_REPR(out, *first++); if(first != last) out << sep; } return out; } template ostream& WRITE_RANGE_STR(ostream& out, InputIt first, InputIt last) { return WRITE_JOIN_STR(out, first, last, " "); } template ostream& WRITE_RANGE_REPR(ostream& out, InputIt first, InputIt last) { out << "["; WRITE_JOIN_REPR(out, first, last, ", "); out << "]"; return out; } template void FROM_STR(const string& s, T& x) { istringstream in(s); in >> x; } template string TO_STR(const T& x) { ostringstream out; WRITE_STR(out, x); return out.str(); } template string TO_REPR(const T& x) { ostringstream out; WRITE_REPR(out, x); return out.str(); } template string RANGE_TO_STR(InputIt first, InputIt last) { ostringstream out; WRITE_RANGE_STR(out, first, last); return out.str(); } template string RANGE_TO_REPR(InputIt first, InputIt last) { ostringstream out; WRITE_RANGE_REPR(out, first, last); return out.str(); } template string JOIN(InputIt first, InputIt last, const string& sep) { ostringstream out; WRITE_JOIN_STR(out, first, last, sep); return out.str(); } template<> struct Formatter { static ostream& write_str(ostream& out, i64 x) { return out << x; } static ostream& write_repr(ostream& out, i64 x) { if(x == INF) return out << "INF"; if(x == -INF) return out << "-INF"; return out << x; } }; template<> struct Formatter { static ostream& write_str(ostream& out, f64 x) { return out << x; } static ostream& write_repr(ostream& out, f64 x) { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" if(x == FINF) return out << "FINF"; if(x == -FINF) return out << "-FINF"; #pragma GCC diagnostic pop return out << x; } }; template struct Formatter> { static ostream& write_str(ostream& out, const vector& v) { return WRITE_RANGE_STR(out, begin(v), end(v)); } static ostream& write_repr(ostream& out, const vector& v) { out << "vector"; return WRITE_RANGE_REPR(out, begin(v), end(v)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const pair& p) { WRITE_STR(out, p.first); out << ' '; WRITE_STR(out, p.second); return out; } static ostream& write_repr(ostream& out, const pair& p) { out << "("; WRITE_REPR(out, p.first); out << ","; WRITE_REPR(out, p.second); out << ")"; return out; } }; template struct Formatter> { template = nullptr> static ostream& write_str_impl(ostream& out, const tuple&) { return out; } template = nullptr> static ostream& write_str_impl(ostream& out, const tuple& t) { if(I != 0) out << ' '; WRITE_STR(out, get(t)); return write_str_impl(out, t); } template = nullptr> static ostream& write_repr_impl(ostream& out, const tuple&) { if(sizeof...(TS) == 0) out << "("; return out << ")"; } template = nullptr> static ostream& write_repr_impl(ostream& out, const tuple& t) { if(I == 0) out << "("; else out << ","; WRITE_REPR(out, get(t)); return write_repr_impl(out, t); } static ostream& write_str(ostream& out, const tuple& t) { return write_str_impl(out, t); } static ostream& write_repr(ostream& out, const tuple& t) { return write_repr_impl(out, t); } }; template void RD(T& x) { cin >> x; #ifdef PROCON_LOCAL assert(cin); #endif } template void RD(vector& v, i64 n) { v.reserve(n); REP(_, n) { T e; RD(e); v.emplace_back(e); } } void PRINT() {} template void PRINT(const T& x, const TS& ...args) { WRITE_STR(cout, x); if(sizeof...(args)) { cout << ' '; PRINT(args...); } } template void PRINTLN(const TS& ...args) { PRINT(args...); cout << '\n'; } [[noreturn]] void EXIT() { #ifdef PROCON_LOCAL cerr.flush(); #endif cout.flush(); _Exit(0); } template void DBG_IMPL(i64 line, const char* expr, const T& value) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr << " = "; WRITE_REPR(cerr, value); cerr << "\n"; #endif } template void DBG_ARRAY_IMPL(i64 line, const char* expr, const T (&ary)[N]) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr << " = "; WRITE_RANGE_REPR(cerr, begin(ary), end(ary)); cerr << "\n"; #endif } template void DBG_RANGE_IMPL(i64 line, const char* expr1, const char* expr2, InputIt first, InputIt last) { #ifdef PROCON_LOCAL cerr << "[L " << line << "]: "; cerr << expr1 << "," << expr2 << " = "; WRITE_RANGE_REPR(cerr, first, last); cerr << "\n"; #endif } #define DBG(expr) DBG_IMPL(__LINE__, #expr, (expr)) #define DBG_ARRAY(expr) DBG_ARRAY_IMPL(__LINE__, #expr, (expr)) #define DBG_RANGE(first,last) DBG_RANGE_IMPL(__LINE__, #first, #last, (first), (last)) #define PAIR make_pair #define TUPLE make_tuple // }}} // init {{{ struct ProconInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; ProconInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); #ifdef PROCON_LOCAL cerr << fixed << setprecision(IOS_PREC); #endif if(AUTOFLUSH) cout << unitbuf; } } PROCON_INIT; // }}} // container {{{ // BoolArray {{{ class BoolArray { public: using value_type = bool; using reference = value_type&; using const_reference = const value_type&; using iterator = value_type*; using const_iterator = const value_type*; using difference_type = ptrdiff_t; using size_type = size_t; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; BoolArray() : BoolArray(0) {} explicit BoolArray(size_t n) : BoolArray(n,false) {} BoolArray(size_t n, bool value) : size_(n), data_(new bool[n]) { ALL(fill, *this, value); } BoolArray(initializer_list init) : size_(init.size()), data_(new bool[size_]) { ALL(copy, init, begin()); } template BoolArray(InputIt first, InputIt last) { deque tmp(first, last); size_ = tmp.size(); data_ = new bool[size_]; ALL(copy, tmp, begin()); } BoolArray(const BoolArray& other) : size_(other.size_), data_(new bool[size_]) { ALL(copy, other, begin()); } BoolArray(BoolArray&& other) noexcept : size_(other.size_), data_(other.data_) { other.data_ = nullptr; } BoolArray& operator=(const BoolArray& other) { if(this == &other) return *this; if(!data_ || size_ < other.size_) { delete[] data_; data_ = new bool[other.size_]; } size_ = other.size_; ALL(copy, other, begin()); return *this; } BoolArray& operator=(BoolArray&& other) noexcept { if(this == &other) return *this; size_ = other.size_; data_ = other.data_; other.data_ = nullptr; } BoolArray& operator=(initializer_list init) { if(!data_ || size_ < init.size()) { delete[] data_; data_ = new bool[init.size()]; } size_ = init.size(); ALL(copy, init, begin()); return *this; } void swap(BoolArray& other) noexcept { std::swap(size_, other.size_); std::swap(data_, other.data_); } ~BoolArray() { delete[] data_; data_ = nullptr; } bool empty() const noexcept { return size_ == 0; } size_type size() const noexcept { return size_; } size_type max_size() const noexcept { return 1'010'000'000; } iterator begin() noexcept { return data_; } const_iterator begin() const noexcept { return data_; } const_iterator cbegin() const noexcept { return data_; } iterator end() noexcept { return data_+size_; } const_iterator end() const noexcept { return data_+size_; } const_iterator cend() const noexcept { return data_+size_; } reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } reverse_iterator rend() noexcept { return reverse_iterator(begin()); } const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } reference operator[](size_type pos) { return data_[pos]; } const_reference operator[](size_type pos) const { return data_[pos]; } bool* data() noexcept { return data_; } const bool* data() const noexcept { return data_; } private: size_t size_; bool* data_; }; void swap(BoolArray& lhs, BoolArray& rhs) noexcept { lhs.swap(rhs); } bool operator==(const BoolArray& lhs, const BoolArray& rhs) { return equal(begin(lhs), end(lhs), begin(rhs), end(rhs)); } bool operator!=(const BoolArray& lhs, const BoolArray& rhs) { return !(lhs == rhs); } bool operator<(const BoolArray& lhs, const BoolArray& rhs) { return lexicographical_compare(begin(lhs), end(lhs), begin(rhs), end(rhs)); } bool operator> (const BoolArray& lhs, const BoolArray& rhs) { return rhs < lhs; } bool operator<=(const BoolArray& lhs, const BoolArray& rhs) { return !(rhs < lhs); } bool operator>=(const BoolArray& lhs, const BoolArray& rhs) { return !(lhs < rhs); } // }}} // set/map/multiset/multimap search {{{ // set {{{ template auto set_lt(set& s, const T& x) { auto it = s.lower_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_lt(const set& s, const T& x) { auto it = s.lower_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_le(set& s, const T& x) { auto it = s.upper_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_le(const set& s, const T& x) { auto it = s.upper_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_gt(set& s, const T& x) { return s.upper_bound(x); } template auto set_gt(const set& s, const T& x) { return s.upper_bound(x); } template auto set_ge(set& s, const T& x) { return s.lower_bound(x); } template auto set_ge(const set& s, const T& x) { return s.lower_bound(x); } // }}} // map {{{ template auto map_lt(map& m, const K& x) { auto it = m.lower_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_lt(const map& m, const K& x) { auto it = m.lower_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_le(map& m, const K& x) { auto it = m.upper_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_le(const map& m, const K& x) { auto it = m.upper_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_gt(map& m, const K& x) { return m.upper_bound(x); } template auto map_gt(const map& m, const K& x) { return m.upper_bound(x); } template auto map_ge(map& m, const K& x) { return m.lower_bound(x); } template auto map_ge(const map& m, const K& x) { return m.lower_bound(x); } // }}} // multiset {{{ template auto set_lt(multiset& s, const T& x) { auto it = s.lower_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_lt(const multiset& s, const T& x) { auto it = s.lower_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_le(multiset& s, const T& x) { auto it = s.upper_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_le(const multiset& s, const T& x) { auto it = s.upper_bound(x); if(it == begin(s)) return end(s); return prev(it); } template auto set_gt(multiset& s, const T& x) { return s.upper_bound(x); } template auto set_gt(const multiset& s, const T& x) { return s.upper_bound(x); } template auto set_ge(multiset& s, const T& x) { return s.lower_bound(x); } template auto set_ge(const multiset& s, const T& x) { return s.lower_bound(x); } // }}} // multimap {{{ template auto map_lt(multimap& m, const K& x) { auto it = m.lower_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_lt(const multimap& m, const K& x) { auto it = m.lower_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_le(multimap& m, const K& x) { auto it = m.upper_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_le(const multimap& m, const K& x) { auto it = m.upper_bound(x); if(it == begin(m)) return end(m); return prev(it); } template auto map_gt(multimap& m, const K& x) { return m.upper_bound(x); } template auto map_gt(const multimap& m, const K& x) { return m.upper_bound(x); } template auto map_ge(multimap& m, const K& x) { return m.lower_bound(x); } template auto map_ge(const multimap& m, const K& x) { return m.lower_bound(x); } // }}} // }}} template bool map_contains(const map& m, const typename map::key_type& k) { return m.find(k) != end(m); } template bool map_contains(const unordered_map& m, const typename unordered_map::key_type& k) { return m.find(k) != end(m); } template bool multiset_erase_one(multiset& m, const typename multiset::key_type& k) { auto it = m.find(k); if(it == end(m)) return false; m.erase(it); return true; } template bool multiset_erase_one(unordered_multiset& m, const typename unordered_multiset::key_type& k) { auto it = m.find(k); if(it == end(m)) return false; m.erase(it); return true; } template using MaxHeap = priority_queue, less>; template using MinHeap = priority_queue, greater>; // POP() 系 {{{ // 効率は悪い template T POP_FRONT(vector& v) { T x = v.front(); v.erase(begin(v)); return x; } template T POP_BACK(vector& v) { T x = v.back(); v.pop_back(); return x; } template T POP_FRONT(deque& v) { T x = v.front(); v.pop_front(); return x; } template T POP_BACK(deque& v) { T x = v.back(); v.pop_back(); return x; } template T POP_FRONT(forward_list& ls) { T x = ls.front(); ls.pop_front(); return x; } template T POP_FRONT(list& ls) { T x = ls.front(); ls.pop_front(); return x; } template T POP_BACK(list& ls) { T x = ls.back(); ls.pop_back(); return x; } template T POP(stack& stk) { T x = stk.top(); stk.pop(); return x; } template T POP(queue& que) { T x = que.front(); que.pop(); return x; } template T POP(priority_queue& que) { T x = que.top(); que.pop(); return x; } // }}} // Formatter {{{ template struct Formatter> { static ostream& write_str(ostream& out, const array& a) { return WRITE_RANGE_STR(out, begin(a), end(a)); } static ostream& write_repr(ostream& out, const array& a) { out << "array"; return WRITE_RANGE_REPR(out, begin(a), end(a)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const deque& deq) { return WRITE_RANGE_STR(out, begin(deq), end(deq)); } static ostream& write_repr(ostream& out, const deque& deq) { out << "deque"; return WRITE_RANGE_REPR(out, begin(deq), end(deq)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const forward_list& ls) { return WRITE_RANGE_STR(out, begin(ls), end(ls)); } static ostream& write_repr(ostream& out, const forward_list& ls) { out << "forward_list"; return WRITE_RANGE_REPR(out, begin(ls), end(ls)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const list& ls) { return WRITE_RANGE_STR(out, begin(ls), end(ls)); } static ostream& write_repr(ostream& out, const list& ls) { out << "list"; return WRITE_RANGE_REPR(out, begin(ls), end(ls)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const set& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const set& s) { out << "set"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const multiset& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const multiset& s) { out << "multiset"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_set& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const unordered_set& s) { out << "unordered_set"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_multiset& s) { return WRITE_RANGE_STR(out, begin(s), end(s)); } static ostream& write_repr(ostream& out, const unordered_multiset& s) { out << "unordered_multiset"; return WRITE_RANGE_REPR(out, begin(s), end(s)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const map& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const map& m) { out << "map"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const multimap& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const multimap& m) { out << "multimap"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_map& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const unordered_map& m) { out << "unordered_map"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const unordered_multimap& m) { return WRITE_RANGE_STR(out, begin(m), end(m)); } static ostream& write_repr(ostream& out, const unordered_multimap& m) { out << "unordered_multimap"; return WRITE_RANGE_REPR(out, begin(m), end(m)); } }; template struct Formatter> { static ostream& write_str(ostream& out, const stack& orig) { stack stk(orig); while(!stk.empty()) { WRITE_STR(out, stk.top()); stk.pop(); if(!stk.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const stack& orig) { stack stk(orig); out << "stack["; while(!stk.empty()) { WRITE_REPR(out, stk.top()); stk.pop(); if(!stk.empty()) out << ", "; } out << "]"; return out; } }; template struct Formatter> { static ostream& write_str(ostream& out, const queue& orig) { queue que(orig); while(!que.empty()) { WRITE_STR(out, que.front()); que.pop(); if(!que.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const queue& orig) { queue que(orig); out << "queue["; while(!que.empty()) { WRITE_REPR(out, que.front()); que.pop(); if(!que.empty()) out << ", "; } out << "]"; return out; } }; template struct Formatter> { static ostream& write_str(ostream& out, const priority_queue& orig) { priority_queue que(orig); while(!que.empty()) { WRITE_STR(out, que.top()); que.pop(); if(!que.empty()) out << ' '; } return out; } static ostream& write_repr(ostream& out, const priority_queue& orig) { priority_queue que(orig); out << "priority_queue["; while(!que.empty()) { WRITE_REPR(out, que.top()); que.pop(); if(!que.empty()) out << ", "; } out << "]"; return out; } }; template<> struct Formatter { static ostream& write_str(ostream& out, const BoolArray& a) { return WRITE_RANGE_STR(out, begin(a), end(a)); } static ostream& write_repr(ostream& out, const BoolArray& a) { out << "BoolArray"; return WRITE_RANGE_REPR(out, begin(a), end(a)); } }; // }}} // }}} // grid (container が必要) {{{ struct Index2 { i64 y, x; Index2(i64 yy, i64 xx) : y(yy), x(xx) {} explicit Index2(const pair& p) : Index2(p.first,p.second) {} Index2 operator-() const { return Index2(-y,-x); } Index2& operator+=(const Index2& rhs) { y += rhs.y; x += rhs.x; return *this; } Index2& operator-=(const Index2& rhs) { y -= rhs.y; x -= rhs.x; return *this; } Index2& operator*=(i64 rhs) { y *= rhs; x *= rhs; return *this; } Index2& operator/=(i64 rhs) { y /= rhs; x /= rhs; return *this; } i64 norm1() const { return abs(y) + abs(x); } vector neighbor4() const { return { { y-1, x }, { y , x-1 }, { y , x+1 }, { y+1, x }, }; } vector neighbor8() const { return { { y-1, x-1 }, { y-1, x }, { y-1, x+1 }, { y , x-1 }, { y , x+1 }, { y+1, x-1 }, { y+1, x }, { y+1, x+1 }, }; } }; Index2 operator+(const Index2& lhs, const Index2& rhs) { return Index2(lhs) += rhs; } Index2 operator-(const Index2& lhs, const Index2& rhs) { return Index2(lhs) -= rhs; } Index2 operator*(const Index2& lhs, i64 rhs) { return Index2(lhs) *= rhs; } Index2 operator*(i64 lhs, const Index2& rhs) { return Index2(rhs) *= lhs; } Index2 operator/(const Index2& lhs, i64 rhs) { return Index2(lhs) /= rhs; } bool operator==(const Index2& lhs, const Index2& rhs) { return lhs.y == rhs.y && lhs.x == rhs.x; } template<> struct Formatter { static ostream& write_str(ostream& out, const Index2& idx) { WRITE_STR(out, idx.y); out << ' '; WRITE_STR(out, idx.x); return out; } static ostream& write_repr(ostream& out, const Index2& idx) { out << "("; WRITE_REPR(out, idx.y); out << ","; WRITE_REPR(out, idx.x); out << ")"; return out; } }; template struct Grid2Container { using inner = vector; using outer = vector; }; template<> struct Grid2Container { using inner = BoolArray; using outer = vector; }; template struct Grid2 { using inner = typename Grid2Container::inner; using outer = typename Grid2Container::outer; outer cont_; Grid2(i64 h, i64 w, const T& val) : cont_(h, inner(w,val)) { assert(h >= 1); assert(w >= 1); } Grid2(i64 h, i64 w) : Grid2(h, w, T()) {} i64 h() const { return SIZE(cont_); } i64 w() const { return SIZE(cont_[0]); } T& operator[](const Index2& idx) { return at(idx.y, idx.x); } const T& operator[](const Index2& idx) const { return at(idx.y, idx.x); } T& at(i64 y, i64 x) { return cont_[y][x]; } const T& at(i64 y, i64 x) const { return cont_[y][x]; } bool idx_is_valid(const Index2& idx) const { return idx_is_valid(idx.y, idx.x); } bool idx_is_valid(i64 y, i64 x) const { return 0 <= y && y < h() && 0 <= x && x < w(); } vector neighbor4(const Index2& idx) const { vector res = idx.neighbor4(); auto it = ALL(remove_if, res, [this](const auto& to) { return !this->idx_is_valid(to); }); res.erase(it, end(res)); return res; } vector neighbor4(i64 y, i64 x) const { return neighbor4({y,x}); } vector neighbor8(const Index2& idx) const { vector res = idx.neighbor8(); auto it = ALL(remove_if, res, [this](const auto& to) { return !this->idx_is_valid(to); }); res.erase(it, end(res)); return res; } vector neighbor8(i64 y, i64 x) const { return neighbor8({y,x}); } }; template struct Formatter> { static ostream& write_str(ostream& out, const Grid2& grid) { return write_repr(out, grid); } static ostream& write_repr(ostream& out, const Grid2& grid) { out << "\n"; for(const auto& row : grid.cont_) { WRITE_STR(out, row); out << "\n"; } out << "\n"; return out; } }; template<> struct Formatter> { static ostream& write_str(ostream& out, const Grid2& grid) { return write_repr(out, grid); } static ostream& write_repr(ostream& out, const Grid2& grid) { out << "\n"; for(const auto& row : grid.cont_) { ALL(copy, row, ostream_iterator(out)); out << "\n"; } out << "\n"; return out; } }; // }}} //-------------------------------------------------------------------- void solve() { i64 H,W; RD(H); RD(W); Grid2 grid(H, W); REP(y, H) REP(x, W) { RD(grid.at(y,x)); } auto dfs = FIX([&grid](auto self, const Index2& idx) -> void { grid[idx] = 0; for(const auto& to : grid.neighbor4(idx)) { if(grid[to] == 1) self(to); } }); i64 ans = 0; REP(y, H) REP(x, W) { if(grid.at(y,x) == 1) { ++ans; dfs(Index2(y,x)); DBG(grid); } } // * 無効値 INF をそのまま出力してない? // * MOD はとった? // * 入出力の 0-based/1-based 確認した? // * 時間/メモリ制限は確認した? // * 違うやつ提出してない? // * 違うやつテストしてない? PRINTLN(ans); } signed main() { solve(); EXIT(); }