んぐのルーズリーフ

んぐの日記。最近はScrapBoxが主

【C++】競プロ用のテンプレートとスニペット紹介

んぐです。

今回は僕が普段使っている競プロ用のC++テンプレートを紹介します。

テンプレート

こんな感じのテンプレートを使っています。軸として「繰り返しを避ける」というのを設定して作成しています。

# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define range_for(i, b, e) for(int i=(b), i##_len=(e); i<=i##_len; ++i)
# define step(n) rep(_, n)
# define ALL(x) (x).begin(), (x).end()
# define RALL(x) (x).rbegin(), (x).rend()
# define Unique(a) a.erase(unique(ALL(a)), a.end())
# define pb push_back
# define len(x) ((int)(x).size())
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
# define debug(x) std::cerr<<#x<<": "<<(x)<<endl;
# define LINT_MAX (LLONG_MAX)
# define LINT_MIN (LLONG_MIN)
# define cauto const auto
using namespace std;
using lint = long long;
template <class Type> inline constexpr Type Square(Type x) { return x * x; }
template <class Type> inline constexpr bool InRange(const Type& x, const Type& i, const Type& a) { return (i <= x) && (x <= a); }
template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a<b) { a=b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b<a) { a=b; return 1; } return 0; }
template<class Integer>bool IsOdd(Integer &n) { return n & 1; }
template<class Integer>bool IsEven(Integer &n) { return !(n & 1); }
int ctoi(const char c) { return ('0' <= c && c <= '9') ? (c - '0') : -1; }
string YesNo(bool b) { return b ? "Yes" : "No"; }
string YESNO(bool b) { return b ? "YES" : "NO"; }
string yesno(bool b) { return b ? "yes" : "no"; }
void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
#define Cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define Cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define Cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define Cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]
void Print() { cout << endl; }
template <class Head, class... Tail> void Print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; Print(forward<Tail>(tail)...); }
template <class Type> void Print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void Print(vector<vector<Type>> &df) { for (auto& vec : df) { Print(vec); } }
void Debug() { cerr << endl; }
template <class Head, class... Tail> void Debug(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(tail) != 0) cerr << " "; Debug(forward<Tail>(tail)...); }
template <class Type> void Debug(vector<Type> &vec) { for (auto& a : vec) { cerr << a; if (&a != &vec.back()) cerr << " "; } cerr << endl; }
template <class Type> void Debug(vector<vector<Type>> &df) { for (auto& vec : df) { Debug(vec); } }

int main()
{
    

    return 0;
}


repaet関係

for文短縮系のマクロです。

# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define range_for(i, b, e) for(int i=(b), i##_len=(e); i<=i##_len; ++i)
# define step(n) rep(_, n)

repを使うメリットは - カウント変数iを繰り返し書くことを避ける - 二重forを書いたとき for (int j = 0; j < N; ++i) などの典型的なミスを避ける - 「普通の」for文ですよ、ということを明示的にする(逆にforを書くということは特別な処理を書いているということを強く認識させる) - 読みやすい

などが挙げられます。決して短く書くってだけの理由ではないわけですね。そういうわけでrepeatなどと定義する人もいるみたいです。 ちなみに興味本位でstepというマクロを追加しましたが未だ使ったことはありません。正直競プロでは使わなそう。


STL関係マクロ

# define ALL(x) (x).begin(), (x).end()
# define RALL(x) (x).rbegin(), (x).rend()

このマクロは省略の意味が強いですね。とはいえ普通に書くとxを2回書く必要があるのでそれによって発生するミスを防ぐことができます。 sortを始めとしてSTL関係の関数は最初と最後のイテレータを渡す必要があることが多いので重宝します。


# define Unique(a) a.erase(unique(ALL(a)), a.end())

vectorなどのデータ構造について、要素の重複を削除するためのマクロです。普通にunique使うと削除されずuniqueな要素を左から寄せ集めるだけなので、後からeraseする必要があります。覚えにくいのもあって定義しています。


# define pb push_back
# define len(x) ((int)(x).size())

pbに関しては怠惰によるマクロ定義です。深い意味はありません。

しかし、len(x)の方にはちゃんと意味があって、x.size()は通常unsignedな整数型を返すようで、それにより発生するバグをキャストすることで防いでいます。 たまにこのunsignedに泣く人がいるみたいなので。

入出力最適化

# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)

早くなるらしいです。でもcoutcinが入り乱れるようなプログラムだと致命的なバグになりうるようです。 危険そうなので結局使っていません。


void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
#define Cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define Cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define Cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define Cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<int>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]

cinを簡単にするマクロです。こんな使い方をします。

Cin(int, a, b, c); // a b c
Cinv(int, A, n); // A1 A2 ... An

普通に書くとint a, b, c; cin >> a >> b >> c;と意外とタイプ量多くて辛いんですよね。


void Print() { cout << endl; }
template <class Head, class... Tail> void Print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; Print(forward<Tail>(tail)...); }
template <class Type> void Print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void Print(vector<vector<Type>> &df) { for (auto& vec : df) { Print(vec); } }
void Debug() { cerr << endl; }
template <class Head, class... Tail> void Debug(Head&& head, Tail&&... tail) { cerr << head; if (sizeof...(tail) != 0) cerr << " "; Debug(forward<Tail>(tail)...); }
template <class Type> void Debug(vector<Type> &vec) { for (auto& a : vec) { cerr << a; if (&a != &vec.back()) cerr << " "; } cerr << endl; }
template <class Type> void Debug(vector<vector<Type>> &df) { for (auto& vec : df) { Debug(vec); } }

