1 条题解
-
1
关于快读
笔者对快读的了解仅限于背诵模板
以下是笔者常用的几个快读模板或优化 1:cin关同步
优点:好写 缺点:切勿与scanf等输入同时使用!!!cin.tie(0); cout.tie(0); ios::sync_with_stdio(0);
2:常见getchar版快读 优点:好写
template<class I> inline void read(I &x) { x = 0; I f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } x *= f; } template<class I> inline void write(I x) { if(x == 0) { putchar('0'); return; } I tmp = x > 0 ? x : -x; if(x < 0) putchar('-'); int cnt = 0; while(tmp > 0) { buf1[cnt++] = tmp % 10 + '0'; tmp /= 10; } while(cnt > 0) putchar(buf1[--cnt]); }
3:fread版快读 优点:真的快 缺点:显而易见,难写
namespace IO { char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21]; inline char gc() { if(p1 != p2) return *p1++; p1 = buf; p2 = p1 + fread(buf, 1, 1 << 21, stdin); return p1 == p2 ? EOF : *p1++; } #define G gc #ifndef ONLINE_JUDGE #undef G #define G getchar #endif template<class I> inline void read(I &x) { x = 0; I f = 1; char c = G(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = G(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = G(); } x *= f; } template<class I> inline void write(I x) { if(x == 0) { putchar('0'); return; } I tmp = x > 0 ? x : -x; if(x < 0) putchar('-'); int cnt = 0; while(tmp > 0) { buf1[cnt++] = tmp % 10 + '0'; tmp /= 10; } while(cnt > 0) putchar(buf1[--cnt]); } }
4:fread版快读Pro 第三版快读上在速度上已经非常优秀了,但是,他不够人性化。
For exmaple: 第三版快读读入多个数据int a,b,c,d,e; read(a),read(b),read(c),read(d),read(e);
别担心,一段神奇的代码能解决这个问题。
template <typename T,typename ...Args> inline void read(T &tmp,Args &...tmps) { read(tmp); read(tmps...); }
所以第四版的fread在读入多个数据时就可以这样
For exmaple:int a,b,c,d,e; read(a,b,c,d,e);
是不是看着更顺眼了?
最后奉上完整代码namespace IO { char buf[1<<21], *p1 = buf, *p2 = buf, buf1[1<<21]; inline char gc() { if(p1 != p2) return *p1++; p1 = buf; p2 = p1 + fread(buf, 1, 1 << 21, stdin); return p1 == p2 ? EOF : *p1++; } #define G gc #ifndef ONLINE_JUDGE #undef G #define G getchar #endif template<class I> inline void read(I &x) { x = 0; I f = 1; char c = G(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = G(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = G(); } x *= f; } template <typename T,typename ...Args> inline void read(T &tmp,Args &...tmps) { read(tmp); read(tmps...); } template<class I> inline void write(I x) { if(x == 0) { putchar('0'); return; } I tmp = x > 0 ? x : -x; if(x < 0) putchar('-'); int cnt = 0; while(tmp > 0) { buf1[cnt++] = tmp % 10 + '0'; tmp /= 10; } while(cnt > 0) putchar(buf1[--cnt]); } }
额外小知识,在linux上还有一个函数同getchar,但比getchar()更快,他就是getchar_unlocked()。
遗憾的是,你无法在windows使用它,因为会CE。
但只要你想,在Hydro或者Luogu的IDE上测试它也不成问题
- 1
信息
- ID
- 10290
- 时间
- 2000ms
- 内存
- 512MiB
- 难度
- 2
- 标签
- 递交数
- 5
- 已通过
- 2
- 上传者