├── .DS_Store ├── 00-Setting-up-local-environment └── README.md ├── 01-Material └── README.md ├── 02-Codes ├── .DS_Store ├── Editorials │ ├── .DS_Store │ ├── ConvertToStrictlyIncreasing │ │ ├── ConevertToStrictlyIncreasing.md │ │ ├── convertToIncreasing.cpp │ │ └── convertToIncreasing.py │ └── Random │ │ └── TransposeColorsWork.cpp ├── README.md └── Snippets │ ├── .DS_Store │ ├── Raw │ └── 166_e_MatMul_w_Ashishgup.cpp │ ├── Sublime │ ├── .DS_Store │ ├── FastReadWrite.sublime-snippet │ ├── My Snippets not used now.tar.gz │ ├── addundirectededge.sublime-snippet │ ├── basic-template.sublime-snippet │ ├── check execution time.sublime-snippet │ ├── checkvalid.sublime-snippet │ ├── cmp-priority.sublime-snippet │ ├── common-completions.sublime-completions │ ├── dsu-array.sublime-snippet │ ├── dsu.sublime-snippet │ ├── errorWatch.sublime-snippet │ ├── eulerTotientFunc.sublime-snippet │ ├── leetcode-solution-class.sublime-snippet │ ├── manipulated-sieve-of-eratosthene.sublime-snippet │ ├── matrix-expo.sublime-snippet │ ├── max-biparite-matching.sublime-snippet │ ├── modop.sublime-snippet │ ├── myReadTemplate.sublime-snippet │ ├── ncr-on-demand.sublime-snippet │ ├── pbds.sublime-snippet │ ├── power-expo.sublime-snippet │ ├── sieve-of-eratosthene.sublime-snippet │ └── topcoder-template.sublime-snippet │ └── mydebug.h ├── 03-Interview Preparation ├── Docs │ ├── .gitkeep │ ├── 0x01_Interview_Preparation_in_C_v1.0.docx │ └── System Design Guide.docx ├── PDF │ ├── .gitkeep │ ├── 0x01 C Interview Preparation-compressed.pdf │ ├── Algorithm Analysis.pdf │ ├── Computer networks- most asked interview questions.pdf │ ├── DBMS Exclusive Notes.pdf │ ├── LeetCode 50 Common Interview Questions with Solutions.pdf │ ├── OOPs in C++.pdf │ ├── Operating System Exclusive Notes. .pdf │ └── SQL Exclusive Notes.pdf └── README.md └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/.DS_Store -------------------------------------------------------------------------------- /00-Setting-up-local-environment/README.md: -------------------------------------------------------------------------------- 1 | # Setting up Competitive Programming on Local Machine : 2 | 3 | #### Content 4 | 5 | - [Benefits](#Benefits) 6 | 7 | - [SetUp](#Setting-up-env) 8 | 9 | - [Snippets](#Snippets) 10 | 11 | 12 |


13 | 14 | 15 | ## Benefits 16 | 17 | 18 | #### It is always a good practice to set up your coding environment right on your pc as : 19 | 20 | * It helps in faster code execution. 21 | 22 | * Independent of the load on IDE's during the contest. 23 | 24 | * Various Code Snippets can be used. 25 | 26 | * Interaction with command line and terminal. 27 | 28 | * Less chances of your code getting leaked. 29 | 30 | 31 | 32 |


33 | 34 | 35 | 36 | ## Setting up env: 37 | 38 | So below are some good setups which you may like and may modify as well as per your choice. 39 | 40 | Also, you can set up your environment as you like if you have basic knowledge and read any of the blogs below. Just set up the compiler and a text editor or ide. Remember to add the path of your compiler to environment variable. 41 | 42 | p.s. Should read about precompiling the headers if you use C++ for faster build. 43 | 44 | [Codeforces blog for some editor preferences](https://codeforces.com/blog/entry/356) 45 | 46 |
47 | 48 | ### VSCode and C++ : 49 | * [Setting up VS Code for CP - Rachit Jain - Youtube](https://www.youtube.com/watch?v=Y-_3rXgrRAY) 50 | 51 |
52 | 53 | ### Sublime and C++ : 54 | * [Codingblocks Blog](https://blog.codingblocks.com/2019/setting-up-a-c-competitive-programming-environment/) 55 | * [Codingblocks - Youtube](https://www.youtube.com/watch?v=Mt6Jb8u9XBk) 56 | * [Blog - the-hyp0cr1t3](https://github.com/the-hyp0cr1t3/CC/blob/master/Setting%20up%20Sublime.md) 57 | 58 |
59 | 60 | ### C++ in Ubuntu : 61 | * [GeeksforGeeks Blog](https://www.geeksforgeeks.org/setting-up-a-c-competitive-programming-environment/) 62 | 63 |
64 | 65 | ### Linux Setup with Geany : 66 | * [Linux Setup with Geany - Errichto - Youtube](https://www.youtube.com/watch?v=ePZEkbbf3fc) 67 | 68 |
69 | 70 | ### Precompiling C++ headers : 71 | 72 | It is always important to precompile the C++ header files to save your precious time in compilation. You can search for the same on the internet if you run into error. 73 | 74 | All you have to do is to find your stdc++.h file in your compiler path and then compile it like normal file with the flags you use to run. 75 | 76 | `g++ [your-flags] stdc++.h` 77 | 78 | * [Codeforces blog for windows](https://codeforces.com/blog/entry/66809) 79 | 80 | * [Codeforces blog for linux distro](https://codeforces.com/blog/entry/53909?) 81 | 82 | * [Another Codeforces blog](https://codeforces.com/blog/entry/79026) 83 | 84 |
85 | 86 | ### VSCode and Python3 : 87 | 88 | I prefer Python and VSCode setup. Here's how you can set it up : 89 | 90 | * Download and install Python3 and VSCode. 91 | 92 | * Add path to environment variable. 93 | 94 | * Install CodeRunner Extension on VSCode. 95 | 96 | * Open your settings.json file on VSCode and modify the coderunner command for any python file as `python -u output.txt` to take input from input.txt and write to output.txt. Then you can easily run your file with ctrl + alt + n 97 | 98 |
99 | 100 | ### Bonus 101 | 102 | [CP Editor](https://cpeditor.org/) 103 | 104 | This editor is purposely designed for CP and Codeforces. Easy to use and many interesting features. It greatly improves typing speed as one of my friends who is a big fan of this editor suggests. 105 | 106 |


