Skip to content

Instantly share code, notes, and snippets.

View arielherself's full-sized avatar
🦆
Duck typing

Ariel arielherself

🦆
Duck typing
View GitHub Profile
constexpr int MAXN=1e5+10;
int n,cnt,root;
int d[MAXN*2],ls[MAXN*2],rs[MAXN*2];
void update(int s,int t,int &p,int x,int c){ // add constant c to #x
if(!p)p=++cnt; // create node
if(s==t){
d[p]+=c;
return;
}
constexpr int MAXN=1e5+10;
int a[MAXN],d[4*MAXN],b[4*MAXN]; // tree d, tags b
void build(int s,int t,int p){ // root p, range [s,t]
if(s==t){
d[p]=a[s];
return;
}
int m=s+(t-s>>1);
build(s,m,p*2),build(m+1,t,p*2+1);
@arielherself
arielherself / tarjan-scc.cc
Created December 1, 2023 13:03
find strongly-connected components
// st = stack, vis = visited, br = branch(result)
constexpr int MAXN=1e5+10;
int dfn[MAXN],low[MAXN],st[MAXN],vis[MAXN],br[MAXN];
int n,cnt,stn,scn;
vector<int>ch[MAXN];
void tarjan(int v){
dfn[v]=low[v]=++cnt;
st[stn++]=v;
vis[v]=1;
@arielherself
arielherself / BIT.py
Created November 20, 2023 15:13
binary indexed tree template
# @ref https://oi.wiki/ds/fenwick
n=...
c=[0]*(n+1)
lowbit=lambda x: x&-x
def prep(a):
res=[0]*(n+1)
s=0
void Tarjan(int u, int father) {
vis[u] = true;
low[u] = dfn[u] = ++cnt;
int child = 0;
for (auto v : edge[u]) {
if (!vis[v]) {
child++;
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (father != u && low[v] >= dfn[u] && !flag[u]) {
@arielherself
arielherself / fhq_treap.cc
Last active November 9, 2023 02:07
Template of FHQ-Treap
// @link https://www.acwing.com/file_system/file/content/whole/index/content/8807719/
// Index starts from 1
#include <bits/stdc++.h>
using namespace std;
#define lson fhq[u].l
#define rson fhq[u].r
const int N = 1e6 + 10;
/**
* 作者:力扣官方题解
* 链接:https://leetcode.cn/problems/reconstruct-itinerary/solutions/
* 来源:力扣(LeetCode)
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
class Solution {
public:
unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;
#include <bits/stdc++.h>
using namespace std;
int preorder[1010];
int inorder[1010];
int n;
struct TreeNode {
int val;
TreeNode *lc;
#include <bits/stdc++.h>
using namespace std;
template <typename T> class Number {
template <typename U> friend Number<U> &operator++(Number<U> &);
public:
using iterator_category = forward_iterator_tag;
using value_type = T;
using difference_type = ptrdiff_t;
@arielherself
arielherself / lc105.cc
Created October 21, 2023 21:25
Build a tree from its preorder & inorder traversal
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {