Skip to content

Instantly share code, notes, and snippets.

@toxdes
Last active November 21, 2022 02:54
Show Gist options
  • Save toxdes/e3908fefd2a7f587ea08110021bd7086 to your computer and use it in GitHub Desktop.
Save toxdes/e3908fefd2a7f587ea08110021bd7086 to your computer and use it in GitHub Desktop.
vscode-cpp-snippets
{
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"template-with-testcase": {
"prefix": "qw",
"body": [
"#include \"bits/stdc++.h\"",
"#ifdef LOCAL",
"#include \"bits/debug.h\"",
"#else",
"#define dd(...) 42",
"#endif",
"#define ll long long",
"#define ull unsigned long long",
"#define nl '\\n'",
"#define all(a) begin(a), end(a)",
"#define rall(a) rbegin(a) rend(a)",
"#define sz(a) ((int)a.size())",
"#define ff first",
"#define ss second",
"#define f(i, n) for (int i = 0; i < n; ++i)",
"// #define PROB_NAME \"sliding\"",
"",
"using namespace std;",
"",
"// const int INF = 1e9;",
"// const ll INFF = 1e18;",
"// const int M = 1e9 + 7;",
"",
"ll g, h, n, m, k, p, q, x, y, z;",
"",
"void solve() {",
" $0",
"}",
"",
"void TESTCASES() {",
" int t = 1;",
" cin >> t;",
" for (int i = 1; i <= t; ++i) {",
" solve();",
" }",
"}",
"",
"int32_t main() {",
" ios_base::sync_with_stdio(false);",
" cin.tie(0);",
"#ifdef PROB_NAME",
" freopen(PROB_NAME \".in\", \"r\", stdin);",
" freopen(PROB_NAME \".out\", \"w\", stdout);",
"#endif",
" TESTCASES();",
"}"
],
"description": "template"
},
"debug-template": {
"prefix": "qd",
"body": [
"void __print(int x) { cerr << x; }",
"void __print(long x) { cerr << x; }",
"void __print(long long x) { cerr << x; }",
"void __print(unsigned x) { cerr << x; }",
"void __print(unsigned long x) { cerr << x; }",
"void __print(unsigned long long x) { cerr << x; }",
"void __print(float x) { cerr << x; }",
"void __print(double x) { cerr << x; }",
"void __print(long double x) { cerr << x; }",
"void __print(char x) { cerr << '\\'' << x << '\\''; }",
"void __print(const char *x) { cerr << '\\\"' << x << '\\\"'; }",
"void __print(const string &x) { cerr << '\\\"' << x << '\\\"'; }",
"void __print(bool x) { cerr << (x ? \"true\" : \"false\"); }",
"",
"template <typename T, typename V>",
"void __print(const pair<T, V> &x) {",
" cerr << '{';",
" __print(x.first);",
" cerr << ',';",
" __print(x.second);",
" cerr << '}';",
"}",
"template <typename T>",
"void __print(const T &x) {",
" int f = 0;",
" cerr << '{';",
" for (auto &i : x) cerr << (f++ ? \",\" : \"\"), __print(i);",
" cerr << \"}\";",
"}",
"void _print() { cerr << \"]\\n\"; }",
"template <typename T, typename... V>",
"void _print(T t, V... v) {",
" __print(t);",
" if (sizeof...(v)) cerr << \", \";",
" _print(v...);",
"}",
"#ifndef ONLINE_JUDGE",
"#define dd(x...) \\",
" cerr << \"[L\" << __LINE__ << \"] \" \\",
" << \"[\" << #x << \"] = [\"; \\",
" _print(x)",
"#else",
"#define dd(x...)",
"#endif",
""
],
"description": "Code for easier debugging of variables. https://codeforces.com/topic/69283/en1"
},
"Ceiling Division of two integers": {
"prefix": "tcdiv",
"body": [
"template <typename T>",
"inline T cdiv(T a, T b) {",
" return ((a / b) + (((a / b) * b) < a));",
"}"
],
"description": "Ceiling Division of two integers"
},
"Modulo Operations": {
"prefix": "tmod",
"body": [
"const ll mod = 1e9 + 7;",
"namespace modop {",
"ll madd(ll a, ll b) { return (a + b) % mod; }",
"ll msub(ll a, ll b) { return (((a - b) % mod) + mod) % mod; }",
"ll mmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }",
"ll mpow(ll base, ll exp) {",
" ll res = 1;",
" while (exp) {",
" if (exp % 2 == 1) {",
" res = (res * base) % mod;",
" }",
" exp >>= 1;",
" base = (base * base) % mod;",
" }",
" return res;",
"}",
"ll minv(ll base) { return mpow(base, mod - 2); }",
"ll mdiv(ll a, ll b) { return mmul(a, minv(b)); }",
"} // namespace modop",
"",
"using namespace modop;",
"",
""
],
"description": "Modulo Operations"
},
"Dijkstra Template": {
"prefix": "tdijkstra",
"body": [
"struct dijkstra {",
" int n;",
" const ll inf = 4e18;",
" vector<ll> dists; /* for a single run */",
" vector<int> par;",
" vector<bool> vis;",
" vector<vector<pair<ll, int>>> edges; /* weight, to */",
"",
" void init(int s) {",
" n = s;",
" dists = vector<ll>(n);",
" vis = vector<bool>(n);",
" par = vector<int>(n);",
" edges = vector<vector<pair<ll, int>>>(n);",
" }",
"",
" void edge(int a, int b, ll wt) {",
" edges[a].push_back(make_pair(wt, b));",
" edges[b].push_back(make_pair(wt, a));",
" }",
"",
" using ptype = pair<ll, int>;",
" void run(int src) {",
" fill(dists.begin(), dists.end(), inf);",
" fill(vis.begin(), vis.end(), false);",
" fill(par.begin(), par.end(), -1);",
"",
" priority_queue<ptype, vector<ptype>, greater<ptype>> pq;",
" dists[src] = 0;",
" pq.push(make_pair(0, src));",
" while (!pq.empty()) {",
" ptype foc = pq.top();",
" pq.pop();",
"",
" if (vis[foc.s]) continue;",
" vis[foc.s] = 1;",
"",
" dists[foc.s] = min(dists[foc.s], foc.f);",
" for (ptype x : edges[foc.s]) {",
" ll d = dists[foc.s] + x.f;",
" if (d < dists[x.s]) {",
" dists[x.s] = d;",
" par[x.s] = foc.s;",
" pq.push(make_pair(d, x.s));",
" }",
" }",
" }",
" }",
"};"
],
"description": "Dijkstra Template"
},
"DSU - Disjoint Set-Union DS": {
"prefix": "tdsu",
"body": [
"template <typename n_t>",
"struct dsu {",
" vector<n_t> parent;",
" void init(int size) {",
" parent = vector<n_t>(size);",
" for (n_t i = 0; i < (n_t)size; ++i) parent[i] = i;",
" }",
" void make_set(n_t v) { parent[v] = v; }",
" int find_set(n_t v) {",
" if (v == parent[v]) return v;",
" // with path compression",
" return parent[v] = find_set(parent[v]);",
" }",
" void union_sets(n_t a, n_t b) {",
" a = find_set(a);",
" b = find_set(b);",
" if (a != b) parent[b] = a;",
" }",
"};"
],
"description": "Disjoint Set Union DS with path compression"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment