C++ 快读两种方法

法一 T宝法:

ios::sync_with_stdio(false);
cin.tie(0);

法二:究极优化法:

namespace Fastio
{
    struct Reader
    {
        template <typename T> Reader& operator >> (T &x)
        {
            x = 0;
            short f = 1;
            char c = getchar();
            while (c < '0' || c > '9') { if (c == '-') f *= -1; c = getchar(); }
            while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
            x *= f;
            return *this;
        }
        Reader& operator >> (double &x)
        {
            x = 0;
            double t = 0;
            short f = 1, s = 0;
            char c = getchar();
            while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
            while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
            if (c == '.') c = getchar();
            else { x *= f; return *this; }
            while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
            while (s--) t /= 10.0;
            x = (x + t) * f;
            return *this;
        }
        Reader& operator >> (long double &x)
        {
            x = 0;
            long double t = 0;
            short f = 1, s = 0;
            char c = getchar();
            while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
            while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
            if (c == '.') c = getchar();
            else { x *= f; return *this; }
            while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
            while (s--) t /= 10.0;
            x = (x + t) * f;
            return *this;
        }
        Reader& operator >> (__float128 &x)
        {
            x = 0;
            __float128 t = 0;
            short f = 1, s = 0;
            char c = getchar();
            while ((c < '0' || c > '9') && c != '.') { if (c == '-') f *= -1; c = getchar(); }
            while (c >= '0' && c <= '9' && c != '.') x = x * 10 + (c ^ 48), c = getchar();
            if (c == '.') c = getchar();
            else { x *= f; return *this; }
            while (c >= '0' && c <= '9') t = t * 10 + (c ^ 48), s++, c = getchar();
            while (s--) t /= 10.0;
            x = (x + t) * f;
            return *this;
        }
        Reader& operator >> (char &c)
        {
            c = getchar();
            while (c == ' ' || c == '\n' || c == '\r') c = getchar();
            return *this;
        }
        Reader& operator >> (char *str)
        {
            int len = 0;
            char c = getchar();
            while (c == ' ' || c == '\n' || c == '\r') c = getchar();
            while (c != ' ' && c != '\n' && c != '\r') str[len++] = c, c = getchar();
            str[len] = '\0';
            return *this;
        }
        Reader& operator >> (string &str)
        {
            str.clear();
            char c = getchar();
            while (c == ' ' || c == '\n' || c == '\r') c = getchar();
            while (c != ' ' && c != '\n' && c != '\r') str.push_back(c), c = getchar();
            return *this;
        }
        Reader() {}
    } cin;
    const char endl = '\n';
    struct Writer
    {
        const int Setprecision = 6;
        typedef int mxdouble;
        template <typename T> Writer& operator << (T x)
        {
            if (x == 0) { putchar('0'); return *this; }
            if (x < 0) putchar('-'), x = -x;
            static short sta[40];
            short top = 0;
            while (x > 0) sta[++top] = x % 10, x /= 10;
            while (top > 0) putchar(sta[top] + '0'), top--;
            return *this;
        }
        Writer& operator << (double x)
        {
            if (x < 0) putchar('-'), x = -x;
            mxdouble _ = x;
            x -= (double)_;
            static short sta[40];
            short top = 0;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            if (top == 0) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            putchar('.');
            for (int i = 0; i < Setprecision; i++) x *= 10;
            _ = x;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            for (int i = 0; i < Setprecision - top; i++) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            return *this;
        }
        Writer& operator << (long double x)
        {
            if (x < 0) putchar('-'), x = -x;
            mxdouble _ = x;
            x -= (long double)_;
            static short sta[40];
            short top = 0;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            if (top == 0) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            putchar('.');
            for (int i = 0; i < Setprecision; i++) x *= 10;
            _ = x;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            for (int i = 0; i < Setprecision - top; i++) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            return *this;
        }
        Writer& operator << (__float128 x)
        {
            if (x < 0) putchar('-'), x = -x;
            mxdouble _ = x;
            x -= (__float128)_;
            static short sta[40];
            short top = 0;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            if (top == 0) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            putchar('.');
            for (int i = 0; i < Setprecision; i++) x *= 10;
            _ = x;
            while (_ > 0) sta[++top] = _ % 10, _ /= 10;
            for (int i = 0; i < Setprecision - top; i++) putchar('0');
            while (top > 0) putchar(sta[top] + '0'), top--;
            return *this;
        }
        Writer& operator << (char c) { putchar(c); return *this; }
        Writer& operator << (char *str)
        {
            int cur = 0;
            while (str[cur]) putchar(str[cur++]);
            return *this;
        }
        Writer& operator << (const char *str)
        {
            int cur = 0;
            while (str[cur]) putchar(str[cur++]);
            return *this;
        }
        Writer& operator << (string str)
        {
            int st = 0, ed = str.size();
            while (st < ed) putchar(str[st++]);
            return *this;
        }
        Writer() {}
    } cout;
}
using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
支持__int128和__float128以及string

E - Cross Swapping

同一题目,时限 1 s,T宝法 872ms,究极优化 421ms。

总结:除了cf之外 T宝法足以,如果自己算法时间复杂度算的没错还是卡了,用究极优化。

你可能感兴趣的:(杂项,c++,数据结构,算法)