Last active
July 9, 2025 08:35
-
-
Save sitano/effb2554fa5cb50df2c4fcb6ace3a107 to your computer and use it in GitHub Desktop.
leetcode c++ headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma GCC optimize("Ofast,unroll-loops") | |
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,mmx,abm,popcnt,tune=native") | |
int const _ = [] { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return 0; } (); | |
template <class T> using vec = vector<T>; | |
using T = TreeNode; | |
using ll = long long; | |
using vi = vec<int>; | |
// auto __unsync_with_stdio = std::ios::sync_with_stdio(false); | |
// auto __uncin_tie = std::cin.tie(nullptr); | |
//////////////////////////////////////////////////////////////////////////////////// | |
#pragma GCC optimize("O3,unroll-loops,Ofast") | |
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") | |
static const auto harsh = []() { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
cout.tie(nullptr); | |
return 0; | |
}(); | |
#define LC_HACK | |
#ifdef LC_HACK | |
const auto __ = []() { | |
struct ___ { static void _() { std::ofstream("display_runtime.txt") << 0 << '\n'; } }; | |
std::atexit(&___::_); | |
return 0; | |
}(); | |
#endif | |
//////////////////////////////////////////////////////////////////////////////////// | |
template <class T> using vec = vector<T>; | |
using T = TreeNode; | |
using T = ListNode; | |
using vvi = vector<vector<int>>; | |
using vvc = vector<vector<char>>; | |
using vi = vector<int>; | |
using str = string; | |
using pii = pair<int, int>; | |
using pis = pair<int, string>; | |
using vs = vector<string>; | |
// strigbuf | |
std::stringstream ss(std::string(reserve, '\0')); | |
string(ss.str()); | |
// reading words | |
stringstream ss(s); | |
string word; | |
stack<string> stk; | |
while(ss >> word) stk.push(word); | |
// priority queue / heap | |
make_heap(p.begin(), p.end()); | |
pop_heap(p.begin(), p.end()); | |
push_heap(p.begin(), p.end()); | |
using pi = pair<int, int> | |
priority_queue<int> max_heap; max_heap.top(); max_heap.pop(); max_heap.push(x); | |
priority_queue<pi, vector<pi>, greater<pi> > min_heap; // over pi.first | |
sort(v.begin(), v.end(), [](const T & a, const T & b) -> bool { return a.t > b.t; }); | |
auto comp = []( adjist a, adjlist b ) { return a.second > b.second; }; | |
priority_queue< adjlist_edge , vector<adjlist_edge>, decltype( comp ) > | |
adjlist_pq( comp ); | |
/// vector slices | |
vector<int> s = {10, 20, 30, 40, 50, 60}; | |
vector<int> d(s.begin(), s.end() - 1); // vector copy | |
span sp(s); // since 0x20 | |
span ss = sp.subspan(2, 5); // (offset, count) |
hash map with custom key:
struct pair_hash
{
template <class T, class U>
std::size_t operator()(std::pair<T, U> const& pair) const {
return std::hash<T>()(pair.first) ^ std::hash<U>()(pair.second);
}
};
std::unordered_map<std::pair<int, int>, int, pair_hash> umap;
for array<int, X>:
struct array_hash
{
template <class T, long unsigned int U>
std::size_t operator()(array<T, U> const& a) const {
std::size_t h = 0; for (auto i: a) h = h ^ std::hash<T>()(i); return h;
}
};
prefix sums in c++
const int kLimit = *max_element(potions.begin(), potions.end());
// get freq array up to max potion (10^5 * 4 bytes ~ 0.5MB)
vector<int> freq(kLimit + 1, 0);
for (int p : potions) {
freq[p]++;
}
// suffix sums
partial_sum(freq.rbegin(), freq.rend(), freq.rbegin());
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://usaco.guide/silver/custom-cpp-stl?lang=cpp (Optional) C++ Sets with Custom Comparators
https://stackoverflow.com/questions/3221812/how-to-sum-up-elements-of-a-c-vector
https://stackoverflow.com/questions/2462951/c-equivalent-of-stringbuffer-stringbuilder
std::stringstream ss(std::string(reserve, '\0')); string(ss.str())
https://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c11 Recursive Lambdas
https://usaco.guide/general/lambda-funcs?lang=cpp
https://stackoverflow.com/questions/17663186/initializing-a-two-dimensional-stdvector 2D vectors init
https://stackoverflow.com/questions/5122804/how-to-sort-with-a-lambda sort(start, end, lambda -> bool)
make_heap(p.begin(), p.end()), pop_heap(p.begin(), p.end()), push_heap(p.begin(), p.end());
https://stackoverflow.com/questions/19019252/create-n-element-constexpr-array-in-c11 constexpr array init