Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create spoj-cot.mdx (solution for the problem I added about persisten… #5073

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
fe06370
Create spoj-cot.mdx (solution for the problem I added about persisten…
1Nigar357 Jan 22, 2025
53a37c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 22, 2025
9a8a775
Update spoj-cot.mdx
1Nigar357 Jan 22, 2025
997b2e1
Update spoj-cot.mdx
TheGamingMousse Jan 22, 2025
daddec5
Update Persistent.problems.json (change the kind in the solution meta…
1Nigar357 Jan 23, 2025
0ddcbd2
Update solutions/advanced/spoj-cot.mdx
1Nigar357 Jan 23, 2025
b872dea
Update spoj-cot.mdx
1Nigar357 Jan 23, 2025
1dc48cf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2025
0b80073
Update spoj-cot.mdx (Modified the explanation part and organized the …
1Nigar357 Jan 23, 2025
6ffd62c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2025
3a0ace6
Update spoj-cot.mdx
1Nigar357 Jan 23, 2025
0114d34
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2025
43ecc8c
Update spoj-cot.mdx
1Nigar357 Jan 23, 2025
9878da2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 23, 2025
74478a6
Update solutions/advanced/spoj-cot.mdx
1Nigar357 Jan 23, 2025
cb7950b
Update solutions/advanced/spoj-cot.mdx
1Nigar357 Jan 23, 2025
ea05116
Update spoj-cot.mdx
1Nigar357 Jan 23, 2025
8933218
Update spoj-cot.mdx
1Nigar357 Jan 24, 2025
5f43b1e
Update spoj-cot.mdx
1Nigar357 Jan 24, 2025
70012c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2025
c205dd5
Create example1.png
1Nigar357 Jan 24, 2025
9032de1
Add files via upload
1Nigar357 Jan 24, 2025
46c28cc
Rename Screenshot 2025-01-24 at 1.39.50 AM.png to example.png
1Nigar357 Jan 24, 2025
af2d2e0
Delete solutions/advanced/spoj-cot/example1.png
1Nigar357 Jan 24, 2025
5427b2f
Update spoj-cot.mdx (Added a picture)
1Nigar357 Jan 24, 2025
37d8141
Update spoj-cot.mdx
1Nigar357 Jan 24, 2025
95b3933
Update spoj-cot.mdx (added \texttt)
1Nigar357 Jan 28, 2025
8c6ba59
Update spoj-cot.mdx
1Nigar357 Jan 28, 2025
64139d4
Update spoj-cot.mdx
1Nigar357 Jan 28, 2025
d13cdc7
Update spoj-cot.mdx
1Nigar357 Jan 28, 2025
f700aea
Update spoj-cot.mdx
1Nigar357 Jan 28, 2025
c60fda1
Update spoj-cot.mdx
1Nigar357 Jan 29, 2025
c65e0ea
Update spoj-cot.mdx
1Nigar357 Jan 29, 2025
a956b3b
Update spoj-cot.mdx ("Wrapped the elements of formula inside "\texttt"")
1Nigar357 Jan 29, 2025
34e2eee
Update spoj-cot.mdx (Modified the code)
1Nigar357 Feb 1, 2025
122943a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 1, 2025
336dc27
Update spoj-cot.mdx (Decreased the number of nodes)
1Nigar357 Feb 1, 2025
05e5cf9
Update spoj-cot.mdx (updated the solution code)
1Nigar357 Feb 1, 2025
fdb6e90
Update spoj-cot.mdx
1Nigar357 Feb 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion content/6_Advanced/Persistent.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"isStarred": true,
"tags": ["Persistent Segtree"],
"solutionMetadata": {
"kind": "none"
"kind": "internal"
}
},
{
Expand Down
152 changes: 152 additions & 0 deletions solutions/advanced/spoj-cot.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
id: spoj-cot
source: SPOJ
title: Count on a tree
author: Nigar Hajiyeva
---
## Explanation
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

This code solves the "Count on a Tree" problem using a persistent segment tree. It efficiently handles queries on a tree to find the k-th smallest value in the path between two nodes.

**Key steps:**

Persistent Segment Tree Construction: Builds a segment tree where each update creates a new version, allowing access to historical states.

DFS for Tree Traversal: Computes entry/exit times (tin/tout) and initializes the segment tree for each node.

LCA Calculation: Uses binary lifting to find the Lowest Common Ancestor (LCA) of two nodes.

Value Compression: Compresses the node values to handle large numbers efficiently, making them suitable for indexing in the segment tree.


**Time Complexity:** $\mathcal{O}((N+M) \cdot \log{N})$
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

## Implementation
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

<LanguageSection>
<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vi vector<ll>
#define pb push_back
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
#define INF 100000000000

const int MAX = 2e5 + 10, MAX_NODES = 7e6, LOG = 20;
int L[MAX_NODES + 1], R[MAX_NODES + 1], t[MAX_NODES + 1], trs[MAX + 1],
up[MAX + 1][LOG + 1];
int tin[MAX + 1], tout[MAX + 1], val[MAX + 1];
vi g[MAX + 1];
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
ll nxt = 0, timer = 0;
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

int build(int v, int l, int r) {
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
int nw = ++nxt;
if (l == r) { return nw; }
int m = (l + r) / 2;

L[v] = build(nw, l, m);
R[v] = build(nw, m + 1, r);
return nw;
}

ll update(int v, int l, int r, int pos) {
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
int nw = ++nxt;

if (l == r) {
t[nw] += t[v] + 1;
return nw;
}

L[nw] = L[v];
R[nw] = R[v];
int m = (l + r) / 2;

if (pos <= m) L[nw] = update(L[v], l, m, pos);
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
else R[nw] = update(R[v], m + 1, r, pos);

t[nw] = t[L[nw]] + t[R[nw]];
return nw;
}

void dfs(int from, int p) {
int root = trs[p];
root = update(root, 1, MAX, val[from]);

trs[from] = root;
tin[from] = ++timer;

if (from != 1) up[from][0] = p;
else up[from][0] = from;

for (int i = 1; i <= LOG - 1; i++) { up[from][i] = up[up[from][i - 1]][i - 1]; }
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

for (int to : g[from]) {
if (to == p) continue;
dfs(to, from);
}
tout[from] = timer;
}

bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }

int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = LOG - 1; i >= 0; i--) {
if (is_ancestor(up[u][i], v)) continue;
u = up[u][i];
}
return up[u][0];
}

ll query(int a, int b, int anc, int pr, int l, int r, int k) {
if (l == r) { return l; }
int m = (l + r) / 2;
ll cnt = t[L[a]] + t[L[b]] - t[L[anc]] - t[L[pr]];
if (cnt >= k) return query(L[a], L[b], L[anc], L[pr], l, m, k);
else return query(R[a], R[b], R[anc], R[pr], m + 1, r, k - cnt);
}

int main() {
// input variables
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
int n, m;
cin >> n >> m;
vi compr;
compr.pb(-INF);
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 1; i <= n; i++) {
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
cin >> val[i];
compr.pb(val[i]);
}

sort(compr.begin(), compr.end());
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
compr.resize(unique(compr.begin(), compr.end()) - compr.begin());

for (int i = 1; i <= n; i++) {
val[i] = (lower_bound(compr.begin(), compr.end(), val[i]) - compr.begin());
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
}

int a, b, k, common, pr;
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 1; i < n; i++) {
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}

trs[0] = build(nxt, 1, MAX);
dfs(1, 0);

for (int i = 0; i < m; i++) {
cin >> a >> b >> k;
common = lca(a, b);
pr = up[common][0];
if (common == 1) pr = 0;
cout << compr[query(trs[a], trs[b], trs[common], trs[pr], 1, MAX, k)] << endl;
1Nigar357 marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

</CPPSection>
</LanguageSection>
Loading