巨坑(std::cin)

P1923 【深基9.例4】求第 k 小的数

haust_dingbang @ 2021-03-15 20:15:13

无语了,我都已经分治了,用了全局变量了,结果还给我超时,然后才看到题解中说要用快读,我。。。换了好几种方式,先是std::sort,然后自己写个优先队列,然后分治(快速排序类似),我都想用基数排序了,嘤嘤嘤


by _caiji_ @ 2021-03-15 20:17:16

std::cin 读 5e6,不卡你卡谁?


by BotDand @ 2021-03-15 20:18:50

快读是个好东西


by PragmaGCC @ 2021-03-15 20:21:10

ios::sync_with_stdio(false);


by 天南星魔芋 @ 2021-03-15 20:23:31

不应该默认使用scanf吗


by BlachSnake @ 2021-03-15 20:26:45

namespace io{
    int Read(){
        char c=getchar();
        int x=0,d=1;
        while(!isdigit(c)){
            if(c=='-')d=-1;
            c=getchar();
        }
        while(isdigit(c))
            x=x*10-48+c,c=getchar();
        return x*d;
    }
    void pr(int x){
        if(!x)return;
        pr(x/10);
        putchar(x%10+48);
    }
    void Print(int x){
        if(x){
            if(x<0)x=-x,putchar('-');
            pr(x);
        }
        else putchar('0');
        putchar('\n');
    }
}

快读,请


by 日居月诸 @ 2021-03-15 20:29:11

请加上优化。

ios::sync_with_stdio(0);
cin.tie(0);

by Ryo_Yamada @ 2021-03-15 20:30:01

fread 不是更香吗


by konjacq @ 2021-03-15 20:31:12

> `std::cin` 读 5e6,不卡你卡谁?

这年头了不会还有人手里没个`fread` / `fwrite` 的 IO 优化板子吧

