Skip to content

Commit

Permalink
添加了若干内容,修复了若干内容
Browse files Browse the repository at this point in the history
  • Loading branch information
lr580 committed Jun 5, 2024
1 parent 0c8839a commit 0ecfcf8
Show file tree
Hide file tree
Showing 5 changed files with 591 additions and 12 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**当前最新普通版发布版本为 `v1.2.0`****最新打印版发布版本为 `v1.2.0` (总词数约10.0w(含代码))**
**当前最新普通版发布版本为 `v1.2.2`****最新打印版发布版本为 `v1.2.0` (总词数约10.0w(含代码))**

成品为 `template.pdf` (移步 [releases](https://github.com/lr580/algorithm_template/releases) 查看/下载)

Expand Down Expand Up @@ -293,15 +293,31 @@ lr580's 算法模板

## 更新日志

- 23/10-27 - 24/06/05 (`v1.2.2`)

- 添加了动态开点线段树模板

- 微加了位运算语法应用
- 添加了暴力 LCA
- 微修了 Dijkstra 最短路应用例子表述错误、朴素法代码等
- 添加了调试输出版本的语法
- 添加了枚举组合 Gosper's Hack 模板
- 修正了整数三分模板
- 增加了树上 k 级祖先倍增法
- 增加了背包方案数模板
- 修正了 Miller Rabin 素性测试复杂度
- 微加了差分约束内容
- 微修了 STL vector 语法表述错误

- 23/10/22 - 23/10/26 (`v1.2.1`)

- 重制了匹配问题等目录排版

- 微加了 STL 内容
- 添加了树状数组上倍增、线段树上二分、pb_ds 哈希表
- 修正了整数三分模板
- 添加了立体计算几何公式

- 23/10/19 - 23/10/21 (`v1.2.0`)

- 添加了快速矩阵前 n 项和模板
Expand Down
38 changes: 38 additions & 0 deletions code/gospers_hack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <vector>

// Function to generate the next subset
unsigned int nextSubset(unsigned int subset) {
unsigned int u = subset & -subset;
unsigned int v = subset + u;
return v | (((v ^ subset) / u) >> 2);
}

// Function to generate all k-subsets of a set of size n
void gospersHack(int k, int n) {
unsigned int subset = (1 << k) - 1;
unsigned int limit = (1 << n);

while (subset < limit) {
// Process the subset here
// For example, printing it out
for (int i = 0; i < n; ++i) {
if (subset & (1 << i)) {
std::cout << i + 1 << " ";
}
}
std::cout << std::endl;

subset = nextSubset(subset);
}
}

int main() {
int k = 3; // Size of subsets
int n = 5; // Size of the set

std::cout << "All " << k << "-subsets of a " << n << "-element set:\n";
gospersHack(k, n);

return 0;
}
97 changes: 97 additions & 0 deletions code/lca_tarjan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

class Edge {
public:
int toVertex, fromVertex;
int next;
int LCA;
Edge() : toVertex(-1), fromVertex(-1), next(-1), LCA(-1){};
Edge(int u, int v, int n) : fromVertex(u), toVertex(v), next(n), LCA(-1){};
};

const int MAX = 100;
int head[MAX], queryHead[MAX];
Edge edge[MAX], queryEdge[MAX];
int parent[MAX], visited[MAX];
int vertexCount, queryCount;

void init() {
for (int i = 0; i <= vertexCount; i++) {
parent[i] = i;
}
}

int find(int x) {
if (parent[x] == x) {
return x;
} else {
return find(parent[x]);
}
}

void tarjan(int u) {
parent[u] = u;
visited[u] = 1;

for (int i = head[u]; i != -1; i = edge[i].next) {
Edge& e = edge[i];
if (!visited[e.toVertex]) {
tarjan(e.toVertex);
parent[e.toVertex] = u;
}
}

for (int i = queryHead[u]; i != -1; i = queryEdge[i].next) {
Edge& e = queryEdge[i];
if (visited[e.toVertex]) {
queryEdge[i ^ 1].LCA = e.LCA = find(e.toVertex);
}
}
}

int main() {
memset(head, 0xff, sizeof(head));
memset(queryHead, 0xff, sizeof(queryHead));

cin >> vertexCount >> queryCount;
int count = 0;
for (int i = 0; i < vertexCount - 1; i++) {
int start = 0, end = 0;
cin >> start >> end;

edge[count] = Edge(start, end, head[start]);
head[start] = count;
count++;

edge[count] = Edge(end, start, head[end]);
head[end] = count;
count++;
}

count = 0;
for (int i = 0; i < queryCount; i++) {
int start = 0, end = 0;
cin >> start >> end;

queryEdge[count] = Edge(start, end, queryHead[start]);
queryHead[start] = count;
count++;

queryEdge[count] = Edge(end, start, queryHead[end]);
queryHead[end] = count;
count++;
}

init();
tarjan(1);

for (int i = 0; i < queryCount; i++) {
Edge& e = queryEdge[i * 2];
cout << "(" << e.fromVertex << "," << e.toVertex << ") " << e.LCA << endl;
}

return 0;
}
102 changes: 102 additions & 0 deletions code/segment_tree_lazy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <bits/stdc++.h> // https://leetcode.cn/problems/range-module
using namespace std;
using ll = long long;
class RangeModule
{
constexpr static ll inf = 1e9;
constexpr static ll maxn = 60 * 1e4; // log(inf)*q
struct node
{
ll ls, rs, sum, laz;
} t[maxn] = {};
int cnt = 1;
#define cll const ll &
#define mkcf ll cf = (lf + rf) >> 1
void pushdown(int r, cll lf, cll rf)
{
if (!t[r].ls)
t[r].ls = ++cnt;
if (!t[r].rs)
t[r].rs = ++cnt;
if (!t[r].laz)
return;
if (t[r].laz == 1)
{
mkcf;
t[t[r].ls].sum = (cf - lf + 1); // len-len/2
t[t[r].rs].sum = rf - cf; // len/2
}
else
{
t[t[r].ls].sum = t[t[r].rs].sum = 0;
}
t[t[r].ls].laz = t[t[r].rs].laz = t[r].laz;
t[r].laz = 0;
}
void pushup(int r)
{
t[r].sum = t[t[r].ls].sum + t[t[r].rs].sum;
}
void update(int r, ll lf, ll rf, cll lc, cll rc, cll v)
{
if (lc <= lf && rf <= rc)
{
t[r].sum += (v == 1) * (rf - lf + 1);
t[r].laz = v;
return;
}
pushdown(r, lf, rf);
mkcf;
if (lc <= cf)
update(t[r].ls, lf, cf, lc, rc, v);
if (rc >= cf + 1)
update(t[r].rs, cf + 1, rf, lc, rc, v);
pushup(r);
}
ll query(int r, ll lf, ll rf, cll lc, cll rc)
{
if (lc <= lf && rf <= rc)
return t[r].sum;
pushdown(r, lf, rf);
ll res = 0;
mkcf;
if (lc <= cf)
res += query(t[r].ls, lf, cf, lc, rc);
if (rc >= cf + 1)
res += query(t[r].rs, cf + 1, rf, lc, rc);
return res;
}

public:
RangeModule() {}

void addRange(int left, int right)
{
update(1, 1, inf, left, right - 1, 1);
}

bool queryRange(int left, int right)
{
return query(1, 1, inf, left, right - 1) == right - left;
}

void removeRange(int left, int right)
{
update(1, 1, inf, left, right - 1, -1);
}
};

signed main()
{
ios::sync_with_stdio(false), cin.tie(0);

return 0;
}

/**
* Your RangeModule object will be instantiated and called as such:
* RangeModule* obj = new RangeModule();
* obj->addRange(left,right);
* bool param_2 = obj->queryRange(left,right);
* obj->removeRange(left,right);
*/
Loading

0 comments on commit 0ecfcf8

Please sign in to comment.