Fast I/O & Debugging
Why I/O Speed Matters
In competitive programming, I/O can be the bottleneck. A problem with 10^6 integers usingcin/cout without optimization will TLE, while the same code with fast I/O passes easily.
The I/O Bottleneck: Standard
cin/cout is synchronized with C’s scanf/printf by default, making it significantly slower. Disable this sync to gain 5-10x speedup.The CP Template
Every competitive programmer has a template. Here’s a solid starting point:Fast I/O Techniques
Pattern 1: Sync Disable (Essential)
Pattern 2: Reading Entire Lines
Pattern 3: Reading Unknown Number of Inputs
Pattern 4: Fast Output
Debugging Techniques
Pattern 1: Debug Macro
g++ -DLOCAL solution.cpp
Pattern 2: Visualizing 2D Arrays
Pattern 3: Assert for Assumptions
Common I/O Patterns in CP
Pattern: Multi-Test Case
Pattern: Graph Input
Pattern: Matrix Input
Avoiding Common Mistakes
Mistake 1: Integer Overflow
Mistake 2: Array Index Out of Bounds
Mistake 3: Uninitialized Variables
Mistake 4: Forgetting to Reset Global Variables
Output Format Gotchas
Precision for Floating Point
Yes/No Output
Multiple Values on Same Line
Stress Testing
When your solution gets WA and you can’t find the bug:Key Takeaways
Always Use Fast I/O
ios::sync_with_stdio(false) and cin.tie(nullptr) are essential.Use Debug Macros
Create debug tools that compile out for submission.
Watch for Overflow
Use
long long when numbers can exceed 2×10^9.Reset State
Clear global variables between test cases.
Next Up
Chapter 3: STL for Competitive Programming
Master the C++ Standard Template Library—the competitive programmer’s toolkit.