XCPC: jiangly C++ Style
Overview
Proven C++ coding style from jiangly's Codeforces submissions. Focuses on safety, maintainability, and modern C++ features for competitive programming.
Core principle: Zero global pollution, zero global arrays, explicit types, modern C++.
When to Use
- Writing C++ solutions for Codeforces, ICPC, AtCoder, etc.
- Creating competitive programming templates
- Reviewing contest solutions
- Teaching competitive programming best practices
Don't use for:
- Production C++ code (different requirements)
- Non-competitive programming projects
MANDATORY WORKFLOW - READ BEFORE WRITING CODE
This skill enforces strict adherence to jiangly's coding style. You MUST follow this workflow:
Step 1: Consult Rules Before Writing
Before writing ANY code, you MUST:
- Read the relevant rule files in
rules/ directory based on your task
- Check Red Flags table below to avoid common mistakes
- Reference examples from rule files - they are the source of truth
Step 2: Match Rule Requirements
For each code decision, verify against rules:
| Task |
Rule File |
Key Points |
| Variable naming |
rules/naming.md |
snake_case, uppercase for temp count arrays (S, T) |
| String operations |
rules/in-place-operations.md |
reserve(), swap() for reuse |
| Temporary arrays |
rules/scope-control.md |
Block scopes {}, timely release |
| Logical operators |
rules/formatting.md |
Use and, or, not keywords |
| Loop comparisons |
rules/minimal-code.md |
Symmetric patterns: s[i] <= t[j] |
| Memory usage |
rules/in-place-operations.md |
vis arrays, no duplicate containers |
| Function design |
rules/struct-patterns.md |
Constructors, const correctness |
Step 3: Verify Against Red Flags
Before finalizing code, check EVERY row in the Red Flags table below.
If any pattern matches, you MUST fix it before output.
Step 4: Self-Correction Checklist
After writing code, verify:
Rule Files are PRIMARY Reference
The Quick Reference table below is a summary. Rule files contain the complete, authoritative examples. When in doubt, read the rule file.
| Category |
Rule |
File |
| Namespace |
Never using namespace std; |
no-global-namespace |
| Arrays |
Zero global arrays |
zero-global-arrays |
| Types |
Use using i64 = long long; |
explicit-types |
| Indexing |
Follow problem's natural indexing |
keep-indexing-consistent |
| I/O |
Disable sync, use \n |
fast-io |
| Naming |
snake_case for vars, PascalCase for structs |
naming |
| Minimal |
Avoid unnecessary variables, merge conditions |
minimal-code |
| Formatting |
4-space indent, K&R braces, no line compression |
formatting |
| Simplicity |
Prefer simple data structures |
simplicity-first |
| Memory |
Use in-place operations |
in-place-operations |
| Scope |
Use block scopes for temporaries |
scope-control |
| DFS |
Use depth array, avoid parent parameter |
dfs-techniques |
| Recursion |
Lambda + self pattern |
recursion |
| Structs |
Constructor patterns, const correctness |
struct-patterns |
| Operators |
Overload patterns for custom types |
operator-overloading |
| Helpers |
chmax, ceilDiv, gcd, power, etc. |
helper-functions |
| DP |
Use vector, never memset |
dp-patterns |
| Modern C++ |
CTAD, structured binding, C++20 features |
modern-cpp-features |
Code Template
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Red Flags - STOP
| Anti-pattern |
Correct approach |
using namespace std; |
Use std:: prefix |
int a[100005]; global |
std::vector<int> a(n); in solve() |
#define int long long |
using i64 = long long; |
for (int i = 1; i <= n; i++) |
for (int i = 0; i < n; i++) OR keep problem's indexing |
void dfs(int u, int p) global |
Lambda with self capture |
std::endl |
Use "\n" |
if (x) do_something(); |
Always use braces |
a && b logical operators |
Use and, or, not keywords |
| Over-engineered HLD + segment tree |
Use binary lifting for simple path queries |
Creating e1, e2 containers |
Use vis boolean array |
| Converting 1-indexed to 0-indexed |
Keep original indexing |
| Expanding boolean logic into verbose if-else |
Merge conditions |
| Introducing unnecessary intermediate variables |
Use direct formulas |
| Over-semantic local variable names |
Use x, y, l, r for short-lived vars |
| Passing parent in DFS |
Use depth array to check visited |
| Explicit template parameters |
Use CTAD where possible |
| Large arrays at function scope |
Use block scopes {} for temporaries |
String without reserve() |
Preallocate for known size |
Variable assignment instead of swap() |
Use O(1) swap |
| Creating struct for simple index-value sort |
Use std::iota + lambda |
| Loop variable initialized outside for |
Initialize in for header: for (int i = 0, j = 0; ...) |
| Output with trailing space handling |
Use " \n"[i == n - 1] trick |
Full Documentation
For complete details on all rules: AGENTS.md
FINAL CHECKPOINT - BEFORE OUTPUTTING CODE
You MUST verify ALL items below before showing code to user:
□ Read relevant rule files (naming.md, formatting.md, etc.)
□ Checked against Red Flags table
□ No `using namespace std;`
□ No global arrays
□ Used `i64` for long long
□ Used `and`/`or`/`not` keywords
□ Used `std::` prefix throughout
□ 4-space indent, K&R braces
□ No compressed lines
□ Used `"\n"` not `std::endl`
□ Temporary arrays in block scopes
□ String operations use `reserve()` and `swap()`
□ Symmetric comparison patterns
□ Uppercase temp array names (S, T)
□ Use iota + lambda for sorting with indices
□ Loop vars in for header when scope permits
□ Output uses " \n"[i == n - 1] trick
If ANY item fails, fix before output. This is non-negotiable.
1---2name: xcpc-jiangly-style3description: Use when writing C++ competitive programming solutions for Codeforces, ICPC, or similar contests. Apply when creating XCPC solutions to ensure code follows jiangly's proven style patterns.4license: MIT5---67# XCPC: jiangly C++ Style89## Overview1011Proven C++ coding style from jiangly's Codeforces submissions. Focuses on safety, maintainability, and modern C++ features for competitive programming.1213**Core principle:** Zero global pollution, zero global arrays, explicit types, modern C++.1415## When to Use1617- Writing C++ solutions for Codeforces, ICPC, AtCoder, etc.18- Creating competitive programming templates19- Reviewing contest solutions20- Teaching competitive programming best practices2122**Don't use for:**23- Production C++ code (different requirements)24- Non-competitive programming projects2526---2728<IMPORTANT>2930## MANDATORY WORKFLOW - READ BEFORE WRITING CODE3132**This skill enforces strict adherence to jiangly's coding style. You MUST follow this workflow:**3334### Step 1: Consult Rules Before Writing3536**Before writing ANY code, you MUST:**37381. **Read the relevant rule files** in `rules/` directory based on your task392. **Check Red Flags table** below to avoid common mistakes403. **Reference examples** from rule files - they are the source of truth4142### Step 2: Match Rule Requirements4344For each code decision, verify against rules:4546| Task | Rule File | Key Points |47|------|-----------|------------|48| Variable naming | `rules/naming.md` | `snake_case`, uppercase for temp count arrays (`S, T`) |49| String operations | `rules/in-place-operations.md` | `reserve()`, `swap()` for reuse |50| Temporary arrays | `rules/scope-control.md` | Block scopes `{}`, timely release |51| Logical operators | `rules/formatting.md` | Use `and`, `or`, `not` keywords |52| Loop comparisons | `rules/minimal-code.md` | Symmetric patterns: `s[i] <= t[j]` |53| Memory usage | `rules/in-place-operations.md` | `vis` arrays, no duplicate containers |54| Function design | `rules/struct-patterns.md` | Constructors, const correctness |5556### Step 3: Verify Against Red Flags5758**Before finalizing code, check EVERY row in the Red Flags table below.**5960If any pattern matches, you MUST fix it before output.6162### Step 4: Self-Correction Checklist6364After writing code, verify:6566- [ ] No using namespace std;67- [ ] No global arrays (all in solve())68- [ ] Used using i64 = long long;69- [ ] Used and/or/not instead of &&/||/!70- [ ] Used std:: prefix everywhere71- [ ] 4-space indentation, K&R braces72- [ ] No line compression (one statement per line)73- [ ] Used "\n" not std::endl74- [ ] Large temporary arrays in block scopes {}75- [ ] Strings preallocated with reserve() when size known76- [ ] Used swap() for O(1) variable reuse77- [ ] Used push_back() for single characters, not +=78- [ ] Symmetric comparison patterns (a[i] <= b[j])79- [ ] Uppercase names for temporary count arrays (S, T)80- [ ] Use iota + lambda for index-value sorting, not structs81- [ ] Loop variables initialized in for header when possible82- [ ] Output uses " \n"[i == n - 1] for space-separated values8384### Rule Files are PRIMARY Reference8586The Quick Reference table below is a summary. **Rule files contain the complete, authoritative examples.** When in doubt, read the rule file.8788</IMPORTANT>8990---9192| Category | Rule | File |93|----------|------|------|94| Namespace | Never `using namespace std;` | [no-global-namespace](rules/no-global-namespace.md) |95| Arrays | Zero global arrays | [zero-global-arrays](rules/zero-global-arrays.md) |96| Types | Use `using i64 = long long;` | [explicit-types](rules/explicit-types.md) |97| Indexing | Follow problem's natural indexing | [keep-indexing-consistent](rules/keep-indexing-consistent.md) |98| I/O | Disable sync, use `\n` | [fast-io](rules/fast-io.md) |99| Naming | `snake_case` for vars, `PascalCase` for structs | [naming](rules/naming.md) |100| Minimal | Avoid unnecessary variables, merge conditions | [minimal-code](rules/minimal-code.md) |101| Formatting | 4-space indent, K&R braces, no line compression | [formatting](rules/formatting.md) |102| Simplicity | Prefer simple data structures | [simplicity-first](rules/simplicity-first.md) |103| Memory | Use in-place operations | [in-place-operations](rules/in-place-operations.md) |104| Scope | Use block scopes for temporaries | [scope-control](rules/scope-control.md) |105| DFS | Use depth array, avoid parent parameter | [dfs-techniques](rules/dfs-techniques.md) |106| Recursion | Lambda + self pattern | [recursion](rules/recursion.md) |107| Structs | Constructor patterns, const correctness | [struct-patterns](rules/struct-patterns.md) |108| Operators | Overload patterns for custom types | [operator-overloading](rules/operator-overloading.md) |109| Helpers | chmax, ceilDiv, gcd, power, etc. | [helper-functions](rules/helper-functions.md) |110| DP | Use `vector`, never `memset` | [dp-patterns](rules/dp-patterns.md) |111| Modern C++ | CTAD, structured binding, C++20 features | [modern-cpp-features](rules/modern-cpp-features.md) |112113## Code Template114115```cpp116#include <bits/stdc++.h>117118using i64 = long long;119120void solve() {121 int n;122 std::cin >> n;123124 std::vector<int> a(n);125 for (int i = 0; i < n; i++) {126 std::cin >> a[i];127 }128129 std::cout << ans << "\n";130}131132int main() {133 std::ios::sync_with_stdio(false);134 std::cin.tie(nullptr);135136 int t;137 std::cin >> t;138139 while (t--) {140 solve();141 }142143 return 0;144}145```146147## Red Flags - STOP148149| Anti-pattern | Correct approach |150|--------------|------------------|151| `using namespace std;` | Use `std::` prefix |152| `int a[100005];` global | `std::vector<int> a(n);` in solve() |153| `#define int long long` | `using i64 = long long;` |154| `for (int i = 1; i <= n; i++)` | `for (int i = 0; i < n; i++)` **OR** keep problem's indexing |155| `void dfs(int u, int p)` global | Lambda with self capture |156| `std::endl` | Use `"\n"` |157| `if (x) do_something();` | Always use braces |158| `a && b` logical operators | Use `and`, `or`, `not` keywords |159| **Over-engineered** HLD + segment tree | Use binary lifting for simple path queries |160| **Creating** `e1, e2` containers | Use `vis` boolean array |161| **Converting** 1-indexed to 0-indexed | Keep original indexing |162| **Expanding** boolean logic into verbose if-else | Merge conditions |163| **Introducing** unnecessary intermediate variables | Use direct formulas |164| **Over-semantic** local variable names | Use `x, y, l, r` for short-lived vars |165| **Passing** parent in DFS | Use depth array to check visited |166| **Explicit** template parameters | Use CTAD where possible |167| **Large** arrays at function scope | Use block scopes `{}` for temporaries |168| **String** without `reserve()` | Preallocate for known size |169| **Variable** assignment instead of `swap()` | Use O(1) swap |170| **Creating** struct for simple index-value sort | Use `std::iota` + lambda |171| **Loop** variable initialized outside for | Initialize in for header: `for (int i = 0, j = 0; ...)` |172| **Output** with trailing space handling | Use `" \n"[i == n - 1]` trick |173174## Full Documentation175176For complete details on all rules: [AGENTS.md](AGENTS.md)177178---179180<CRITICAL>181182## FINAL CHECKPOINT - BEFORE OUTPUTTING CODE183184**You MUST verify ALL items below before showing code to user:**185186```187□ Read relevant rule files (naming.md, formatting.md, etc.)188□ Checked against Red Flags table189□ No `using namespace std;`190□ No global arrays191□ Used `i64` for long long192□ Used `and`/`or`/`not` keywords193□ Used `std::` prefix throughout194□ 4-space indent, K&R braces195□ No compressed lines196□ Used `"\n"` not `std::endl`197□ Temporary arrays in block scopes198□ String operations use `reserve()` and `swap()`199□ Symmetric comparison patterns200□ Uppercase temp array names (S, T)201□ Use iota + lambda for sorting with indices202□ Loop vars in for header when scope permits203□ Output uses " \n"[i == n - 1] trick204```205206**If ANY item fails, fix before output. This is non-negotiable.**207208</CRITICAL>