-
Bio
比cin,cout好用多了😄
#ifndef FAST_IO_H #define FAST_IO_H #include <cctype> #include <cstdint> #include <cstdio> #include <string> #include <type_traits> #include <cmath> #include <cstring> #include <limits> #include <iomanip> #include <ios> #include <atomic> #include <stdexcept> #define FASTIO_VERSION_MAJOR 1 #define FASTIO_VERSION_MINOR 5 #define FASTIO_VERSION_PATCH 0 #if defined(_WIN32) #include <Windows.h> #define FAST_IO_TLS __declspec(thread) #else #define FAST_IO_TLS __thread #endif namespace std { namespace fastio_detail { static constexpr size_t kBufferSize = 1 << 16; struct Buffer { char in_buf[kBufferSize]; char out_buf[kBufferSize]; size_t in_pos = 0; size_t in_len = 0; std::atomic<size_t> out_pos{0}; Buffer() = default; ~Buffer() { try { flush(); } catch (...) { } } void refill() { in_len = fread(in_buf, 1, kBufferSize, stdin); in_pos = 0; if (in_len == 0) in_buf[0] = EOF; } void flush() { size_t pos = out_pos.load(std::memory_order_relaxed); if (pos > 0) { if (out_pos.exchange(0, std::memory_order_acq_rel) > 0) { if (fwrite(out_buf, 1, pos, stdout) != pos) { throw std::runtime_error("Write failed"); } fflush(stdout); } } } char get() { if (in_pos >= in_len) refill(); return in_buf[in_pos++]; } void safe_put(char c) { size_t pos = out_pos.load(std::memory_order_relaxed); if (pos >= kBufferSize - 1) { flush(); pos = 0; } out_buf[pos] = c; out_pos.store(pos + 1, std::memory_order_release); } }; FAST_IO_TLS Buffer tls_buffer; constexpr bool is_space(char c) noexcept { return (c == ' ') | (c == '\t') | (c == '\n') | (c == '\r'); } template <typename T> constexpr bool is_readable_v = disjunction_v< is_integral<T>, is_floating_point<T>, is_same<T, char>, is_same<T, bool>, is_same<T, string>, is_same<T, ios_base &(*)(ios_base &)>>; template <typename T, enable_if_t<is_integral_v<T> && !is_same_v<T, bool>, int> = 0> void read(T &val) { val = 0; bool neg = false; char c = tls_buffer.get(); while (is_space(c)) c = tls_buffer.get(); if constexpr (is_signed_v<T>) { if (c == '-') { neg = true; c = tls_buffer.get(); } } while (isdigit(static_cast<unsigned char>(c))) { val = val * 10 + (c - '0'); c = tls_buffer.get(); } if constexpr (is_signed_v<T>) { if (neg) val = -val; } } inline void read(bool &b) { char c = tls_buffer.get(); while (is_space(c)) c = tls_buffer.get(); b = (c == '1') | (tolower(c) == 't') | (tolower(c) == 'y'); } inline void read(char &c) { c = tls_buffer.get(); while (is_space(c)) c = tls_buffer.get(); } inline void read(string &s) { s.clear(); char c = tls_buffer.get(); while (is_space(c)) c = tls_buffer.get(); s.reserve(32); while (!is_space(c) && c != EOF) { s.push_back(c); c = tls_buffer.get(); } } template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0> void read(T &val) { val = 0; bool neg = false; char c = tls_buffer.get(); while (is_space(c)) c = tls_buffer.get(); if (c == '-') { neg = true; c = tls_buffer.get(); } while (isdigit(static_cast<unsigned char>(c))) { val = val * 10 + (c - '0'); c = tls_buffer.get(); } if (c == '.') { c = tls_buffer.get(); T frac = 1; while (isdigit(static_cast<unsigned char>(c))) { frac *= 0.1; val += (c - '0') * frac; c = tls_buffer.get(); } } if (tolower(c) == 'e') { c = tls_buffer.get(); bool exp_neg = false; T exp = 0; if (c == '-') { exp_neg = true; c = tls_buffer.get(); } else if (c == '+') { c = tls_buffer.get(); } while (isdigit(static_cast<unsigned char>(c))) { exp = exp * 10 + (c - '0'); c = tls_buffer.get(); } val *= pow(static_cast<T>(10), exp_neg ? -exp : exp); } if (neg) val = -val; } template <typename T, enable_if_t<is_integral_v<T> && !is_same_v<T, bool>, int> = 0> void write(T val, ios_base::fmtflags flags = ios_base::dec, char fill = ' ', int width = 0) { if (val == 0 && !(flags & ios_base::showbase)) { tls_buffer.safe_put('0'); return; } if constexpr (is_signed_v<T>) { if (val < 0) { tls_buffer.safe_put('-'); val = -val; } else if (flags & ios_base::showpos) { tls_buffer.safe_put('+'); } } char buf[32]; size_t len = 0; const int base = (flags & ios_base::oct) ? 8 : ((flags & ios_base::hex) ? 16 : 10); const char *digits = (flags & ios_base::uppercase) ? "0123456789ABCDEF" : "0123456789abcdef"; do { buf[len++] = digits[val % base]; val /= base; } while (val > 0); if (width > 0 && len < static_cast<size_t>(width)) { for (size_t i = len; i < static_cast<size_t>(width); ++i) { tls_buffer.safe_put(fill); } } while (len-- > 0) { tls_buffer.safe_put(buf[len]); } } inline void write(bool b, ios_base::fmtflags = ios_base::dec, char = ' ', int = 0) { tls_buffer.safe_put(b ? '1' : '0'); } inline void write(char c, ios_base::fmtflags = ios_base::dec, char = ' ', int = 0) { tls_buffer.safe_put(c); } inline void write(const string &s, ios_base::fmtflags = ios_base::dec, char = ' ', int width = 0) { if (width > 0 && s.length() < static_cast<size_t>(width)) { for (size_t i = s.length(); i < static_cast<size_t>(width); ++i) { tls_buffer.safe_put(' '); } } for (char c : s) { tls_buffer.safe_put(c); } } inline void write(const char *s, ios_base::fmtflags = ios_base::dec, char = ' ', int width = 0) { size_t len = strlen(s); if (width > 0 && len < static_cast<size_t>(width)) { for (size_t i = len; i < static_cast<size_t>(width); ++i) { tls_buffer.safe_put(' '); } } while (*s) { tls_buffer.safe_put(*s++); } } template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0> void write(T val, ios_base::fmtflags flags = ios_base::dec, char fill = ' ', int width = 0, int prec = 6) { if (std::isnan(val)) { write("nan", flags, fill, width); return; } if (std::isinf(val)) { write(val < 0 ? "-inf" : "inf", flags, fill, width); return; } if (val < 0) { tls_buffer.safe_put('-'); val = -val; } else if (flags & ios_base::showpos) { tls_buffer.safe_put('+'); } T int_part; T frac = modf(val, &int_part); write(static_cast<int64_t>(int_part), flags, fill, width); if (prec > 0) { tls_buffer.safe_put('.'); T round = 0.5 * pow(10, -prec); frac += round; for (int i = 0; i < prec; ++i) { frac *= 10; int digit = static_cast<int>(frac); tls_buffer.safe_put('0' + digit); frac -= digit; } } } } class FastIOState : public ios_base { public: static FastIOState &get_instance() { FAST_IO_TLS static FastIOState instance; return instance; } int precision() const { return precision_; } int precision(int p) { return precision_ = p; } int width() const { return width_; } int width(int w) { return width_ = w; } char fill() const { return fill_; } char fill(char c) { return fill_ = c; } private: FastIOState() : precision_(6), width_(0), fill_(' ') {} int precision_; int width_; char fill_; }; class FastIn { public: FastIn &operator>>(int &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(long &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(long long &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(unsigned &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(unsigned long &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(unsigned long long &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(float &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(double &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(long double &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(bool &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(char &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(string &val) { fastio_detail::read(val); return *this; } FastIn &operator>>(ios_base &(*manip)(ios_base &)) { manip(FastIOState::get_instance()); return *this; } }; class FastOut { public: ~FastOut() { try { flush(); } catch (...) { } } FastOut &operator<<(int val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(long val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(long long val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(unsigned val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(unsigned long val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(unsigned long long val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(float val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); auto prec = FastIOState::get_instance().precision(); fastio_detail::write(val, flags, fill, width, prec); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(double val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); auto prec = FastIOState::get_instance().precision(); fastio_detail::write(val, flags, fill, width, prec); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(long double val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); auto prec = FastIOState::get_instance().precision(); fastio_detail::write(val, flags, fill, width, prec); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(bool val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(char val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(const char *val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(const string &val) { auto flags = FastIOState::get_instance().flags(); auto fill = FastIOState::get_instance().fill(); auto width = FastIOState::get_instance().width(); fastio_detail::write(val, flags, fill, width); FastIOState::get_instance().width(0); return *this; } FastOut &operator<<(ios_base &(*manip)(ios_base &)) { manip(FastIOState::get_instance()); return *this; } FastOut &operator<<(ostream &(*manip)(ostream &)) { if (manip == static_cast<ostream &(*)(ostream &)>(&endl)) { fastio_detail::tls_buffer.safe_put('\n'); flush(); } else if (manip == static_cast<std::ostream &(*)(std::ostream &)>(&std::flush)) { flush(); } return *this; } void flush() { fastio_detail::tls_buffer.flush(); } int precision() const { return FastIOState::get_instance().precision(); } int precision(int p) { return FastIOState::get_instance().precision(p); } int width() const { return FastIOState::get_instance().width(); } int width(int w) { return FastIOState::get_instance().width(w); } char fill() const { return FastIOState::get_instance().fill(); } char fill(char c) { return FastIOState::get_instance().fill(c); } }; FAST_IO_TLS inline FastIn in; FAST_IO_TLS inline FastOut out; } using std::in; using std::out; #endif
-
Accepted Problems
-
Recent Activities
Problem Tags
- 一本通编程启蒙
- 98
- 字符串
- 4
- 模拟
- 4
- CSP J 入门级
- 4
- 课课通
- 3
- 结构体
- 3
- 动态数组
- 3
- 动态规划
- 3
- 其他
- 2
- 排序
- 2
- NOIP全国联赛普及组-2007年NOIP全国联赛普及组
- 2
- NOIp 普及组
- 2
- O2优化
- 2
- 二维数组
- 2
- 1999
- 1
- 2018
- 1
- 2019
- 1
- 2020
- 1
- 2021
- 1
- 2022
- 1