先看c++c++ 版本以及是否打开优化开关

  • 快读快写(见文末)
  • 频繁改变的变量加 register
  • 访问 a[i] 后访问 a[i+1]a[i+2] ···
  • 数组开奇数(原因不明)
  • 循环展开: for(int i=1;i<=10000;i++)a[i]++; \Rightarrow for(int i=1;i<=10000;i+=4)a[i]++,a[i+1]++,a[i+2]++,a[i+3]++;
  • 位运算:
    • x*=(2,4,8,16...) \Rightarrow x<<=(1,2,3,4...) ( /= \Rightarrow >>= )
    • x%2 \Rightarrow x&1
    • x>y \Rightarrow int: y-x>>31 long long: y-x>>63
    • -x \Rightarrow ~x+1
    • swap(x,y) \Rightarrow x^=y^=x^=y
    • abs(x) \Rightarrow (x^(x>>31))-(x>>31) long long: 31 \Rightarrow 63
  • bool \Rightarrow int/bitset
  • inline
  • if(a)b;else c; \Rightarrow a?b:c;
  • if(a)b; \Rightarrow a&&b;
  • ; \Rightarrow ,
  • int i=1; \Rightarrow int i(1);
  • i++; \Rightarrow ++i;
快读快写改前链接
namespace io {
    const int __SIZE = (1 << 21) + 1;
    char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
    #define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
    inline void gc (char &x) { x = Gc(); }
    inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
    inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
    inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
        for(; __c > 31 && __c < 127 && __c != ' ' && __c != '\n' && __c != '\r'; ++s, __c = Gc()) *s = __c; *s = 0; }
    template <class I> inline bool gi (I &x) { _eof = 0;
        for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
        for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
    template <typename I,typename ...Args> inline bool gi(I &tmp, Args &...tmps) { return gi(tmp) ? gi(tmps...), 1 : 0; }
    template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
        while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
    template <class I> inline void write(char _c,  I x) { print(x), pc(_c); }
    template <typename I,typename ...Args> inline void write(char __enc, I tmp, Args ...tmps) { write(__enc, tmp), write(__enc, tmps...); }
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::write; using io::print;

用法:

  1. 使用 gi(x, ...); 读入任意多个任意的整型 x 等价于 scanf("%d...", &x, ...); 其中可以将 %d 自动识别为对应类型。

  2. 使用 print(x); 输出一个任意的整型 x 等价于 printf("%d", x); 其中可以将 %d 自动识别为对应类型。

  3. 使用 write(c, x, ...); 输出任意多个任意的整型 x 并以 c 为分隔符,等价于 printf("%d%c...", x, c, ...); 其中可以将 %d 自动识别为对应类型。

  4. 使用 gc(c); 读入一个字符 c 等价于 scanf("%c", &c)

  5. 使用 pc(c); 输出一个字符 c 等价于 putchar(c);

  6. 使用 gstr(str); 读入一个字符串 str 等价于 scanf("%s", str); 可以用 gstr(str + 1); 替换 scanf("%s", str + 1);

  7. 使用 pstr(str); 输出一个字符串 str 等价于 printf("%s", str);

更新于 阅读次数