Skip to main content

Overview

Templates provide starting points for your competitive programming solutions. Each template is optimized for specific contest formats and includes essential utilities.

Available Templates

Standard Template

Trigger: template_std The most comprehensive template for general competitive programming.
/**
 * @created     : March 05, 2026
 * @handle      : @LuchoBazz
 */

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    return 0;
}
Features:
  • Clean, minimal starting point
  • Fast I/O optimization
  • Date and author placeholders
  • Compatible with all contest formats
Best for:
  • Codeforces rounds
  • AtCoder contests
  • General practice problems
This is the most commonly used template. Start here if unsure which template to choose.

Header Template

Trigger: template_header Comprehensive header with optimizations and utility macros.
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

#define endl '\n'
#define _ << ' ' <<
#define PB push_back
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

// Loop macros
#define forn(i, a) for (int i = 0; i < int(a); ++i)
#define rof(i, a) for (int i = int(a) - 1; i >= 0; --i)

// Constants
const string ALPHA = "abcdefghijklmnopqrstuvwxyz";
const string YES = "YES", NO = "NO";
const int d4x[4] = {0, -1, 1, 0};
const int d4y[4] = {-1, 0, 0, 1};

// Utility functions
template <class T, class S> 
inline bool xmax(T &a, const S &b) {
    return (a < b ? a = b, 1 : 0);
}

template <class T, class S> 
inline bool xmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}

// Type aliases
using ll = int64_t;
using ld = long double;
Features:
  • Compiler optimization pragmas
  • Common macros and shortcuts
  • Direction arrays for grid problems
  • Min/max utility functions
  • Vector I/O operators
  • Type aliases
Best for:
  • Complex problems requiring many utilities
  • When you need grid navigation
  • Problems with lots of container operations
Optimization pragmas only activate on online judges to avoid local debugging issues.

Test Case Template

Trigger: template_cases Optimized for problems with multiple test cases.
#include <bits/stdc++.h>
using namespace std;

void solve() {
    // Your solution here
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int T;
    cin >> T;
    while(T--) {
        solve();
    }
    
    return 0;
}
Features:
  • Clean separation of solve logic
  • Handles multiple test cases automatically
  • Fast I/O enabled
Best for:
  • Codeforces Division 2/3 problems
  • Most AtCoder contests
  • Any problem starting with “T test cases”

Google Template

Trigger: template_google Specialized for Google Code Jam and Kick Start.
#include <bits/stdc++.h>
using namespace std;

void solve(int case_num) {
    // Your solution here
    
    cout << "Case #" << case_num << ": ";
    // Output answer
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int T;
    cin >> T;
    for(int t = 1; t <= T; t++) {
        solve(t);
    }
    
    return 0;
}
Features:
  • Automatic case numbering
  • Proper output format: Case #X: answer
  • One-indexed test cases
Best for:
  • Google Code Jam
  • Google Kick Start
  • Facebook Hacker Cup
Google competitions require exact output format. Don’t modify the case numbering format.

USACO Template

Trigger: template_usaco Optimized for USA Computing Olympiad format.
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    freopen("problemname.in", "r", stdin);
    freopen("problemname.out", "w", stdout);
    
    // Your solution here
    
    return 0;
}
Features:
  • File I/O setup
  • Reads from .in file
  • Writes to .out file
  • Automatic file handling
Best for:
  • USACO contests
  • Any competition requiring file I/O
Replace "problemname" with the actual problem name (e.g., "paint.in" and "paint.out").

LeetCode Template

Trigger: template_std_leetcode Class-based structure for LeetCode problems.
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    // Your solution method here
};

int main() {
    Solution sol;
    
    // Test your solution
    
    return 0;
}
Features:
  • Class-based structure matching LeetCode format
  • Easy local testing setup
  • Compatible with LeetCode copy-paste
Best for:
  • LeetCode practice
  • Interview preparation
  • Class-based problem formats

Random Test Template

Trigger: template_random Generate random test cases for stress testing.
#include <bits/stdc++.h>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int rand_int(int l, int r) {
    uniform_int_distribution<int> dist(l, r);
    return dist(rng);
}

vector<int> rand_vector(int n, int l, int r) {
    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        v[i] = rand_int(l, r);
    }
    return v;
}

int main() {
    // Generate test cases
    int n = rand_int(1, 100);
    vector<int> arr = rand_vector(n, 1, 1000);
    
    return 0;
}
Features:
  • Proper random number generation
  • Utility functions for common random data
  • Stress testing helpers
Best for:
  • Finding edge cases
  • Comparing brute force vs optimized solutions
  • Validating correctness
Use with random_init, random_vector, and random_permutation snippets for more functionality.

Template Selection Guide

Decision Tree

1

Identify Contest Type

General Contests

Codeforces, AtCoder, etc.template_std or template_header

Google Competitions

Code Jam, Kick Starttemplate_google

USACO

USA Computing Olympiadtemplate_usaco

LeetCode

Interview practicetemplate_std_leetcode
2

Consider Problem Format

Single test case:
  • Use template_std
Multiple test cases:
  • Use template_cases
File I/O:
  • Use template_usaco
Stress testing:
  • Use template_random
3

Assess Complexity Needs

Simple problem:
  • template_std (minimal)
Complex problem:
  • template_header (full utilities)
Grid problem:
  • template_header (includes direction arrays)

Combining Templates with Snippets

Common Combinations

template_std
graph_graph
graph_digraph
graph_dijkstra_std
misc_debug  // for testing
template_header  // for loop macros
numeric_mint_compress  // if modular arithmetic needed
misc_debug
template_std
string_kmp_std
// or
string_hashing_static_compress
template_header  // for math utilities
2d_geometry_point
2d_geometry_convex_hull_mc
misc_precise  // for decimal precision
template_std
// Remove fast I/O for interactive:
// Comment out: ios_base::sync_with_stdio(0);
// Add: cout.flush() after each output

Template Customization

Modifying Templates

You can customize templates for your preferences:

Add Your Handle

/**
 * @created     : March 05, 2026
 * @handle      : @YourHandle  // Change this
 */

Custom Macros

Add frequently used macros:
#define int long long  // If you always use long long
#define double long double  // For precision
#define vi vector<int>
#define pii pair<int,int>

Favorite Utilities

Add your most-used functions:
template<typename T>
void print(vector<T> v) {
    for(auto x : v) cout << x << " ";
    cout << endl;
}
If you modify the source .cpp files, re-run the snippet generator:
python3 generate_vscode_snippets.py
# or
bash generate_snippets.sh

Template Best Practices

DO:

  • ✅ Always start with a template
  • ✅ Use template_cases for multiple test cases
  • ✅ Enable fast I/O for large inputs
  • ✅ Use appropriate template for contest format
  • ✅ Add misc_debug during development
  • ✅ Remove debug code before submission

DON’T:

  • ❌ Disable fast I/O for interactive problems
  • ❌ Use Google template for non-Google contests
  • ❌ Forget to change USACO filename
  • ❌ Submit with debug statements enabled
  • ❌ Use unnecessary includes or snippets
  • ❌ Modify template structure unless needed

Interactive Problems

For interactive problems, modify the template:
#include <bits/stdc++.h>
using namespace std;

int main() {
    // DON'T use fast I/O for interactive problems
    // ios_base::sync_with_stdio(0);  // Comment this out
    // cin.tie(0);                    // Comment this out
    
    // Your solution
    int query;
    cin >> query;
    
    int response = process(query);
    cout << response << endl;
    cout.flush();  // IMPORTANT: flush after every output
    
    return 0;
}
Critical for interactive problems:
  1. Disable fast I/O
  2. Add cout.flush() after each output
  3. Never use endl without flush

Template Cheat Sheet

TemplateTriggerUse CaseKey Features
Standardtemplate_stdGeneral purposeClean, minimal
Headertemplate_headerComplex problemsMacros, optimizations
Casestemplate_casesMultiple test casesAuto T handling
Googletemplate_googleCode Jam, Kick StartCase numbering
USACOtemplate_usacoUSACO contestsFile I/O
LeetCodetemplate_std_leetcodeInterview prepClass structure
Randomtemplate_randomStress testingRandom generation

Common Mistakes

Problem: Using template_std for Google Code JamSolution: Use template_google for proper case numbering:
cout << "Case #" << case_num << ": " << answer << endl;
Problem: Submitting with problemname.in/outSolution: Always update filenames:
freopen("paint.in", "r", stdin);   // Actual problem name
freopen("paint.out", "w", stdout);
Problem: Solution hangs or gets wrong answerSolution: Disable fast I/O and use flush:
// ios_base::sync_with_stdio(0);  // Commented out
cout << answer << endl;
cout.flush();  // Add this
Problem: Large template with unused codeSolution: Remove what you don’t need:
  • Don’t need direction arrays? Remove them
  • Don’t need vector I/O? Remove those operators
  • Keep templates minimal for compilation speed

Advanced Usage

Creating Problem-Specific Templates

For recurring problem types, create specialized templates:
my_dp_template.cpp
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;

ll dp[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    // DP solution structure
    
    return 0;
}
Add it to your snippets and regenerate.

Template Inheritance

Build on existing templates:
// Start with template_header
// Then add your customizations
#define int long long  // Auto long long

void setIO(string name = "") {
    if(name.size()) {
        freopen((name + ".in").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
}

signed main() {  // Note: signed main() when #define int long long
    setIO("problemname");
    return 0;
}

Next Steps

Snippet Usage

Learn how to efficiently trigger and combine snippets

Competitive Programming

Master contest strategies and best practices

API Reference

Explore all available algorithms and data structures

Quick Start

Build your first solution with snippets

Build docs developers (and LLMs) love