C++自用模板

本文是本人自用的 C++ 模板,防止本人换电脑后找不到。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
#define N ((int)())


signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    
    return 0;
}

以下是注释版

// 万能头文件几乎都能用,很少数情况不能用
#include <bits/stdc++.h>
using namespace std;
// 懒的写 long long 的写法,多数情况没什么问题
#define int long long
// 0x3f...的写法防止 INF + INF 爆 long long
// 如果需要 int 型的 INF 只需要 4 个 3f 即可,0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// 为变量 x 取 max 和取 min 的函数
template<typename T> void up(T &x, T y) { x < y ? x = y : 0; }
template<typename T> void down(T &x, T y) { x > y ? x = y : 0; }
// 下面两行自动进行文本替换,减少出错概率、加速敲代码
#define rep(i, a, b) for(int i = (a), i##END = (b); i <= i##END; i++)
#define Rep(i, a, b) for(int i = (a), i##END = (b); i >= i##END; i--)
// 下面这行定义某个数组开多大,比如填 1e3 + 15,那么就是 a[1015]
#define N ((int)())


signed main() // #define int long long 后只能写 signed main
{
    ios::sync_with_stdio(0); // 关闭 C++ 输入输出流与 C 输入输出函数的同步机制,使用后不可混用两种输入方式
    cin.tie(0); cout.tie(0); // 解绑 cin 刷新 cout 输出缓冲区的操作,加速读写
    freopen("check.in","r",stdin); // 输入文件为同目录下的 check.in 文件
    freopen("check.out","w",stdout); // 输出文件为同目录下的 check.out 文件,会优先清空 check.out
    
    return 0;
}

关于万能头文件,详见 MacOS 下使用C++万能头文件

另外在开头部分可选加入或替换的一些内容如下,仅供参考

typedef long long ll; // 须替换 #define int long long
// 这样就可以 ll ans = 0; 这样声明 long long 变量了

typedef pair<int,int> pii;

#define lowbit(x) ((x) & (-(x)))
#define popc(x) __builtin_popcountll(x)

const double eps = 1e-8;
int dcmp(double x) { if(fabs(x) <= eps) return 0; return x > eps ? 1 : -1; } // 判符号

#define ls(x) ((x) << 1)
#define rs(x) ((x) << 1 | 1)

const int mod = 1e9 + 7;
template<typename T> void add(T & x, T y) { (x += y) >= mod ? x -= mod : 0; }
// 取模加法 add(ans, x % mod), 0 <= ans < mod < x
// 取模减法 add(ans, mod - x % mod), 0 <= ans < mod < x
// 注意 add(x,y) 传进去的 x 和 y 必须都为非负数且小于 mod , 否则多半出错
// 这个主要是 % mod 操作用太多会降低速度,而很多 % mod 操作是没必要的