![cy](https://cdn.luogu.com.cn/upload/pic/62225.png)

申必洛谷排版检测在干什么 我暂且蒙在鼓里

建议你谷关掉排版检测 不然就解释为什么上面那一段未正确标记代码


by 王熙文 @ 2021-03-15 20:31:12

struct FastIO {
    static const int S=1e7;int wpos;char wbuf[S];
    FastIO():wpos(0){}
    inline int readchar(){static char buf[S];static int len=0,pos=0;if(pos==len){pos=0,len=fread(buf,1,S,stdin);}if(pos==len){exit(0);}return buf[pos++];}
    inline void writechar(int x){if(wpos==S){fwrite(wbuf,1,S,stdout),wpos=0;}wbuf[wpos++]=x;}
#ifndef ONLINE_JUDGE
#define readchar getchar
#define writechar putchar
#endif  
    inline int readint(){int s=1,c=readchar(),x=0;while(c<=32){c=readchar();}if(c=='-'){s=-1,c=readchar();}for(;('0'<=c && c<='9');c=readchar()){x=x*10+c-'0';}return x*s;}
    inline int readuint(){int c=readchar(),x=0;while(c<=32){c=readchar();}for(;('0'<=c && c<='9');c=readchar()){x=x*10+c-'0';}return x;}
    inline void writeint(int x){if(x<0){writechar('-'),x=-x;}char s[24];int n=0;while(x || !n){s[n++]='0'+x%10,x/=10;}while(n--){writechar(s[n]);}}
    inline long long readlonglong(){int c=readchar();long long x=0,s=1;while(c<=32){c=readchar();}if(c=='-'){s=-1,c=readchar();}for(;('0'<=c && c<='9');c=readchar()){x=x*10+c-'0';}return x*s;}
    inline void writelonglong(long long x){if(x<0){writechar('-'),x=-x;}char s[25];int n=0;while(x || !n){s[n++]='0'+x%10,x/=10;}while(n--){writechar(s[n]);}}
    inline void readstring(char *s){int c=readchar();while(c<=32){c=readchar();}for(;c>32;c=readchar()){*s++=c;}*s=0;}
    inline void writestring(const char *s){while(*s){writechar(*s++);}}
    inline int readdouble(){int zheng=0,fuhao=1;bool xiaoshudian=0;double xiao=0,chushu=10;char c;while((c=readchar())<'0' || c>'9'){if(c=='-'){fuhao=-1;}}while(c>='0' && c<='9'){zheng=zheng*10+c-'0';c=readchar();}if(c!='.'){return fuhao*zheng;}while((c=readchar())>='0' && c<='9'){xiao+=(c-'0')/chushu;chushu*=10;}return fuhao*(zheng+xiao);}
    ~FastIO(){if(wpos){fwrite(wbuf,1,wpos,stdout),wpos=0;}}
}io;
#define readc  io.readchar
#define writec io.writechar
#define read   io.readint
#define readu  io.readuint
#define write  io.writeint
#define readl  io.readlonglong
#define writel io.writelonglong
#define reads  io.readstring
#define writes io.writestring
#define readd  io.readdouble

by _caiji_ @ 2021-03-15 20:31:57

#include <cstdio>
namespace FAST_IO{
    //#define DEBUG
    class IO_chars{
        private:
            static const int _MAX_IO_SIZE=1<<20;
            char _I_buf[_MAX_IO_SIZE],*_I_p1,*_I_p2;
            char _O_buf[_MAX_IO_SIZE],*_O_p1,*_O_p2;
        public:
            #ifndef DEBUG
            IO_chars():_I_p1(_I_buf),_I_p2(_I_buf),_O_p1(_O_buf),_O_p2(_O_buf+_MAX_IO_SIZE){}
            ~IO_chars(){std::fwrite(_O_buf,1,_O_p1-_O_buf,stdout);}
            #endif
            char getchar(){
                #ifdef DEBUG
                return std::getchar();
                #endif
                if(_I_p1==_I_p2){
                    _I_p1=_I_buf;
                    _I_p2=_I_p1+std::fread(_I_buf,1,_MAX_IO_SIZE,stdin);
                }
                return _I_p1==_I_p2?EOF:*_I_p1++;
            }
            void putchar(const char &_O_ch){
                #ifdef DEBUG
                std::putchar(_O_ch);
                return ;
                #endif
                if(_O_p1==_O_p2){
                    std::fwrite(_O_buf,1,_MAX_IO_SIZE,stdout);
                    _O_p1=_O_buf;
                }
                *_O_p1++=_O_ch;
            }
    } IOER;
    class IO{
        private:
            static const int _MAX_LEN=20,_ZERO=(int)'0';
            int stack[_MAX_LEN];
        public:
            bool isdigit(const char &ch){return '0'<=ch&&ch<='9';}
            bool isblank(const char &ch){return ch==' '||ch==EOF||ch=='\n'||ch=='\r'||ch=='\t';}
            template<typename T>
            inline void read(T &x){
                #ifdef DEBUG
                std::scanf("%d",&x);
                return ;
                #endif
                x=0;int f=0;char ch=IOER.getchar();
                for(;!isdigit(ch);f|=(ch=='-'),ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(; isdigit(ch);ch=IOER.getchar())
                    x=(x<<3)+(x<<1)+(ch^_ZERO);
                if(f) x=(~x+1);
            }
            template<typename T>
            inline void write(const T &x){
                #ifdef DEBUG
                std::printf("%d",x);
                return ;
                #endif
                int num=x,top=0;
                if(num<0) num=(~num+1),IOER.putchar('-');
                do stack[top++]=num%10,num/=10; while(num);
                while(top) IOER.putchar(stack[--top]+_ZERO);
            }
            inline void readstr(char *a){
                #ifdef DEBUG
                std::scanf("%s",a);
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; isblank(ch);ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!isblank(ch);ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void gets(char *a){
                #ifdef DEBUG
                std::scanf("%[^\n\r]c",a);
                //不知道这种方式读入的字符串有没有\0,没有那就换成std::gets吧
                return ;
                #endif
                int i=0;char ch=IOER.getchar();
                for(; (ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    if(ch==EOF) return ;
                for(;!(ch==EOF||ch=='\n'||ch=='\r');ch=IOER.getchar())
                    a[i++]=ch;
                a[i]='\0';
            }
            inline void puts(const char *a){
                #ifdef DEBUG
                std::puts(a);
                return ;
                #endif
                for(int i=0;a[i]!='\0';i++)
                    IOER.putchar(a[i]);
                IOER.putchar('\n');
            }
    } io;
    //#undef DEBUG
}
using FAST_IO::io;
int x;
char a[10010];
int main(){
    io.read(x);
    io.write(x);
    io.puts("");
    io.gets(a);
    io.puts(a);
    return 0;
}

| 下一页