真的读入优化与假的读入优化
一直以来,我都是这样写读入优化的。
inline char nc() { static char b[100000],*s=b,*t=b; return s==t&&(t=(s=b)+fread(b,1,100000,stdin),s==t)?-1:*s++; } inline void read(int &x) { static char buf = nc(); x = 0; static int op = 1; for (; !isdigit(buf); buf = nc()) if (buf == '-') op = -1; for (; isdigit(buf); buf = nc()) x = x * 10 + buf - '0'; x *= op; }
然后有道题死活过不去,把读入优化改成scanf就过了。这才发现,我一直都写的是假的!!!!读入优化!!!!
真正的读入优化要这样写!!
inline char nc() { static char b[100000],*s=b,*t=b; return s==t&&(t=(s=b)+fread(b,1,100000,stdin),s==t)?-1:*s++; } inline void read(int &x) { static char buf = nc(); x = 0; int op = 1; for (; !isdigit(buf); buf = nc()) if (buf == '-') op = -1; for (; isdigit(buf); buf = nc()) x = x * 10 + buf - '0'; x *= op; }
又测了测速,貌似不写static更快?
inline char nc() { static char b[100000],*s=b,*t=b; return s==t&&(t=(s=b)+fread(b,1,100000,stdin),s==t)?-1:*s++; } inline void read(int &x) { char buf = nc(); x = 0; int op = 1; for (; !isdigit(buf); buf = nc()) if (buf == '-') op = -1; for (; isdigit(buf); buf = nc()) x = x * 10 + buf - '0'; x *= op; }
震惊!把int换成short又可以变快!
inline char nc() { static char b[100000],*s=b,*t=b; return s==t&&(t=(s=b)+fread(b,1,100000,stdin),s==t)?-1:*s++; } inline void read(int &x) { char buf = nc(); x = 0; short op = 1; for (; !isdigit(buf); buf = nc()) if (buf == '-') op = -1; for (; isdigit(buf); buf = nc()) x = x * 10 + buf - '0'; x *= op; }