zoukankan      html  css  js  c++  java
  • ACM:读入优化

    两个简单的读入优化

    1 int getin(){
    2     int ans=0;char tmp;bool sign=0;
    3     while(!isdigit(tmp=getchar()) && tmp!='-');
    4     if(tmp=='-')sign=1,tmp=getchar();
    5     do ans=(ans<<3)+(ans<<1)+tmp-'0';
    6     while(isdigit(tmp=getchar()));
    7     return  sign?-ans:ans;
    8 }
     1 inline int read(){
     2     int p,data=0;
     3     char ch=0;
     4     while ((ch!='-') && ch<'0' || ch>'9') ch=getchar();
     5     if (ch=='-')
     6     {
     7         p=-1;
     8         ch=getchar();
     9     } else p=1;
    10     while (ch>='0' && ch<='9') data=data*10+ch-'0',ch=getchar();
    11     return data*p;
    12 }

    大佬的读入优化

    from:https://blog.csdn.net/x_iya/article/details/9003416

      1 namespace fastIO{
      2     #define BUF_SIZE 100000
      3     #define OUT_SIZE 100000
      4     #define ll long long
      5     //fread->read
      6     bool IOerror=0;
      7     inline char nc(){
      8         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
      9         if (p1==pend){
     10             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
     11             if (pend==p1){IOerror=1;return -1;}
     12             //{printf("IO error!
    ");system("pause");for (;;);exit(0);}
     13         }
     14         return *p1++;
     15     }
     16     inline bool blank(char ch){return ch==' '||ch=='
    '||ch=='
    '||ch=='	';}
     17     inline void read(int &x){
     18         bool sign=0; char ch=nc(); x=0;
     19         for (;blank(ch);ch=nc());
     20         if (IOerror)return;
     21         if (ch=='-')sign=1,ch=nc();
     22         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
     23         if (sign)x=-x;
     24     }
     25     inline void read(ll &x){
     26         bool sign=0; char ch=nc(); x=0;
     27         for (;blank(ch);ch=nc());
     28         if (IOerror)return;
     29         if (ch=='-')sign=1,ch=nc();
     30         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
     31         if (sign)x=-x;
     32     }
     33     inline void read(double &x){
     34         bool sign=0; char ch=nc(); x=0;
     35         for (;blank(ch);ch=nc());
     36         if (IOerror)return;
     37         if (ch=='-')sign=1,ch=nc();
     38         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
     39         if (ch=='.'){
     40             double tmp=1; ch=nc();
     41             for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
     42         }
     43         if (sign)x=-x;
     44     }
     45     inline void read(char *s){
     46         char ch=nc();
     47         for (;blank(ch);ch=nc());
     48         if (IOerror)return;
     49         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
     50         *s=0;
     51     }
     52     inline void read(char &c){
     53         for (c=nc();blank(c);c=nc());
     54         if (IOerror){c=-1;return;}
     55     }
     56     //getchar->read
     57     inline void read1(int &x){
     58         char ch;int bo=0;x=0;
     59         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
     60         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
     61         if (bo)x=-x;
     62     }
     63     inline void read1(ll &x){
     64         char ch;int bo=0;x=0;
     65         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
     66         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
     67         if (bo)x=-x;
     68     }
     69     inline void read1(double &x){
     70         char ch;int bo=0;x=0;
     71         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
     72         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
     73         if (ch=='.'){
     74             double tmp=1;
     75             for (ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar());
     76         }
     77         if (bo)x=-x;
     78     }
     79     inline void read1(char *s){
     80         char ch=getchar();
     81         for (;blank(ch);ch=getchar());
     82         for (;!blank(ch);ch=getchar())*s++=ch;
     83         *s=0;
     84     }
     85     inline void read1(char &c){for (c=getchar();blank(c);c=getchar());}
     86     //scanf->read
     87     inline void read2(int &x){scanf("%d",&x);}
     88     inline void read2(ll &x){
     89         #ifdef _WIN32
     90             scanf("%I64d",&x);
     91         #else
     92         #ifdef __linux
     93             scanf("%lld",&x);
     94         #else
     95             puts("error:can't recognize the system!");
     96         #endif
     97         #endif
     98     }
     99     inline void read2(double &x){scanf("%lf",&x);}
    100     inline void read2(char *s){scanf("%s",s);}
    101     inline void read2(char &c){scanf(" %c",&c);}
    102     inline void readln2(char *s){gets(s);}
    103     //fwrite->write
    104     struct Ostream_fwrite{
    105         char *buf,*p1,*pend;
    106         Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
    107         void out(char ch){
    108             if (p1==pend){
    109                 fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
    110             }
    111             *p1++=ch;
    112         }
    113         void print(int x){
    114             static char s[15],*s1;s1=s;
    115             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
    116             while(x)*s1++=x%10+'0',x/=10;
    117             while(s1--!=s)out(*s1);
    118         }
    119         void println(int x){
    120             static char s[15],*s1;s1=s;
    121             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
    122             while(x)*s1++=x%10+'0',x/=10;
    123             while(s1--!=s)out(*s1); out('
    ');
    124         }
    125         void print(ll x){
    126             static char s[25],*s1;s1=s;
    127             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
    128             while(x)*s1++=x%10+'0',x/=10;
    129             while(s1--!=s)out(*s1);
    130         }
    131         void println(ll x){
    132             static char s[25],*s1;s1=s;
    133             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
    134             while(x)*s1++=x%10+'0',x/=10;
    135             while(s1--!=s)out(*s1); out('
    ');
    136         }
    137         void print(double x,int y){
    138             static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
    139                 1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
    140                 100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
    141             if (x<-1e-12)out('-'),x=-x;x*=mul[y];
    142             ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1;
    143             ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2);
    144             if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i); print(x3);}
    145         }
    146         void println(double x,int y){print(x,y);out('
    ');}
    147         void print(char *s){while (*s)out(*s++);}
    148         void println(char *s){while (*s)out(*s++);out('
    ');}
    149         void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
    150         ~Ostream_fwrite(){flush();}
    151     }Ostream;
    152     inline void print(int x){Ostream.print(x);}
    153     inline void println(int x){Ostream.println(x);}
    154     inline void print(char x){Ostream.out(x);}
    155     inline void println(char x){Ostream.out(x);Ostream.out('
    ');}
    156     inline void print(ll x){Ostream.print(x);}
    157     inline void println(ll x){Ostream.println(x);}
    158     inline void print(double x,int y){Ostream.print(x,y);}
    159     inline void println(double x,int y){Ostream.println(x,y);}
    160     inline void print(char *s){Ostream.print(s);}
    161     inline void println(char *s){Ostream.println(s);}
    162     inline void println(){Ostream.out('
    ');}
    163     inline void flush(){Ostream.flush();}
    164     //puts->write
    165     char Out[OUT_SIZE],*o=Out;
    166     inline void print1(int x){
    167         static char buf[15];
    168         char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x;
    169         while(x)*p1++=x%10+'0',x/=10;
    170         while(p1--!=buf)*o++=*p1;
    171     }
    172     inline void println1(int x){print1(x);*o++='
    ';}
    173     inline void print1(ll x){
    174         static char buf[25];
    175         char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x;
    176         while(x)*p1++=x%10+'0',x/=10;
    177         while(p1--!=buf)*o++=*p1;
    178     }
    179     inline void println1(ll x){print1(x);*o++='
    ';}
    180     inline void print1(char c){*o++=c;}
    181     inline void println1(char c){*o++=c;*o++='
    ';}
    182     inline void print1(char *s){while (*s)*o++=*s++;}
    183     inline void println1(char *s){print1(s);*o++='
    ';}
    184     inline void println1(){*o++='
    ';}
    185     inline void flush1(){if (o!=Out){if (*(o-1)=='
    ')*--o=0;puts(Out);}}
    186     struct puts_write{
    187         ~puts_write(){flush1();}
    188     }_puts;
    189     inline void print2(int x){printf("%d",x);}
    190     inline void println2(int x){printf("%d
    ",x);}
    191     inline void print2(char x){printf("%c",x);}
    192     inline void println2(char x){printf("%c
    ",x);}
    193     inline void print2(ll x){
    194         #ifdef _WIN32
    195             printf("%I64d",x);
    196         #else
    197         #ifdef __linux
    198             printf("%lld",x);
    199         #else
    200             puts("error:can't recognize the system!");
    201         #endif
    202         #endif
    203     }
    204     inline void println2(ll x){print2(x);printf("
    ");}
    205     inline void println2(){printf("
    ");}
    206     #undef ll
    207     #undef OUT_SIZE
    208     #undef BUF_SIZE
    209 };
    210 using namespace fastIO;
  • 相关阅读:
    CodeForces 757C Felicity is Coming!(排列组合)
    Ural 1519 Formula 1( 插头dp )
    FZU 2187 回家种地 ( 扫描线 + 离散 求矩阵单次覆盖面积 )
    HDU 1255 覆盖的面积 ( 扫描线 + 离散 求矩阵大于k次面积并 )
    ZOJ 3841 Cards
    HDU 4012 Paint on a Wall(状压+bfs)
    Topcoder SRM653div2
    2015 GDUT校赛
    Topcoder SRM652div2
    CodeChef A String Game(SG)
  • 原文地址:https://www.cnblogs.com/jyroy/p/9785374.html
Copyright © 2011-2022 走看看