准备CSP 复赛

AC-13-13 / 2024-11-11 / 原文

用来方便自己复习

版本C++14

目录
  • 快读和快输
  • 注意事项
  • 缺省源
  • 对拍
  • 编译器选项

快读和快输

链接:

  • 浅谈C++ IO优化——读优输优方法集锦
  • 最全快读、快写模板「持续更新」 - 凌云_void - 博客园
  • 读入、输出优化 - OI Wiki

打的时候一定要注意运算符优先级QWQ(有时候真的很难发现)

错误示例:

int read()
{
	static int x,f;
	static char ch;
	x=f=0;
	while(!isdigit(ch=getchar()) ) if(ch=='-') f=!f;
	while(isdigit(ch)) x=(x<<3)+(x<<1)+ch&15,ch=getchar();
	if(!f) return x;
	else return -x;
}

原因: 加法的优先级高于按位与……

正常代码:

const int L=1<<20;
char buf[L],*p1=buf,*p2=buf;
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,L,stdin),p1==p2)?EOF:*p1++)

int read()
{
	static int x,f;
	static char ch;
	x=f=0;
	while(!isdigit(ch=getchar()) ) if(ch=='-') f=!f;
	while(isdigit(ch)) x=x*10+(ch&15),ch=getchar();
	if(!f) return x;
	else return -x;
}

小数据(\(n\le 10^3\))还是scanf快点

注意事项

链接:

  • CSP/NOIP 防爆指南 - ahawzlc - 博客园

缺省源

//减少码量的
#define rep(i,st,n) for(int i=(st);i<=(n);++i)
#define _rep(i,st,n) for(int i=(st);i<(n);++i)
#define dwh(i,st,n) for(int i=(st);i>=(n);--i)
#define _dwh(i,st,n) for(int i=(st);i>(n);--i)

//快读快输...
//...

//还是想cin的
#define FAST_IO ios::sync_with_stdio(0),cin.tie(0)

//max() and min()
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int bmax(int &x,int y){return x=max(x,y);}
int bmin(int &x,int y){return x=min(x,y);}

对拍

程序

编译器选项

链接:

  • Awesome sanitizers - 洛谷专栏
  • 使用g++编译器扩大程序可用栈空间

编译器选项加入:

-Wl,--stack=536870912 -std=c++14 -lm -Wall

解释:

  • -Wl, 可用于查找一些编译器编译时无法查出的问题,如scanf("%d",a)
  • -Wl,--stack=SIZE 把栈的空间限制为SIZE byte(536870912 byte=512MB)
  • -std=c++14 按照c++14的标准编译
  • -Wall 打开"显示最多警告信息"