



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
C++ Data Structure Cheat Sheet
Typology: Cheat Sheet
1 / 5
This page cannot be seen from the preview
Don't miss anything!
On special offer
Pointers Storing, data type of pointers and variables must be the same &var Returns address of memory location of variable data_type *pointer; Initialize. Put * in front of name *pointer Returns value at the memory location stored by pointer Array variables are actually pointers to the first element in the array. The amount that your pointer moves from an arithmetic operation *array Returns first element in array *(array+2) Returns third element in array โ Variables that you declare are stored in a memory location in your computer โ The address of these memory locations can be stored in pointers โ Addresses are in hexadecimal Iterators //Append ::iterator to your data type declaration to create an iterator of that data type vector
Priority Queue priority_queue<data_type> pq; // Largest at top priority_queue<data_type, vector<data_type>, greater<data_type> > pq; // Smallest at top pq.push(5); // pushes element into it. Duplicates are allowed pq.top() // Returns largest or smallest element pq.pop() // Removes largest or smallest element pq.size(); // Returns size pq.empty(); Check if queue is empty Like a queue except that only the element with the greatest priority (eg. largest/smallest) can be accessed Fenwick tree //Below here can mix & match long long ft[100001]; // note: this fenwick tree is 1-indexed. ////PUโ PQ//////////////////////////////////////////////// ////// void fenwick_update(int pos, long long value) { while (pos <= N) { //cout<<"Fenwick Updating: "<<pos<<","<โ <value<<endl; ft[pos] += value; pos += pos&-pos; } } long long fenwick_query(int pos) { long long sum = 0; while (pos) { // while p > 0 sum += ft[pos]; pos -= pos&-pos; } return sum; } ////RUโ PQ//////////////////////////////////////////////// ////// void fenwick_range_update(int pos_a, int pos_b, int val){ //TLE way Fenwick tree (cont) //for (int i=pos_a;i<=pos_b;i++){fenwick_updโ ate(i, val);} fenwick_update(pos_a, val); fenwick_update(pos_b+1, -val); } ////PURQ////////////// //////////////////////////////////////// long long fenwick_range_query(int pos_a, int pos_b) { return fenwick_query(pos_b) - fenwick_query(pโ os_a-1); } ////RUโ RQ//////////////////////////////////////////////// ////// long long B1[100 001 ];long long B2[100001]; void base_update(long long *ft, int pos, long long value){ //Add largest power of 2 dividing x / Last set bit in number x for (; pos <= N; pos += pos&(-pos)) ft[pos] += value; } void rurq_range_update(int a, int b,long long v){ base_update(B1, a, v); base_update(B1, b + 1, -v); base_update(B2, a, v * (a-1)); base_update(B2, b + 1, -v * b); } void rurq_point_update(int a, long long v){ rurq_range_update(a,a,v); } long long base_query(long long *ft,int b){ long long sum = 0; for(; b > 0; b -= b&(-b)) sum += ft[b]; return sum; } // Return sum A[1...b] long long rurq_query(int b){ By Hackin cheatography.com/hackin7/ Published 12th December, 2018. Last updated 27th December, 2019. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!
Deque deque
Segment Tree (cont) int rangeMinimumQuery(int lower_bound, int upper_bound) { //cout<<"Node:"<<start<<" "<<end<<" "<<mid<<" "<<val<<endl; //If Query Range Corresponds//////////////// if (start==lower_bound && end==upper_bound){ return value(); } //Query Right Tree if range only lies there else if (lower_bound > mid){ return right->rangeMinimumQuery(lowerโ bound, upper_bound); } //Query Left Tree if range only lies there else if (upper_bound <= mid){ return left->rangeMinimumQuery(lowerโ bound, upper_bound); } //Query both ranges as range spans both trees else{ return min(left->rangeMinimumQuery(lowerโ bound, mid),right->rangeMinimumQuery(mid+1, upperโ bound)); } //Eโ nd////////////////////////////////////////// } } *root; void init(int N){ root = new node(0, N-1); // creates seg tree of size n } void update(int P, int V){ root->update(P,V); } int query(int A, int B){ int val = root->rangeMinimumQuery(A,B); return val; } By Hackin cheatography.com/hackin7/ Published 12th December, 2018. Last updated 27th December, 2019. Sponsored by CrosswordCheats.com Learn to solve cryptic crosswords!