-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
591 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |
Oops, something went wrong.