读入优化? fread ??? NAIVE !!! streambuf -- sgetn

来源:互联网 发布:四知太守杨震 编辑:程序博客网 时间:2024/06/05 06:09

大家都很强, 可与之共勉 。

比fread快的读入优化
使用底层的streambuf, 用rdbuf ( )获取。

# include <cctype># include <cstdio># include <iostream>inline int read ( )  {# define SIZE 1000005    static std :: streambuf *fb ( std :: cin.rdbuf ( ) ) ;    static char buf [SIZE], *ss ( 0 ), *tt ( 0 ) ;# define pick( )  ( (ss == tt) ? ( tt = buf + fb -> sgetn ( ss = buf, SIZE ), ((ss == tt) ? -1 : *(ss ++)) ) :*(ss ++) )    register int x, c ;    bool opt ( 1 ) ;    while ( ! isdigit ( c = pick ( ) ) && ( c ^ -1 ) && ( c ^ 45 ) ) ;    if ( c == 45 )  c = pick ( ), opt = 0 ;    for ( x = -48 + c ; isdigit ( c = pick ( ) ) ; ( x *= 10 ) += c - 48 ) ;    return opt ? x : -x ;}int main ( )  {    std :: ios :: sync_with_stdio ( 0 ) ;    std :: cin.tie ( 0 ) ;    int s ( 0 ) ;    int n ( read ( ) ) ;    while ( n -- )  s ^= read ( ) ;    printf ( "%d\n", s ) ;}