Skip to content

Commit dcf6cd0

Browse files
authored
Merge pull request #23 from r6mez/main
adding stuff
2 parents 991f5fd + a2802da commit dcf6cd0

16 files changed

Lines changed: 308 additions & 36 deletions

content/contest/template.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ void solve() {
1111
signed main() {
1212
cin.tie(nullptr)->sync_with_stdio(false);
1313
int t = 1;
14-
cin >> t;
15-
while (t--)
16-
solve();
14+
// cin >> t;
15+
while (t--) solve();
1716
return 0;
1817
}
1918

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Author: Ramez
3+
* Description: binary trie that supports update(val, op),
4+
* where op = +1 to insert, op = –1 to erase and query(x) → ans is the maximum XOR you can
5+
* achieve between x and any value currently in the trie.
6+
* Time: $O(1)$ time, $O(N)$ space
7+
*/
8+
9+
struct node {
10+
int ch[2]{}, frq[2]{}, sz{};
11+
12+
int& operator[](int x) {
13+
return ch[x];
14+
}
15+
};
16+
17+
const int M = 60;
18+
19+
struct BinaryTrie {
20+
vector<node> nodes;
21+
22+
int newNode() { return nodes.emplace_back(), nodes.size() - 1; }
23+
24+
void init() { nodes.clear(), newNode(); }
25+
26+
BinaryTrie() { init(); }
27+
28+
void update(int val, int op) { /// 1 -> add , -1 -> delete
29+
int u = 0;
30+
for (int i = M - 1; i >= 0; --i) {
31+
int v = val >> i & 1;
32+
if (!nodes[u][v]) {
33+
nodes[u][v] = newNode();
34+
}
35+
nodes[u].frq[v] += op;
36+
nodes[u].sz += op;
37+
u = nodes[u][v];
38+
}
39+
nodes[u].sz++;
40+
}
41+
42+
int query(int x) {
43+
int ans = 0, u = 0;
44+
for (int i = M - 1; i >= 0 && u >= 0; --i) {
45+
int v = x >> i & 1;
46+
if (nodes[u].frq[v]) {
47+
u = nodes[u][v];
48+
}
49+
else {
50+
u = nodes[u][!v];
51+
ans |= 1LL << i;
52+
}
53+
}
54+
return ans;
55+
}
56+
};

content/data-structures/DSU.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
/**
2-
* Author: Lukas Polacek
3-
* Date: 2009-10-26
4-
* License: CC0
5-
* Source: folklore
2+
* Author: Ramez
63
* Description: Disjoint-set data structure.
74
* Time: $O(\alpha(N))$
85
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Author: Ramez Medhat
3+
* Description: Range Update, Point Query
4+
* Time: O(logN)
5+
*/
6+
struct FenwickRUPQ {
7+
int n;
8+
vi f;
9+
FenwickRUPQ(int _n) : n(_n), f(n + 1, 0) {}
10+
11+
void update(int idx, int val) {
12+
for (; idx <= n; idx += idx & -idx)
13+
f[idx] += val;
14+
}
15+
16+
void rangeAdd(int l, int r, int val) {
17+
update(l, val);
18+
if (r + 1 <= n) update(r + 1, -val);
19+
}
20+
21+
int pointQuery(int idx) {
22+
int res = 0;
23+
for (; idx > 0; idx -= idx & -idx) res += f[idx];
24+
return res;
25+
}
26+
};
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
#pragma once
1414

1515
template<class T>
16-
struct SubMatrix {
16+
struct PrefixSum2D {
1717
vector<vector<T>> p;
18-
SubMatrix(vector<vector<T>>& v) {
19-
int R = sz(v), C = sz(v[0]);
20-
p.assign(R+1, vector<T>(C+1));
21-
rep(r,0,R) rep(c,0,C)
22-
p[r+1][c+1] = v[r][c] + p[r][c+1] + p[r+1][c] - p[r][c];
18+
PrefixSum2D(vector<vector<T>>& v) {
19+
int R = v.size(), C = v[0].size();
20+
p.assign(R + 1, vector<T>(C + 1));
21+
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++)
22+
p[r + 1][c + 1] = v[r][c] + p[r][c + 1] + p[r + 1][c] - p[r][c];
2323
}
2424
T sum(int u, int l, int d, int r) {
2525
return p[d][r] - p[d][l] - p[u][r] + p[u][l];

content/data-structures/Trie.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Author: Ramez
3+
* Description: Trie
4+
* Time: $O(1)$ time, $O(N)$ space
5+
*/
6+
7+
struct Trie {
8+
struct Node {
9+
Node* child[26];
10+
int IsEnd, Prefix;
11+
Node() {
12+
memset(child, 0, sizeof child);
13+
IsEnd = Prefix = 0;
14+
}
15+
};
16+
17+
Node* root = new Node();
18+
19+
void insert(string& s)
20+
{
21+
Node* cur = root;
22+
for (auto it : s)
23+
{
24+
int idx = it - 'a';
25+
if (cur->child[idx] == 0)
26+
{
27+
cur->child[idx] = new Node();
28+
}
29+
cur = cur->child[idx];
30+
cur->Prefix++;
31+
}
32+
cur->IsEnd++;
33+
}
34+
35+
bool searchWord(string& s)
36+
{
37+
Node* cur = root;
38+
for (auto it : s)
39+
{
40+
int idx = it - 'a';
41+
if (cur->child[idx] == 0)return 0;
42+
cur = cur->child[idx];
43+
}
44+
return cur->IsEnd;
45+
}
46+
47+
int countWord(string& s)
48+
{
49+
Node* cur = root;
50+
for (auto it : s)
51+
{
52+
int idx = it - 'a';
53+
if (cur->child[idx] == 0)return 0;
54+
cur = cur->child[idx];
55+
}
56+
return cur->IsEnd;
57+
}
58+
59+
int countPrefix(string& s)
60+
{
61+
Node* cur = root;
62+
for (auto it : s)
63+
{
64+
int idx = it - 'a';
65+
if (cur->child[idx] == 0)return 0;
66+
cur = cur->child[idx];
67+
}
68+
return cur->Prefix;
69+
}
70+
};

content/data-structures/chapter.tex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ \chapter{Data structures}
33
\kactlimport{LazySegmentTree.h}
44
\kactlimport{MergeSortTree.h}
55
\kactlimport{FenwickPURQ.cpp}
6+
\kactlimport{FenwickRUPQ.cpp}
67
\kactlimport{FenwickRURQ.cpp}
78
\kactlimport{Fenwick2d.h}
89
\kactlimport{Fenwick2dAdd.h}
910
\kactlimport{Fenwick2dXor.h}
1011
\kactlimport{OrderStatisticTree.h}
1112
\kactlimport{HashMap.h}
13+
\kactlimport{LazySegmentTree.h}
1214
\kactlimport{DSU.h}
1315
\kactlimport{DSURollback.h}
14-
\kactlimport{SubMatrix.h}
16+
\kactlimport{PrefixSum2D.h}
1517
\kactlimport{Matrix.h}
1618
\kactlimport{LineContainer.h}
1719
\kactlimport{Treap.h}
1820
\kactlimport{RMQ.h}
1921
\kactlimport{MoQueries.h}
22+
\kactlimport{MergeSortTree.h}
23+
\kactlimport{Trie.cpp}
24+
\kactlimport{BinaryTrie.cpp}
25+
26+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Author: Ramez
3+
* Description: Function to solve combinatorics problems.
4+
* Time: O(n) for init and O(1) for query
5+
*/
6+
7+
namespace combinatorics {
8+
vector<int> fact, inv, invFact;
9+
10+
int pwmod(int a, int b) {
11+
a %= MOD;
12+
int result = 1;
13+
while (b > 0) {
14+
if (b & 1) result = (result * a) % MOD;
15+
a = (a * a) % MOD;
16+
b /= 2;
17+
}
18+
return result;
19+
}
20+
21+
int inverse(int x) { return pwmod(x, MOD - 2); }
22+
int multiply(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; }
23+
int divide(int a, int b) { return multiply(a, inverse(b)); }
24+
25+
void init(int n) {
26+
fact.resize(n + 1); inv.resize(n + 1); invFact.resize(n + 1);
27+
fact[0] = fact[1] = inv[0] = inv[1] = invFact[0] = invFact[1] = 1;
28+
for (int i = 2; i <= n; ++i){
29+
fact[i] = fact[i - 1] * i % MOD;
30+
inv[i] = MOD - ((MOD / i) * inv[MOD % i]) % MOD;
31+
invFact[i] = invFact[i - 1] * inv[i] % MOD;
32+
}
33+
}
34+
35+
int nPr(int n, int r) {
36+
if (n < 0 || r < 0 || r > n) return 0;
37+
return fact[n] * invFact[n - r] % MOD;
38+
}
39+
40+
int nCr(int n, int r) {
41+
if (n < 0 || r < 0 || r > n) return 0;
42+
return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;
43+
}
44+
45+
int nPrLinear(int n, int r){
46+
int answer = 1;
47+
for (int i = n - r + 1; i <= n; i++){
48+
answer = multiply(answer, i);
49+
}
50+
return answer;
51+
}
52+
53+
int nCrLinear(int n, int r){
54+
int answer = 1;
55+
for (int i = r + 1; i <= n; i++){
56+
answer = multiply(answer, i);
57+
answer = divide(answer, i - r);
58+
}
59+
return answer;
60+
}
61+
};
62+
using namespace combinatorics;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ vi eratosthenes() {
3737
for (int i : pr) isPrime[i] = 1;
3838
return pr;
3939
}
40+

content/number-theory/IsPrime.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Author: Ramez Medhat
3+
* Description: Checks if a number is prime or not
4+
* Time: $O(\sqrt{n})$
5+
* Status: Tested
6+
*/
7+
8+
9+
bool isPrime(int n) {
10+
if (n == 2) return true;
11+
if (n == 1 || n % 2 == 0) return false;
12+
for (int i = 3; i * i <= n; i += 2) {
13+
if (n % i == 0) return false;
14+
}
15+
return true;
16+
}

0 commit comments

Comments
 (0)