Skip to content

Shivam5022/templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 

Repository files navigation

KEEP IN MIND

  1. For Ordered Set, use :

    #include <ext/pb_ds/assoc_container.hpp>
    using namespace __gnu_pbds;
    #define fbo find_by_order
    #define ook order_of_key
     
    template<typename T>
    using ordered_set = tree<T,  null_type,  less<T>,  rb_tree_tag,  tree_order_statistics_node_update>;
    
    // used as ordered_set<int>  S; 
    // S.insert(123);
    // use 'less_equal<T>' to support duplicates
    // S.ook(x) : number of elements in S strictly less than 'x'
    // S.size() - S.ook(x) : number of elements in S >= x
    // *S.fbo(i) : i'th element in S (0 - based indexing)
  2. For random number generator: (add these two lines on top, range is [a, b] )

std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
  1. Priority Queue Initialisation :

    template<class T> using pq = priority_queue<T>;
    template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
  2. For accurate square root use : (credits : errichto)

    long long x = sqrtl(a) + 2;
    while (x * x > a) x--;
  3. Grid cell (i, j) is uniquely mapped to (i + j * n) OR (j + i * m). Print the values.

  4. Never use endl, unless in interactive problems. Always make a habit of using '\n' for new line.

  5. Rather than clearing globals before each test case, you can also do:

    // GLOBAL DECLARATION WITHOUT SIZE
    vector<vector<int>> adj;
    vector<vector<int>> up; 
    vector<int> tin, tout;
    vector<int> depth;
    
    // INSIDE MAIN ASSIGN SIZE:
    adj = vector<vector<int>> (n + 5);
    up = vector<vector<int>> (n + 5, vector<int>(24));
    tin = vector<int> (n + 5);
    tout = vector<int> (n + 5);
    depth = vector<int> (n + 5);
  6. ckmax and ckmin . It returns TRUE if assignment happens else FALSE.

    template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
    template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

About

This repo contains implementation of frequently used algorithms in cp.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages