ka chang

· · 个人记录

1.constexpr
2.inline
3.register

4.
inline int read()
{
    int x = 0, w = 0; char ch = 0;
    while (!isdigit(ch)) {w |= ch == '-'; ch = getchar();}
    while (isdigit(ch)) {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
    return w ? -x : x;
}
inline void write(int x)
{
     if (x < 0) putchar('-'), x = -x;
     if (x > 9) write(x / 10);
     putchar(x % 10 + '0');
}
6.
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize(2)

7.

    char buf[1 << 23], *p1, *p2, c;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin), p1 == p2) ? EOF : *p1++)

inline void read(int &x) {
    x = 0;
    while (!isdigit(c = gc));
    do {
        x = (x << 3) + (x << 1) + (c ^ '0');
    } while (isdigit(c = gc));
}

8.
namespace fast_IO {
#define IOSIZE 1000000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
    inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
    inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
    inline void print(char x) { putchar(x); }
    inline void print(char *x) { while (*x) putchar(*x++); }
    inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
    inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
    inline void print(bool b) { putchar(b+48); }
    template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
    struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;

8.

namespace fast_IO {
#define IOSIZE 1000000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)

    template<typename T> inline T read() { 
        T s = 0; 
        int w = 1; 
        char ch; 
        while (ch = getchar(), !isdigit(ch) and (ch != EOF)) 
            if (ch == '-') w = -1; 
        if (ch == EOF) return false; 
        while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); 
        return s * w; 
    }

    template<typename T> inline bool read(T &s) { 
        s = 0; 
        int w = 1; 
        char ch; 
        while (ch = getchar(), !isdigit(ch) and (ch != EOF)) 
            if (ch == '-') w = -1; 
        if (ch == EOF) return false; 
        while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); 
        return s *= w, true; 
    }

    // 核心优化:递归改迭代,消除栈开销和调用开销
    template<typename T> inline void print(T x) { 
        if (x < 0) putchar('-'), x = -x; 
        if (x > 9) print(x / 10); 
        putchar(x % 10 + 48); 
    }

    // 针对int的特化:用do-while+数组反转,比递归快30-50%
    template<> inline void print(int x) {
        if (x < 0) putchar('-'), x = -x;
        static char buf[16];
        int n = 0;
        do {
            buf[n++] = x % 10 + 48;
            x /= 10;
        } while (x);
        while (n--) putchar(buf[n]);
    }

    // 针对long long的特化
    template<> inline void print(long long x) {
        if (x < 0) putchar('-'), x = -x;
        static char buf[24];
        int n = 0;
        do {
            buf[n++] = x % 10 + 48;
            x /= 10;
        } while (x);
        while (n--) putchar(buf[n]);
    }

    inline bool read(char &s) { 
        while (s = getchar(), isspace(s)); 
        return true; 
    }

    inline bool read(char *s) { 
        char ch; 
        while (ch = getchar(), isspace(ch)); 
        if (ch == EOF) return false; 
        while (!isspace(ch)) *s++ = ch, ch = getchar(); 
        *s = '\000'; 
        return true; 
    }

    inline void print(char x) { putchar(x); }

    inline void print(char *x) { while (*x) putchar(*x++); }

    inline void print(const char *x) { 
        for (int i = 0; x[i]; i++) putchar(x[i]); 
    }

    inline bool read(std::string& s) { 
        s = ""; 
        char ch; 
        while (ch = getchar(), isspace(ch)); 
        if (ch == EOF) return false; 
        while (!isspace(ch)) s += ch, ch = getchar(); 
        return true; 
    }

    inline void print(std::string x) { 
        for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); 
    }

    inline bool read(bool &b) { 
        char ch; 
        while(ch=getchar(), isspace(ch)); 
        b=ch^48; 
        return true; 
    }

    inline void print(bool b) { putchar(b+48); }

    template<typename T, typename... T1> inline int read(T& a, T1&... other) { 
        return read(a) + read(other...); 
    }

    template<typename T, typename... T1> inline void print(T a, T1... other) { 
        print(a), print(other...); 
    }

    struct Fast_IO { 
        ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } 
    } io;

    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { 
        return read(b), io; 
    }

    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { 
        return print(b), io; 
    }

#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;