Pythonライクにprintできます。間に空白を入れなければならないような出力形式だと便利です。


その他

# define debug(x) std::cerr<<#x<<": "<<(x)<<endl;

変数名とともに中の値を標準エラー出力します。使っていません。


# define LINT_MAX (LLONG_MAX)
# define LINT_MIN (LLONG_MIN)
# define cauto const auto
using lint = long long;

型関係です。僕はllよりもlintが好きなのでそうしています。理由は

  • integer感を残したかった
  • lが2回続くのが嫌だった
  • int64は打ちにくい、押し心地が一番良い

です。


template <class Type> inline constexpr Type Square(Type x) { return x * x; }
template <class Type> inline constexpr bool InRange(const Type& x, const Type& i, const Type& a) { return (i <= x) && (x <= a); }

template<class Integer>bool IsOdd(Integer &n) { return n & 1; }
template<class Integer>bool IsEven(Integer &n) { return !(n & 1); }

string YesNo(bool b) { return b ? "Yes" : "No"; }
string YESNO(bool b) { return b ? "YES" : "NO"; }
string yesno(bool b) { return b ? "yes" : "no"; }

タイプミス防止や見栄えを重視して定義しています。IsOddIsEvenの参照を外すか迷ってます。参照だとif (IsEven(n/2))みたいなことができません。

あと、InRange(x, -1, 1)の形式にするかInRange(-1, x, 1)の形式にするか悩ましいところですね。


template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a<b) { a=b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b<a) { a=b; return 1; } return 0; }

「もしaのほうが小さければ(大きければ)、bに更新する」関数です。競プロでは頻出の構文です。たとえばrep (i, n) ans = a[i]; // a[i]の最大値を取得みたいな使い方ができますね。


int ctoi(const char c) { return ('0' <= c && c <= '9') ? (c - '0') : -1; }

実はC++にはこの関数がないので定義しています。なんか標準でいい関数ないんですかね。


スニペット

snippet for-bit "bit全探索" b
for (int bit = 0; bit < (1<<${1:n}); ++bit)
{
    for (int ${2:i} = 0; ${2:i} < ${1:n}; ++${2:i})
    {
        $3
        if (bit & (1<<${2:i}))
        {
        }
    }
}
endsnippet

snippet mint "auto mod int" b
static const int MOD = 1000000007;
struct mint {
    long long x;
    mint(long long x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(long long t) const {
    if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    mint inv() const { return pow(MOD-2); }
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
    bool operator<(const mint& iValue) const { return this->x < iValue.x; }
    bool operator<=(const mint& iValue) const { return this->x <= iValue.x; }
    bool operator>(const mint& iValue) const { return this->x > iValue.x; }
    bool operator>=(const mint& iValue) const { return this->x >= iValue.x; }
};
inline std::istream& operator >>(std::istream& is, const mint& a) { return is >> a.x; }
inline std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x; }
endsnippet

snippet combination "二項係数クラス" b
struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
};
endsnippet

snippet divisor "ある数の約数を列挙する( O(√N) )" b
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i*i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(begin(ret), end(ret));
    return (ret);
}
endsnippet

snippet divisor "素因数分解する( O(√N) )" b
map<long long, int> prime_factor(long long n) {
    map<long long, int> ret;
    for (long long i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            ret[i]++;
            n /= i;
        }
    }
    if (n != 1) ret[n] = 1;
    return ret;
}
endsnippet

snippet dxdy "4近傍" b
static const int dx[4] = {1, 0, -1, 0};
static const int dy[4] = {0, 1, 0, -1};
endsnippet


snippet phash "pairをsetやpairで利用するためのhash" b
struct phash {
    inline size_t operator ()(const pair<int,int>& p) const {
        const auto h1 = hash<int>()(p.first);
        const auto h2 = hash<int>()(p.second);
        return h1 ^ (h2 << 1);
    }
};
endsnippet

snippet Point "座標を表すためのpairエイリアス" b
using P = pair<lint, lint>;
# define X first
# define Y second
endsnippet

snippet GetDigit "非負整数の桁数を返す" b
template <class Integer> Integer GetDigit(Integer n) {
    if (n == 0) return 1;
    Integer d = 0;
    while (n) {
        n /= 10;
        d++;
    }
    return d;
}
endsnippet

snippet IsPrime "素数かどうかを返す" b
template <class Integer>
constexpr bool IsPrime(const Integer n) noexcept {
    if (n < 4) return n == 2 || n == 3;
    if (n % 2 == 0 || n % 3 == 0 || (n % 6 != 1 && n % 6 != 5)) return false;
    for (Integer i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false;
    return true;
}
endsnippet

スニペットcoc-nvimを利用しています。

github.com

典型アルゴリズムをまとめてます。個人的にPointはよく使いますね。mintが必要な問題解けるようになりたいな。


まとめ

ざっと紹介しました。個人的にはライブラリが小さすぎず大きすぎずで結構気に入っています。

また実力をあげて必要なライブラリが増えるといいですね。