107 | 108 | ## Snippets 109 | 110 | Snippets are the cool features offered by the text editors through which you can add a bunch of code just by a few taps. 111 | 112 | - [CP Snippets by Atom.io](https://atom.io/packages/cpp-competitive-programming-snippets) 113 | 114 |
115 | You can add your own snippets as for loading your template, adding a bfs code or for anything you want. Just look for how to add custom snippets in your text editor. 116 | 117 | - [VSCode Guide - Blog](https://code.visualstudio.com/docs/editor/userdefinedsnippets) 118 | 119 | - [Sublime Text 3 - Video & Blog](https://devdojo.com/episode/how-to-create-sublime-text-snippets) 120 | 121 |

122 | 123 | > Now you're all set to solve the [problems](https://github.com/luctivud/All-Of-Competitive-Programming/tree/master/01-Material). 124 | 125 | > I've mentioned all the popular setups. If you're facing an error in setting up it is easy to google it and find solution on stackoverflow. -------------------------------------------------------------------------------- /01-Material/README.md: -------------------------------------------------------------------------------- 1 | # CP Material 2 | 3 | Many of the seniors as well good competitive coders advice to not waste any money on the courses which are designed for beginners. It is always easy to start cp on your own and be good at it. However you can choose to have a good certification on Data Structures and ALgorithms offered by [Codechef](https://www.codechef.com/certification/data-structures-and-algorithms/about) or choose a course which aims at advanced data structures and algorithms. 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | #### Content 12 | 13 | * [Books](#books) 14 | 15 | * [Informative Blogs](#some-good-and-informative-blogs-) 16 | 17 | * [Problemset](#Problemset) 18 | 19 | 20 | 21 |


22 | 23 | 24 | 25 | ## Books: 26 |
27 | 28 | * Check out [this](https://github.com/lnishan/awesome-competitive-programming#books-for-algorithms) section for a long list of books. 29 | 30 | * I prefer to have [Competitive Programmers Handbook by Antti Laaksonen](https://cses.fi/book/book.pdf) and [Introduction To Algorithms by CLRS](https://web.iiit.ac.in/~pratik.kamble/storage/Algorithms/Cormen_Algorithms_3rd.pdf) as mandatory. 31 | 32 | > Note: The best time to read the topics in any book is either in the morning or in the evening to grasp everything. 33 | 34 | 35 | 36 | 37 |


38 | 39 | 40 | 41 | ## Some good and informative blogs : 42 |
43 | 44 | Below is the encyclopedia of all that one needs to set up the schedule of cp and start away asap. 45 | 46 | * [ACM-ICPC Preparation Repository by BedirT](https://github.com/BedirT/ACM-ICPC-Preparation) : The curriculum and schedule one needs to follow to learn data structures and algorithms for ICPC. 47 | 48 | * [Getting into Competitive Coding by hyp0cr1t3](https://github.com/the-hyp0cr1t3/CC) : This repo is best for starting cp. It contains everything you need to master cp from basic setup to data structures along with decent problemset. Make sure to check the tips and tricks section. 49 | 50 | * [Awesome Competitive Programming by lnishan](https://github.com/lnishan/awesome-competitive-programming) : The encyclopedia of cp. It contains in-depth analysis as well as topic wise practice problems. Probably more than one can ask for. 51 | 52 | * [Competitive Coding Implementation (C++) by Ashishgup](https://github.com/Ashishgup1/Competitive-Coding) : Whenever you run into any problem, just check the implementation of Ashish Sir. His codes are easy to understand and beautifully formatted. 53 | 54 | * [Competitive Coding Implementation (Python) by cheran-senthil](https://github.com/cheran-senthil/PyRival) : Python Oriented Library for Competitive Programming. 55 | 56 | * [OEIS](https://oeis.org/) : who doesn't love integer sequences? Well everyone should. 57 | 58 | 59 | 60 | 61 | 62 | 63 |
[Back To Top](#cp-material)

64 | 65 | 66 | 67 | 68 | 69 | 70 | ## Problemset 71 |
72 | 73 | The problemset is divided into two broad categories based on beginner or intermediate level of knowledge. 74 | 75 | * [Beginners](#Beginners) 76 | 77 | * [Intermediate](#intermediate) 78 | 79 | 80 | 81 |
[Back To Top](#cp-material)

82 | 83 | 84 | 85 | 86 | ### Beginners 87 | 88 |
89 | 90 | 91 | * **Start :** 92 | 93 | :page_facing_up: [TopCoder-Introduction to Competitive Programming](https://www.topcoder.com/community/competitive-programming/tutorials) 94 | 95 | :movie_camera: [Starting CP - Errichto - Youtube](https://www.youtube.com/watch?v=xAeiXy8-9Y8) 96 | 97 | :page_facing_up: [Codingblocks](https://blog.codingblocks.com/2019/start-with-competitive-programming/) 98 | 99 | :page_facing_up: [How to start CC - cf](https://codeforces.com/blog/entry/49157) 100 |
101 | 102 | * **Train :** 103 | 104 | :page_facing_up: [Must do Maths in CP - Gfg](https://www.geeksforgeeks.org/math-in-competitive-programming/?ref=leftbar-rightbar) 105 | 106 | :page_facing_up: [Hackerearth - Codemonk](https://www.hackerearth.com/blog/developers/hackerearth-codemonk-v2-0/) 107 | 108 | :page_facing_up: [Training Roadmap for newcomers - cf](https://codeforces.com/blog/entry/65133) 109 | 110 | - [ ] [Arabic CP](https://github.com/mostafa-saad/ArabicCompetitiveProgramming/) and the [Junior Training Sheet](https://docs.google.com/spreadsheets/d/1iJZWP2nS_OB3kCTjq8L6TrJJ4o-5lhxDOyTaocSYc-k/edit#gid=1160016643) as well as [Supervision Template](https://docs.google.com/spreadsheets/d/1QUegTqKnVtdejCwttYFXHeOQs6Q5nmAT77sxWh3qYMc/edit#gid=2139527441). 111 | 112 | - [ ] [20+ leetcode problems on Sliding Window](https://leetcode.com/discuss/general-discussion/657507/Sliding-Window-for-Beginners-Problems-or-Template-or-Sample-Solutions) 113 |
114 | 115 | 116 | * **Solve :** 117 | 118 | It is adviced to solve beginner level problems with lower rating first and then jump to higher ratings when you start feeling comfortable. 119 | 120 | Do not copy paste solutions directly from editorial without implementing on your own. 121 | 122 | - [ ] [CSES Problemset](https://cses.fi/problemset/) 123 | 124 | - [ ] [A2OJ Ladders](https://a2oj.com/ladders) 125 | 126 | - [ ] [Another A2OJ ladder](https://earthshakira.github.io/a2oj-clientside/server/Categories.html) : Mirror 127 | 128 | - [ ] [CodeForces-Ladders with updated problemset](https://github.com/karansinghgit/CodeForces-Ladders) 129 |
130 | 131 | * **Bonus :** 132 | 133 | :page_facing_up: [Simple Trick to detect integer overflow - codechef discuss](https://discuss.codechef.com/t/simple-trick-to-detect-integer-overflow-c-c/54373) 134 | 135 | :page_facing_up: [Catching silly mistakes with GCC](https://codeforces.com/blog/entry/15547) 136 | 137 | 138 | 139 | 140 | 141 |
[Back To Problemset](#problemset)

142 | 143 | 144 | 145 | 146 | ### Intermediate 147 | 148 |
149 | 150 | Solving the beginner level problems from the above resources will be enough to get a sound knowledge of basic Maths, Greedy, AdHoc and other constructive problems and their approach. 151 | 152 | For Data Structures and Algorithms, consider: 153 | 154 | - [ ] [Codechef Blog on Data Structures and Algorithms with 100+ Problems and Tutorial](https://discuss.codechef.com/t/data-structures-and-algorithms/6599) : This blog has tutorials and practice problems for most of the data structures and algorithms that one can come across. 155 | 156 | :page_facing_up: [CP Algorithm](https://cp-algorithms.com/) : This online project is the English translation of a popular [russian site](http://e-maxx.ru/algo) containing descriptions of many algorithms and data structures especially popular in field of competitive programming and practice problems. 157 | 158 | :page_facing_up: [Important Algorithms for Competitive Programming - Code of Geeks](https://codeofgeeks.com/important-algorithms-for-competitive-programming/) 159 | 160 | :page_facing_up: [vplanetcoding](https://www.vplanetcoding.com/course2) : Check out all the free courses. 161 | 162 | - [ ] [Junior Training Sheet](https://docs.google.com/spreadsheets/d/1iJZWP2nS_OB3kCTjq8L6TrJJ4o-5lhxDOyTaocSYc-k/edit#gid=1160016643) & [CSES Prolemset](https://cses.fi/problemset/) - (Again!) 163 | 164 |
165 | 166 | 167 | >The problems are majorly categorized into following topics for now: 168 |
169 | 170 | **Content** 171 |
172 | 173 | * [Graphs](#Graphs) 174 | 175 | * [Dynamic Programming](#Dynamic-Programming) 176 | 177 | * [Trees](#Trees) 178 | 179 | * [Other](#Other) 180 | 181 | 182 | 183 | 184 |
[Back To Problemset](#problemset)

185 | 186 | 187 | 188 | 189 | ## Graphs 190 |
191 | 192 | * #### Tutorial : 193 | 194 | :page_facing_up: [CF-Center of graph](https://codeforces.com/blog/entry/17974) 195 | 196 | :page_facing_up: [Longest Path Problem - Wikipedia](https://en.wikipedia.org/wiki/Longest_path_problem) 197 | 198 | :page_facing_up: [Graph Diameter - Mathworld](https://mathworld.wolfram.com/GraphDiameter.html) 199 | 200 | 201 | * #### Problemset : 202 | 203 | - [ ] [Codeforces comment with 100+ graph problems](https://codeforces.com/blog/entry/55219?#comment-390897) 204 | 205 | - [ ] [international-open-olympiad-kpiopen-2013-round-1](https://codeforces.com/gym/100279/attachments/download/2025/20122013-international-open-olympiad-kpiopen-2013-round-1-en.pdf) 206 | 207 | - [ ] [LeetCode Graph Related Problems](https://leetcode.com/list/x1vj23fh/) 208 | 209 | - [ ] [500+ Problems on BFS/DFS/Dijkshtra - A2OJ](https://a2oj.com/category?ID=13) 210 | 211 | 212 | **IARCS** 213 | - [ ] [Graphs](https://www.iarcs.org.in/inoi/online-study-material/topics/graphs.php) 214 | - [ ] [Advanced Graph Algo](https://www.iarcs.org.in/inoi/online-study-material/topics/advanced-graph-algorithms.php) 215 | 216 | **Union Find** 217 | - [ ] [leetcode : friend-circle](https://leetcode.com/problems/friend-circles/) 218 | - [ ] [leetcode : redundant-connection](https://leetcode.com/problems/redundant-connection/) 219 | - [ ] [leetcode : most-stones-removed-with-same-row-or-column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) 220 | - [ ] [leetcode : number-of-operations-to-make-network-connected](https://leetcode.com/problems/number-of-operations-to-make-network-connected/) 221 | - [ ] [leetcode : satisfiability-of-equality-equation](https://leetcode.com/problems/satisfiability-of-equality-equations/) 222 | - [ ] [leetcode : accounts-merge](https://leetcode.com/problems/accounts-merge/) 223 | 224 | **DFS - Basic** 225 | - [ ] [leetcode : surrounded-region](https://leetcode.com/problems/surrounded-regions/) 226 | - [ ] [leetcode : number-of-enclave](https://leetcode.com/problems/number-of-enclaves/) 227 | - [ ] [leetcode : time-needed-to-inform-all-employee](https://leetcode.com/problems/time-needed-to-inform-all-employees/) 228 | - [ ] [leetcode : find-eventual-safe-state](https://leetcode.com/problems/find-eventual-safe-states/) 229 | 230 | **Island Problems** 231 | - [ ] [leetcode : number-of-closed-island](https://leetcode.com/problems/number-of-closed-islands/) 232 | - [ ] [leetcode : number-of-island](https://leetcode.com/problems/number-of-islands/) 233 | - [ ] [leetcode : keys-and-room](https://leetcode.com/problems/keys-and-rooms/) 234 | - [ ] [leetcode : max-area-of-island](https://leetcode.com/problems/max-area-of-island/) 235 | - [ ] [leetcode : flood-fill](https://leetcode.com/problems/flood-fill/) 236 | 237 | **BFS** 238 | - [ ] [http://codeforces.com/contest/653/problem/E](http://codeforces.com/contest/653/problem/E) 239 | - [ ] [https://codeforces.com/contest/769/problem/C](https://codeforces.com/contest/769/problem/C) 240 | - [ ] [https://codeforces.com/contest/796/problem/D](https://codeforces.com/contest/796/problem/D) 241 | - [ ] [https://codeforces.com/contest/821/problem/D](https://codeforces.com/contest/821/problem/D) 242 | - [ ] [http://www.spoj.com/problems/DIGOKEYS/](http://www.spoj.com/problems/DIGOKEYS/) 243 | - [ ] [http://www.spoj.com/problems/SPIKES/](http://www.spoj.com/problems/SPIKES/) 244 | - [ ] [http://www.spoj.com/problems/MULTII/](http://www.spoj.com/problems/MULTII/) 245 | - [ ] [http://www.spoj.com/problems/ADV04F1/](http://www.spoj.com/problems/ADV04F1/) 246 | - [ ] [http://www.spoj.com/problems/ADV04F1/](http://www.spoj.com/problems/ADV04F1/) 247 | - [ ] [leetcode : 01-matrix](https://leetcode.com/problems/01-matrix/) 248 | - [ ] [leetcode : as-far-from-land-as-possible](https://leetcode.com/problems/as-far-from-land-as-possible/) 249 | - [ ] [leetcode : rotting-orange](https://leetcode.com/problems/rotting-oranges/) 250 | - [ ] [leetcode : shortest-path-in-binary-matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/) 251 | 252 | **Graph Coloring** 253 | - [ ] [leetcode : possible-bipartition](https://leetcode.com/problems/possible-bipartition/) 254 | - [ ] [leetcode : is-graph-bipartite](https://leetcode.com/problems/is-graph-bipartite/) 255 | 256 | **Topological Sort** 257 | - [ ] [leetcode : course-schedule](https://leetcode.com/problems/course-schedule/) 258 | - [ ] [leetcode : course-schedule-ii](https://leetcode.com/problems/course-schedule-ii/) 259 | - [ ] [codeforces : 510C-Fox and names](https://codeforces.com/contest/510/problem/C) 260 | 261 | **Other** 262 | - [ ] [https://dmoj.ca/problem/tle16c4p4](https://dmoj.ca/problem/tle16c4p4) 263 | - [ ] [https://codeforces.com/problemset/problem/322/E](https://codeforces.com/problemset/problem/322/E) 264 | - [ ] [https://codeforces.com/problemset/problem/592/D](https://codeforces.com/problemset/problem/592/D) 265 | - [ ] [https://codeforces.com/contest/456/problem/E](https://codeforces.com/contest/456/problem/E) 266 | 267 | 268 | The above problems were extracted from [this leetcode article](https://leetcode.com/discuss/general-discussion/655708/graph-for-beginners-problems-pattern-sample-solutions). Thanks to the work of [wh0ami sir](https://leetcode.com/wh0ami/). 269 | 270 | 271 | 272 | 273 | 274 | 275 |
[Back To Problemset](#problemset)

276 | 277 | 278 | 279 | 280 | 281 | 282 | ## Dynamic Programming 283 |
284 | 285 | * #### Tutorial : 286 | 287 | :page_facing_up: [Hackerearth](https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/tutorial/) 288 | 289 | :page_facing_up: [Codechef Tutorial and few problems](https://www.codechef.com/wiki/tutorial-dynamic-programming) 290 | 291 | :page_facing_up: [TutorialsPoint Blog](https://www.tutorialspoint.com/data_structures_algorithms/dynamic_programming.htm) 292 | 293 | :movie_camera: [CS Dojo - Youtube](https://www.youtube.com/watch?v=vYquumk4nWw) 294 | 295 | :movie_camera: [Aditya Verma - Youtube](https://www.youtube.com/watch?v=nqowUJzG-iM&list=PL_z_8CaSLPWekqhdCPmFohncHwz8TY2Go) Check this one out. 296 | 297 | :movie_camera: [CodeN Code - youtube](https://www.youtube.com/watch?v=AfE1nZjG5ZM&list=PL2q4fbVm1Ik71hmKyay-GkU6J9GTXCe5g) 298 | 299 | :page_facing_up: [Dynamic Programming Beginner's Course](https://discuss.codechef.com/t/dynamic-programming-beginners-course/70936) contains problems and video tutorials. 300 | 301 | * #### Problemset : 302 | 303 | - [ ] [100+ LeetCode problems - wh0ami](https://leetcode.com/discuss/general-discussion/662866/Dynamic-Programming-for-Practice-Problems-Patterns-and-Sample-Solutions) 304 | 305 | - [ ] [100+ codeforces problems - karansinghgit](https://github.com/karansinghgit/CodeForces-Ladders/blob/master/dynamicProgramming.md) 306 | 307 | - [ ] [1000+ problems on DP - A2OJ](http://a2oj.com/category?ID=33) 308 | 309 | - [ ] [Few other DP problems - A2OJ](https://a2oj.com/category?ID=758) 310 | 311 | - [ ] [AtCoder DP Contest](https://atcoder.jp/contests/dp/tasks) 312 | 313 | - [ ] [30+ Non-Classical Dynamic Programming problems - A2OJ](https://a2oj.com/category?ID=126) 314 | 315 | 316 | 317 |
[Back To Problemset](#problemset)

318 | 319 | 320 | 321 | 322 | 323 | ## Trees 324 |
325 | 326 | * #### Tutorial : 327 | 328 | :page_facing_up: [Trees - hackerearth](https://www.hackerearth.com/practice/notes/trees/) 329 | 330 | :page_facing_up: [Fenwick Tree - gfg](https://www.geeksforgeeks.org/binary-indexed-tree-or-fenwick-tree-2/) 331 | 332 | :page_facing_up: [Dynamic Programming on Trees - gfg](https://www.geeksforgeeks.org/dynamic-programming-trees-set-1/) 333 | 334 | :movie_camera: [Dynamic Programming on Trees - Aditya Verma - Youtube](https://www.youtube.com/playlist?list=PL_z_8CaSLPWfxJPz2-YKqL9gXWdgrhvdn) 335 | 336 | 337 | * #### Problemset : 338 | 339 | - [ ] [Vplanet](https://www.vplanetcoding.com/course3) 340 | 341 | - [ ] [Binary and n-ary trees - Hackerearth](https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/) 342 | 343 | - [ ] [BST - hackerearth](https://www.hackerearth.com/practice/data-structures/trees/binary-search-tree/practice-problems/) 344 | 345 | - [ ] [Heaps - hackerearth](https://www.hackerearth.com/practice/data-structures/trees/heapspriority-queues/practice-problems/) 346 | 347 | - [ ] [GERALD2 - codechef](http://www.codechef.com/NOV13/problems/GERALD2) 348 | 349 | - [ ] [GERALD07 - codechef](http://www.codechef.com/MARCH14/problems/GERALD07) 350 | 351 | - [ ] [FBCHEF - codechef](http://www.codechef.com/APRIL14/problems/FBCHEF) 352 | 353 | - [ ] [GERALD08 - codechef](http://www.codechef.com/APRIL14/problems/GERALD08) 354 | 355 | - [ ] [MLCHEF - codechef](https://www.codechef.com/SEPT13/problems/MLCHEF) 356 | 357 | - [ ] [200+ Problems on Trees - A2OJ](https://a2oj.com/category?ID=89) 358 | 359 | - [ ] [150+ Problems on Segment Trees - A2OJ](https://a2oj.com/category?ID=25) 360 | 361 | - [ ] [20+ problems on Balanced BST - A2OJ](https://a2oj.com/category?ID=14) 362 | 363 | - [ ] [20+ problems on suffix array A2OJ](https://a2oj.com/category?ID=44) 364 | 365 | 366 | 367 | 368 |
[Back To Problemset](#problemset)

369 | 370 | 371 | 372 | 373 | 374 | 375 | ## Other 376 |
377 | 378 | :movie_camera: [Algorithm - SecondThread - Youtube](https://www.youtube.com/watch?v=cVBzMXYc4ss&list=PLZU0kmvryb_HZpDW2yfn-H-RxAu_ts6xq) 379 | 380 | :page_facing_up: [Matrix Exponentiation cf Blog](https://codeforces.com/blog/entry/67776) 381 | 382 | :page_facing_up: [Matrix Expo - gfg](https://www.geeksforgeeks.org/matrix-exponentiation/) 383 | 384 | :movie_camera: [Matrix Expo - Errichto - Youtube](https://www.youtube.com/watch?v=eMXNWcbw75E) 385 | 386 | - [ ] [Matrix Expo - Problems - Mashup](https://codeforces.com/gym/102644) 387 | 388 | - [ ] [90+ problems on matrix expo - A2OJ](https://a2oj.com/category?ID=32) 389 | 390 | 391 | 392 |
[Back To Problemset](#problemset)

393 | 394 | 395 | 396 | 397 | > Note: There's a whole lot of problems still to be added. Any help will be appreciated. -------------------------------------------------------------------------------- /02-Codes/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/02-Codes/.DS_Store -------------------------------------------------------------------------------- /02-Codes/Editorials/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/02-Codes/Editorials/.DS_Store -------------------------------------------------------------------------------- /02-Codes/Editorials/ConvertToStrictlyIncreasing/ConevertToStrictlyIncreasing.md: -------------------------------------------------------------------------------- 1 |

Convert an array into Strictly Increasing Array

2 | Solution by Udit Gupta 3 | 4 |
5 |
6 | 7 |

Problem Statement

8 | 9 | Given an array consisting of N elements namely from A1 to AN. You have to convert this array to strictly increasing array in not more than K cost. 10 | Operations allowed : 11 | 1. Increase an element by 1. 12 | 2. Decrease an element by 1. 13 | 14 | Cost of each operation is 1 unit. 15 | 16 |
17 | 18 |

Constraints

19 | 20 | - N < 3000 21 | 22 | - K < 5000 23 | 24 | - |Ai| < 109 for all i from 1 to N. 25 | 26 |
27 | 28 |

Expected Time Complexity : O(N2)

29 | 30 | 31 |
32 | 33 |
34 | 35 |

Solution

36 | 37 |

Observation

38 |

39 | 40 | 1. After solving a few examples on pen and paper, we get to see that a greedy solution would fail on cases like [8, 1, 2, 3, 4] or [7, 1, 5, 2, 4, 8, 6]. 41 | 42 | 2. Another key observation is that in the transformed array which is strictly increasing, we will find that there is atleast one element which remains unchanged. 43 |

44 | 45 |

Deduction

46 | 47 | The problem statement above can be translated into simpler terms by stating that 48 | 49 | for each i from _1_ to _N-1_,   **Ai   <   Ai+1** 50 | 51 | Now we shall work on simplifying it further and deducing a general formula for all terms, 52 | 53 | - **Ai   <   Ai+1** 54 | 55 | - **Ai   ≤   Ai+1 - 1** 56 | 57 | - Subtracting i from both sides, 58 | 59 | - **Ai-i   ≤   Ai+1-1-i** 60 | 61 | _or_ 62 | 63 | - **Ai-i   ≤   Ai+1-(i+1)** 64 | 65 | This is the general formulation which should work for all indices in range [1, N]. 66 | i.e. 67 | 68 | The sequence represented by **(Ai-i)**   ∀   i ∈ [1, N] should be a **non decreasing sequence**. 69 | 70 | To achieve this we take use of dynamic programming and brute force our way to make the subsequent array element equal to the previous one if not or leaving it as it is depending on the previous dp state. 71 | 72 | This idea, here, comes from observation 2 and we can even end up converting the whole array to a single element. 73 | 74 |

75 | DP transition states: 76 | 77 | - **dp[j] = min(dp[j-1], dp[j] + abs(A[i] - B[j]))**   ∀   i, j ∈ [1, N]. 78 | 79 | Please note that the array **A** here is the converted array represented by (Ai-i)   ∀   i ∈ [1, N] and **B** is its sorted version. 80 | 81 | Our final answer that represents the minimum cost is stored in the final index of our **dp** array which represents the optimal array till last index of **A**. 82 |

83 | 84 | If our minimum cost is not more than **K** then we can certainly achieve the optimal solution. 85 | 86 | Note : This dp solution can be made more state verbose by storing the brute forced optimal substructure into their respective states in dp[i][j] but I prefer it this way. 87 | 88 |
89 | Python Code 90 |

91 | 92 | ```python 93 | N = int(input()) 94 | K = int(input()) 95 | A = [0] + list(map(int, input().split())) 96 | 97 | # Convert A[i] = A[i] - i 98 | for i in range(1, N+1): 99 | A[i] = A[i] - i 100 | 101 | # store sorted A in B to stay working 102 | # with best possible element already 103 | B = [0] + sorted(A[1:]) 104 | 105 | # dp array 106 | dp = [0 for x in range(N+1)] 107 | 108 | # unreachable state 109 | dp[0] = float('inf') 110 | 111 | for i in range(1, N+1): 112 | for j in range(1, N+1): 113 | dp[j] = min(dp[j-1], dp[j] + abs(A[i] - B[j])) 114 | 115 | 116 | # dp[N] represent the ans 117 | print("YES "+str(dp[N]) if dp[N] <= K else "NO") 118 | ``` 119 | 120 |

121 |
122 | 123 |
124 | C++ Code 125 |

126 | 127 | ```cpp 128 | #include 129 | #include 130 | #include 131 | 132 | signed main() { 133 | long long N, K; 134 | std::cin >> N >> K; 135 | 136 | std::vector A(N+1, 0ll); 137 | for (long long i = 1ll; i <= N; i++) { 138 | std::cin >> A.at(i); 139 | A.at(i) -= i; 140 | } 141 | 142 | // sorted version of A 143 | std::vector B{A}; 144 | std::sort(B.begin() + 1ll, B.end()); 145 | 146 | // dp array 147 | std::vector dp(N+1, 0ll); 148 | dp.at(0) = (long long) 1e9; 149 | 150 | for (long long i = 1ll; i <= N; i++) { 151 | for (long long j = 1ll; j <= N; j++) { 152 | dp.at(j) = std::min(dp.at(j-1), dp.at(j) + llabs(A.at(i) - B.at(j))); 153 | } 154 | } 155 | 156 | if (dp.at(N) <= K) { 157 | std::cout << "YES " << dp.at(N); 158 | } else { 159 | std::cout << "NO"; 160 | } 161 | 162 | return 0; 163 | } 164 | ``` 165 | 166 |

167 |
168 | 169 | 170 | 171 | 172 | Time Complexity : O(N2) 173 | 174 | Space Complexity : O (N) 175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /02-Codes/Editorials/ConvertToStrictlyIncreasing/convertToIncreasing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | signed main() { 6 | long long N, K; 7 | cin >> N >> K; 8 | 9 | vector A(N+1, 0ll); 10 | for (long long i = 1ll; i <= N; i++) { 11 | cin >> A.at(i); 12 | A.at(i) -= i; 13 | } 14 | 15 | // sorted version of A 16 | vector B{A}; 17 | sort(B.begin() + 1ll, B.end()); 18 | 19 | // dp array 20 | vector dp(N+1, 0ll); 21 | dp.at(0) = (long long) 1e9; 22 | 23 | for (long long i = 1ll; i <= N; i++) { 24 | for (long long j = 1ll; j <= N; j++) { 25 | dp.at(j) = min(dp.at(j-1), dp.at(j) + llabs(A.at(i) - B.at(j))); 26 | } 27 | } 28 | 29 | if (dp.at(N) <= K) { 30 | cout << "YES " << dp.at(N); 31 | } else { 32 | cout << "NO"; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /02-Codes/Editorials/ConvertToStrictlyIncreasing/convertToIncreasing.py: -------------------------------------------------------------------------------- 1 | N = int(input()) 2 | K = int(input()) 3 | A = [0] + list(map(int, input().split())) 4 | 5 | # Convert A[i] = A[i] - i 6 | for i in range(1, N+1): 7 | A[i] = A[i] - i 8 | 9 | # store sorted A in B to stay working 10 | # with best possible element already 11 | B = [0] + sorted(A[1:]) 12 | 13 | # dp array 14 | dp = [0 for x in range(N+1)] 15 | 16 | # unreachable state 17 | dp[0] = float('inf') 18 | 19 | for i in range(1, N+1): 20 | for j in range(1, N+1): 21 | dp[j] = min(dp[j-1], dp[j] + abs(A[i] - B[j])) 22 | 23 | 24 | # dp[N] represent the ans 25 | print("YES "+str(dp[N]) if dp[N] <= K else "NO") -------------------------------------------------------------------------------- /02-Codes/Editorials/Random/TransposeColorsWork.cpp: -------------------------------------------------------------------------------- 1 | // J A I S H R E E R A M // 2 | 3 | #include 4 | 5 | #pragma GCC optimize "trapv" 6 | // #pragma GCC optimize ("Ofast") 7 | // // #pragma GCC target ("fpmath=sse,sse2") 8 | // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") 9 | // #pragma GCC optimize ("-ffloat-store") 10 | 11 | 12 | using namespace std; 13 | 14 | typedef long long int lld; 15 | typedef unsigned long long int llu; 16 | 17 | #define forn(I7, E4) for(lld I7=0ll; I7 < E4; (I7)+=1ll) 18 | #define forn1(I7, E4) for(lld I7=1ll; I7 < E4+1; (I7)+=1ll) 19 | #define len(v) ((int)((v).size())) 20 | #define all(x) (x).begin(), (x).end() 21 | #define rall(x) (x).rbegin(), (x).rend() 22 | #define f1 first 23 | #define s2 second 24 | 25 | template ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } 26 | template < typename T_container, typename T = typename enable_if < !is_same::value, typename T_container::value_type >::type > 27 | ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } 28 | void dbg_out() { cout << "\n"; } 29 | template void dbg_out(Head H, Tail... T) { cout << H << ", "; dbg_out(T...);} 30 | #define _____error_____(...) cout << #__VA_ARGS__ << " : ", dbg_out(__VA_ARGS__) 31 | 32 | const lld d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; 33 | const lld d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; 34 | 35 | 36 | const long double EPS = 1e-6; 37 | lld TempVar, mod, MOD; 38 | 39 | 40 | /* 41 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 | */ 43 | 44 | 45 | 46 | 47 | 48 | 49 | void solveEachTest(int _TestCase) { 50 | // cout << "Case #" << _TestCase << ": "; 51 | vectormoves; 52 | while(cin >> TempVar) { 53 | moves.push_back((int)TempVar); 54 | } 55 | int n = (int)sqrt(moves.back()); 56 | 57 | vector> matr(n+1, vector(n, '-')); 58 | forn(i, n) { 59 | forn(j, n) { 60 | matr[i][j] = char('A' + i); 61 | } 62 | } 63 | 64 | matr[n][0] = '.'; 65 | 66 | 67 | // for (auto row : matr) { 68 | // cout << row << "\n"; 69 | // } cout << "\n"; 70 | 71 | int sz = (int)moves.size(); 72 | int tempX = n, tempY = 0; 73 | forn(i, sz-1) { 74 | int posX = moves[i] / n, posY = moves[i] % n; 75 | swap(matr[posX][posY], matr[tempX][tempY]); 76 | tempX = posX, tempY = posY; 77 | // for (auto row : matr) { 78 | // cout << row << "\n"; 79 | // } cout << "\n"; 80 | } 81 | 82 | bool ok = true; 83 | forn(i, n) { 84 | set cnt; 85 | forn(j, n) { 86 | cnt.insert(matr[i][j]); 87 | } 88 | ok &= ((int)cnt.size() == n); 89 | } 90 | 91 | cout << "Checker Verdict:\n"; 92 | cout << (ok ? "Accepted" : "Wrong Answer") << "\n"; 93 | cout << "Moves : " << moves.size(); 94 | 95 | return; 96 | } 97 | 98 | 99 | signed main() { 100 | ios_base::sync_with_stdio(false); cin.tie(0); 101 | // cout.precision(10); cout << fixed; 102 | #ifdef LUCTIVUD 103 | // const auto start_time = std::chrono::high_resolution_clock::now(); 104 | freopen("/home/luctivud/CPPractice/Zinput.txt", "r", stdin); 105 | freopen("/home/luctivud/CPPractice/Zoutput.txt", "w", stdout); 106 | #endif 107 | 108 | 109 | MOD = mod = lld(1e9) + 7ll; 110 | lld _T0T4 = 1; 111 | // cin >> _T0T4; 112 | 113 | for (int _TestCase = 1; _TestCase <= _T0T4; _TestCase++) { 114 | solveEachTest(_TestCase); 115 | cout << "\n"; 116 | } 117 | 118 | 119 | #ifdef LUCTIVUD 120 | // auto end_time = std::chrono::high_resolution_clock::now(); 121 | // std::chrono::duration diff = end_time - start_time; 122 | // cerr << "Finished in : " << diff.count() << "\n"; 123 | #endif 124 | 125 | return 0; 126 | } 127 | 128 | 129 | /* ~~ 130 | Author : Udit "luctivud" Gupta 131 | linkedin : https://www.linkedin.com/in/udit-gupta-1b7863135/ 132 | */ 133 | -------------------------------------------------------------------------------- /02-Codes/README.md: -------------------------------------------------------------------------------- 1 |

Containing codes of some problems

2 | 3 |

Check the subfolders for contents, it has some snippets and editorials

4 | -------------------------------------------------------------------------------- /02-Codes/Snippets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/02-Codes/Snippets/.DS_Store -------------------------------------------------------------------------------- /02-Codes/Snippets/Raw/166_e_MatMul_w_Ashishgup.cpp: -------------------------------------------------------------------------------- 1 | // J A I S H R E E R A M // 2 | 3 | #include 4 | 5 | #pragma GCC optimize "trapv" 6 | // #pragma GCC optimize ("Ofast") 7 | // // #pragma GCC target ("fpmath=sse,sse2") 8 | // #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") 9 | // #pragma GCC optimize ("-ffloat-store") 10 | 11 | 12 | using namespace std; 13 | 14 | typedef long long int lld; 15 | typedef unsigned long long int llu; 16 | 17 | #define forn(I7, E4) for(lld I7=0ll; I7 < E4; (I7)+=1ll) 18 | #define forn1(I7, E4) for(lld I7=1ll; I7 < E4+1; (I7)+=1ll) 19 | #define len(v) ((int)((v).size())) 20 | #define all(x) (x).begin(), (x).end() 21 | #define rall(x) (x).rbegin(), (x).rend() 22 | #define f1 first 23 | #define s2 second 24 | 25 | template ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } 26 | template < typename T_container, typename T = typename enable_if < !is_same::value, typename T_container::value_type >::type > 27 | ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } 28 | void dbg_out() { cout << "\n"; } 29 | template void dbg_out(Head H, Tail... T) { cout << H << ", "; dbg_out(T...);} 30 | #define _____error_____(...) cout << #__VA_ARGS__ << " : ", dbg_out(__VA_ARGS__) 31 | 32 | const lld d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; 33 | const lld d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; 34 | 35 | 36 | const long double EPS = 1e-6; 37 | lld TempVar, mod, MOD; 38 | 39 | 40 | /* 41 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 42 | */ 43 | 44 | 45 | // matrix exponentiation snippet:: 46 | // // courtesy Ashishgup3 via Github 47 | 48 | 49 | 50 | 51 | 52 | lld add(lld a, lld b) { 53 | lld res = a + b; 54 | if(res >= MOD) return res - MOD; 55 | return res; 56 | } 57 | 58 | lld mult(lld a, lld b) { 59 | long long res = a; 60 | res *= b; 61 | if(res >= MOD) return res % MOD; 62 | return res; 63 | } 64 | 65 | 66 | 67 | const lld SZ = 2; // size of MaGiC 68 | 69 | struct matrix { 70 | lld MaGiC[SZ][SZ]; 71 | 72 | void reset() { 73 | memset(MaGiC, 0, sizeof(MaGiC)); 74 | } 75 | 76 | void makeiden() { 77 | reset(); 78 | for(lld i=0;i>= 1; 119 | } 120 | return res; 121 | } 122 | 123 | 124 | // // GUIDE : 125 | // 126 | // matrix m; 127 | // m.makeiden(); 128 | // 129 | // m.MaGiC[0][0] = 0, m.MaGiC[0][1] = 1; 130 | // m.MaGiC[1][0] = 3, m.MaGiC[1][1] = 2; 131 | // 132 | // m = power(m, n); 133 | // 134 | // cout << m.MaGiC[0][0]; 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | void solveEachTest(int _TestCase) { 144 | // cout << "Case #" << _TestCase << ": "; 145 | lld n; cin >> n; 146 | 147 | matrix m; 148 | m.makeiden(); 149 | 150 | m.MaGiC[0][0] = 0, m.MaGiC[0][1] = 1; 151 | m.MaGiC[1][0] = 3, m.MaGiC[1][1] = 2; 152 | 153 | m = power(m, n); 154 | 155 | cout << m.MaGiC[0][0]; 156 | 157 | return; 158 | } 159 | 160 | 161 | signed main() { 162 | ios_base::sync_with_stdio(false); cin.tie(0); 163 | // cout.precision(10); cout << fixed; 164 | #ifdef LUCTIVUD 165 | // const auto start_time = std::chrono::high_resolution_clock::now(); 166 | freopen("/home/luctivud/CPPractice/Zinput.txt", "r", stdin); 167 | freopen("/home/luctivud/CPPractice/Zoutput.txt", "w", stdout); 168 | #endif 169 | 170 | 171 | MOD = mod = lld(1e9) + 7ll; 172 | lld _T0T4 = 1; 173 | // cin >> _T0T4; 174 | 175 | for (int _TestCase = 1; _TestCase <= _T0T4; _TestCase++) { 176 | solveEachTest(_TestCase); 177 | cout << "\n"; 178 | } 179 | 180 | 181 | #ifdef LUCTIVUD 182 | // auto end_time = std::chrono::high_resolution_clock::now(); 183 | // std::chrono::duration diff = end_time - start_time; 184 | // cerr << "Finished in : " << diff.count() << "\n"; 185 | #endif 186 | 187 | return 0; 188 | } 189 | 190 | 191 | /* ~~ 192 | Author : Udit "luctivud" Gupta 193 | linkedin : https://www.linkedin.com/in/udit-gupta-1b7863135/ 194 | */ 195 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/02-Codes/Snippets/Sublime/.DS_Store -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/FastReadWrite.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 57) && c != '-'); c = gc()); 12 | if (c == '-') {neg = 1; c = gc();} 13 | for (; c > 47 && c < 58; c = gc()) {x = (x << 1) + (x << 3) + c - 48;} 14 | if (neg) x = -x; 15 | } 16 | 17 | void scanLong(lld &x) { 18 | register lld c = gc(); 19 | x = 0; 20 | bool neg = 0; 21 | for (; ((c < 48 || c > 57) && c != '-'); c = gc()); 22 | if (c == '-') {neg = 1; c = gc();} 23 | for (; c > 47 && c < 58; c = gc()) {x = (x << 1) + (x << 3) + c - 48;} 24 | if (neg) x = -x; 25 | } 26 | 27 | #define pc(x) putchar_unlocked(x); 28 | 29 | inline void writeInt (int n) { 30 | int N = n, rev, count = 0; 31 | rev = N; 32 | if (N == 0) { pc('0'); pc('\n'); return ;} 33 | while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s 34 | rev = 0; 35 | while (N != 0) { rev = (rev << 3) + (rev << 1) + N % 10; N /= 10;} //store reverse of N in rev 36 | while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;} 37 | while (count--) pc('0'); 38 | pc ('\n'); 39 | } 40 | 41 | 42 | inline void writeLong (lld n) { 43 | lld N = n, rev, count = 0; 44 | rev = N; 45 | if (N == 0) { pc('0'); pc('\n'); return ;} 46 | while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s 47 | rev = 0; 48 | while (N != 0) { rev = (rev << 3) + (rev << 1) + N % 10; N /= 10;} //store reverse of N in rev 49 | while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;} 50 | while (count--) pc('0'); 51 | pc ('\n'); 52 | } 53 | 54 | 55 | 56 | ]]> 57 | snip-read-write-fast 58 | FastIo 59 | 60 | source.c++ 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/My Snippets not used now.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/02-Codes/Snippets/Sublime/My Snippets not used now.tar.gz -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/addundirectededge.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | adj[]) { 5 | adj[a].push_back(b); 6 | adj[b].push_back(a); 7 | return; 8 | } 9 | 10 | 11 | ]]> 12 | snip-add-undirected-edge 13 | Add Undirected Edge 14 | 15 | source.c++ 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/basic-template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 4 | #pragma GCC optimize "trapv" 5 | 6 | using namespace std; 7 | 8 | typedef long long int lld; 9 | 10 | signed main() { 11 | 12 | // Input - Output and optimization 13 | 14 | ios_base::sync_with_stdio(false); cin.tie(0); 15 | cout.precision(10); cout << fixed; 16 | 17 | #ifdef LUCTIVUD 18 | freopen("/home/luctivud/CPPractice/Zinput.txt", "r", stdin); 19 | freopen("/home/luctivud/CPPractice/Zoutput.txt", "w", stdout); 20 | #endif 21 | 22 | $0 23 | return 0; 24 | } 25 | 26 | ]]> 27 | snip-basic-template 28 | Basic Template for c++ 29 | 30 | source.c++ 31 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/check execution time.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | (S34p - S34t); 6 | cout << "Time Elapsed: " << D34n.count() / (long double) 1e6 << " seconds" << endl; 7 | 8 | ]]> 9 | snip-debug 10 | CP Template 11 | 12 | source.c++ 13 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/checkvalid.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | = 0 and i < n; 6 | } 7 | 8 | ]]> 9 | 10 | snip-checkvalid 11 | 12 | source.c++ 13 | 14 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/cmp-priority.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | > 5 | class ${1:cmp} { 6 | public: 7 | bool operator() (${2:const pair } &a, ${2:const pair } &b) { 8 | return ${3:a.s2 > b.s2}; 9 | } 10 | }; 11 | ]]> 12 | snip-cmp-pq 13 | cmp for priority queue 14 | 15 | source.c++ 16 | 17 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/common-completions.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c++, meta.block.c++, meta.function.c++", 3 | 4 | "completions": 5 | [ 6 | {"trigger": "vectorlld", "contents": "vector ${1:arr}"}, 7 | {"trigger": "vectorpalld", "contents": "vector> ${1:arr}"}, 8 | {"trigger": "vectorvelldp2", "contents": "vector> ${1:dp}(${2:n}, vector(${3:m}, ${4:-1})) $0"}, 9 | {"trigger": "vectorstr", "contents": "vector ${1:arr}"}, 10 | {"trigger": "vectorcha", "contents": "vector ${1:arr}"}, 11 | {"trigger": "vectorboo", "contents": "vector ${1:visited}"}, 12 | {"trigger": "setlld", "contents": "set ${1:visited}"}, 13 | {"trigger": "setpall", "contents": "set> ${1:arr}"}, 14 | {"trigger": "setstr", "contents": "set ${1:arr}"}, 15 | {"trigger": "setchar", "contents": "set ${1:arr}"}, 16 | {"trigger": "setbool", "contents": "set ${1:arr}"}, 17 | {"trigger": "maplld", "contents": "map ${1:arr}"}, 18 | {"trigger": "mapstr", "contents": "map ${1:arr}"}, 19 | {"trigger": "front", "contents": "front()"}, 20 | {"trigger": "first", "contents": "first"}, 21 | {"trigger": "second", "contents": "second"}, 22 | {"trigger": "make_pair", "contents": "make_pair(${1:fir}, ${2:sec})"}, 23 | {"trigger": "make_tuple", "contents": "make_tuple"}, 24 | {"trigger": "push_back", "contents": "push_back(${1:el})"}, 25 | {"trigger": "max_element", "contents": "max_element(all(${1:arr}))"}, 26 | {"trigger": "min_element", "contents": "min_element(all(${1:arr}))"}, 27 | {"trigger": "accumulate", "contents": "accumulate(all(${1:arr}), ${2:0ll})"}, 28 | {"trigger": "sortarr", "contents": "sort(${1:r}all(${2:arr}))"}, 29 | {"trigger": "emplace_back", "contents": "emplace_back()"}, 30 | {"trigger": "insert", "contents": "insert()"}, 31 | {"trigger": "push", "contents": "push()"}, 32 | {"trigger": "for4", "contents": "for4(${1: i}, ${2:0ll}, ${3:n}, ${4:1ll}) {${0}}"}, 33 | {"trigger": "EACH", "contents": "EACH(${1:it}, ${2:arr}) {${0}}"}, 34 | {"trigger": "find", "contents": "find(all(${1:arr}), ${2:val}) != ${1:arr}.end()"}, 35 | {"trigger": "lower_bound", "contents": "lower_bound(all(${1:arr}), ${2:val})"}, 36 | {"trigger": "upper_bound", "contents": "upper_bound(all(${1:arr}), ${2:val})"}, 37 | {"trigger": "reverse", "contents": "reverse(all(${1:arr}))"}, 38 | {"trigger": "len", "contents": "len(${1:arr})"}, 39 | // {"trigger": "debspace", "contents": "debspace(${1})"}, 40 | // {"trigger": "debline", "contents": "debline(${1})"}, 41 | {"trigger": "error", "contents": "_____error_____(${1})"}, 42 | {"trigger": "cin", "contents": "cin >> "}, 43 | {"trigger": "cout", "contents": "cout << "}, 44 | {"trigger": "d4dir", "contents": "const lld d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};"}, 45 | {"trigger": "d8dir", "contents": "const lld d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};"}, 46 | // {"trigger": "read", "contents": "Read(${1})"}, 47 | // {"trigger": "println", "contents": "Println(${1})"}, 48 | // {"trigger": "NOYES", "contents": "Println(\"NO\\0YES\" + 3 * (${1:condition}));"}, 49 | {"trigger": "transform", "contents": "transform(all(${1:str}), ${1:str}.begin(), ::to${2:low}er);"}, 50 | ] 51 | } 52 | 53 | 54 | 55 | // "vector ${1:arr}", 56 | // "vector> ${1:arr}", 57 | // "vector> ${1:dp}(${2:n}, vector(${3:m}, ${4:-1})) $0", 58 | // "vector ${1:arr}", 59 | // "vector ${1:arr}", 60 | // "vector ${1:visited}", 61 | // "set ${1:visited}", 62 | // "set> ${1:arr}", 63 | // "set ${1:arr}", 64 | // "set ${1:arr}", 65 | // "set ${1:arr}", 66 | // "map ${1:arr}", 67 | // "map ${1:arr}", 68 | // "map ${1:arr}", 69 | // "front()", 70 | // "first", 71 | // "second", 72 | // "make_pair(${1:fir}, ${2:sec})", 73 | // "make_tuple", 74 | // "push_back(${1:el})", 75 | // "max_element(all(${1:arr}))", 76 | // "min_element(all(${1:arr}))", 77 | // "accumulate(all(${1:arr}), ${2:0ll})", 78 | // "emplace_back()", 79 | // "insert()", 80 | // "push()", 81 | // "input(${0})", 82 | // "for4(${1: i}, ${2:0ll}, ${3:n}, ${4:1ll}) {${0}}", 83 | // "EACH(${1:it}, ${2:arr}) {${0}}", 84 | // "find(all(${1:arr}), ${2:val}) != ${1:arr}.end()", 85 | // "lower_bound(all(${1:arr}), ${2:val})", 86 | // "upper_bound(all(${1:arr}), ${2:val})", 87 | // "reverse(all(${1:arr}))", 88 | // "len(${1:arr})", 89 | // "debspace(${1})", 90 | // "debline(${1})", 91 | // "error(${1})", 92 | // "read(${1})", 93 | // "println(${1})", -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/dsu-array.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | struct DSU { 7 | lld par3t[SZ], ran4k[SZ]; 8 | DSU() { 9 | forn(i,SZ) par3t[i] = i, ran4k[i] = 1; 10 | } 11 | 12 | lld find_repr(lld x) { // path compression 13 | if (par3t[x] != x) par3t[x] = find_repr(par3t[x]); 14 | return par3t[x]; 15 | } 16 | 17 | bool Union(lld x, lld y) { // union-by-ran4k 18 | x = find_repr(x), y = find_repr(y); 19 | if (x == y) return 0; 20 | if (ran4k[x] < ran4k[y]) swap(x,y); 21 | ran4k[x] += ran4k[y], par3t[y] = x; 22 | return 1; 23 | } 24 | }; 25 | 26 | DSU<100001> D; 27 | 28 | 29 | ]]> 30 | snip-dsu-array 31 | dsu using path compression 32 | 33 | source.c++ 34 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/dsu.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | &par3t, vector &ran4k) { 6 | iota(all(par3t), 0ll); 7 | } 8 | 9 | lld find_repr(lld x, vector &par3t, vector &ran4k) { 10 | if (x != par3t[x]) 11 | par3t[x] = find_repr(par3t[x], par3t, ran4k); 12 | return par3t[x]; 13 | } 14 | 15 | void Union(lld x, lld y, vector &par3t, vector &ran4k) { 16 | x = find_repr(x, par3t, ran4k); 17 | y = find_repr(y, par3t, ran4k); 18 | 19 | if (x == y) return; 20 | 21 | if (ran4k[x] < ran4k[y]) { 22 | par3t[x] = y; 23 | } else if (ran4k[x] > ran4k[y]) { 24 | par3t[y] = x; 25 | } else { 26 | par3t[y] = x; 27 | ran4k[x] = ran4k[x] + 1; 28 | } 29 | return; 30 | } 31 | 32 | 33 | 34 | // create par3t and ran4k at the place. 35 | vector par3t(n, 0); 36 | vector ran4k(n, 0); 37 | makeSet(n, par3t, ran4k); 38 | ]]> 39 | snip-dsu 40 | dsu using path compression 41 | 42 | source.c++ 43 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/errorWatch.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | _it(_ss); huehue(_it, args); cout << "\n";} 5 | 6 | void huehue(istream_iterator it) {} 7 | template 8 | void huehue(istream_iterator it, T a, Args... args) { 9 | cout << *it << " = " << a << ", "; 10 | huehue(++it, args...); 11 | } 12 | 13 | ]]> 14 | snip-error-watch 15 | Error Watch 16 | 17 | source.c++ 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/eulerTotientFunc.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 1) 16 | result -= result / n0n; 17 | return result; 18 | } 19 | 20 | vector phiVal; 21 | // precompute_phiVal complexity O(N.log(log(N))) 22 | void precompute_phiVal(lld n0n) { 23 | phiVal.resize(n0n+1); 24 | phiVal[0] = 0; 25 | phiVal[1] = 1; 26 | for (lld i = 2; i <= n0n; i++) 27 | phiVal[i] = i; 28 | 29 | for (lld i = 2; i <= n0n; i++) { 30 | if (phiVal[i] == i) { 31 | for (lld j = i; j <= n0n; j += i) 32 | phiVal[j] -= phiVal[j] / i; 33 | } 34 | } 35 | } 36 | 37 | // precompute_phiVal(n); 38 | 39 | 40 | ]]> 41 | snip-totient-euler-phi 42 | Euler totient Phi 43 | 44 | source.c++ 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/leetcode-solution-class.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 7 | #pragma GCC optimize "trapv" 8 | 9 | using namespace std; 10 | // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 11 | 12 | // Solution class to work with 13 | class Solution { 14 | 15 | }; 16 | 17 | 18 | // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 19 | 20 | signed main() { 21 | 22 | ios_base::sync_with_stdio(false); cin.tie(0);cout.precision(10); 23 | 24 | Solution Obj; 25 | 26 | // read input vars here and pass it to solve. 27 | 28 | int ans = Obj.solve(); 29 | 30 | cout << ans; 31 | 32 | return 0; 33 | } 34 | // Random Thought : null 35 | ]]> 36 | snip-leetcode-solution 37 | Solution class implementation 38 | 39 | source.c++ 40 | 41 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/manipulated-sieve-of-eratosthene.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | isPrime(MAXN , true); // checkIfPrime 10 | vectorprime_numbers; // List of prime numbers 11 | vectorsmallest_prime_factor(MAXN); // smallest_prime_factor of a number 12 | 13 | 14 | void manipulated_seive() { 15 | isPrime[0] = isPrime[1] = false ; 16 | 17 | prime_numbers.push_back(2); 18 | smallest_prime_factor[2] = 2ll; 19 | 20 | for (long long int i=4; i < MAXN ; i+=2) { 21 | isPrime[i] = false; 22 | smallest_prime_factor[i] = 2ll; 23 | } 24 | 25 | for (long long int i = 3; i < MAXN ; i+=2) { 26 | if (isPrime[i]) { 27 | prime_numbers.push_back(i); 28 | smallest_prime_factor[i] = i; 29 | } 30 | 31 | for (long long int j = 0; j < (int)prime_numbers.size() && i * prime_numbers[j] < MAXN && prime_numbers[j] <= smallest_prime_factor[i]; j++) { 32 | isPrime[i * prime_numbers[j]] = false; 33 | smallest_prime_factor[i * prime_numbers[j]] = prime_numbers[j] ; 34 | } 35 | } 36 | } 37 | 38 | 39 | /* This should be called inside main. */ 40 | manipulated_seive(); 41 | 42 | 43 | 44 | ]]> 45 | snip-fast-sieve 46 | sieve-manipulated 47 | 48 | source.c++ 49 | 50 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/matrix-expo.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | = MOD) return res - MOD; 14 | return res; 15 | } 16 | 17 | lld mult(lld a, lld b) { 18 | long long res = a; 19 | res *= b; 20 | if(res >= MOD) return res % MOD; 21 | return res; 22 | } 23 | 24 | 25 | 26 | const lld SZ = 2; // size of MaGiC 27 | 28 | struct matrix { 29 | lld MaGiC[SZ][SZ]; 30 | 31 | void reset() { 32 | memset(MaGiC, 0, sizeof(MaGiC)); 33 | } 34 | 35 | void makeiden() { 36 | reset(); 37 | for(lld i=0;i>= 1; 78 | } 79 | return res; 80 | } 81 | 82 | 83 | // // GUIDE : 84 | // 85 | // matrix m; 86 | // m.makeiden(); 87 | // 88 | // m.MaGiC[0][0] = 0, m.MaGiC[0][1] = 1; 89 | // m.MaGiC[1][0] = 3, m.MaGiC[1][1] = 2; 90 | // 91 | // m = power(m, n); 92 | // 93 | // cout << m.MaGiC[0][0]; 94 | 95 | 96 | ]]> 97 | snip-matrix-expo 98 | Matrix Expo on recurrence formula 99 | 100 | source.c++ 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/max-biparite-matching.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | > graph, lld u, vector &visited, vector &matchR) { 7 | for (lld v = 0; v < n; v++) { 8 | if (graph[u][v] and !visited[v]) { 9 | visited[v] = true; 10 | if (matchR[v] < 0 || bpm(graph, matchR[v], visited, matchR)) { 11 | matchR[v] = u; 12 | return true; 13 | } 14 | } 15 | } 16 | return false; 17 | } 18 | 19 | 20 | lld maxBPM(vector> graph, vector &matchR) { 21 | lld result = 0; 22 | for (lld u = 0; u < m; u++) { 23 | vector visited(n, 0); 24 | if (bpm(graph, u, visited, matchR)) 25 | result++; 26 | } 27 | return result; 28 | } 29 | 30 | 31 | // This should be in main // 0.indexed 32 | n = 6, m = 6; 33 | // for i -> m then for j -> n 34 | vector> graph(m, vector(n, false)); 35 | vector matchR(n, -1); 36 | maxBPM(graph, matchR); 37 | ]]> 38 | snip-bipartite-matching 39 | Max B M 40 | 41 | source.c++ 42 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/modop.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | >= 1; 20 | base = (base * base) % mod; 21 | } 22 | return res; 23 | } 24 | lld minv(lld base) { 25 | return mpow(base, mod - 2); 26 | } 27 | lld mdiv(lld a, lld b) { 28 | return mmul(a, minv(b)); 29 | } 30 | } 31 | 32 | 33 | 34 | lld nck(lld n, lld k) { 35 | if (n < k) return 0; 36 | lld r = 1; 37 | 38 | for (lld i = n; i > n - k; i--) { 39 | r = (r * i) % mod; 40 | } 41 | 42 | for (lld i = 1; i <= k; i++) { 43 | r = (r * minv(i)) % mod; 44 | } 45 | return r; 46 | } 47 | ]]> 48 | snip-namespace-modop 49 | modops and ncr on demand 50 | 51 | source.c++ 52 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/myReadTemplate.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | T Read(T& x) { cin >> x; return x; } 8 | template void Print(T a) { cout << a; } 9 | template void Println(T a) { cout << a << "\n"; } 10 | 11 | template void Read(vector &arr) { EACH(i, arr) cin >> (i); } 12 | template void Print(vector arr) { EACH(i, arr) {cout << i << " ";} } 13 | template void Println(vector arr) { EACH(i, arr) {cout << i << " ";} cout << "\n"; } 14 | 15 | template void Read(vector> &arr) { EACH(i, arr) Read(i); } 16 | template void Print(vector> arr) { EACH(i, arr) Println(i); } 17 | template void Println(vector> arr) { EACH(i, arr) Println(i); } 18 | 19 | template void Read(vector &arr, Args &... args) { Read(arr); Read(args...);} 20 | template void Read(vector> &arr, Args &... args) { Read(arr); Read(args...);} 21 | template void Read(T &a, Args &... args) { cin >> (a); Read(args...); } 22 | template void Print(vector &arr, Args &... args) { Print(arr); Print(args...);} 23 | template void Print(T a, Args... args) { cout << a << " "; Print(args...); }; 24 | template void Println(vector &arr, Args &... args) { Print(arr); Println(args...);} 25 | template void Println(T a, Args... args) { cout << a << " "; Println(args...); }; 26 | 27 | ]]> 28 | snip-read-template 29 | Read-Print-Template 30 | 31 | source.c++ 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/ncr-on-demand.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 48 | snip-ncr-binomial 49 | NCR on demand 50 | 51 | source.c++ 52 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/pbds.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 4 | #include 5 | using namespace __gnu_pbds; 6 | 7 | template using PBSET = tree, rb_tree_tag, tree_order_statistics_node_update>; 8 | 9 | /* 10 | .insert(el) - set hai! 11 | .find_by_order(3) - returns an iterator to the k-th largest element (counting from zero) 12 | .order_of_key(6) - the number of items in a set that are strictly smaller than our item 13 | */ 14 | 15 | ]]> 16 | snip-pbds 17 | pbds 18 | 19 | source.c++ 20 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/power-expo.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 0) { 13 | if (y & 1) 14 | res = (res * x) % MOD; 15 | 16 | y = y >> 1; 17 | x = (x * x) % MOD; 18 | } 19 | return res; 20 | } 21 | 22 | 23 | ]]> 24 | snip-power 25 | power exponentiation 26 | 27 | source.c++ 28 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/sieve-of-eratosthene.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | isPrime(MAXN, true); // Temp vector for sieve result 8 | vector prime_numbers; // vector of primes 9 | 10 | void sieve() { 11 | isPrime[0] = isPrime[1] = false; 12 | prime_numbers.push_back(2); 13 | for (lld i=4; i< MAXN; i+=2) 14 | isPrime[i] = false; 15 | for (lld i = 3; i < MAXN; i+=2) { 16 | if (isPrime[i]) { 17 | prime_numbers.push_back(i); // remove if not necessary 18 | for (lld j = 2 * i; j < MAXN; j += i) { 19 | isPrime[j] = false; 20 | } 21 | } 22 | } 23 | } 24 | 25 | /* This should be called inside main. */ 26 | sieve(); // function call in main 27 | 28 | 29 | ]]> 30 | snip-sieve 31 | sieve 32 | 33 | source.c++ 34 | -------------------------------------------------------------------------------- /02-Codes/Snippets/Sublime/topcoder-template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 5 | using namespace std; 6 | 7 | 8 | typedef long long int lld; 9 | typedef unsigned long long int llu; 10 | 11 | #define forn(I7, E4) for(lld I7=0ll; I7 < E4; (I7)+=1ll) 12 | #define forn1(I7, E4) for(lld I7=1ll; I7 < E4+1; (I7)+=1ll) 13 | #define len(v) ((int)((v).size())) 14 | #define all(x) (x).begin(), (x).end() 15 | #define rall(x) (x).rbegin(), (x).rend() 16 | #define f1 first 17 | #define s2 second 18 | 19 | template ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } 20 | template < typename T_container, typename T = typename enable_if < !is_same::value, typename T_container::value_type >::type > 21 | ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } 22 | void dbg_out() { cout << "\n"; } 23 | template void dbg_out(Head H, Tail... T) { cout << H << ", "; dbg_out(T...);} 24 | #define _____error_____(...) cout << #__VA_ARGS__ << " : ", dbg_out(__VA_ARGS__) 25 | 26 | const lld d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1}; 27 | const lld d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1}; 28 | 29 | 30 | const long double EPS = 1e-6; 31 | lld TempVar, mod, MOD; 32 | 33 | 34 | class ${1:} { 35 | public: 36 | ${0:} 37 | }; 38 | 39 | 40 | 41 | 42 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 43 | 44 | 45 | 46 | /* 47 | signed main() { 48 | 49 | ios_base::sync_with_stdio(false); cin.tie(0); 50 | // cout.precision(10); cout << fixed; 51 | #ifdef LUCTIVUD 52 | // const auto start_time = std::chrono::high_resolution_clock::now(); 53 | freopen("/home/luctivud/CPPractice/Zinput.txt", "r", stdin); 54 | freopen("/home/luctivud/CPPractice/Zoutput.txt", "w", stdout); 55 | #endif 56 | 57 | ${1:} obj; 58 | 59 | 60 | 61 | 62 | return 0; 63 | } 64 | 65 | */ 66 | 67 | ]]> 68 | snip-topcoder-template 69 | Topcoder class template 70 | 71 | source.c++ 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /02-Codes/Snippets/mydebug.h: -------------------------------------------------------------------------------- 1 | // C++ includes used for precompiling -*- C++ -*- 2 | 3 | // Copyright (C) 2003-2021 Free Software Foundation, Inc. 4 | // 5 | // This file is part of the GNU ISO C++ Library. This library is free 6 | // software; you can redistribute it and/or modify it under the 7 | // terms of the GNU General Public License as published by the 8 | // Free Software Foundation; either version 3, or (at your option) 9 | // any later version. 10 | 11 | // This library is distributed in the hope that it will be useful, 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | // GNU General Public License for more details. 15 | 16 | // Under Section 7 of GPL version 3, you are granted additional 17 | // permissions described in the GCC Runtime Library Exception, version 18 | // 3.1, as published by the Free Software Foundation. 19 | 20 | // You should have received a copy of the GNU General Public License and 21 | // a copy of the GCC Runtime Library Exception along with this program; 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 | // . 24 | 25 | /** @file stdc++.h 26 | * This is an implementation file for a precompiled header. 27 | */ 28 | 29 | // 17.4.1.2 Headers 30 | 31 | // C 32 | #ifndef _GLIBCXX_NO_ASSERT 33 | #include 34 | #endif 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | #if __cplusplus >= 201103L 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #endif 63 | 64 | // C++ 65 | #include 66 | #include 67 | #include 68 | #include 69 | #include 70 | #include 71 | #include 72 | #include 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | #include 79 | #include 80 | #include 81 | #include 82 | #include 83 | #include 84 | #include 85 | #include 86 | #include 87 | #include 88 | #include 89 | #include 90 | #include 91 | #include 92 | #include 93 | #include 94 | #include 95 | #include 96 | #include 97 | 98 | #include // for prettyprint 99 | 100 | #if __cplusplus >= 201103L 101 | #include 102 | #include 103 | #include 104 | #include 105 | #include 106 | #include 107 | #include 108 | #include 109 | #include 110 | #include 111 | #include 112 | #include 113 | #include 114 | #include 115 | #include 116 | #include 117 | #include 118 | #include 119 | #include 120 | #include 121 | #endif 122 | 123 | #if __cplusplus >= 201402L 124 | #include 125 | #endif 126 | 127 | #if __cplusplus >= 201703L 128 | #include 129 | #include 130 | // #include 131 | #include 132 | #include 133 | #include 134 | #include 135 | #include 136 | #endif 137 | 138 | #if __cplusplus > 201703L 139 | #include 140 | #include 141 | #include 142 | #include 143 | #if __cpp_impl_coroutine 144 | # include 145 | #endif 146 | #include 147 | #include 148 | #include 149 | #include 150 | #include 151 | #include 152 | #include 153 | #include 154 | #include 155 | #endif 156 | 157 | 158 | /* 159 | 160 | stdc++.h end 161 | 162 | */ 163 | 164 | using namespace std; 165 | 166 | 167 | 168 | 169 | template 170 | ostream &operator<<(ostream &os, const pair &p) { 171 | return os << '{' << p.first << ", " << p.second << '}'; 172 | } 173 | 174 | template ()))>, 175 | class = enable_if_t::value>> 176 | ostream &operator<<(ostream &os, const T &c) { 177 | os << '['; 178 | for (auto it = c.begin(); it != c.end(); ++it) 179 | os << &", "[2 * (it == c.begin())] << *it; 180 | return os << ']'; 181 | } 182 | 183 | 184 | 185 | //support up to 5 args 186 | #define _NTH_ARG(_1, _2, _3, _4, _5, _6, N, ...) N 187 | #define _FE_0(_CALL, ...) 188 | #define _FE_1(_CALL, x) _CALL(x) 189 | #define _FE_2(_CALL, x, ...) _CALL(x) _FE_1(_CALL, __VA_ARGS__) 190 | #define _FE_3(_CALL, x, ...) _CALL(x) _FE_2(_CALL, __VA_ARGS__) 191 | #define _FE_4(_CALL, x, ...) _CALL(x) _FE_3(_CALL, __VA_ARGS__) 192 | #define _FE_5(_CALL, x, ...) _CALL(x) _FE_4(_CALL, __VA_ARGS__) 193 | #define FOR_EACH_MACRO(MACRO, ...) \ 194 | _NTH_ARG(dummy, ##__VA_ARGS__, _FE_5, _FE_4, _FE_3, _FE_2, _FE_1, _FE_0) \ 195 | (MACRO, ##__VA_ARGS__) 196 | 197 | 198 | //Change output format here 199 | #define out(x) #x " = " << x << "; " 200 | 201 | 202 | #define _____error_____(...) \ 203 | cerr << "(Line " << __LINE__ << "): " FOR_EACH_MACRO(out, __VA_ARGS__) << "\n" 204 | 205 | 206 | 207 | 208 | 209 | 210 | /* 211 | _______ _______ ______ ___ __ _ _______ 212 | | || || _ | | | | | | || | 213 | | _ || _ || | || | | | |_| ||_ _| 214 | | |_| || |_| || |_||_ | | | | | | 215 | | ___|| ___|| __ || | | _ | | | 216 | | | | | | | | || | | | | | | | 217 | |___| |___| |___| |_||___| |_| |__| |___| 218 | Pretty Printer for Modern C++ 219 | https://github.com/p-ranav/pprint 220 | Licensed under the MIT License . 221 | SPDX-License-Identifier: MIT 222 | Copyright (c) 2019 Pranav Srinivas Kumar . 223 | Permission is hereby granted, free of charge, to any person obtaining a copy 224 | of this software and associated documentation files (the "Software"), to deal 225 | in the Software without restriction, including without limitation the rights 226 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 227 | copies of the Software, and to permit persons to whom the Software is 228 | furnished to do so, subject to the following conditions: 229 | The above copyright notice and this permission notice shall be included in all 230 | copies or substantial portions of the Software. 231 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 232 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 233 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 234 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 235 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 236 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 237 | SOFTWARE. 238 | */ 239 | #pragma once 240 | // #include 241 | // #include 242 | // #include 243 | // #include 244 | // #include 245 | // #include 246 | // #include 247 | // #include 248 | // #include 249 | // #include 250 | // #include 251 | // #include 252 | // #include 253 | // #include 254 | // #include 255 | // #include 256 | // #include 257 | // #include 258 | // #include 259 | // #include 260 | // #include 261 | // #include 262 | // #include 263 | // #include 264 | // #include 265 | // #include 266 | // #include 267 | // #include 268 | // #include 269 | // #include 270 | #ifdef __GNUG__ 271 | // #include 272 | // #include 273 | // #include 274 | #endif 275 | 276 | // Check if a type is stream writable, i.e., std::cout << foo; 277 | template 278 | struct is_to_stream_writable: std::false_type {}; 279 | 280 | template 281 | struct is_to_stream_writable()<() ) >> 283 | : std::true_type {}; 284 | 285 | 286 | // Printing std::tuple 287 | // The indices trick: http://loungecpp.wikidot.com/tips-and-tricks:indices 288 | namespace pprint { 289 | 290 | template struct seq{}; 291 | 292 | template 293 | struct gen_seq : gen_seq{}; 294 | 295 | template 296 | struct gen_seq<0, Is...> : seq{}; 297 | 298 | template 299 | inline T to_string(T value) { 300 | return value; 301 | } 302 | 303 | inline std::string to_string(char value) { 304 | return "'" + std::string(1, value) + "'"; 305 | } 306 | 307 | inline std::string to_string(const char * value) { 308 | return "\"" + std::string(value) + "\""; 309 | } 310 | 311 | inline std::string to_string(const std::string& value) { 312 | return "\"" + value + "\""; 313 | } 314 | 315 | template 316 | void print_tuple(std::basic_ostream& os, Tuple const& t, seq){ 317 | using swallow = int[]; 318 | (void)swallow{0, (void(os << (Is == 0? "" : ", ") << to_string(std::get(t))), 0)...}; 319 | } 320 | 321 | } 322 | 323 | template 324 | auto operator<<(std::basic_ostream& os, std::tuple const& t) 325 | -> std::basic_ostream& { 326 | os << "("; 327 | pprint::print_tuple(os, t, pprint::gen_seq()); 328 | return os << ")"; 329 | } 330 | 331 | // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128. 332 | // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN. 333 | #if !defined(MAGIC_ENUM_RANGE_MIN) 334 | # define MAGIC_ENUM_RANGE_MIN -128 335 | #endif 336 | 337 | // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128. 338 | // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX. 339 | #if !defined(MAGIC_ENUM_RANGE_MAX) 340 | # define MAGIC_ENUM_RANGE_MAX 128 341 | #endif 342 | 343 | namespace magic_enum { 344 | 345 | // Enum value must be in range [-MAGIC_ENUM_RANGE_MAX, MAGIC_ENUM_RANGE_MIN]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. 346 | // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX and MAGIC_ENUM_RANGE_MIN. 347 | // If need another range for specific enum type, add specialization enum_range for necessary enum type. 348 | template 349 | struct enum_range final { 350 | static_assert(std::is_enum_v, "magic_enum::enum_range requires enum type."); 351 | static constexpr int min = std::is_signed_v> ? MAGIC_ENUM_RANGE_MIN : 0; 352 | static constexpr int max = MAGIC_ENUM_RANGE_MAX; 353 | }; 354 | 355 | static_assert(MAGIC_ENUM_RANGE_MAX > 0, 356 | "MAGIC_ENUM_RANGE_MAX must be greater than 0."); 357 | static_assert(MAGIC_ENUM_RANGE_MAX < std::numeric_limits::max(), 358 | "MAGIC_ENUM_RANGE_MAX must be less than INT_MAX."); 359 | 360 | static_assert(MAGIC_ENUM_RANGE_MIN <= 0, 361 | "MAGIC_ENUM_RANGE_MIN must be less or equals than 0."); 362 | static_assert(MAGIC_ENUM_RANGE_MIN > std::numeric_limits::min(), 363 | "MAGIC_ENUM_RANGE_MIN must be greater than INT_MIN."); 364 | 365 | namespace detail { 366 | 367 | template > 368 | [[nodiscard]] constexpr int min_impl() { 369 | static_assert(std::is_enum_v, "magic_enum::detail::min_impl requires enum type."); 370 | constexpr int min = enum_range::min > (std::numeric_limits::min)() ? enum_range::min : (std::numeric_limits::min)(); 371 | 372 | return min; 373 | } 374 | 375 | template > 376 | [[nodiscard]] constexpr decltype(auto) range_impl() { 377 | static_assert(std::is_enum_v, "magic_enum::detail::range_impl requires enum type."); 378 | static_assert(enum_range::max > enum_range::min, "magic_enum::enum_range requires max > min."); 379 | constexpr int max = enum_range::max < (std::numeric_limits::max)() ? enum_range::max : (std::numeric_limits::max)(); 380 | constexpr auto range = std::make_integer_sequence() + 1>{}; 381 | 382 | return range; 383 | } 384 | 385 | 386 | [[nodiscard]] constexpr bool is_name_char(char c, bool front) noexcept { 387 | return (!front && c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; 388 | } 389 | 390 | template 391 | [[nodiscard]] constexpr std::string_view name_impl() noexcept { 392 | static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); 393 | #if defined(__clang__) 394 | std::string_view name{__PRETTY_FUNCTION__}; 395 | constexpr auto suffix = sizeof("]") - 1; 396 | #elif defined(__GNUC__) && __GNUC__ >= 9 397 | std::string_view name{__PRETTY_FUNCTION__}; 398 | constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; 399 | #elif defined(_MSC_VER) 400 | std::string_view name{__FUNCSIG__}; 401 | constexpr auto suffix = sizeof(">(void) noexcept") - 1; 402 | #else 403 | return {}; // Unsupported compiler. 404 | #endif 405 | 406 | #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) 407 | name.remove_suffix(suffix); 408 | for (std::size_t i = name.size(); i > 0; --i) { 409 | if (!is_name_char(name[i - 1], false)) { 410 | name.remove_prefix(i); 411 | break; 412 | } 413 | } 414 | 415 | if (name.length() > 0 && is_name_char(name.front(), true)) { 416 | return name; 417 | } else { 418 | return {}; // Value does not have name. 419 | } 420 | #endif 421 | } 422 | 423 | template 424 | [[nodiscard]] constexpr decltype(auto) strings_impl(std::integer_sequence) noexcept { 425 | static_assert(std::is_enum_v, "magic_enum::detail::strings_impl requires enum type."); 426 | constexpr std::array names{{name_impl(I + min_impl())>()...}}; 427 | 428 | return names; 429 | } 430 | 431 | template 432 | [[nodiscard]] constexpr std::string_view name_impl(int value) noexcept { 433 | static_assert(std::is_enum_v, "magic_enum::detail::name_impl requires enum type."); 434 | constexpr auto names = strings_impl(range_impl()); 435 | const int i = value - min_impl(); 436 | 437 | if (i >= 0 && static_cast(i) < names.size()) { 438 | return names[i]; 439 | } else { 440 | return {}; // Value out of range. 441 | } 442 | } 443 | 444 | template 445 | [[nodiscard]] constexpr decltype(auto) values_impl(std::integer_sequence) noexcept { 446 | static_assert(std::is_enum_v, "magic_enum::detail::values_impl requires enum type."); 447 | constexpr int n = sizeof...(I); 448 | constexpr std::array valid{{!name_impl(I + min_impl())>().empty()...}}; 449 | constexpr int num_valid = ((valid[I] ? 1 : 0) + ...); 450 | 451 | std::array enums{}; 452 | for (int i = 0, v = 0; i < n && v < num_valid; ++i) { 453 | if (valid[i]) { 454 | enums[v++] = static_cast(i + min_impl()); 455 | } 456 | } 457 | 458 | return enums; 459 | } 460 | 461 | template 462 | [[nodiscard]] constexpr decltype(auto) names_impl(std::integer_sequence) noexcept { 463 | static_assert(std::is_enum_v, "magic_enum::detail::names_impl requires enum type."); 464 | constexpr auto enums = values_impl(range_impl()); 465 | constexpr std::array names{{name_impl()...}}; 466 | 467 | return names; 468 | } 469 | 470 | template 471 | [[nodiscard]] constexpr std::optional enum_cast_impl(std::string_view value) noexcept { 472 | static_assert(std::is_enum_v, "magic_enum::detail::enum_cast_impl requires enum type."); 473 | constexpr auto values = values_impl(range_impl()); 474 | constexpr auto count = values.size(); 475 | constexpr auto names = names_impl(std::make_index_sequence{}); 476 | 477 | for (std::size_t i = 0; i < count; ++i) { 478 | if (names[i] == value) { 479 | return values[i]; 480 | } 481 | } 482 | 483 | return std::nullopt; // Invalid value or out of range. 484 | } 485 | 486 | template 487 | using enable_if_enum_t = typename std::enable_if>::type; 488 | 489 | template> 490 | struct is_scoped_enum_impl : std::false_type {}; 491 | 492 | template 493 | struct is_scoped_enum_impl : std::bool_constant>> {}; 494 | 495 | template> 496 | struct is_unscoped_enum_impl : std::false_type {}; 497 | 498 | template 499 | struct is_unscoped_enum_impl : std::bool_constant>> {}; 500 | 501 | } // namespace magic_enum::detail 502 | 503 | // Checks whether T is an Unscoped enumeration type. 504 | // Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. 505 | // Otherwise, value is equal to false. 506 | template 507 | struct is_unscoped_enum : detail::is_unscoped_enum_impl {}; 508 | 509 | template 510 | inline constexpr bool is_unscoped_enum_v = is_unscoped_enum::value; 511 | 512 | // Checks whether T is an Scoped enumeration type. 513 | // Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. 514 | // Otherwise, value is equal to false. 515 | template 516 | struct is_scoped_enum : detail::is_scoped_enum_impl {}; 517 | 518 | template 519 | inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; 520 | 521 | // Obtains enum value from enum string name. 522 | template > 523 | [[nodiscard]] constexpr std::optional enum_cast(std::string_view value) noexcept { 524 | static_assert(std::is_enum_v, "magic_enum::enum_cast requires enum type."); 525 | 526 | return detail::enum_cast_impl(value); 527 | } 528 | 529 | // Obtains enum value from integer value. 530 | template > 531 | [[nodiscard]] constexpr std::optional enum_cast(std::underlying_type_t value) noexcept { 532 | static_assert(std::is_enum_v, "magic_enum::enum_cast requires enum type."); 533 | 534 | if (detail::name_impl(static_cast(value)).empty()) { 535 | return std::nullopt; // Invalid value or out of range. 536 | } else { 537 | return static_cast(value); 538 | } 539 | } 540 | 541 | // Returns enum value at specified index. 542 | // No bounds checking is performed: the behavior is undefined if index >= number of enum values. 543 | template> 544 | [[nodiscard]] constexpr E enum_value(std::size_t index) { 545 | static_assert(std::is_enum_v, "magic_enum::enum_value requires enum type."); 546 | constexpr auto values = detail::values_impl(detail::range_impl()); 547 | 548 | return assert(index < values.size()), values[index]; 549 | } 550 | 551 | // Obtains value enum sequence. 552 | template > 553 | [[nodiscard]] constexpr decltype(auto) enum_values() noexcept { 554 | static_assert(std::is_enum_v, "magic_enum::enum_values requires enum type."); 555 | constexpr auto values = detail::values_impl(detail::range_impl()); 556 | 557 | return values; 558 | } 559 | 560 | // Returns number of enum values. 561 | template > 562 | [[nodiscard]] constexpr std::size_t enum_count() noexcept { 563 | static_assert(std::is_enum_v, "magic_enum::enum_count requires enum type."); 564 | constexpr auto count = detail::values_impl(detail::range_impl()).size(); 565 | 566 | return count; 567 | } 568 | 569 | // Obtains string enum name from enum value. 570 | template , typename = detail::enable_if_enum_t> 571 | [[nodiscard]] constexpr std::optional enum_name(E value) noexcept { 572 | static_assert(std::is_enum_v, "magic_enum::enum_name requires enum type."); 573 | const auto name = detail::name_impl(static_cast(value)); 574 | 575 | if (name.empty()) { 576 | return std::nullopt; // Invalid value or out of range. 577 | } else { 578 | return name; 579 | } 580 | } 581 | 582 | // Obtains string enum name sequence. 583 | template > 584 | [[nodiscard]] constexpr decltype(auto) enum_names() noexcept { 585 | static_assert(std::is_enum_v, "magic_enum::enum_names requires enum type."); 586 | constexpr auto count = detail::values_impl(detail::range_impl()).size(); 587 | constexpr auto names = detail::names_impl(std::make_index_sequence{}); 588 | 589 | return names; 590 | } 591 | 592 | namespace ops { 593 | 594 | template , typename = detail::enable_if_enum_t> 595 | std::ostream& operator<<(std::ostream& os, E value) { 596 | static_assert(std::is_enum_v, "magic_enum::ops::operator<< requires enum type."); 597 | const auto name = detail::name_impl(static_cast(value)); 598 | 599 | if (!name.empty()) { 600 | os << name; 601 | } 602 | 603 | return os; 604 | } 605 | 606 | template > 607 | std::ostream& operator<<(std::ostream& os, std::optional value) { 608 | static_assert(std::is_enum_v, "magic_enum::ops::operator<< requires enum type."); 609 | 610 | if (value.has_value()) { 611 | const auto name = detail::name_impl(static_cast(value.value())); 612 | if (!name.empty()) { 613 | os << name; 614 | } 615 | } 616 | 617 | return os; 618 | } 619 | 620 | } // namespace magic_enum::ops 621 | 622 | } // namespace magic_enum 623 | 624 | namespace pprint { 625 | 626 | // Some utility structs to check template specialization 627 | template class Ref> 628 | struct is_specialization : std::false_type {}; 629 | 630 | template class Ref, typename... Args> 631 | struct is_specialization, Ref> : std::true_type {}; 632 | 633 | template 634 | using to_void = void; 635 | 636 | template 637 | struct is_container : std::false_type 638 | {}; 639 | 640 | template 641 | struct is_container().begin()), 643 | decltype(std::declval().end()), 644 | typename T::value_type 645 | >> : std::true_type // will be enabled for iterable objects 646 | {}; 647 | 648 | class PrettyPrinter { 649 | private: 650 | std::ostream& stream_; 651 | std::string line_terminator_; 652 | size_t indent_; 653 | bool quotes_; 654 | bool compact_; 655 | 656 | public: 657 | 658 | PrettyPrinter(std::ostream& stream = std::cout) : 659 | stream_(stream), 660 | line_terminator_("\n"), 661 | indent_(2), 662 | quotes_(false), 663 | compact_(false) {} 664 | 665 | PrettyPrinter& line_terminator(const std::string& value) { 666 | line_terminator_ = value; 667 | return *this; 668 | } 669 | 670 | PrettyPrinter& indent(size_t indent) { 671 | indent_ = indent; 672 | return *this; 673 | } 674 | 675 | PrettyPrinter& compact(bool value) { 676 | compact_ = value; 677 | return *this; 678 | } 679 | 680 | PrettyPrinter& quotes(bool value) { 681 | quotes_ = value; 682 | return *this; 683 | } 684 | 685 | template 686 | void print(T value) { 687 | print_internal(value, 0, line_terminator_, 0); 688 | } 689 | 690 | template 691 | void print(std::initializer_list value) { 692 | print_internal(value, 0, line_terminator_, 0); 693 | } 694 | 695 | template 696 | void print(T value, Targs... Fargs) { 697 | print_internal(value, 0, "", 0); 698 | auto current_quotes = quotes_; 699 | quotes_ = false; 700 | print_internal(" ", 0, "", 0); 701 | quotes_ = current_quotes; 702 | print(Fargs...); 703 | } 704 | 705 | template 706 | void print_inline(T value) { 707 | print_internal(value, indent_, "", 0); 708 | } 709 | 710 | template 711 | void print_inline(std::initializer_list value) { 712 | print_internal(value, indent_, "", 0); 713 | } 714 | 715 | template 716 | void print_inline(T value, Targs... Fargs) { 717 | print_internal(value, indent_, "", 0); 718 | auto current_quotes = quotes_; 719 | quotes_ = false; 720 | print_internal(" ", 0, "", 0); 721 | quotes_ = current_quotes; 722 | print_inline(Fargs...); 723 | } 724 | 725 | private: 726 | 727 | template 728 | typename std::enable_if::value == true, void>::type 729 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 730 | stream_ << std::string(indent, ' ') << value << line_terminator; 731 | } 732 | 733 | template 734 | typename std::enable_if::value == true, void>::type 735 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 736 | stream_ << std::string(indent, ' ') << "nullptr" << line_terminator; 737 | } 738 | 739 | void print_internal(float value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 740 | stream_ << std::string(indent, ' ') << value << 'f' << line_terminator; 741 | } 742 | 743 | void print_internal(double value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 744 | stream_ << std::string(indent, ' ') << value << line_terminator; 745 | } 746 | 747 | void print_internal(const std::string& value, size_t indent = 0, const std::string& line_terminator = "\n", 748 | size_t level = 0) { 749 | if (!quotes_) 750 | print_internal_without_quotes(value, indent, line_terminator, level); 751 | else 752 | stream_ << std::string(indent, ' ') << "\"" << value << "\"" << line_terminator; 753 | } 754 | 755 | void print_internal(const char * value, size_t indent = 0, const std::string& line_terminator = "\n", 756 | size_t level = 0) { 757 | if (!quotes_) 758 | print_internal_without_quotes(value, indent, line_terminator, level); 759 | else 760 | stream_ << std::string(indent, ' ') << "\"" << value << "\"" << line_terminator; 761 | } 762 | 763 | void print_internal(char value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 764 | if (!quotes_) 765 | print_internal_without_quotes(value, indent, line_terminator, level); 766 | else 767 | stream_ << std::string(indent, ' ') << "'" << value << "'" << line_terminator; 768 | } 769 | 770 | void print_internal_without_quotes(const std::string& value, size_t indent = 0, 771 | const std::string& line_terminator = "\n", size_t level = 0) { 772 | stream_ << std::string(indent, ' ') << value << line_terminator; 773 | } 774 | 775 | void print_internal_without_quotes(const char * value, size_t indent = 0, 776 | const std::string& line_terminator = "\n", size_t level = 0) { 777 | stream_ << std::string(indent, ' ') << value << line_terminator; 778 | } 779 | 780 | void print_internal_without_quotes(char value, size_t indent = 0, const std::string& line_terminator = "\n", 781 | size_t level = 0) { 782 | stream_ << std::string(indent, ' ') << value << line_terminator; 783 | } 784 | 785 | void print_internal(bool value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 786 | stream_ << std::string(indent, ' ') << (value ? "true" : "false") << line_terminator; 787 | } 788 | 789 | template 790 | typename std::enable_if::value == true, void>::type 791 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 792 | if (value == nullptr) { 793 | return print_internal(nullptr, indent, line_terminator, level); 794 | } 795 | stream_ << std::string(indent, ' ') << "<" << type(value) << " at " 796 | << value << ">" << line_terminator; 797 | } 798 | 799 | std::string demangle(const char* name) { 800 | #ifdef __GNUG__ 801 | int status = -4; 802 | std::unique_ptr res { 803 | abi::__cxa_demangle(name, NULL, NULL, &status), 804 | std::free 805 | }; 806 | return (status==0) ? res.get() : name; 807 | #else 808 | return name; 809 | #endif 810 | } 811 | 812 | template 813 | std::string type(const T& t) { 814 | return demangle(typeid(t).name()); 815 | } 816 | 817 | template 818 | typename std::enable_if::value == true, void>::type 819 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 820 | auto enum_string = magic_enum::enum_name(value); 821 | if (enum_string.has_value()) { 822 | stream_ << std::string(indent, ' ') << enum_string.value() 823 | << line_terminator; 824 | } 825 | else { 826 | stream_ << std::string(indent, ' ') << static_cast>(value) 827 | << line_terminator; 828 | } 829 | } 830 | 831 | template 832 | typename std::enable_if::value == true && 833 | is_to_stream_writable::value == true && 834 | std::is_enum::value == false && 835 | is_specialization::value == false && 836 | is_specialization::value == false && 837 | is_specialization::value == false && 838 | is_specialization::value == false && 839 | is_specialization::value == false && 840 | is_specialization::value == false && 841 | is_specialization::value == false && 842 | is_specialization::value == false && 843 | is_specialization::value == false && 844 | is_specialization::value == false && 845 | is_specialization::value == false && 846 | is_specialization::value == false && 847 | is_specialization::value == false && 848 | is_specialization::value == false && 849 | is_specialization::value == false && 850 | is_specialization::value == false && 851 | is_specialization::value == false && 852 | is_specialization::value == false && 853 | is_specialization::value == false, void>::type 854 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 855 | stream_ << std::string(indent, ' ') << value << line_terminator; 856 | } 857 | 858 | template 859 | typename std::enable_if::value == true && 860 | is_to_stream_writable::value == false && 861 | std::is_enum::value == false && 862 | is_specialization::value == false && 863 | is_specialization::value == false && 864 | is_specialization::value == false && 865 | is_specialization::value == false && 866 | is_specialization::value == false && 867 | is_specialization::value == false && 868 | is_specialization::value == false && 869 | is_specialization::value == false && 870 | is_specialization::value == false && 871 | is_specialization::value == false && 872 | is_specialization::value == false && 873 | is_specialization::value == false && 874 | is_specialization::value == false && 875 | is_specialization::value == false && 876 | is_specialization::value == false && 877 | is_specialization::value == false && 878 | is_specialization::value == false && 879 | is_specialization::value == false && 880 | is_specialization::value == false, void>::type 881 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 882 | stream_ << std::string(indent, ' ') << "" 883 | << line_terminator; 884 | } 885 | 886 | template 887 | typename std::enable_if::value == true, void>::type 888 | print_internal(T value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 889 | stream_ << std::string(indent, ' ') << "" 891 | << line_terminator; 892 | } 893 | 894 | template 895 | typename std::enable_if::value, void>::type 896 | print_internal(const Container& value, size_t indent = 0, const std::string& line_terminator = "\n", 897 | size_t level = 0) { 898 | typedef typename Container::value_type T; 899 | if (level == 0 && !compact_) { 900 | if (value.size() == 0) { 901 | print_internal_without_quotes("[", 0, ""); 902 | } 903 | else if (value.size() == 1) { 904 | print_internal_without_quotes("[", 0, ""); 905 | print_internal(value.front(), 0, "", level + 1); 906 | } 907 | else if (value.size() > 0) { 908 | print_internal_without_quotes("[", 0, "\n"); 909 | print_internal(value.front(), indent + indent_, "", level + 1); 910 | if (value.size() > 1 && is_container::value == false) 911 | print_internal_without_quotes(", ", 0, "\n"); 912 | else if (is_container::value) 913 | print_internal_without_quotes(", ", 0, "\n"); 914 | for (size_t i = 1; i < value.size() - 1; i++) { 915 | print_internal(value[i], indent + indent_, "", level + 1); 916 | if (is_container::value == false) 917 | print_internal_without_quotes(", ", 0, "\n"); 918 | else 919 | print_internal_without_quotes(", ", 0, "\n"); 920 | } 921 | if (value.size() > 1) { 922 | print_internal(value.back(), indent + indent_, "\n", level + 1); 923 | } 924 | } 925 | if (value.size() == 0) 926 | print_internal_without_quotes("]", indent, ""); 927 | else if (is_container::value == false) 928 | print_internal_without_quotes("]", indent, ""); 929 | else 930 | print_internal_without_quotes(line_terminator_ + "]", indent, ""); 931 | print_internal_without_quotes(line_terminator_, 0, ""); 932 | } 933 | else { 934 | if (value.size() == 0) { 935 | print_internal_without_quotes("[", indent, ""); 936 | } 937 | else if (value.size() == 1) { 938 | print_internal_without_quotes("[", indent, ""); 939 | print_internal(value.front(), 0, "", level + 1); 940 | } 941 | else if (value.size() > 0) { 942 | print_internal_without_quotes("[", indent, ""); 943 | print_internal(value.front(), 0, "", level + 1); 944 | if (value.size() > 1) 945 | print_internal_without_quotes(", ", 0, ""); 946 | for (size_t i = 1; i < value.size() - 1; i++) { 947 | print_internal(value[i], 0, "", level + 1); 948 | print_internal_without_quotes(", ", 0, ""); 949 | } 950 | if (value.size() > 1) { 951 | print_internal(value.back(), 0, "", level + 1); 952 | } 953 | } 954 | print_internal_without_quotes("]", 0, ""); 955 | if (level == 0 && compact_) 956 | print_internal_without_quotes(line_terminator_, 0, ""); 957 | } 958 | 959 | } 960 | 961 | template 962 | void print_internal(const std::array& value, size_t indent = 0, const std::string& line_terminator = "\n", 963 | size_t level = 0) { 964 | if (level == 0 && !compact_) { 965 | if (value.size() == 0) { 966 | print_internal_without_quotes("[", 0, ""); 967 | } 968 | else if (value.size() == 1) { 969 | print_internal_without_quotes("[", 0, ""); 970 | print_internal(value.front(), 0, "", level + 1); 971 | } 972 | else if (value.size() > 0) { 973 | print_internal_without_quotes("[", 0, "\n"); 974 | print_internal(value.front(), indent + indent_, "", level + 1); 975 | if (value.size() > 1 && is_container::value == false) 976 | print_internal_without_quotes(", ", 0, "\n"); 977 | else if (is_container::value) 978 | print_internal_without_quotes(", ", 0, "\n"); 979 | for (size_t i = 1; i < value.size() - 1; i++) { 980 | print_internal(value[i], indent + indent_, "", level + 1); 981 | if (is_container::value == false) 982 | print_internal_without_quotes(", ", 0, "\n"); 983 | else 984 | print_internal_without_quotes(", ", 0, "\n"); 985 | } 986 | if (value.size() > 1) { 987 | print_internal(value.back(), indent + indent_, "\n", level + 1); 988 | } 989 | } 990 | if (value.size() == 0) 991 | print_internal_without_quotes("]", indent, ""); 992 | else if (is_container::value == false) 993 | print_internal_without_quotes("]", indent, ""); 994 | else 995 | print_internal_without_quotes(line_terminator_ + "]", indent, ""); 996 | print_internal_without_quotes(line_terminator_, 0, ""); 997 | } 998 | else { 999 | if (value.size() == 0) { 1000 | print_internal_without_quotes("[", indent, ""); 1001 | } 1002 | else if (value.size() == 1) { 1003 | print_internal_without_quotes("[", indent, ""); 1004 | print_internal(value.front(), 0, "", level + 1); 1005 | } 1006 | else if (value.size() > 0) { 1007 | print_internal_without_quotes("[", indent, ""); 1008 | print_internal(value.front(), 0, "", level + 1); 1009 | if (value.size() > 1) 1010 | print_internal_without_quotes(", ", 0, ""); 1011 | for (size_t i = 1; i < value.size() - 1; i++) { 1012 | print_internal(value[i], 0, "", level + 1); 1013 | print_internal_without_quotes(", ", 0, ""); 1014 | } 1015 | if (value.size() > 1) { 1016 | print_internal(value.back(), 0, "", level + 1); 1017 | } 1018 | } 1019 | print_internal_without_quotes("]", 0, ""); 1020 | if (level == 0 && compact_) 1021 | print_internal_without_quotes(line_terminator_, 0, ""); 1022 | } 1023 | 1024 | } 1025 | 1026 | template 1027 | typename std::enable_if::value || 1028 | is_specialization::value, 1029 | void>::type print_internal(const Container& value, size_t indent = 0, 1030 | const std::string& line_terminator = "\n", 1031 | size_t level = 0) { 1032 | typedef typename Container::value_type T; 1033 | if (level == 0 && !compact_) { 1034 | if (value.size() == 0) { 1035 | print_internal_without_quotes("[", 0, ""); 1036 | } 1037 | else if (value.size() == 1) { 1038 | print_internal_without_quotes("[", 0, ""); 1039 | print_internal(value.front(), 0, "", level + 1); 1040 | } 1041 | else if (value.size() > 0) { 1042 | print_internal_without_quotes("[", 0, "\n"); 1043 | print_internal(value.front(), indent + indent_, "", level + 1); 1044 | if (value.size() > 1 && is_container::value == false) 1045 | print_internal_without_quotes(", ", 0, "\n"); 1046 | else if (is_container::value) 1047 | print_internal_without_quotes(", ", 0, "\n"); 1048 | 1049 | typename Container::const_iterator iterator; 1050 | for (iterator = std::next(value.begin()); iterator != std::prev(value.end()); ++iterator) { 1051 | print_internal(*iterator, indent + indent_, "", level + 1); 1052 | if (is_container::value == false) 1053 | print_internal_without_quotes(", ", 0, "\n"); 1054 | else 1055 | print_internal_without_quotes(", ", 0, "\n"); 1056 | } 1057 | 1058 | if (value.size() > 1) { 1059 | print_internal(value.back(), indent + indent_, "\n", level + 1); 1060 | } 1061 | } 1062 | if (value.size() == 0) 1063 | print_internal_without_quotes("]", indent, ""); 1064 | else if (is_container::value == false) 1065 | print_internal_without_quotes("]", indent, ""); 1066 | else 1067 | print_internal_without_quotes(line_terminator_ + "]", indent, ""); 1068 | print_internal_without_quotes(line_terminator_, 0, ""); 1069 | } 1070 | else { 1071 | if (value.size() == 0) { 1072 | print_internal_without_quotes("[", indent, ""); 1073 | } 1074 | else if (value.size() == 1) { 1075 | print_internal_without_quotes("[", indent, ""); 1076 | print_internal(value.front(), 0, "", level + 1); 1077 | } 1078 | else if (value.size() > 0) { 1079 | print_internal_without_quotes("[", indent, ""); 1080 | print_internal(value.front(), 0, "", level + 1); 1081 | if (value.size() > 1) 1082 | print_internal_without_quotes(", ", 0, ""); 1083 | 1084 | typename Container::const_iterator iterator; 1085 | for (iterator = std::next(value.begin()); iterator != std::prev(value.end()); ++iterator) { 1086 | print_internal(*iterator, 0, "", level + 1); 1087 | print_internal_without_quotes(", ", 0, ""); 1088 | } 1089 | 1090 | if (value.size() > 1) { 1091 | print_internal(value.back(), 0, "", level + 1); 1092 | } 1093 | } 1094 | print_internal_without_quotes("]", 0, ""); 1095 | if (level == 0 && compact_) 1096 | print_internal_without_quotes(line_terminator_, 0, ""); 1097 | } 1098 | 1099 | } 1100 | 1101 | template 1102 | typename std::enable_if::value || 1103 | is_specialization::value || 1104 | is_specialization::value || 1105 | is_specialization::value, void>::type 1106 | print_internal(const Container& value, size_t indent = 0, const std::string& line_terminator = "\n", 1107 | size_t level = 0) { 1108 | typedef typename Container::value_type T; 1109 | if (level == 0 && !compact_) { 1110 | if (value.size() == 0) { 1111 | print_internal_without_quotes("{", 0, ""); 1112 | } 1113 | else if (value.size() == 1) { 1114 | print_internal_without_quotes("{", 0, ""); 1115 | print_internal(*(value.begin()), 0, "", level + 1); 1116 | } 1117 | else { 1118 | print_internal_without_quotes("{", 0, "\n"); 1119 | print_internal(*(value.begin()), indent + indent_, "", level + 1); 1120 | if (value.size() > 1 && is_container::value == false) 1121 | print_internal_without_quotes(", ", 0, "\n"); 1122 | else if (is_container::value) 1123 | print_internal_without_quotes(", ", 0, "\n"); 1124 | 1125 | typename Container::const_iterator iterator; 1126 | for (iterator = std::next(value.begin()); (iterator != value.end()) && (std::next(iterator) != value.end()); ++iterator) { 1127 | print_internal(*iterator, indent + indent_, "", level + 1); 1128 | if (is_container::value == false) 1129 | print_internal_without_quotes(", ", 0, "\n"); 1130 | else 1131 | print_internal_without_quotes(", ", 0, "\n"); 1132 | } 1133 | 1134 | if (value.size() > 1) { 1135 | print_internal(*iterator, indent + indent_, "\n", level + 1); 1136 | } 1137 | } 1138 | if (value.size() == 0) 1139 | print_internal_without_quotes("}", indent, ""); 1140 | else if (is_container::value == false) 1141 | print_internal_without_quotes("}", indent, ""); 1142 | else 1143 | print_internal_without_quotes(line_terminator_ + "}", indent, ""); 1144 | print_internal_without_quotes(line_terminator_, 0, ""); 1145 | } 1146 | else { 1147 | if (value.size() == 0) { 1148 | print_internal_without_quotes("{", indent, ""); 1149 | } 1150 | else if (value.size() == 1) { 1151 | print_internal_without_quotes("{", indent, ""); 1152 | print_internal(*(value.begin()), 0, "", level + 1); 1153 | } 1154 | else { 1155 | print_internal_without_quotes("{", indent, ""); 1156 | print_internal(*(value.begin()), 0, "", level + 1); 1157 | if (value.size() > 1) 1158 | print_internal_without_quotes(", ", 0, ""); 1159 | 1160 | typename Container::const_iterator iterator; 1161 | for (iterator = std::next(value.begin()); (iterator != value.end()) && (std::next(iterator) != value.end()); ++iterator) { 1162 | print_internal(*iterator, 0, "", level + 1); 1163 | print_internal_without_quotes(", ", 0, ""); 1164 | } 1165 | 1166 | if (value.size() > 1) { 1167 | print_internal(*iterator, 0, "", level + 1); 1168 | } 1169 | } 1170 | print_internal_without_quotes("}", 0, ""); 1171 | if (level == 0 && compact_) 1172 | print_internal_without_quotes(line_terminator_, 0, ""); 1173 | } 1174 | 1175 | } 1176 | 1177 | template 1178 | typename std::enable_if::value == true || 1179 | is_specialization::value == true || 1180 | is_specialization::value == true || 1181 | is_specialization::value == true, void>::type 1182 | print_internal(const T& value, size_t indent = 0, const std::string& line_terminator = "\n", size_t level = 0) { 1183 | typedef typename T::mapped_type Value; 1184 | if (level == 0 && !compact_) { 1185 | if (value.size() == 0) { 1186 | print_internal_without_quotes("{", 0, ""); 1187 | } 1188 | else if (value.size() == 1) { 1189 | print_internal_without_quotes("{", 0, ""); 1190 | for (auto& kvpair : value) { 1191 | print_internal(kvpair.first, 0, "", level + 1); 1192 | print_internal_without_quotes(" : ", 0, ""); 1193 | print_internal(kvpair.second, 0, "", level + 1); 1194 | } 1195 | } 1196 | else if (value.size() > 0) { 1197 | size_t count = 0; 1198 | for (auto& kvpair : value) { 1199 | if (count == 0) { 1200 | print_internal_without_quotes("{", 0, "\n"); 1201 | print_internal(kvpair.first, indent + indent_, "", level + 1); 1202 | print_internal_without_quotes(" : ", 0, ""); 1203 | print_internal(kvpair.second, 0, "", level + 1); 1204 | if (value.size() > 1 && is_container::value == false) 1205 | print_internal_without_quotes(", ", 0, "\n"); 1206 | else if (is_container::value) 1207 | print_internal_without_quotes(", ", 0, "\n"); 1208 | } 1209 | else if (count + 1 < value.size()) { 1210 | print_internal(kvpair.first, indent + indent_, "", level + 1); 1211 | print_internal_without_quotes(" : ", 0, ""); 1212 | print_internal(kvpair.second, 0, "", level + 1); 1213 | if (is_container::value == false) 1214 | print_internal_without_quotes(", ", 0, "\n"); 1215 | else 1216 | print_internal_without_quotes(", ", 0, "\n"); 1217 | } 1218 | else { 1219 | print_internal(kvpair.first, indent + indent_, "", level + 1); 1220 | print_internal_without_quotes(" : ", 0, ""); 1221 | print_internal(kvpair.second, 0, "\n", level + 1); 1222 | } 1223 | count += 1; 1224 | } 1225 | } 1226 | if (value.size() == 0) 1227 | print_internal_without_quotes("}", indent, ""); 1228 | else if (is_container::value == false) 1229 | print_internal_without_quotes("}", indent, ""); 1230 | else 1231 | print_internal_without_quotes(line_terminator_ + "}", indent, ""); 1232 | print_internal_without_quotes(line_terminator_, 0, ""); 1233 | } 1234 | 1235 | else { 1236 | if (value.size() == 0) { 1237 | print_internal_without_quotes("{", indent, ""); 1238 | } 1239 | else if (value.size() == 1) { 1240 | print_internal_without_quotes("{", indent, ""); 1241 | for (auto& kvpair : value) { 1242 | print_internal(kvpair.first, 0, "", level + 1); 1243 | print_internal_without_quotes(" : ", 0, ""); 1244 | print_internal(kvpair.second, 0, "", level + 1); 1245 | } 1246 | } 1247 | else if (value.size() > 0) { 1248 | size_t count = 0; 1249 | for (auto& kvpair : value) { 1250 | if (count == 0) { 1251 | print_internal_without_quotes("{", indent, ""); 1252 | print_internal(kvpair.first, 0, "", level + 1); 1253 | print_internal_without_quotes(" : ", 0, ""); 1254 | print_internal(kvpair.second, 0, "", level + 1); 1255 | print_internal_without_quotes(", ", 0, ""); 1256 | } 1257 | else if (count + 1 < value.size()) { 1258 | print_internal(kvpair.first, indent + indent_, "", level + 1); 1259 | print_internal_without_quotes(" : ", 0, ""); 1260 | print_internal(kvpair.second, 0, "", level + 1); 1261 | print_internal_without_quotes(", ", 0, ""); 1262 | } 1263 | else { 1264 | print_internal(kvpair.first, 0, "", level + 1); 1265 | print_internal_without_quotes(" : ", 0, ""); 1266 | print_internal(kvpair.second, 0, "", level + 1); 1267 | } 1268 | count += 1; 1269 | } 1270 | } 1271 | print_internal_without_quotes("}", 0, ""); 1272 | if (level == 0 && compact_) 1273 | print_internal_without_quotes(line_terminator_, 0, ""); 1274 | } 1275 | } 1276 | 1277 | template 1278 | void print_internal(std::pair value, size_t indent = 0, const std::string& line_terminator = "\n", 1279 | size_t level = 0) { 1280 | print_internal_without_quotes("(", indent, ""); 1281 | print_internal(value.first, 0, ""); 1282 | print_internal_without_quotes(", ", 0, ""); 1283 | print_internal(value.second, 0, ""); 1284 | print_internal_without_quotes(")", 0, line_terminator, level); 1285 | } 1286 | 1287 | template 1288 | void print_internal(std::variant value, size_t indent = 0, 1289 | const std::string& line_terminator = "\n", size_t level = 0) { 1290 | std::visit([=](const auto& value) { print_internal(value, indent, line_terminator, level); }, value); 1291 | } 1292 | 1293 | template 1294 | void print_internal(std::optional value, size_t indent = 0, 1295 | const std::string& line_terminator = "\n", size_t level = 0) { 1296 | if (value) { 1297 | print_internal(value.value(), indent, line_terminator, level); 1298 | } 1299 | else { 1300 | print_internal_without_quotes("nullopt", indent, line_terminator, level); 1301 | } 1302 | } 1303 | 1304 | template 1305 | typename std::enable_if::value, void>::type 1306 | print_internal(const Container& value, size_t indent = 0, const std::string& line_terminator = "\n", 1307 | size_t level = 0) { 1308 | auto current_compact = compact_; 1309 | compact_ = true; 1310 | typedef typename Container::value_type T; 1311 | auto local = value; 1312 | std::vector local_vector; 1313 | while (!local.empty()) { 1314 | local_vector.push_back(local.front()); 1315 | local.pop(); 1316 | } 1317 | print_internal(local_vector, indent, line_terminator, level); 1318 | compact_ = current_compact; 1319 | } 1320 | 1321 | template 1322 | typename std::enable_if::value, void>::type 1323 | print_internal(const Container& value, size_t indent = 0, const std::string& line_terminator = "\n", 1324 | size_t level = 0) { 1325 | auto current_compact = compact_; 1326 | compact_ = true; 1327 | typedef typename Container::value_type T; 1328 | auto local = value; 1329 | std::vector local_vector; 1330 | while (!local.empty()) { 1331 | local_vector.push_back(local.top()); 1332 | local.pop(); 1333 | } 1334 | print_internal(local_vector, indent, line_terminator, level); 1335 | compact_ = current_compact; 1336 | } 1337 | 1338 | template 1339 | void print_internal(std::initializer_list value, size_t indent = 0, 1340 | const std::string& line_terminator = "\n", size_t level = 0) { 1341 | std::multiset local; 1342 | for(const T& x : value) { 1343 | local.insert(x); 1344 | } 1345 | print_internal(local, indent, line_terminator_, level); 1346 | } 1347 | 1348 | template 1349 | typename std::enable_if::value, void>::type 1350 | print_internal(const Container& value, size_t indent = 0, const std::string& line_terminator = "\n", 1351 | size_t level = 0) { 1352 | bool current_compact = compact_; 1353 | compact_ = false; // Need to print a stack like its a stack, i.e., vertical 1354 | typedef typename Container::value_type T; 1355 | auto local = value; 1356 | std::vector local_vector; 1357 | while (!local.empty()) { 1358 | local_vector.push_back(local.top()); 1359 | local.pop(); 1360 | } 1361 | print_internal(local_vector, indent, line_terminator, level); 1362 | compact_ = current_compact; 1363 | } 1364 | 1365 | template 1366 | void print_internal(const std::tuple& value, size_t indent = 0, const std::string& line_terminator = "\n", 1367 | size_t level = 0) { 1368 | stream_ << std::string(indent, ' ') << value 1369 | << line_terminator; 1370 | } 1371 | 1372 | template 1373 | void print_internal(const std::complex& value, size_t indent = 0, const std::string& line_terminator = "\n", 1374 | size_t level = 0) { 1375 | stream_ << std::string(indent, ' ') << "(" << 1376 | value.real() << " + " << value.imag() << "i)" 1377 | << line_terminator; 1378 | } 1379 | 1380 | template 1381 | typename std::enable_if::value || 1382 | is_specialization::value || 1383 | is_specialization::value, void>::type 1384 | print_internal(const Pointer& value, size_t indent = 0, const std::string& line_terminator = "\n", 1385 | size_t level = 0) { 1386 | stream_ << std::string(indent, ' ') << "<" << 1387 | type(value) << " at " << &value << ">" 1388 | << line_terminator; 1389 | } 1390 | 1391 | }; 1392 | 1393 | } -------------------------------------------------------------------------------- /03-Interview Preparation/Docs/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /03-Interview Preparation/Docs/0x01_Interview_Preparation_in_C_v1.0.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/Docs/0x01_Interview_Preparation_in_C_v1.0.docx -------------------------------------------------------------------------------- /03-Interview Preparation/Docs/System Design Guide.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/Docs/System Design Guide.docx -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/.gitkeep: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/0x01 C Interview Preparation-compressed.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/0x01 C Interview Preparation-compressed.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/Algorithm Analysis.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/Algorithm Analysis.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/Computer networks- most asked interview questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/Computer networks- most asked interview questions.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/DBMS Exclusive Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/DBMS Exclusive Notes.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/LeetCode 50 Common Interview Questions with Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/LeetCode 50 Common Interview Questions with Solutions.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/OOPs in C++.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/OOPs in C++.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/Operating System Exclusive Notes. .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/Operating System Exclusive Notes. .pdf -------------------------------------------------------------------------------- /03-Interview Preparation/PDF/SQL Exclusive Notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luctivud/All-Of-Competitive-Programming/0a8926089af68a439d78ea6c92281209af050222/03-Interview Preparation/PDF/SQL Exclusive Notes.pdf -------------------------------------------------------------------------------- /03-Interview Preparation/README.md: -------------------------------------------------------------------------------- 1 | ⚙️ Under construction ⚙️ 2 | 3 | Aiming to help my fellow coders to clear Technical Interviews. 4 | 5 | I have compiled the resources yet again to simplify the work and save time. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # All-Of-Competitive-Programming 2 | 3 | This repo contains all the resources a beginner needs to start competitive programming and an intermediate to excel in it. It is a curated blog of all other blogs that I've found informative while going through several posts for the past some time. 4 | 5 | 6 | 7 | #### Contents 8 | 9 | * [CP - Essentials](#The-essentials-that-every-competitive-programmer-should-have) 10 | * [Technique](#Technique) 11 | * [Tools](#Tools-for-Competitive-Programming) 12 | * [Speed](#Speed) 13 | 14 |
15 | 16 | > Skip to [setting-up your working environment on your pc](https://github.com/luctivud/All-Of-Competitive-Programming/tree/master/00-Setting-up-local-environment) or [CP Materials](https://github.com/luctivud/All-Of-Competitive-Programming/tree/master/01-Material). 17 | 18 |

19 | 20 | 21 | ### The essentials that every competitive programmer should have: 22 |
23 | 24 | 1. Competitive Programming environment set up on their local machine. 25 | 26 | 2. CP Books to keep them on track. 27 | 28 | 3. Habit of reading blogs. 29 | 30 | 4. Practice! Practice! Practice! 31 | 32 | 5. Should NOT have a complaining nature. 33 | 34 | 35 | 36 |


37 | 38 | 39 | 40 | ### Technique 41 |
42 | 43 | The everyday schedule that one follows should be very balanced and should not leave a person exhausted in the end. The most appropriate time of reading is said to be in the morning itself and a practice which involves heavy mental exercise should be done at night. 44 | 45 | The following schedule is optimal to be followed by a beginner to make it a second nature. However, it is not mandatory and one can follow a plan in which they feel most comfortable. 46 | 47 | * Morning : Read blogs / books / texts. 48 | 49 | * Noon : Implement the concepts and solve related problems. 50 | 51 | * Evening : Read blogs or watch video tutorials. 52 | 53 | * Night : Solve problems based on the concepts learnt. 54 | 55 |
56 | 57 | The following practices should be kept in mind as these are also helpful : 58 | 59 | * Implementing the concepts after reading it. 60 | 61 | * Reading Editorial even if you have solved the problem. 62 | 63 | * Reading and understanding other users' solution and implementing the same. 64 | 65 | * Elaborating the concepts on your own and thinking about the different ways any concept can be modified and then implementing the same. 66 | 67 | * Helping others in understanding the concepts you have learnt so far: This might seem like a waste of time but solving others' doubts and making someone understand a concept will make your concept crystal clear. 68 | 69 | * Having a Good Sleep is always necessary to start the next day with zeal. 70 | 71 |
72 | 73 | * **How much time should we spend on a problem ?** 74 | 75 | The answer depends from person to person and from problem to problem. 76 | 77 | Solving a problem on your own without taking any help from editorial is always a confidence booster but spending a lot of time on probably something you are not aware of yet is not upto the mark. 78 | 79 | In my opinion, you should never take help of any editorial if the problem is simply an implementation as editorials are useless in this case. The problem should be solved by yourself. Consider the editorial only if you're stuck at it and can't bear it anymore. However, this should not be made into habit. 80 | 81 | 82 | 83 | > Side Note: One should take competitive programming as a sport or hobby. If you really want to improve in cp then you should remove all the unnecessary burden of *placements, etc* from your mind. It will take some time and practice to master cp which can only be achieved when you start to enjoy solving the problems rather than treating them as a task. It is not necessary to extensively keep on solving the problems to improve your skills. Instead you can take a break, relax, and then get to the problem. 84 | 85 | > Tip for implementation: To write a good and bug-free code in one-go, you should write your code following the same steps as you would if you were teaching a newbie to do something. (Atleast that's how I've managed a good accuracy so far even in contests.) Your speed will tend to increase once it becomes an habit. 86 | 87 | 88 | 89 | 90 |
[Back to top](#All-Of-Competitive-Programming)

91 | 92 | 93 | 94 | 95 | ### Tools for Competitive Programming 96 |
97 | 98 | Check out the following tools, these may turn out to be of great use: 99 | 100 | * [CP Tracker](http://cptracker.herokuapp.com/) : The current and future cp contests listed from various sites. 101 | 102 | * [C List](https://clist.by/) : Similar to CP Tracker but it contains them all in a single list. 103 | 104 | - [StopStalk](https://www.stopstalk.com/) : A goto for analysing your daily performance and finding problems based on tags. 105 | 106 | * [Codeforces Visualiser](https://cfviz.netlify.app/) : You can track your progress on codeforces and can see the ratings of problems you have currently solved and a complete package for your codeforces profile. 107 | 108 | - [Kenkooo Atcoder](https://kenkoooo.com/atcoder) : A visualisation tool for AtCoder, here you can solve problems from different contests, see its ratings and many more. It is basically a problemset for AtCoder. 109 | 110 | - [CodeDigger](http://codedigger.tech/practice/topicwise) : Solve codeforces problems based on tags and ladders. 111 | 112 | * [Contest Mania](https://contestmania.web.app/) : Past as well as Upcoming contests in a list. 113 | 114 | * [Lockout bot for discord](https://codeforces.com/blog/entry/78546) : You can try this tool for competiting with your friend in a one on one contest simulation on a discord server. 115 | 116 | * [TLE - Discord Bot](https://github.com/cheran-senthil/TLE) : Another Discord bot centred around CP. 117 | 118 | * [Graph Visualizer](https://csacademy.com/app/graph_editor/) : Easily visualize graphs. 119 | 120 | * Chrome extensions : There are a variety of extensions available on Chrome as well as Firefox which can be of great use inculcating the start of competitive programming. 121 | 122 | 123 | 124 |
[Back to top](#All-Of-Competitive-Programming)

125 | 126 | 127 | 128 | 129 | ### Speed 130 |
131 | 132 | 133 | Speed plays a very important role in perfoming well during the live contests. 134 | 135 | For improving the speed, [AtCoder](https://atcoder.jp/) is a great platform. Try attempting the problems and solving them in alloted time which is: 136 | 137 | * 1-2 minute for A & B. 138 | * Approx 5 minutes for C 139 | * Approx 10 - 15 minutes for D & E. 140 | 141 | This will greatly boost the confidence and enhance the logic building and typing speed, in general. 142 | 143 | Virtual Contests can also help in speed as these are simulation of real contest. You can give virtual contests by : 144 | - Giving a virtual contest on codeforces. 145 | - Creating a mashup on codeforces. 146 | - Using [VJudge](https://vjudge.net/) to create a contest of your own from past problems of different OJs. 147 | - Using [Light OJ](http://lightoj.com/login_main.php?url=index.php) to simulate the same. 148 | 149 | 150 | 151 |
[Back to top](#All-Of-Competitive-Programming)

152 | 153 | 154 | 155 | 156 | > You can now [set-up your working environment on your pc](https://github.com/luctivud/All-Of-Competitive-Programming/tree/master/00-Setting-up-local-environment) or move directly to the [CP Materials](https://github.com/luctivud/All-Of-Competitive-Programming/tree/master/01-Material). 157 | 158 | > This repo will keep on updating as new resources are found. 159 | 160 | > Note : I don't claim any of the works to be mine. I have just curated them all at one place. --------------------------------------------------------------------------------