How to speed up your Competitive Programming?

Arthak
Coding Blocks
Published in
7 min readMar 10, 2020
Image from Hash Code 2019

If you know about Competitive Programming, then you probably will be familiar with the fun and rush it provides when you see that AC on a difficult problem.

I think how good you are, is vastly dependent on your ability to Analyse the statement, identify subproblems, finding solutions, and implementing them. A lot of resources/websites are available for the first three but not a lot of on the later one. So in this series of blogs, I will be focusing on how to implement your solutions and implement them fast.

Note: As I usually use C++ for my solutions, this blog and its examples will be for C++, but can be easily extended to other languages as well.

Setup

I use a Windows Machine with MinGW, and the text editor of choice is vanilla Sublime Text with some minor customizations. If you want to have a similar setup, link to a tutorial at the end of the article.

Snippets

Doing a Breadth-First Search on a tree and executing in less than 25 seconds

If you don’t know what snippets are then, “Snippet is a programming term for a small region of Re-Usable Source Code,” and a lot of modern text editors like Sublime provide you a functionality to automatically write a predefined snippet by just writing a keyword.

I cannot emphasize enough on how much I think, and one can speed up their implementation by using Snippets. So whether you need to do a Breadth-First or Depth-first Search or need to use Segment Tree or want to do Matrix Exponentiation, you are just a (Keyword+Tab) away from its code.

It will not only improve your speed but, in turn, allow you to quickly try multiple approaches and switch between them whenever necessary without losing much time.

How to Add a Snippet

Adding a snippet in Sublime Text is pretty straightforward. Just navigate to Tools>Developers>New Snippet.

Base Template for Starting a 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.

Example: A Completed Snippet of a Fenwick Tree

I like to use Snippets a lot and the hardest part is to use them is to make them before you need them. So to ease things for you, I have created a Github Repository with some of my most-used snippets and relevant instructions. Feel free to use them and consider contributing.

Macros

A Macro stands for ‘macroinstruction’ is a “ Single instruction that expands automatically into a set of instructions to perform a particular task.”

In layman terms, they are a way to assign shorter notations to things that are usually longer to write. They are pretty similar to Snippets but are generally really small.

For example, We all have to write for loops in our programs. A usual one goes as for(int i=0;i<N;i++) Although it is a tiny piece of code, we may have to write them several times in a program, and it is slower to type because of all the different characters that it can contain.

So a solution is to write a macro that can just shorten it to FOR(i,N) It not only can help you code faster but also makes your code visually less cluttered, which can help in debugging.

How to Write a Macro

Demo on Making a Macro

They are quite easy to write using #define, you can take the following Gist as an example and follow it to make your Macros. I have used some of the macros in my competitive template which you can find in the above repository as well.

A lot of random useful things/tricks

A lot of you may already be aware of some of them, but rest can still be helpful. Some of them focus on Sublime Text.

Example of simultaneous typing
  • Simultaneous Typing: Sublime Text provides you with a feature of typing simultaneously at various locations and can help to make similar changes throughout your code at once. To type simultaneously, just click on places where you want to type while holding Ctrl Key, and once you have at all positions where you want to type, leave control and start typing. It can help you sometimes when you have made the same mistake at multiple locations in your program and correct them.
  • Pre Compiled Headers: Sometimes, we have to run our program 10s of times while debugging, and the faster the execution, the better it is. Pre-compiled headers can save up to 3–5 seconds every time you run your program which may sound much but if in a contest you compiled and ran your codes for 20 times, effectively you saved around 1:30 minutes which sometimes can be the difference between solving the last problem and not being able to complete it. Steps on how to do this are available in the Environment Setup Blog that I listed in the resources below.
  • Skip reading the statements for very easy questions: If you regularly take part in contests, and have some experience with easier problems then you can start skipping long statements (especially for something like Codeforces A) and focus on the last 2–3 sentences of the problem or if there is a sentence that starts with formally like “formally, the ith task was described by two integers ti and ci — the receiving time (the second) and the number of the text messages and you have to maximize ci.” and the test cases as well. This can give you a 2–3 minutes advantage which is always welcomed.
  • Keyboard Shortcuts: Some of the useful ones are:

Ctrl + Shift + D: When used without anything selected, your current line duplicates, and if used with something chosen, the chosen text duplicates.

Example of Shortcut Ctrl + Shift + D

Ctrl + Shift + K: When used without anything selected, your current line is deleted, and if used with something chosen, the chosen lines are deleted.

Example of Shortcut Ctrl + Shift + K

Ctrl + Shift + Up/Down Key: When used without anything selected, your current line can be moved up or down, and if used with something selected, the selected lines moved up or down.

Example of Shortcut Ctrl + Shift + Up/Down Key

Ctrl + /: Can be used to quickly comment and uncomment the selected lines if nothing is selected then the effect is on the current line.

  • Matching Brackets and the same text highlight: If you click next to an opening bracket or ending bracket then automatically shows the other matching bracket which can sometimes be helpful during debugging code. Also, If you select a text or variable then Sublime will automatically highlight all the places where it is present in the document.
  • Using a Template: Whatever language you use, it is always a good idea to have a base template that has your macros or maybe usual input that you want to take or maybe a Crazy ASCII Art (I know you want a link 😉). You can also have a look at my template to have a better idea(LINK).
  • No Brackets for Single line statements: In C++, you can save you a split second or, more importantly, can make your code a bit cleaner by skipping the brackets if you want to write a single statement in a loop or an if statement.
  • Find and Replace: You can use Ctrl + Shift + F to use the Find and Replace or for Find All and use simultaneous typing to make the changes.

Some Resources

Thanks a lot for reading through all of it, please let me know if you have some possible improvement or addition. Do Clap and share if you liked it. You can also contribute to the Snippet Repository over on Github.

--

--