Skip to content
Snippets Groups Projects
Commit bef2be80 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Small fixes

parent 3ba56335
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
* *** = used in both * and ** * *** = used in both * and **
*/ */
#define MAXLOG 20 //log2(MAX)
vector<int> graph[MAX]; //*** vector<ii> vector<int> graph[MAX]; //*** vector<ii>
int h[MAX]; int h[MAX];
......
...@@ -13,7 +13,7 @@ int ncomp, ind; ...@@ -13,7 +13,7 @@ int ncomp, ind;
int cont[MAX], parent[MAX]; int cont[MAX], parent[MAX];
int low[MAX], L[MAX]; int low[MAX], L[MAX];
// Fills scc with strongly connected components of graph // Fills SCC with strongly connected components of graph
void dfs(int x) { void dfs(int x) {
L[x] = ind; L[x] = ind;
low[x] = ind++; low[x] = ind++;
......
...@@ -28,7 +28,7 @@ bool dfs(int x) { ...@@ -28,7 +28,7 @@ bool dfs(int x) {
} }
// Returns if graph contains cycle or not, and fills tsort vector with // Returns whether graph contains cycle or not, and fills tsort vector with
// topological sort of the graph // topological sort of the graph
bool topological_sort(int n, vector<int> &tsort) { bool topological_sort(int n, vector<int> &tsort) {
mset(cont, 0); mset(cont, 0);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
// ========== Fermat's Little Theorem ========== // ========== Fermat's Little Theorem ==========
// Used if m is prime // Used when m is prime
ll power(ll x, ll y) { ll power(ll x, ll y) {
ll ans = 1; ll ans = 1;
...@@ -31,7 +31,7 @@ ll mod_inverse(ll a) { ...@@ -31,7 +31,7 @@ ll mod_inverse(ll a) {
// ========== Extended Euclidean Algorithm ========== // ========== Extended Euclidean Algorithm ==========
// Used if m and a are coprime // Used when m and a are coprime
// return gcd(a, b) and find x, y where: // return gcd(a, b) and find x, y where:
// a*x + b*y = gcd(a, b) // a*x + b*y = gcd(a, b)
......
/** /**
* Kadane * Kadane - Largest Sum Contiguous Subarray
* *
* Complexity (Time): O(n + m) * Complexity (Time): O(n + m)
* Complexity (Space): O(n + m) * Complexity (Space): O(n + m)
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
int v[MAX]; int v[MAX];
int kadane() { int kadane() {
// Maximum so far (msf), Maximum ending here (meh). // Maximum so far (msf), Maximum ending here (meh).
int msf = -0x3f3f3f3f, meh = 0; int msf = -0x3f3f3f3f, meh = 0;
int start = 0, end = 0, s = 0; int start = 0, end = 0, s = 0;
...@@ -15,12 +16,16 @@ int kadane() { ...@@ -15,12 +16,16 @@ int kadane() {
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
meh += v[i]; meh += v[i];
// Store maximum so far as well as starting and ending position
// of the subsequence
if (msf < meh) { if (msf < meh) {
msf = meh; msf = meh;
start = s; start = s;
end = i; end = i;
} }
// If maximum ending here is negative, then it's definitely
// better to restart
if (meh < 0) { if (meh < 0) {
meh = 0; meh = 0;
s = i + 1; s = i + 1;
......
/** /**
* Knuth-Morris-Pratt - KMP * Knuth-Morris-Pratt (KMP)
* *
* Complexity (Time): * Complexity (Time):
* preprocess -> O(m) * preprocess -> O(m)
......
/** /**
* Binary Indexed Tree - BIT * Binary Indexed Tree (BIT)
* *
* Complexity (Time): * Complexity (Time):
* Update -> O(log n) * Update -> O(log n)
......
/** /**
* Binary Indexed Tree 2D - BIT2D * Binary Indexed Tree 2D (BIT2D)
* *
* Complexity (Time): * Complexity (Time):
* Update -> O(log^2 n) * Update -> O(log^2 n)
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
int par[MAX]; int par[MAX];
int h[MAX]; int h[MAX];
int sz[MAX]; int size[MAX];
// Initialize element x // Initialize element x
void make_set(int x) { void make_set(int x) {
par[x] = x; par[x] = x;
h[x] = 0; h[x] = 0;
sz[x] = 1; size[x] = 1;
} }
...@@ -39,13 +39,12 @@ void union_set(int x, int y) { ...@@ -39,13 +39,12 @@ void union_set(int x, int y) {
if (h[xroot] < h[yroot]) { if (h[xroot] < h[yroot]) {
par[xroot] = yroot; par[xroot] = yroot;
sz[yroot] += sz[xroot]; size[yroot] += size[xroot];
} else if (h[xroot] > h[yroot]) {
par[yroot] = xroot;
sz[xroot] += sz[yroot];
} else { } else {
par[yroot] = xroot; par[yroot] = xroot;
sz[xroot] += sz[yroot]; size[xroot] += size[yroot];
if (h[xroot] == h[yroot])
h[xroot]++; h[xroot]++;
} }
} }
/** /**
* Policy Tree * Policy Tree
* *
* A set-like STL structure with order statistics
*
* Complexity (Time): * Complexity (Time):
* insert -> O(log n) * insert -> O(log n)
* erase -> O(log n) * erase -> O(log n)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment