diff --git a/caderno.pdf b/caderno.pdf index 585d47a5f89b2e02a33380d8badcf56467e495f2..4e30bc3034b81198fa337a28498df566ebab24b7 100644 Binary files a/caderno.pdf and b/caderno.pdf differ diff --git a/caderno/caderno.tex b/caderno/caderno.tex index 43c0c7e6c99af19e019cd3530a8887c9903a62d3..fe3a092110df0585e85787fdd293b023c57d7d1b 100644 --- a/caderno/caderno.tex +++ b/caderno/caderno.tex @@ -85,8 +85,8 @@ \twocolumn \title{Caderno de Maratona de Programação} -\author{Fernando Kiotheka$^1$} -\date{$^1$Universidade Federal do Paraná\\[2ex]\today} +\author{Fernando Kiotheka} +\date{Universidade Federal do Paraná\\[2ex]\today} \maketitle \tableofcontents @@ -195,7 +195,7 @@ onde $p$ é primo. \subsection{Módulo no expoente} \begin{align*} - a^n = a^{n \pmod{\phi(m)}} \pmod{m} + a^n = a^{n \pmod{\varphi(m)}} \pmod{m} \end{align*} para $a$ e $m$ coprimos. @@ -204,7 +204,6 @@ A área de um triângulo pode ser escrita como \begin{align*} A = \sqrt{s(s-a)(s-b)(s-c)} \end{align*} - onde $s = \frac{a+b+c}{2}$. \subsection{Séries} @@ -254,15 +253,14 @@ Aplicações: \lstinputlisting{fontes/template.cpp} \subsection{Teste de estresse} \lstinputlisting[language=bash]{fontes/stress.sh} -\subsection{Vimrc} +\subsection{Hash de linhas em $[l, r]$} +\lstinputlisting[language=bash]{fontes/hash.sh} +\subsection{vimrc} \lstinputlisting{fontes/vimrc} \subsection{Gerador} \lstinputlisting{fontes/gen.cpp} \section{Estrutura de Dados} -\subsection{Soma de prefixos} -\lstinputlisting{fontes/prefix-sum.h} - \subsection{União-busca} \lstinputlisting{fontes/disjoint.h} @@ -316,16 +314,16 @@ Aplicações: \lstinputlisting{fontes/min-stack.h} \subsection{Maior elemento anterior} -\lstinputlisting{fontes/pge.h} +\lstinputlisting{fontes/previous-greater-element.h} \subsection{Menor elemento anterior} -\lstinputlisting{fontes/ple.h} +\lstinputlisting{fontes/previous-lesser-element.h} \subsection{Próximo maior elemento} -\lstinputlisting{fontes/nge.h} +\lstinputlisting{fontes/next-greater-element.h} \subsection{Próximo menor elemento} -\lstinputlisting{fontes/nle.h} +\lstinputlisting{fontes/next-lesser-element.h} \subsection{Fila mínima (sabendo o que remover)} \lstinputlisting{fontes/min-queue.h} @@ -339,9 +337,6 @@ Aplicações: \subsection{Hash customizado} \lstinputlisting{fontes/custom-hash.h} -\subsection{Nó de árvore de segmentos} -\lstinputlisting{fontes/node.h} - \subsection{Árvore de segmentos iterativa} \vspace{4pt} @@ -356,27 +351,24 @@ Aplicações: \hline \end{tabular} -\lstinputlisting{fontes/stit.h} +\lstinputlisting{fontes/segment-tree.h} \subsection{Árvore de segmentos preguiçosa iter.} -\lstinputlisting{fontes/lstit.h} +\lstinputlisting{fontes/lazy-segment-tree.h} \subsection{Árvore de segmentos recursiva} -\lstinputlisting{fontes/strec.h} - -\subsection{Hash map simples} -\lstinputlisting{fontes/simple-hash.h} +\lstinputlisting{fontes/recursive-segment-tree.h} \subsection{Conjunto de intervalos coloridos} \lstinputlisting{fontes/interval-set.h} \subsection{Árvore de segmentos preguiçosa rec.} -\lstinputlisting{fontes/lstrec.h} +\lstinputlisting{fontes/recursive-lazy-segment-tree.h} \section{Grafos} \subsection{Articulações e pontes} -\lstinputlisting{fontes/articbridges.h} +\lstinputlisting{fontes/articulations-bridges.h} \subsection{Componentes fortes -- Tarjan} \lstinputlisting{fontes/tarjan.h} @@ -385,10 +377,10 @@ Aplicações: \lstinputlisting{fontes/kosaraju.h} \subsection{Ordenação topológica} -\lstinputlisting{fontes/topo.h} +\lstinputlisting{fontes/topological-order.h} \subsection{Busca em largura 0-1} -\lstinputlisting{fontes/bfs01.h} +\lstinputlisting{fontes/bfs-01.h} \subsection{Emparelhamento estável} \begin{tabular}{l} @@ -404,7 +396,6 @@ Aplicações: \subsection{Caminho mínimo -- Dijkstra} \lstinputlisting{fontes/dijkstra.h} -\clearpage \subsection{Caminho mínimo -- Bellman-Ford} \lstinputlisting{fontes/bellman-ford.h} @@ -424,20 +415,22 @@ Aplicações: \lstinputlisting{fontes/stoer-wagner.h} \subsection{Fluxo máximo} -\lstinputlisting{fontes/ffek.h} +\lstinputlisting{fontes/ford-fulkerson-edmons-karp.h} \subsection{Fluxo de custo mínimo} -\lstinputlisting{fontes/ffekmincost.h} +\lstinputlisting{fontes/ford-fulkerson-edmons-karp-min-cost.h} + +\subsection{Fluxo máximo Dinic} +\lstinputlisting{fontes/dinic.h} \subsection{Menor no maior} \lstinputlisting{fontes/small-to-large.h} \subsection{Ascensão binária} -\lstinputlisting{fontes/bl.h} +\lstinputlisting{fontes/binary-lifting.h} -\clearpage \subsection{Menor anc. comum -- Ancensão binária} -\lstinputlisting{fontes/bllca.h} +\lstinputlisting{fontes/lca-binary-lifting.h} \subsection{Menor anc. comum -- Offline} \lstinputlisting{fontes/lca-offline.h} @@ -460,6 +453,9 @@ Aplicações: \end{tabular} \lstinputlisting{fontes/hld.h} +\subsection{Decomposição pesado-leve -- K-ésimo} +\lstinputlisting{fontes/hld-kth.h} + \subsection{Empar. máximo bipartido -- Kuhn} \begin{tabular}{l} \hline @@ -468,20 +464,20 @@ Aplicações: \end{tabular} \lstinputlisting{fontes/kuhn.h} -\subsection{Empar. máximo bipartido -- Hopcroft} -\lstinputlisting{fontes/hopcroft.h} - \subsection{Decomposição raiz quadrada de árvore} -\lstinputlisting{fontes/stdt.h} +\lstinputlisting{fontes/tree-square-root-decomposition.h} \subsection{Menor anc. comum -- Dec. raiz quad.} -\lstinputlisting{fontes/stdtlca.h} +\lstinputlisting{fontes/lca-square-root-decomposition.h} \section{Matemática} \subsection{Operações comuns} \lstinputlisting{fontes/numbers.h} +\subsection{Inteiro modular} +\lstinputlisting{fontes/modular-integer.h} + \subsection{Matriz} \lstinputlisting{fontes/matrix.h} @@ -490,10 +486,10 @@ A probabilidade do evento $X$ acontecer $k$ vezes é dada por \begin{align*} Pr(X = k) = \binom{n}{k}p^k (1 - p)^{n-k} \end{align*} -\lstinputlisting{fontes/binom.h} +\lstinputlisting{fontes/binomial-distribution.h} \subsection{Integração pelo método de Simpson} -\lstinputlisting{fontes/integr.h} +\lstinputlisting{fontes/simpson-integration.h} \subsection{Sequência de Bruijn} Número de sequências distintas: @@ -502,21 +498,21 @@ Número de sequências distintas: \end{align*} \lstinputlisting{fontes/bruijn.h} -\subsection{Division trick} +\subsection{Truque de divisão} Gera um conjunto $n/i$ para todo $i$ em $\bigO(\sqrt{n})$. -\lstinputlisting{fontes/divtrick.h} +\lstinputlisting{fontes/division-trick.h} + +\subsection{PD de Dígito} +\lstinputlisting{fontes/digit-dp.h} \subsection{Compressão de coordenadas} -\lstinputlisting{fontes/coordmap.h} +\lstinputlisting{fontes/coordinate-compression.h} \subsection{Problema de Josephus} \lstinputlisting{fontes/josephus.h} -\subsection{Primeiros e últimos digitos de $n^k$} -\lstinputlisting{fontes/nk-first-last-digits.h} - -\subsection{Triângulo de Pascal} -\lstinputlisting{fontes/pascal-triangle.h} +\subsection{Primeiros digitos de $n^k$} +\lstinputlisting{fontes/nk-first-digits.h} \subsection{2-SAT} \lstinputlisting{fontes/2-sat.h} @@ -547,7 +543,7 @@ $\bigO(\lg \min(a, b))$ \\ ax &= gcd(a, b) \pmod{b} \\ (\gcd(a, b) = 1) &\implies (ax = 1 \pmod{b}) \end{align*} -\lstinputlisting{fontes/extgcd.h} +\lstinputlisting{fontes/extended-euclidean.h} \subsection{Detecção de ciclo} \lstinputlisting{fontes/floyd.h} @@ -585,22 +581,6 @@ ax + by = c \end{tabular} \lstinputlisting{fontes/sieve.h} -\subsection{Crivo de número de divisores} -\begin{tabular}{l} - \hline - $\bigO(n \lg n)$ \\ -\hline -\end{tabular} -\lstinputlisting{fontes/divisors.h} - -\subsection{Crivo de soma dos divisores} -\begin{tabular}{l} - \hline - $\bigO(n \lg n)$ \\ -\hline -\end{tabular} -\lstinputlisting{fontes/sumdivsieve.h} - \subsection{Totiente de Euler} \begin{tabular}{l} \hline @@ -609,30 +589,6 @@ ax + by = c \end{tabular} \lstinputlisting{fontes/euler-totient.h} -\subsection{Crivo de soma dos divisores} -\begin{tabular}{l} - \hline - $\bigO(n \lg n)$ \\ -\hline -\end{tabular} -\lstinputlisting{fontes/sumdivsieve.h} - -\subsection{Crivo de maior divisor primo} -\begin{tabular}{l} - \hline - $\bigO(n \lg n)$ \\ -\hline -\end{tabular} -\lstinputlisting{fontes/bigpsieve.h} - -\subsection{Crivo linear} -\begin{tabular}{l} - \hline - $\bigO(n)$ \\ -\hline -\end{tabular} -\lstinputlisting{fontes/linearsieve.h} - \subsection{Eliminação gaussiana} \lstinputlisting{fontes/gauss.h} @@ -648,7 +604,7 @@ ax + by = c $\bigO(n^{1/4})$ \\ \hline \end{tabular} -\lstinputlisting{fontes/pollardrho.h} +\lstinputlisting{fontes/pollard-rho.h} \subsection{Teorema chinês do resto} \begin{tabular}{l} @@ -656,7 +612,7 @@ ax + by = c $\bigO(t \lg(m_1 m_2 \ldots m_t))$ \\ \hline \end{tabular} -\lstinputlisting{fontes/crt.h} +\lstinputlisting{fontes/chinese-remainder-theorem.h} \subsection{Teorema chinês do resto generalizado} \begin{tabular}{l|l} @@ -665,7 +621,7 @@ ax + by = c Infinitas soluções $\pm \text{lcm}(m_1m_2\ldots m_t)$ \\ \hline \end{tabular} -\lstinputlisting{fontes/noncoprimecrt.h} +\lstinputlisting{fontes/generalized-chinese-remainder-theorem.h} \section{Strings} @@ -686,9 +642,6 @@ $z[i]$ é o tamanho da maior string que é ao mesmo tempo, um prefixo de $s$ e u \subsection{String Hashing} \lstinputlisting{fontes/string-hash.h} -\subsection{Rabin-Karp} -\lstinputlisting{fontes/rabinkarp.h} - \subsection{Autômato KMP} \lstinputlisting{fontes/kmp-automaton.h} @@ -696,27 +649,25 @@ $z[i]$ é o tamanho da maior string que é ao mesmo tempo, um prefixo de $s$ e u \lstinputlisting{fontes/trie.h} \subsection{Aho-Corasick} -\lstinputlisting{fontes/acs.h} +\lstinputlisting{fontes/aho-corasick.h} \subsection{Árvore de sufixos} -\lstinputlisting{fontes/st.h} +\lstinputlisting{fontes/suffix-tree.h} -\clearpage \subsection{Vetor de sufixos} -\lstinputlisting{fontes/sa.h} +\lstinputlisting{fontes/suffix-array.h} \subsection{Vetor de sufixos radix} -\lstinputlisting{fontes/sar.h} +\lstinputlisting{fontes/suffix-array-radix.h} \subsection{Autômato de Sufixo} -\lstinputlisting{fontes/saut.h} +\lstinputlisting{fontes/suffix-automaton.h} \section{Geometria} \subsection{Pontos} \lstinputlisting{fontes/point.h} -\clearpage \subsection{Fecho convexo com corrente monotônica} \begin{tabular}{l} \hline @@ -734,35 +685,34 @@ $z[i]$ é o tamanho da maior string que é ao mesmo tempo, um prefixo de $s$ e u \lstinputlisting{fontes/graham.h} \subsection{Ponto dentro do polígono?} -\lstinputlisting{fontes/isinsidepoly.h} +\lstinputlisting{fontes/point-in-polygon.h} \subsection{Ponto dentro do polígono convexo?} -\lstinputlisting{fontes/isinsidepolyconvex.h} +\lstinputlisting{fontes/point-in-convex-polygon.h} \subsection{Área de polígono} -\lstinputlisting{fontes/polyarea.h} +\lstinputlisting{fontes/polygon-area.h} \subsection{Pontos inteiros no polígono} \begin{align*} A = i + b/2 - 1 \end{align*} -\lstinputlisting{fontes/latticepoints.h} +\lstinputlisting{fontes/polygon-lattice-points.h} \subsection{Segmento} -\lstinputlisting{fontes/seg.h} +\lstinputlisting{fontes/segment.h} \subsection{Ponto e segmento} -\lstinputlisting{fontes/pointseg.h} +\lstinputlisting{fontes/point-in-seg.h} \subsection{Varredura angular} \lstinputlisting{fontes/angular-sweep.h} -\clearpage \subsection{Interseção de retângulos} \lstinputlisting{fontes/rectangle-intersection.h} \subsection{Segmento e segmento} -\lstinputlisting{fontes/segseg.h} +\lstinputlisting{fontes/segment-vs-segment.h} \subsection{Árvore KD} \lstinputlisting{fontes/kd-tree.h} @@ -773,13 +723,13 @@ A = i + b/2 - 1 \section{Algoritmos} \subsection{Ordenação por fusão} -\lstinputlisting{fontes/mergesort.h} +\lstinputlisting{fontes/merge-sort.h} \subsection{Subconjuntos de tamanho $K$} -\lstinputlisting{fontes/combination.h} +\lstinputlisting{fontes/next-combination.h} \subsection{Agendamento ótimo de tarefas} -\lstinputlisting{fontes/jobs.h} +\lstinputlisting{fontes/optimal-schedule-jobs.h} \subsection{Soma sobre subconjuntos} \begin{align*} @@ -789,68 +739,46 @@ Para mudar para superconjunto, use \texttt{~mask} no if. \lstinputlisting{fontes/sos.h} \subsection{Algoritmo de Mo} -\lstinputlisting{fontes/mo.h} +\lstinputlisting{fontes/mo-algorithm.h} \subsection{Meet in the middle} -\lstinputlisting{fontes/meet.h} - -\subsection{Maior subvetor com soma $s$} -\lstinputlisting{fontes/subsum.h} +\lstinputlisting{fontes/meet-in-the-middle.h} \subsection{Maior subsequência comum} -\lstinputlisting{fontes/lcs.h} - -\subsection{Maior subsequência crescente} -\lstinputlisting{fontes/lis.h} +\lstinputlisting{fontes/longest-common-subsequence.h} \subsection{Maior subsequência crescente rápido} -\lstinputlisting{fontes/lisnlgn.h} +\lstinputlisting{fontes/longest-increasing-subsequence-n-lg-n.h} -\subsection{Busca binária} -\lstinputlisting{fontes/bs.h} - -\clearpage \subsection{PD de perfil quebrado} \lstinputlisting{fontes/parquet.h} -\subsection{Máscaras de bit e operações em bit} -\lstinputlisting{fontes/bitmask.h} - -\subsection{Busca binária diferentona} -\lstinputlisting{fontes/binarystep.h} +\subsection{Submáscaras} +\lstinputlisting{fontes/submasks.h} \section{Problemas} \subsection{Rainhas no tabuleiro} \lstinputlisting{fontes/queens.h} -\subsection{Moda usando algoritmo de Mo} -\lstinputlisting{fontes/mode.h} +\subsection{Frequência de frequências/Moda usando Mo} +\lstinputlisting{fontes/freq-freq-mode.h} \subsection{Inversões usando algoritmo de Mo} -\lstinputlisting{fontes/inversionmo.h} - -\subsection{Remoção de valores com lista} -\lstinputlisting{fontes/remove.h} +\lstinputlisting{fontes/inversion-mo.h} \subsection{Remoção de valores com deque} -\lstinputlisting{fontes/removedeque.h} +\lstinputlisting{fontes/remove-deque.h} \subsection{Desembarcadouro} \lstinputlisting{fontes/desembarcadouro.h} \subsection{Média e mediana} -\lstinputlisting{fontes/midmed.h} +\lstinputlisting{fontes/mean-median.h} \subsection{Distância de edição} \lstinputlisting{fontes/edit-distance.h} -\subsection{Frequência de frequências} -\lstinputlisting{fontes/freq-freq.h} - \subsection{Código de Gray} -\lstinputlisting{fontes/gray.h} - -\subsection{Entrada rápida} -\lstinputlisting{fontes/fastio.h} +\lstinputlisting{fontes/gray-code.h} \end{document} diff --git a/fontes/acs.h b/fontes/aho-corasick.h similarity index 100% rename from fontes/acs.h rename to fontes/aho-corasick.h diff --git a/fontes/articbridges.h b/fontes/articulations-bridges.h similarity index 100% rename from fontes/articbridges.h rename to fontes/articulations-bridges.h diff --git a/fontes/bfs01.h b/fontes/bfs-01.h similarity index 100% rename from fontes/bfs01.h rename to fontes/bfs-01.h diff --git a/fontes/bigpsieve.h b/fontes/bigpsieve.h deleted file mode 100644 index a45acfe4ed6a7773169a99645d9ed56b38ec268d..0000000000000000000000000000000000000000 --- a/fontes/bigpsieve.h +++ /dev/null @@ -1,7 +0,0 @@ -vector<int> big (N+1, 1); -void biggest_prime_divisor(int n) { - for (int i = 1; i <= n; ++i) - if (big[i] == 1) - for (int j = i; j <= n; j += i) - big[j] = i; -} diff --git a/fontes/bl.h b/fontes/binary-lifting.h similarity index 96% rename from fontes/bl.h rename to fontes/binary-lifting.h index b8f2093beb90b28074202e5af9ac54ce7dc82024..8818dd1ef0f3518973071ae01092b10870211f59 100644 --- a/fontes/bl.h +++ b/fontes/binary-lifting.h @@ -11,7 +11,7 @@ void bl_euler_tour(int u, int p, int w) { } void bl_init(int u, int n) { - dep[u] = 0; bl_euler_tour(u, u, 0); + dep[u] = -1; bl_euler_tour(u, u, 0); // can be put into bl_euler_tour but it will not // work with cycles (could be useful for online) for (int l = 0; l < L; l++) diff --git a/fontes/binarystep.h b/fontes/binarystep.h deleted file mode 100644 index 0aa69e916baba9e58683c125e70d2824d9b9001a..0000000000000000000000000000000000000000 --- a/fontes/binarystep.h +++ /dev/null @@ -1,9 +0,0 @@ -int count(vector<int>& v, int x) { - int n = v.size(); - int l = n, r = n; - for (int p = n; p >= 1; p /= 2) { - while (l-p >= 0 && !(v[l-p] < 2)) { l -= p; } - while (r-p >= 0 && !(v[r-p] <= 2)) { r -= p; } - } - return r - l; -} diff --git a/fontes/binom.h b/fontes/binomial-distribution.h similarity index 100% rename from fontes/binom.h rename to fontes/binomial-distribution.h diff --git a/fontes/bs.h b/fontes/bs.h deleted file mode 100644 index 46ebc2907e44286c2415e70761870dfac2165803..0000000000000000000000000000000000000000 --- a/fontes/bs.h +++ /dev/null @@ -1,9 +0,0 @@ -ll binary_search(ll lo, ll hi) { - while (lo < hi) { - ll mid = lo + (hi-lo) / 2; - if (P(mid)) { hi = mid; } - else { lo = mid + 1; } - } - if (!P(lo)) { /* P(x) is false for all x in S */ } - return lo; -} diff --git a/fontes/crt.h b/fontes/chinese-remainder-theorem.h similarity index 100% rename from fontes/crt.h rename to fontes/chinese-remainder-theorem.h diff --git a/fontes/coordmap.h b/fontes/coordinate-compression.h similarity index 100% rename from fontes/coordmap.h rename to fontes/coordinate-compression.h diff --git a/fontes/desembarcadouro.h b/fontes/desembarcadouro.h index 09e73c6932d50ebf9cbf45d21c758616007fa5f4..f2f008322f90997b359f584e7665d110daf6a304 100644 --- a/fontes/desembarcadouro.h +++ b/fontes/desembarcadouro.h @@ -1,12 +1,12 @@ const int K = 300; -vector<int> containers (100001); -vector<vector<int>> dp (100001 + K, vector<int>(K)); +vector<int> c (1e5+15); +vector<vector<int>> dp (1e5+15 + K, vector<int>(K)); int main() { int n, q; cin >> n >> q; while (q--) { int a, l, d; cin >> a >> l >> d; if (d >= K) - for (int i = 0; i < l; i++) { containers[a + i * d]++; } + for (int i = 0; i < l; i++) { c[a + i * d]++; } else { dp[a][d]++; dp[a + (l * d)][d]--; } } for (int j = 1; j < K; j++) @@ -14,7 +14,7 @@ int main() { dp[i][j] += dp[i - j][j]; for (int i = 1; i <= n; i++) { for (int j = 1; j < K; j++) - containers[i] += dp[i][j]; - cout << containers[i] << ' '; + c[i] += dp[i][j]; + cout << c[i] << ' '; } } diff --git a/fontes/digit-dp.h b/fontes/digit-dp.h new file mode 100644 index 0000000000000000000000000000000000000000..40ed98bef64c75ebc1aa843202f1e3c8b02fc4fa --- /dev/null +++ b/fontes/digit-dp.h @@ -0,0 +1,18 @@ +string num; +vector<vector<ll>> mem (20, vector<ll>(12, -1)); +ll digit_dp(int i, int l, bool lo, bool nz) { + if (i == -1) { return 1; } + if (lo && nz && mem[i][l] != -1) { return mem[i][l]; } + int lim = lo ? 9 : num[i]-'0'; + ll ans = 0; + for (int d = 0; d <= lim; d++) + if (d != l || (!d && !l && !nz)) { + ans += digit_dp(i-1, d, lo || d < lim, nz || d); + } + return (lo && nz ? mem[i][l] = ans : ans); +} + +ll solve(ll b) { + num = to_string(b); reverse(begin(num), end(num)); + return digit_dp(num.size()-1, 10, 0, 0); +} diff --git a/fontes/dinic.h b/fontes/dinic.h new file mode 100644 index 0000000000000000000000000000000000000000..2bda29f9b2376c1a3e65e4ec7c331e8e7b7b0882 --- /dev/null +++ b/fontes/dinic.h @@ -0,0 +1,55 @@ +struct edge { int u, v; ll cap, flow; }; +vector<edge> edges; +const ll oo = 1e18; +vector<vector<int>> res (N); +vector<int> level (N), ptr (N); +queue<int> q; + +void link(int u, int v, ll cap) { + res[u].push_back(edges.size()); + edges.push_back({ u, v, cap, 0 }); + res[v].push_back(edges.size()); + edges.push_back({ v, u, 0, 0 }); +} + +bool minimum_path(int s, int t) { + while (!q.empty()) { + int u = q.front(); q.pop(); + for (int i : res[u]) { + if (edges[i].cap - edges[i].flow < 1) { continue; } + if (level[edges[i].v] != -1) { continue; } + level[edges[i].v] = level[u] + 1; + q.push(edges[i].v); + } + } + return level[t] != -1; +} + +ll dfs(int u, int t, ll pushed) { + if (pushed == 0) { return 0; } + if (u == t) { return pushed; } + for (int& cid = ptr[u]; cid < res[u].size(); cid++) { + int i = res[u][cid]; + int v = edges[i].v; + if (level[u] + 1 != level[v]) { continue; } + if (edges[i].cap - edges[i].flow < 1) { continue; } + ll tr = dfs(v, t, min(pushed, edges[i].cap - edges[i].flow)); + if (tr == 0) { continue; } + edges[i].flow += tr; + edges[i^1].flow -= tr; + return tr; + } + return 0; +} + +ll dinic /* O(EV^2) */(int s, int t) { + ll max_flow = 0; + while (1) { + fill(begin(level), end(level), -1); + level[s] = 0; q.push(s); + if (!minimum_path(s, t)) { break; } + fill(begin(ptr), end(ptr), 0); + while (ll pushed = dfs(s, t, oo)) { max_flow += pushed; } + } + return max_flow; +} diff --git a/fontes/disjoint.h b/fontes/disjoint.h index 877e533aeb07b961286840f99bdaa59e3ef6ecee..fda3eb40ae7a2f150153aa28a6f4ebb8beb9996a 100644 --- a/fontes/disjoint.h +++ b/fontes/disjoint.h @@ -15,7 +15,7 @@ void ds_unite(int u, int v) { } void ds_init(int n) { - for (int u = 0; u < n; u++) { - rep[u] = u; rnk[u] = 0; siz[u] = 1; - } + for (int u = 0; u < n; u++) { + rep[u] = u; rnk[u] = 0; siz[u] = 1; + } } diff --git a/fontes/divtrick.h b/fontes/division-trick.h similarity index 100% rename from fontes/divtrick.h rename to fontes/division-trick.h diff --git a/fontes/divisors.h b/fontes/divisors.h deleted file mode 100644 index 45d4ed83f1d2a0dae39a3f6941f41998daea2e4f..0000000000000000000000000000000000000000 --- a/fontes/divisors.h +++ /dev/null @@ -1,6 +0,0 @@ -vector<int> divisors (N); -void number_of_divisors(int n) { - for (int i = 1; i <= n; ++i) - for (int j = i; j <= n; j += i) - divisors[j]++; -} diff --git a/fontes/edit-distance.h b/fontes/edit-distance.h index 7fb89626e78310f26dec2b52d2370f79078aac3c..e16fcac7ee6ee5bc7b8db72449b1407d41945f18 100644 --- a/fontes/edit-distance.h +++ b/fontes/edit-distance.h @@ -1,4 +1,4 @@ -void edit_distance(string s1, s2) { +void edit_distance(string s1, string s2) { int n1 = s1.size(), n2 = s2.size(); vector<vector<int>> dist (n1+1, vector<int>(n2+1)); vector<vector<int>> op (n1+1, vector<int>(n2+1)); @@ -12,7 +12,6 @@ void edit_distance(string s1, s2) { for (int j = 1; j <= n2; j++) { int bdist, bop; if (s1[i-1] == s2[j-1]) { - // Skip bdist = dist[i-1][j-1]; bop = 0; } else { int repl = dist[i-1][j-1] + 1; diff --git a/fontes/extgcd.h b/fontes/extended-euclidean.h similarity index 100% rename from fontes/extgcd.h rename to fontes/extended-euclidean.h diff --git a/fontes/fastio.h b/fontes/fastio.h deleted file mode 100644 index 71381e9c5ad85c62d8bfdc084a30dd4f20a4640f..0000000000000000000000000000000000000000 --- a/fontes/fastio.h +++ /dev/null @@ -1,13 +0,0 @@ -int read_int() { - bool minus = 0; int result = 0; - char ch; - do { ch = getchar(); } - while (!(ch == '-' || (ch >= '0' && ch <= '9'))); - if (ch == '-') { minus = 1; } else { result = ch-'0'; } - while (1) { - ch = getchar(); - if (ch < '0' || ch > '9') break; - result = result*10 + (ch - '0'); - } - return minus ? -result : result; -} diff --git a/fontes/ffekmincost.h b/fontes/ford-fulkerson-edmons-karp-min-cost.h similarity index 100% rename from fontes/ffekmincost.h rename to fontes/ford-fulkerson-edmons-karp-min-cost.h diff --git a/fontes/ffek.h b/fontes/ford-fulkerson-edmons-karp.h similarity index 95% rename from fontes/ffek.h rename to fontes/ford-fulkerson-edmons-karp.h index 1131688cd9aae1f4cb8c5cb038ffc211cac0ae6a..a811d8a63348cc028578b171c2d4661bb4e3327f 100644 --- a/fontes/ffek.h +++ b/fontes/ford-fulkerson-edmons-karp.h @@ -26,7 +26,7 @@ bool minimum_path(int s, int t) { return dist[t] < oo; } -pair<int, int> ffek(int s, int t) { +pair<int, int> ffek /* O(VE^2) */(int s, int t) { int min_cost = 0, max_flow = 0; while (minimum_path(s, t)) { int flow = oo; diff --git a/fontes/freq-freq-mode.h b/fontes/freq-freq-mode.h new file mode 100644 index 0000000000000000000000000000000000000000..8237e8b5616662da9c3b056d5e3ebd9aa39b7949 --- /dev/null +++ b/fontes/freq-freq-mode.h @@ -0,0 +1,15 @@ +vector<int> v (N), freqfreq (N), freq (N); +vector<ll> sumfreq (N); +int ans = 0; +void update_freq(int x, int d) { + freqfreq[freq[x]]--; + if (freq[x]) { sumfreq[freq[x]] -= x; } + freq[x] += d; + if (freq[x]) { sumfreq[freq[x]] += x; } + freqfreq[freq[x]]++; + if (freqfreq[ans+1]) { ans++; } + if (ans > 0 && freqfreq[ans] == 0) { ans--; } +} +void ins(int i, char dir) { update_freq(v[i], +1); } +void rem(int i, char dir) { update_freq(v[i], -1); } +int get_ans() { return ans; } diff --git a/fontes/freq-freq.h b/fontes/freq-freq.h deleted file mode 100644 index 5d03eeeec7688c85bb7bb273eadd33a6b852d233..0000000000000000000000000000000000000000 --- a/fontes/freq-freq.h +++ /dev/null @@ -1,11 +0,0 @@ -vector<int> cnt (C), freqfreq (N); -vector<ll> sumfreq (N); -int mf = 0; - -freqfreq[cnt[w]]--; -if (cnt[w]) { sumfreq[cnt[w]] -= w; } -cnt[w] += x; -if (cnt[w]) { sumfreq[cnt[w]] += w; } -freqfreq[cnt[w]]++; -if (freqfreq[mf+1]) { mf++; } -if (mf > 0 && freqfreq[mf] == 0) { mf--; } diff --git a/fontes/gauss.h b/fontes/gauss.h index 0b129602ebef9f235fe5c7981f80194fc658ccba..b33a322477e813fb8daaa8beb49c0b3df8f4b73d 100644 --- a/fontes/gauss.h +++ b/fontes/gauss.h @@ -1,4 +1,3 @@ - template<typename T> pair<int, vector<T>> gauss(vector<vector<T>> a, vector<T> b) { const double eps = 1e-6; diff --git a/fontes/noncoprimecrt.h b/fontes/generalized-chinese-remainder-theorem.h similarity index 100% rename from fontes/noncoprimecrt.h rename to fontes/generalized-chinese-remainder-theorem.h diff --git a/fontes/gray.h b/fontes/gray-code.h similarity index 51% rename from fontes/gray.h rename to fontes/gray-code.h index 3ecd167a3b2d9e38a79108626130a1d413d6d092..7d5d7e7734e81ba5305fdf3d705563f7010f7095 100644 --- a/fontes/gray.h +++ b/fontes/gray-code.h @@ -1,6 +1,6 @@ vector<int> gray_code(int n) { - vector<int> ret(1<<n); + vector<int> ans (1<<n); for (int i = 0; i < (1<<n); i++) - ret[i] = i^(i>>1); - return ret; + ans[i] = i^(i>>1); + return ans; } diff --git a/fontes/hash.sh b/fontes/hash.sh new file mode 100644 index 0000000000000000000000000000000000000000..0e1e9cb496b50acaa9c51db50a720a5d78d443c5 --- /dev/null +++ b/fontes/hash.sh @@ -0,0 +1,2 @@ +sed -n $2','$3' p' $1 | sed '/^#w/d' | cpp -dD -P \ + -fpreprocessed | tr -d '[:space:]' | md5sum | cut -c -6 diff --git a/fontes/hld-kth.h b/fontes/hld-kth.h new file mode 100644 index 0000000000000000000000000000000000000000..c3814bbd56d35269ae25532f744a539ffcfede90 --- /dev/null +++ b/fontes/hld-kth.h @@ -0,0 +1,39 @@ +int hld_len(int u, int v) { + int ans = 0; + for (; hds[u] != hds[v]; v = par[hds[v]]) { + if (dep[hds[u]] > dep[hds[v]]) { swap(u, v); } + ans += ixs[v] - ixs[hds[v]] + 1; + } + if (dep[u] > dep[v]) { swap(u, v); } + return ans + ixs[v] - ixs[u] + 1; +} + +int hld_kth(int u, int v, int k) { + bool sw = 0; int l = 0, r = hld_len(u, v)-1; + for (; hds[u] != hds[v]; v = par[hds[v]]) { + if (dep[hds[u]] > dep[hds[v]]) { swap(u, v); sw ^= 1; } + int sz = ixs[v]-ixs[hds[v]]+1; + int i = sw ? k-l : r-k; + if (0 <= i && i < sz) { return rixs[ixs[v]-i]; } + if (sw) { l += sz; } else { r -= sz; } + } + if (dep[u] > dep[v]) { swap(u, v); sw ^= 1; } + int i = sw ? k-l : r-k; + return rixs[ixs[v]-i]; +} + +vector<int> hld_path(int u, int v, int k) { + int len = hld_len(u, v); vector<int> path (len); + bool sw = 0; int l = 0, r = len-1; + for (; hds[u] != hds[v]; v = par[hds[v]]) { + if (dep[hds[u]] > dep[hds[v]]) { swap(u, v); sw ^= 1; } + for (int i = ixs[v]; i >= ixs[hds[v]]; i--) { + path[sw ? l++ : r--] = rixs[i]; + } + } + if (dep[u] > dep[v]) { swap(u, v); sw ^= 1; } + for (int i = ixs[v]; i >= ixs[u]; i--) { + path[sw ? l++ : r--] = rixs[i]; + } + return path; +} diff --git a/fontes/hopcroft.h b/fontes/hopcroft.h deleted file mode 100644 index da76bb88c76f96257f1148cb13fbdd5bc77e720c..0000000000000000000000000000000000000000 --- a/fontes/hopcroft.h +++ /dev/null @@ -1,41 +0,0 @@ -vector<vector<int>> g (L); -vector<int> matchl (L, -1), matchr (R, -1); -vector<int> dist (L); - -bool bfs(int l) { - queue<int> Q; - for (int l = 0; l < L; l++) - if (matchl[l] == -1) { dist[l] = 0; Q.push(l); } - else { dist[l] = -1; } - bool ans = false; - while (!Q.empty()) { - int l = Q.front(); Q.pop(); - for (auto r : g[l]) - if (matchr[r] == -1) { - ans = true; - } else if (dist[matchr[r]] == -1) { - dist[matchr[r]] = dist[l] + 1; - Q.push(matchr[r]); - } - } - return ans; -} - -bool dfs(int l) { - if (l == -1) { return true; } - for (auto r : g[l]) - if (matchr[r] == -1 || dist[matchr[r]] == dist[l] + 1) - if (dfs(matchr[r])) { - matchr[r] = l; - matchl[l] = r; - return true; - } - return false; -} - -int hopcroft(int ln) { - int ans = 0; - while (bfs(ln)) for (int l = 0; l < ln; l++) - if (matchl[l] == -1 && dfs(l)) { ans++; } - return ans; -} diff --git a/fontes/inversionmo.h b/fontes/inversion-mo.h similarity index 100% rename from fontes/inversionmo.h rename to fontes/inversion-mo.h diff --git a/fontes/josephus.h b/fontes/josephus.h index a52bd9e9835b27239f20949e0db1a77f34658519..89062e672ccd2b2f083d5291342eb0e918675852 100644 --- a/fontes/josephus.h +++ b/fontes/josephus.h @@ -1,11 +1,18 @@ -int rec(int n, int k) { return (rec(n-1, k) + k) % n; } - -int klogn(int n, int k) { +int josephusrec(int n, int k) { + return n ? (josephusrec(n-1, k) + k) % n : 0; +} +int josephus /* O(n) */ (int n, int k) { + int res = 0; + for (int i = 1; i <= n; ++i) + res = (res + k) % i; + return res+1; +} +int josephusklgn /* O(k log n) */ (int n, int k) { if (n == 1) { return 0; } if (k == 1) { return n-1; } - if (k > n) { return (klogn(n-1, k) + k) % n; } + if (k > n) { return (josephusklgn(n-1, k) + k) % n; } int cnt = n / k; - int res = klogn(n - cnt, k); + int res = josephusklgn(n - cnt, k); res -= n % k; if (res < 0) { res += n; } else { res += res / (k - 1); } diff --git a/fontes/lstit.h b/fontes/lazy-segment-tree.h similarity index 93% rename from fontes/lstit.h rename to fontes/lazy-segment-tree.h index 205959c2b99df0e1a15f7c960dddb0ff0e417ada..3c66786719a0acd25d9ddd4068b8d16e46c4bfb8 100644 --- a/fontes/lstit.h +++ b/fontes/lazy-segment-tree.h @@ -4,7 +4,7 @@ vector<ll> t (2*N); vector<dlta> delta (2*N); void build(vector<int>& src, int n) { for (int i = 1; i < src.size(); i++) - t[N+i] = src[i]; + t[n+i] = src[i]; for (int ti = n-1; ti > 0; ti--) t[ti] = OP(t[2*ti], t[2*ti+1]); } @@ -53,8 +53,8 @@ void apply_inclusive(int l, int r, int n, pull(tl); pull(tr); } -void add_inclusive(int l, int r, ll d) { - apply_inclusive(l, r, '+', d); +void add_inclusive(int l, int r, int n, ll d) { + apply_inclusive(l, r, n, '+', d); } ll op_inclusive(int l, int r, int n) { diff --git a/fontes/bllca.h b/fontes/lca-binary-lifting.h similarity index 100% rename from fontes/bllca.h rename to fontes/lca-binary-lifting.h diff --git a/fontes/stdtlca.h b/fontes/lca-square-root-decomposition.h similarity index 100% rename from fontes/stdtlca.h rename to fontes/lca-square-root-decomposition.h diff --git a/fontes/linearsieve.h b/fontes/linearsieve.h deleted file mode 100644 index 0f69a1d399c6869a10f660ac620e3af8583a2f7d..0000000000000000000000000000000000000000 --- a/fontes/linearsieve.h +++ /dev/null @@ -1,9 +0,0 @@ -vector<int> pr, lp (1e7+15); -void eulersieve(int n) { - for (int i = 2; i <= n; i++) { - if (lp[i] == 0) { lp[i] = i; pr.push_back(i); } - for (int j = 0; j < int(pr.size()) - && pr[j] <= lp[i] && i*pr[j] <= n; j++) - lp[i * pr[j]] = pr[j]; - } -} diff --git a/fontes/lis.h b/fontes/lis.h deleted file mode 100644 index 105686802da562f27bc1ed769b63aeb41e79a26f..0000000000000000000000000000000000000000 --- a/fontes/lis.h +++ /dev/null @@ -1,8 +0,0 @@ -vector<int> dp (N, 1); - -int lis(vector<int>& v) { - for (int i = 0; i < v.size(); i++) - for (int j = 0; j < i; j++) if (v[j] < v[i]) - dp[i] = max(dp[i], 1 + dp[j]); - return *max_element(dp.begin(), dp.end()); -} diff --git a/fontes/lisnlgn.h b/fontes/lisnlgn.h deleted file mode 100644 index 8c4f4d684b10f79021f0d63f9690563da96b3391..0000000000000000000000000000000000000000 --- a/fontes/lisnlgn.h +++ /dev/null @@ -1,15 +0,0 @@ -vector<int> dp (N, oo); - -int lis(vector<int>& v) { - int n = v.size(); - dp[0] = -oo; - for (int i = 0; i < n; i++) { - int j = upper_bound(dp.begin(), - dp.begin()+n+1, v[i]) - dp.begin(); - if (dp[j-1] < v[i] && v[i] < dp[j]) - dp[j] = v[i]; - } - for (int i = 0; i <= n; i++) if (dp[i] == oo) - dp[i] = -oo; - return max_element(dp.begin(), dp.begin()+n+1) - dp.begin(); -} diff --git a/fontes/lcs.h b/fontes/longest-common-subsequence.h similarity index 54% rename from fontes/lcs.h rename to fontes/longest-common-subsequence.h index 7158d1477554034a2ab9e1cde725117c6a04c8a2..573c42a6d9fe1517b5cae979d1b4531199eaa3d3 100644 --- a/fontes/lcs.h +++ b/fontes/longest-common-subsequence.h @@ -1,14 +1,14 @@ int lcs(string s, string t) { int m = s.size(), n = t.size(); - vector<vector<int>> L (m+1, vector<int>(n+1)); + vector<vector<int>> l (m+1, vector<int>(n+1)); for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) - L[i][j] = 0; + l[i][j] = 0; else if (s[i - 1] == t[j - 1]) - L[i][j] = L[i - 1][j - 1] + 1; + l[i][j] = l[i-1][j-1] + 1; else - L[i][j] = max(L[i - 1][j], L[i][j - 1]); + l[i][j] = max(l[i-1][j], l[i][j-1]); } - return L[m][n]; + return l[m][n]; } diff --git a/fontes/longest-increasing-subsequence-n-lg-n.h b/fontes/longest-increasing-subsequence-n-lg-n.h new file mode 100644 index 0000000000000000000000000000000000000000..f7c6375d71d9a444c1b56da70d29faf34f4c91f0 --- /dev/null +++ b/fontes/longest-increasing-subsequence-n-lg-n.h @@ -0,0 +1,12 @@ +int lis(vector<int>& v) { + int n = v.size(); + vector<int> ans; + ans.push_back(v[0]); + for (int i = 1; i < n; i++) if (v[i] > ans.back()) { + ans.push_back(v[i]); + } else { + ans[lower_bound(begin(ans), end(ans), v[i]) + - begin(ans)] = v[i]; + } + return ans.size(); +} diff --git a/fontes/matrix.h b/fontes/matrix.h index 1d450cef376654c44496ca4f8973edbd8c1b4e98..196fa0f308a815073b6fcab7522a3a6f36a2ef22 100644 --- a/fontes/matrix.h +++ b/fontes/matrix.h @@ -1,25 +1,14 @@ -struct matrix { - ll m[D][D]; - matrix(bool identify) { - memset(m, 0, sizeof(m)); - for (int i = 0; i < D; i++) { m[i][i] = identify; } - } - matrix(vector<vector<ll>> mat) { - for (int i = 0; i < D; i++) +using matrix = vector<vector<ll>>; +matrix identity_matrix() { + matrix m (D, vector<ll>(D, 0)); + for (int i = 0; i < D; i++) { m[i][i] = 1; } + return m; +} +matrix multiply(matrix a, matrix b) { + matrix ans (D, vector<ll>(D)); + for (int i = 0; i < D; i++) for (int j = 0; j < D; j++) - m[i][j] = mat[i][j]; - } - ll* operator[](int pos){ - return m[pos]; - } - matrix operator*(matrix oth){ - matrix ans (0); - for (int i = 0; i < D; i++) - for (int j = 0; j < D; j++) { - ll &sum = ans[i][j]; for (int k = 0; k < D; k++) - sum = (sum + (m[i][k] * oth[k][j]) % M) % M; - } - return ans; - } -}; + ans[i][j] += a[i][k] * b[k][j]; + return ans; +} diff --git a/fontes/mean-median.h b/fontes/mean-median.h new file mode 100644 index 0000000000000000000000000000000000000000..fe0a4dee007142e91f97736842b20b9689c0cf55 --- /dev/null +++ b/fontes/mean-median.h @@ -0,0 +1,35 @@ +double solve(double x, vector<double>& v) { + int n = v.size(); + vector<double> mem (n); + for (int i = n-1; i >= 0; i--) { + double ans = max(mem[i+1], mem[i+2]); + mem[i] = v[i] + ans; + } + return max(mem[0], mem[1]); +} + +void get_mean_med(vector<int> a) { + double mean; { + double lo = 0, hi = 1e10; + for (int it = 0; it < 100; it++) { + double mi = (lo+hi)/2; + vector<double> b (n); + for (int i = 0; i < n; i++) { b[i] = a[i] - mi; } + if (solve(mi, b) >= 0) { lo = mi; } else { hi = mi; } + } + mean = lo; + } + double med; { + double lo = 0, hi = 1e10; + for (int it = 0; it < 100; it++) { + double mi = (lo+hi)/2; + vector<double> b (n); + for (int i = 0; i < n; i++) { + b[i] = (a[i] >= mi) ? +1 : -1; + } + if (solve(mi, b) > 0) { lo = mi; } else { hi = mi; } + } + med = lo; + } + return make_pair(mean, med); +} diff --git a/fontes/meet.h b/fontes/meet-in-the-middle.h similarity index 100% rename from fontes/meet.h rename to fontes/meet-in-the-middle.h diff --git a/fontes/mergesort.h b/fontes/merge-sort.h similarity index 100% rename from fontes/mergesort.h rename to fontes/merge-sort.h diff --git a/fontes/midmed.h b/fontes/midmed.h deleted file mode 100644 index 1e1772babb958982029778cbe688fffd7aca22d9..0000000000000000000000000000000000000000 --- a/fontes/midmed.h +++ /dev/null @@ -1,34 +0,0 @@ -double solve(double x, vector<double>& v) { - int n = v.size(); - vector<double> mem (n); - for (int i = n-1; i >= 0; i--) { - double ans = max(mem[i+1], mem[i+2]); - mem[i] = v[i] + ans; - } - return max(mem[0], mem[1]); -} - -double mid; -{ - double lo = 0, hi = 1e10; - for (int it = 0; it < 100; it++) { - double mi = (lo+hi)/2; - vector<double> b (n); - for (int i = 0; i < n; i++) { b[i] = a[i] - mi; } - if (solve(mi, b) >= 0) { lo = mi; } else { hi = mi; } - } - mid = lo; -} -double med; -{ - double lo = 0, hi = 1e10; - for (int it = 0; it < 100; it++) { - double mi = (lo+hi)/2; - vector<double> b (n); - for (int i = 0; i < n; i++) { - b[i] = (a[i] >= mi) ? +1 : -1; - } - if (solve(mi, b) > 0) { lo = mi; } else { hi = mi; } - } - med = lo; -} diff --git a/fontes/min-queue-2.h b/fontes/min-queue-2.h index c39e23c3d7aa12da3207ca8409cc6d0232b1c721..7e95f6b41a179c990d6cae53bd0b553cc3251138 100644 --- a/fontes/min-queue-2.h +++ b/fontes/min-queue-2.h @@ -1,11 +1,11 @@ deque<pair<int, int>> q; int add = 0, rem = 0; -int enqueue_el(int x) { +void enqueue_el(int x) { while (!q.empty() && q.back().first > x) q.pop_back(); q.push_back({ x, add++ }); } -int dequeue_el() { +void dequeue_el() { if (!q.empty() && q.front().second == rem) q.pop_front(); rem++; diff --git a/fontes/min-queue-3.h b/fontes/min-queue-3.h index ec94647d0daa436a60b494ac226098093e9371ff..1c0fd81e5c21023657cca97939dca3d5087c4f2a 100644 --- a/fontes/min-queue-3.h +++ b/fontes/min-queue-3.h @@ -1,6 +1,6 @@ stack<pair<int, int>> s1, s2; -int enqueue_el(int x) { +void enqueue_el(int x) { int m = min(x, s1.size() ? s1.top().second : oo); s1.push({ x, m }); } diff --git a/fontes/min-queue.h b/fontes/min-queue.h index ee57f1c2551ca5579e56ee1f7620679b3f1de9b5..913599310ca63e8bcd7841cefd4929d539f7d15a 100644 --- a/fontes/min-queue.h +++ b/fontes/min-queue.h @@ -1,10 +1,10 @@ deque<int> q; -int enqueue_el(int x) { +void enqueue_el(int x) { while (!q.empty() && q.back() > x) q.pop_back(); q.push_back(x); } -int dequeue_el(int x) { +void dequeue_el(int x) { if (!q.empty() && q.front() == x) q.pop_front(); } diff --git a/fontes/mo.h b/fontes/mo-algorithm.h similarity index 89% rename from fontes/mo.h rename to fontes/mo-algorithm.h index 51d41feade984ca69588460eaf7f8d3e2dcb1e65..6bae8ad32a91eeed1260f5041b09e396626af69f 100644 --- a/fontes/mo.h +++ b/fontes/mo-algorithm.h @@ -2,7 +2,7 @@ vector<int> mo(vector<qry> qs) { vector<int> ans (qs.size()); sort(qs.begin(), qs.end(), [](qry a, qry b) { if (a.l/B != b.l/B) { return a.l < b.l; } - return (a.l/B) % 2 ? a.r < b.r : a.r > b.r; + return ((a.l/B) % 2 == 0) ^ (a.r < b.r); }); int l = 0, r = -1; for (qry q : qs) { diff --git a/fontes/mode.h b/fontes/mode.h deleted file mode 100644 index ed9fa540cc2f03f5a16d7024aa93f839520859ee..0000000000000000000000000000000000000000 --- a/fontes/mode.h +++ /dev/null @@ -1,15 +0,0 @@ -vector<int> v (N), freqfreq (N), freq (N); -int ans = 0; -void ins(int i, char dir) { - freqfreq[freq[v[i]]]--; - freq[v[i]]++; - freqfreq[freq[v[i]]]++; - if (freqfreq[ans+1]) { ans++; } -} -void rem(int i, char dir) { - freqfreq[freq[v[i]]]--; - freq[v[i]]--; - freqfreq[freq[v[i]]]++; - if (ans > 0 && freqfreq[ans] == 0) { ans--; } -} -int get_ans() { return ans; } diff --git a/fontes/modular-integer.h b/fontes/modular-integer.h new file mode 100644 index 0000000000000000000000000000000000000000..a6276a837526d2d6d099a8eaa8dfbfe4016ea6dd --- /dev/null +++ b/fontes/modular-integer.h @@ -0,0 +1,18 @@ +template<int p> struct modint { + int x; + modint() : x(0) {} + modint(ll a) : x(a) { + if (!(-p < x && x < p)) x %= p; + if (x < 0) x += p; + } + using m = modint; +#define DEF(n, b) m& n(const m& a) { b; return *this; } + DEF(operator+=, { x += a.x; if (x >= p) { x -= p; } }) + DEF(operator-=, { x -= a.x; if (x < 0) { x += p; } }) + DEF(operator*=, { x = x * ll(a.x) % p; }) + bool operator==(const m& a) { return x == a.x; } + bool operator!=(const m& a) { return x != a.x; } + friend m operator+(m a, m b) { return a += b; } + friend m operator-(m a, m b) { return a -= b; } + friend m operator*(m a, m b) { return a *= b; } +}; diff --git a/fontes/combination.h b/fontes/next-combination.h similarity index 100% rename from fontes/combination.h rename to fontes/next-combination.h diff --git a/fontes/nge.h b/fontes/next-greater-element.h similarity index 100% rename from fontes/nge.h rename to fontes/next-greater-element.h diff --git a/fontes/nle.h b/fontes/next-lesser-element.h similarity index 100% rename from fontes/nle.h rename to fontes/next-lesser-element.h diff --git a/fontes/nk-first-last-digits.h b/fontes/nk-first-digits.h similarity index 55% rename from fontes/nk-first-last-digits.h rename to fontes/nk-first-digits.h index 6070efb3b2bdf20cb692182a403bb84e00a3bd2a..7c3613474c18540668fd66c92d253b3f89487bc9 100644 --- a/fontes/nk-first-last-digits.h +++ b/fontes/nk-first-digits.h @@ -3,8 +3,3 @@ int first_d_digits(int n, int k, int d) { logged -= int(logged) - (d-1); return int(pow(10, logged)); } - -int last_d_digits(int n, int k, int d) { - int p = 1; while (d--) { p *= 10; } - return fast_pow(n, k, p); -} diff --git a/fontes/node.h b/fontes/node.h deleted file mode 100644 index c1428284b70088d8c7b705cd787b80d01ce93f8e..0000000000000000000000000000000000000000 --- a/fontes/node.h +++ /dev/null @@ -1,6 +0,0 @@ -struct node { - array<int, 9> data {{ 0 }}; - node(); explicit node(int x); - node operator+(const node& a) { node n; return n; }; - node operator+=(const int& a) { return *this; }; -}; diff --git a/fontes/jobs.h b/fontes/optimal-schedule-jobs.h similarity index 100% rename from fontes/jobs.h rename to fontes/optimal-schedule-jobs.h diff --git a/fontes/parquet.h b/fontes/parquet.h index d349155f0d759647516873b93b405f9ebdad42c4..22494cf7ce30730e251aa657dae3e60072edb6e4 100644 --- a/fontes/parquet.h +++ b/fontes/parquet.h @@ -7,7 +7,7 @@ void calc(int x = 0, int y = 0, int s = 0, int nxts = 0) { if (s & cs) { calc(x, y+1, s, nxts); } else { calc(x, y+1, s, nxts | cs); - if (y+1 < m && ! (s & cs) && ! (s & (cs << 1))) + if (y+1 < m && !(s & cs) && !(s & (cs << 1))) calc(x, y+2, s, nxts); } } diff --git a/fontes/pascal-triangle.h b/fontes/pascal-triangle.h deleted file mode 100644 index 13b9ed5056385c90c70a62f81c8cceb7cb9ea8d8..0000000000000000000000000000000000000000 --- a/fontes/pascal-triangle.h +++ /dev/null @@ -1,10 +0,0 @@ -vector<vector<ll>> binom (51, vector<ll>(51)); - -void pascal() { - for (int n = 0; n <= 50; n++) { - binom[n][0] = binom[n][n] = 1; - for (int k = 1; k <= n; k++) { - binom[n][k] = binom[n-1][k-1] + binom[n-1][k]; - } - } -} diff --git a/fontes/isinsidepolyconvex.h b/fontes/point-in-convex-polygon.h similarity index 100% rename from fontes/isinsidepolyconvex.h rename to fontes/point-in-convex-polygon.h diff --git a/fontes/isinsidepoly.h b/fontes/point-in-polygon.h similarity index 100% rename from fontes/isinsidepoly.h rename to fontes/point-in-polygon.h diff --git a/fontes/pointseg.h b/fontes/point-in-seg.h similarity index 100% rename from fontes/pointseg.h rename to fontes/point-in-seg.h diff --git a/fontes/pollardrho.h b/fontes/pollard-rho.h similarity index 100% rename from fontes/pollardrho.h rename to fontes/pollard-rho.h diff --git a/fontes/polyarea.h b/fontes/polygon-area.h similarity index 100% rename from fontes/polyarea.h rename to fontes/polygon-area.h diff --git a/fontes/latticepoints.h b/fontes/polygon-lattice-points.h similarity index 100% rename from fontes/latticepoints.h rename to fontes/polygon-lattice-points.h diff --git a/fontes/prefix-sum.h b/fontes/prefix-sum.h deleted file mode 100644 index 646f5144566cd520712eb409e1055f2785716ca3..0000000000000000000000000000000000000000 --- a/fontes/prefix-sum.h +++ /dev/null @@ -1,9 +0,0 @@ -vector<ll> ps (N+1); - -void build(vector<ll>& origin) { - partial_sum(origin.begin(), origin.end(), ps.begin()+1); -} - -ll sum_inclusive(int a, int b) { - return ps[b+1] - ps[a]; -} diff --git a/fontes/pge.h b/fontes/previous-greater-element.h similarity index 100% rename from fontes/pge.h rename to fontes/previous-greater-element.h diff --git a/fontes/ple.h b/fontes/previous-lesser-element.h similarity index 100% rename from fontes/ple.h rename to fontes/previous-lesser-element.h diff --git a/fontes/rabinkarp.h b/fontes/rabinkarp.h deleted file mode 100644 index cca88e3ceef30a779c2dd9f97ba511107e95f4a0..0000000000000000000000000000000000000000 --- a/fontes/rabinkarp.h +++ /dev/null @@ -1,18 +0,0 @@ -const int P = 31; const int M = 1e9 + 9; -int search(string hay, string ne) { - int n = hay.size(), m = ne.size(); - vector<ll> ps (max(n, m), 1); - for (int i = 1; i < max(n, m); i++) - ps[i] = (ps[i-1] * P) % M; - vector<ll> h (n+1, 0); - for (int i = 0; i < n; i++) - h[i+1] = (h[i] + (hay[i]-'a'+1) * ps[i]) % M; - ll hne = 0; - for (int i = 0; i < m; i++) - hne = (hne + (ne[i]-'a'+1) * ps[i]) % M; - int c = 0; - for (int i = 0; i < n - m + 1; i++) - if ((h[i+m] - h[i] + M) % M == hne * ps[i] % M) - /* match at i */ c++; - return c; -} diff --git a/fontes/lstrec.h b/fontes/recursive-lazy-segment-tree.h similarity index 100% rename from fontes/lstrec.h rename to fontes/recursive-lazy-segment-tree.h diff --git a/fontes/strec.h b/fontes/recursive-segment-tree.h similarity index 100% rename from fontes/strec.h rename to fontes/recursive-segment-tree.h diff --git a/fontes/removedeque.h b/fontes/remove-deque.h similarity index 100% rename from fontes/removedeque.h rename to fontes/remove-deque.h diff --git a/fontes/remove.h b/fontes/remove.h deleted file mode 100644 index 84d9ef568152358349d0f2fd3bfb84da25b066f9..0000000000000000000000000000000000000000 --- a/fontes/remove.h +++ /dev/null @@ -1,20 +0,0 @@ -list<int> l; vector<int> bsz (N/B + 1); -vector<list<int>::iterator> bbegin (N/B + 1); -vector<int> remove(vector<int>& v, vector<int>& r) { - vector<int> ans; - int n = v.size(); - for (int i = 0; i < n; i++) { - auto ins = l.insert(l.end(), v[i]); - if (i % B == 0) { bbegin[i/B] = ins; } - bsz[i/B]++; - } - for (int i : r) { - auto it = bbegin[i/B]; advance(it, i%B); - if (i % B == 0) { bbegin[i/B]++; } - ans.push_back(*it); l.erase(it); - int bi = i/B; - while (bsz[bi+1] > 0) { bbegin[bi+1]++; bi++; } - bsz[bi]--; - } - return ans; -} diff --git a/fontes/stit.h b/fontes/segment-tree.h similarity index 96% rename from fontes/stit.h rename to fontes/segment-tree.h index aebf9b9d5fb149e2aeec3ae773c29c1fc6bef5ae..1ed1d74754dd2c0ae6d903a26b7fd57cc6b5fb11 100644 --- a/fontes/stit.h +++ b/fontes/segment-tree.h @@ -2,7 +2,7 @@ vector<int> t (2*N); void build(vector<int>& src, int n) { for (int i = 1; i < src.size(); i++) - t[N+i] = src[i]; + t[n+i] = src[i]; for (int i = n-1; i > 0; i--) t[i] = OP(t[2*i], t[2*i+1]); } diff --git a/fontes/segseg.h b/fontes/segment-vs-segment.h similarity index 100% rename from fontes/segseg.h rename to fontes/segment-vs-segment.h diff --git a/fontes/seg.h b/fontes/segment.h similarity index 100% rename from fontes/seg.h rename to fontes/segment.h diff --git a/fontes/simple-hash.h b/fontes/simple-hash.h deleted file mode 100644 index 43bfc1ecdf7107aaf078c722a89ee52014c10994..0000000000000000000000000000000000000000 --- a/fontes/simple-hash.h +++ /dev/null @@ -1,17 +0,0 @@ -const int HS = 10000; int cts = 0; -vector<ll> key (HS), value (HS), ts (HS); - -void hash_put(ll k, ll v) { - ll pos = k % HS; - while (ts[pos] == cts) { pos++; pos %= HS; } - ts[pos] = cts; key[pos] = k; value[pos] = v; -} - -ll hash_get(ll k) { - ll pos = k % HS; - while (ts[pos] == cts && key[pos] != k) { - pos++; pos %= HS; } - if (ts[pos] == cts && key[pos] == k) - return value[pos]; - return -1; -} diff --git a/fontes/integr.h b/fontes/simpson-integration.h similarity index 100% rename from fontes/integr.h rename to fontes/simpson-integration.h diff --git a/fontes/stdtop.h b/fontes/stdtop.h deleted file mode 100644 index 71cfbf9ff1c420448f7bb3770facae58dec2d4da..0000000000000000000000000000000000000000 --- a/fontes/stdtop.h +++ /dev/null @@ -1,14 +0,0 @@ -int stdt_op(int a, int b) { - int ans = NEUTRAL; - if (!(depth[a]/B > depth[b]/B)) { swap(a, b); } - while (depth[a]/B > depth[b]/B) { - ans = OP(ans, bweiop[a]); a = bup[a]; } - if (!(depth[a] > depth[b])) { swap(a, b); } - while (depth[a] > depth[b]) { - ans = OP(ans, weiop[a]); a = up[a]; } - while (a != b) { - ans = OP(ans, OP(weiop[a], weiop[b])); - a = up[a]; b = up[b]; - } - return ans; -} diff --git a/fontes/string-hash.h b/fontes/string-hash.h index ad01c5b2875b4dd6c07a93c70138a7d1c67ff997..dda3ec6b78a67cbb4d9dc05e9954398a154e30db 100644 --- a/fontes/string-hash.h +++ b/fontes/string-hash.h @@ -3,10 +3,10 @@ struct str_hash { str_hash(string s) : h(s.size()), p(s.size()) { p[0] = 1, h[0] = s[0]; for (int i = 1; i < s.size(); i++) - p[i] = p[i - 1]*P%M, h[i] = (h[i - 1]*P + s[i])%M; + p[i] = p[i-1]*P%M, h[i] = (h[i-1]*P + s[i])%M; } ll operator()(int l, int r) { // retorna hash s[l...r] - ll hash = h[r] - (l ? h[l - 1]*p[r - l + 1]%M : 0); + ll hash = h[r] - (l ? h[l-1]*p[r-l+1]%M : 0); return hash < 0 ? hash + M : hash; } }; @@ -16,7 +16,7 @@ struct node { node() : h(1), sz(0) {} explicit node(char _c) : sz(1), c(_c), h(_c-'a'+1) {} node operator+ (node o) { - node a; a.h = (h + (o.h*psp[sz])%M) % M; + node a; a.h = (h + (o.h*p[sz])%M) % M; a.sz = sz + o.sz; return a; } diff --git a/fontes/bitmask.h b/fontes/submasks.h similarity index 100% rename from fontes/bitmask.h rename to fontes/submasks.h diff --git a/fontes/subsum.h b/fontes/subsum.h deleted file mode 100644 index 675e4b8c27fded7e423cbb3179fd98773b33c6cc..0000000000000000000000000000000000000000 --- a/fontes/subsum.h +++ /dev/null @@ -1,10 +0,0 @@ -ii subsum(vector<int>& v, int s) { - int n = v.size(); - int i = 0, j = 1, w = v[0]; - while (j < n) { - while (w + v[j] > s && i < j-1) { w -= v[i++]; } - if (w < s) { w += v[j++]; } - if (w == s) { return {i, j}; } - } - return {-1,-1}; -} diff --git a/fontes/sar.h b/fontes/suffix-array-radix.h similarity index 100% rename from fontes/sar.h rename to fontes/suffix-array-radix.h diff --git a/fontes/sa.h b/fontes/suffix-array.h similarity index 100% rename from fontes/sa.h rename to fontes/suffix-array.h diff --git a/fontes/saut.h b/fontes/suffix-automaton.h similarity index 100% rename from fontes/saut.h rename to fontes/suffix-automaton.h diff --git a/fontes/st.h b/fontes/suffix-tree.h similarity index 100% rename from fontes/st.h rename to fontes/suffix-tree.h diff --git a/fontes/sumdivsieve.h b/fontes/sumdivsieve.h deleted file mode 100644 index 453d6560f2609ba0758d672be224b90efc5c581f..0000000000000000000000000000000000000000 --- a/fontes/sumdivsieve.h +++ /dev/null @@ -1,6 +0,0 @@ -vector<int> sumdiv (N); -void sum_of_divisors(int n) { - for (int i = 1; i <= n; ++i) - for (int j = i; j <= n; j += i) - sumdiv[j] += i; -} diff --git a/fontes/topo.h b/fontes/topological-order.h similarity index 100% rename from fontes/topo.h rename to fontes/topological-order.h diff --git a/fontes/stdt.h b/fontes/tree-square-root-decomposition.h similarity index 100% rename from fontes/stdt.h rename to fontes/tree-square-root-decomposition.h