\(\text{Warning}\):一定要注意函数返回值,不然可能会错到妈都不认识 qwq.
# include <cstdio>
# include <cctype>
# define print(x,y) write(x), putchar(y)
# define debug(...) do { cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush; } while(0)
template <class T>
inline T read(const T sample) {
T x=0; char s; bool f=0;
while(!isdigit(s=getchar())) f|=(s=='-');
for(; isdigit(s); s=getchar()) x=(x<<1)+(x<<3)+(s^48);
return f? -x : x;
}
template <class T>
inline void write(T x) {
static int writ[50], w_tp=0;
if(x<0) putchar('-'), x=-x;
do writ[++w_tp]=x-x/10*10, x/=10; while(x);
while(putchar(writ[w_tp--]^48), w_tp);
}
template <typename T> void Out(T x) { cerr<<x<<"\n"; }
template <typename T, typename ...I> void Out(T x,I ...NEXT) { cerr<<x<<", "; Out(NEXT...); }
template <class T> inline T fab(const T x) { return x>0?x:-x; }
template <class T> inline void Swap(T &x,T &y) { x^=y^=x^=y; } // notice the return value!!!
另外还有在 int
范围内取 \(\max,\min\) 的函数:
inline int Max(int x,int y) { int m=(x-y)>>31; return y&m|x&~m; }
inline int Min(int x,int y) { int m=(y-x)>>31; return y&m|x&~m; }
它的原理是当 \(x<y\) 时,\(m\) 为 \(-1\);当 \(x\ge y\) 时,\(m\) 为 \(0\)(对于 \(\max\) 而言)。
嫖了一个 \(\text{FastIO}\),也放上来了:
namespace FastIO {
const int SIZE = (1<<21)+1;
int qr; bool f;
char ibuf[SIZE],*iS,*iT,obuf[SIZE];
char *oS=obuf,*oT=oS+SIZE-1,c,qu[55];
#define print(x,y) write(x),putc(y)
#define gc() \
(iS==iT?(iT = (iS=ibuf)+fread(ibuf,1,SIZE,stdin), \
(iS==iT?EOF:*iS++)) \
:*iS++)
/* print the remaining part */
inline void flush() {
fwrite(obuf,1,oS-obuf,stdout);
oS=obuf;
}
inline void putc(char x) {
*oS++ = x;
if(oS==oT) flush();
}
/* input a signed integer */
template <class I>
inline I read(const I sample) {
I x=0; f=0;
while((c=gc())>'9' or c<'0')
f |= (c=='-');
while(c>='0' and c<='9')
x = (x<<1)+(x<<3)+(c^48),
c = gc();
return f?-x:x;
}
/* print a signed integer */
template <class I>
inline void write(I x) {
if(!x) putc('0'); if(x<0) putc('-'),x=-x;
while(x) qu[++qr] = (x-x/10*10)^48,x/=10;
while(qr) putc(qu[qr--]);
}
/* no need to call flush at the end manually! */
struct Flusher_ {~Flusher_(){flush();}} io_flusher_;
}
using FastIO::putc;
using FastIO::read;
using FastIO::write;
需要注意 \(\text{FastIO}\) 和 scanf
不能混用!
最后再加一个编译命令的起手式:
-Wall
:用于启用一批比较常见且易于修改的警告;
-Wextra
:上面的补充;
-Wshadow
:当局部变量屏蔽(\(\rm shadow\))已有已有变量时发出警告;
-fsanitize=undefined
:检测未定义行为;
-Wconversion
:当发生隐式转换时发出警告。