This article focuses on how to implement your solutions and implement them fast while doing competitive programming.
Setup Please refer Setting up a C++ Competitive Programming Environment
Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editors like Sublime provide you a functionality to automatically write a predefined snippet by just writing a keyword. One can speed up their implementation by using Snippets. So if you need to do a BFS or DFS(or any other code), you just need to press ‘Keyword+Tab’ to get it’s code.
How to add a snippet Adding a snippet in Sublime Text is pretty straightforward. Just navigate to Tools>Developers>New Snippet.
<snippet>
<content><![CDATA[
// Type your code here
]]></content>
<tabTrigger>ENTER KEYWORD HERE</tabTrigger>
</snippet>
Use this as your base template for a snippet and paste the code that you want to use in your Snippet by replacing the comment and trigger Keyword.
Macros
They are a way to assign shorter notations to things that are usually longer to write.
They can be easily written using #define.
Example 1:
Before using Macros:
long long int a; vector<long long int> v; map<long long int, long long int> mp; |
After using Macros:
// ll can be used instead of typing long long int #define ll long long int ll a; vector<ll> v; map<ll, ll> mp; |
Example 2:
Before using Macros:
for (int i = 0; i < N; i++) cin >> a[i]; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cout << m[i][j] << endl; } } |
After using Macros:
// Use this macro instead of typing the whole // for-loop syntax #define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++) FOR(i, N) cin >> a[i]; FOR(i, N) { FOR(j, M) { cout << m[i][j] << endl; } } |
Example 3:
vector v; for (int i = 0; i < N; i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } |
After:
#define ll long long int #define MP make_pair #define pb push_back vector<ll, ll> v; for (int i = 0; i < N; i++) { cin >> x >> y; v.pb(MP(x, y)); } |
Note: Write your macros at the beginning of your code(refer to the template given below).
Using a Template
Using a template is one of the best idea to speed up your implementation. Keep your template ready that has your macros written in them already. This helps in reducing a lot of your time which otherwise would have gone in writing the whole code from scratch.
I use the following template, you can use one that suits you.
#include "bits/stdc++.h" using namespace std; #define max(a, b) (a < b ? b : a) #define min(a, b) ((a > b) ? b : a) #define mod 1e9 + 7 #define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++) #define FORL(a, b, c) for (int(a) = (b); (a) <= (c); (a)++) #define FORR(a, b, c) for (int(a) = (b); (a) >= (c); (a)--) #define INF 1000000000000000003 typedef long long int ll; typedef vector<int> vi; typedef pair<int, int> pi; #define F first #define S second #define PB push_back #define POB pop_back #define MP make_pair int main() { ios::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { int N; cin >> N; ll a[N]; FOR(i, N) cin >> a[i]; } return 0; } |
Thank you for reading, please let me know if you have some possible improvement or addition.
Recommended Posts:
- Writing C/C++ code efficiently in Competitive programming
- Writing code faster in C++ STL
- getchar_unlocked() - faster input in C/C++ for Competitive Programming
- C++: Methods of code shortening in competitive programming
- How to setup Competitive Programming in Visual Studio Code for C++
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Writing OS Independent Code in C/C++
- How can competitive programming help you get a job?
- What Are The Best Resources For Competitive Programming?
- Bit Tricks for Competitive Programming
- Must do Math for Competitive Programming
- 5 Best Languages for Competitive Programming
- What is Competitive Programming and How to Prepare for It?
- Python in Competitive Programming
- A Better Way To Approach Competitive Programming
- Fast I/O for Competitive Programming
- C++ tricks for competitive programming (for C++ 11)
- Which C++ libraries are useful for competitive programming?
- How to begin with Competitive Programming?
- How to become a master in competitive programming?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


