一些(或许)有用的 OI 琐碎内容整理

发布于 2020-10-16  2.57k 次阅读


原来这篇博文放了一堆板子,后来发现一点用都没有,还不如直接在洛谷搜模板,于是就改造了一下,现在该博文会陆续放一些非板子的(或许)有用的内容,比较琐杂。

文章目录

快读快写

手写快读

来源:青珹的博客

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

使用 x=read() 读入一个 int 型的值 x(可以将函数中的 int 改为 long long 以读入 long long 型的值)。

IO快读

来源:Luogu7384 说明/提示

#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template <typename T>
inline void read(T& r) {
    r=0;bool w=0; char ch=getchar();
    while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
    while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
    r=w?-r:r;
}

使用 read(x); 读入一个任意的整型 x 等价于 scanf("%d", &x);其中可以将 %d 自动识别为对应类型。

手写快写

来源:_Zer0 的博客

inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

特别的,对于 __int128 型的变量,必须使用快读/快写进行读写操作,因此掌握手写快读/快写很有必要。

对拍

来源:本人

#include<bits/stdc++.h>
using namespace std;
int main(){
    for(int t=1;t<=10000;t++){
        system("rand.exe>in.in");
        system("baoli.exe<in.in>baoli.out");
        system("mine.exe<in.in>mine.out");
        if(system("fc baoli.out mine.out")) break;
    }
    return 0;
}

rand.exe,baoli.exe,mine.exe 分别表示:数据生成程序,暴力程序,自己写的复杂度更优但正确性尚需验证的程序。

(To be continue……)


月流华 岁遗沙 万古吴钩出玉匣