常用代码块

大概是写给我自己看的

一般的读入优化

#define GETCHAR getchar()
#define UNGETCHAR(c) ungetc(c, stdin)

template<typename IntType = int>   // 模板默认类型需要C++11
IntType read() {
	IntType val = 0;
	int c;
	bool invflag = false;
	while (!isdigit(c = GETCHAR))
		if (c == '-')
			invflag = true;
	do {
		val = (val << 1) + (val << 3) + c - '0';
	} while (isdigit(c = GETCHAR));
	UNGETCHAR(c);
	if (invflag)
		return -val;
	else
		return val;
}
template<>
string read<string>() {
	string str;
	str.clear();
	int c;
	while (iscntrl(c = GETCHAR) || isblank(c));
	do {
		str.push_back(c);
	} while (!(iscntrl(c = GETCHAR) || isblank(c)));
	UNGETCHAR(c);
	return str;
}
template<typename IntType>
void read(IntType& x) { x = read<IntType>(); }
template<typename IntType, typename... Args>     // 模板参数包需要C++11
void read(IntType& x, Args&... args) { x = read<IntType>(); read(args...); }

调试输出

#ifdef DEBUG    // ↓不需要C++11
    #define debug(...) printf(__VA_ARGS__)
#else
    #define debug(...)
#endif

几乎最快的fread读入

constexpr int buffersize = 80 * 1024 * 1024;    // 80M的缓冲区(开局占掉八十兆内存)
char buffer[buffersize], *buffertop = buffer;
#define GETCHAR *(buffertop++)
#define UNGETCHAR(c) (--buffertop)

...(与一般的读入优化相同)...
int main() {
    fread(buffer, 1, buffersize, stdin);
    ...
}

还有快速写

char bufferwrite[buffersize], *writetop = bufferwrite;
#define PUTCHAR(c) (*(writetop++) = (c))

template<typename IntType>
void println(IntType val) {
    if (val == 0)
        PUTCHAR('0');
    if (val < 0) {
        PUTCHAR('-');
        val = -val;
    }
    char buf[16], *buftop = buf + 15;
    while (val > 0) {
        *buftop = (val % 10 + '0');
        buftop--;
        val /= 10;
    }
    for (buftop++; buftop <= buf + 15; buftop++)
        PUTCHAR(*buftop);
    PUTCHAR('\n');
}
int main() {
    ...
    fwrite(bufferwrite, 1, writetop - bufferwrite, stdout);
}

手写栈、队列

template<typename Type, int Size = MaxN>
class stack {
public:
    stack() :stacktop(0) {}
    void clear() { stacktop = 0; }
    void push(Type x) { arr[stacktop++] = x; }
    void pop() { if (stacktop > 0) stacktop--; }
    Type top() { return arr[stacktop - 1]; }
    bool empty() { return stacktop <= 0; }
    int size() { return stacktop; }
private:
    int stacktop;
    Type arr[Size];
};
template<typename Type, int Size = MaxN>
class queue {
public:
    queue() { clear(); }
    void clear() { left = 1; right = 0; }
    Type front() { return arr[left]; }
    void push(Type x) { arr[++right] = x; }
    void pop() { left++; }
    bool empty() { return !(right >= left); }
private:
    int left, right;
    Type arr[Size];
};

遇到极度卡常时可以手动开O3(G++专用优化参数):

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

发表评论

电子邮件地址不会被公开。 必填项已用*标注