1
0
Fork 0
This commit is contained in:
arielherself 2023-12-02 12:05:10 +08:00
parent 78b4006392
commit aeb34bff84
9 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,33 @@
#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;
using pointer = const T *;
using reference = const T &;
Number(T a) : dat(a) {}
bool operator==(const Number<T> &o) const { return dat == *o; }
bool operator!=(const Number<T> &o) const { return dat != *o; }
const T &operator*() const { return dat; }
Number<T> operator+(ptrdiff_t dt) { return Number<T>(dat + dt); }
private:
T dat;
void _plusplus() { ++dat; }
};
template <typename T> Number<T> &operator++(Number<T> &it) {
it._plusplus();
return it;
}
int main() {
auto cmp = [](int a, int x) { return a <= x; };
cout << *lower_bound(Number(1), Number(5), 3, cmp); // cout << 3 + 1;
return 0;
}

32
graph/hierholzer.cc Normal file
View File

@ -0,0 +1,32 @@
/**
*
* https://leetcode.cn/problems/reconstruct-itinerary/solutions/
* LeetCode
*
*/
class Solution {
public:
unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;
vector<string> stk;
void dfs(const string& curr) {
while (vec.count(curr) && vec[curr].size() > 0) {
string tmp = vec[curr].top();
vec[curr].pop();
dfs(move(tmp));
}
stk.emplace_back(curr);
}
vector<string> findItinerary(vector<vector<string>>& tickets) {
for (auto& it : tickets) {
vec[it[0]].emplace(it[1]);
}
dfs("JFK");
reverse(stk.begin(), stk.end());
return stk;
}
};

View File

@ -0,0 +1,47 @@
#include <bits/stdc++.h>
using namespace std;
int preorder[1010];
int inorder[1010];
int n;
struct TreeNode {
int val;
TreeNode *lc;
TreeNode *rc;
TreeNode(int v) : val(v) {}
TreeNode(int v, TreeNode *l, TreeNode *r) : val(v), lc(l), rc(r) {}
};
TreeNode* dfs(int p_begin, int p_end, int i_begin, int i_end) {
if (i_begin >= i_end) {
return nullptr;
}
auto i_mid = find(inorder + i_begin, inorder + i_end, preorder[p_begin]) - inorder;
int left_sz = i_mid - i_begin;
TreeNode* res = new TreeNode(preorder[p_begin]);
res->lc = dfs(p_begin + 1, p_begin + 1 + left_sz, i_begin, i_mid);
res->rc = dfs(p_begin + 1 + left_sz, p_end, i_mid + 1, i_end);
return res;
}
void print_postorder(TreeNode *node) {
if (!node) {
return;
}
print_postorder(node->lc);
print_postorder(node->rc);
cout << node->val << ' ';
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> preorder[i];
}
for (int i = 0; i < n; ++i) {
cin >> inorder[i];
}
print_postorder(dfs(0, n, 0, n));
return 0;
}

31
trees/BIT.py Normal file
View File

@ -0,0 +1,31 @@
# @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
for i in range(1,n+1):
s+=a[i]
res[i]=s
return res
def init(a):
pa=prep(a)
for i in range(1,n+1):
c[i]=pa[i]-pa[i-lowbit(i)]
def getsum(x):
ans=0
while x:
ans+=c[x]
x-=lowbit(x)
return ans
def add(x,k):
while x<=n:
c[x]+=k
x+=lowbit(x)

91
trees/fhq_treap.cc Normal file
View File

@ -0,0 +1,91 @@
// @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;
struct Node {
int l, r;
int val, key, size;
} fhq[N];
int cnt, root;
void pushup(int u) {
fhq[u].size = fhq[lson].size + fhq[rson].size + 1;
}
int node(int val) {
fhq[++cnt].val = val;
fhq[cnt].key = rand();
fhq[cnt].size = 1;
return cnt;
}
void split(int u, int val, int &x, int &y) {
if (!u) x = y = 0;
else {
if (fhq[u].val <= val) x = u, split(rson, val, rson, y);
else y = u, split(lson, val, x, lson);
pushup(u);
}
}
int merge(int x, int y) {
if (!x || !y) return x + y;
if (fhq[x].key <= fhq[y].key) {
fhq[x].r = merge(fhq[x].r, y);
pushup(x);
return x;
} else {
fhq[y].l = merge(x, fhq[y].l);
pushup(y);
return y;
}
}
int x, y, z;
void insert(int val) {
split(root, val, x, y);
root = merge(merge(x, node(val)), y);
}
void del(int val) {
split(root, val, x, z);
split(x, val - 1, x, y);
y = merge(fhq[y].l, fhq[y].r);
root = merge(merge(x, y), z);
}
int askRank(int val) {
split(root, val - 1, x, y);
int res = fhq[x].size + 1;
root = merge(x, y);
return res;
}
int askNum(int u, int rank) {
if (fhq[lson].size + 1 == rank) return fhq[u].val;
if (fhq[lson].size >= rank) return askNum(lson, rank);
else return askNum(rson, rank - fhq[lson].size - 1);
}
int askPre(int val) {
split(root, val - 1, x, y);
int u = x;
while (rson) u = rson;
root = merge(x, y);
return fhq[u].val;
}
int askNxt(int val) {
split(root, val, x, y);
int u = y;
while (lson) u = lson;
root = merge(x, y);
return fhq[u].val;
}