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

solution for spoj-cot #5073

Open
wants to merge 72 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
Show all changes
72 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
cced12a
Update spoj-cot.mdx
1Nigar357 Feb 24, 2025
2454592
Update spoj-cot.mdx (Cleaned up the code added struct)
1Nigar357 Feb 25, 2025
7cab838
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 25, 2025
c77a058
Update spoj-cot.mdx (Added comments and braces for all if statements)
1Nigar357 Feb 28, 2025
1b64a7c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 28, 2025
5bef6e9
Update spoj-cot.mdx
1Nigar357 Feb 28, 2025
5485d44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 28, 2025
2c8061d
Update spoj-cot.mdx
1Nigar357 Mar 2, 2025
3a465f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 2, 2025
81defd9
Update spoj-cot.mdx
1Nigar357 Mar 3, 2025
f8279fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 3, 2025
a5a3eef
Update spoj-cot.mdx
1Nigar357 Mar 3, 2025
0d44000
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 3, 2025
ce16b84
Update spoj-cot.mdx
1Nigar357 Mar 3, 2025
2bb3ecf
Merge branch 'master' into patch-1
ryanchou-dev Mar 5, 2025
57448d6
Update spoj-cot.mdx
1Nigar357 Mar 5, 2025
32f1361
Update spoj-cot.mdx
1Nigar357 Mar 7, 2025
513239b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 7, 2025
556bed8
Update spoj-cot.mdx
1Nigar357 Mar 8, 2025
c003723
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 8, 2025
24f1358
Update spoj-cot.mdx
1Nigar357 Mar 8, 2025
5377a7b
Update spoj-cot.mdx
1Nigar357 Mar 8, 2025
18193fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 8, 2025
f3096a5
Update spoj-cot.mdx
1Nigar357 Mar 8, 2025
1419165
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 8, 2025
a3d064f
Update spoj-cot.mdx
1Nigar357 Mar 8, 2025
5506bd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 8, 2025
cc7e401
Update spoj-cot.mdx
1Nigar357 Mar 9, 2025
5f9ac1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 9, 2025
1ef8387
Update spoj-cot.mdx
1Nigar357 Mar 9, 2025
0322022
Update spoj-cot.mdx
1Nigar357 Mar 9, 2025
e9a7bac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 9, 2025
aeacb0b
Update spoj-cot.mdx
1Nigar357 Mar 9, 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
136 changes: 136 additions & 0 deletions solutions/advanced/spoj-cot.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
id: spoj-cot
source: SPOJ
title: Count on a tree
author: Nigar Hajiyeva
---

## Implementation

<LanguageSection>
<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define vi vector<ll>
#define pb push_back
#define endl '\n'
#define INF 100000000000

const int MAX = 2e5 + 10, mxi = 7e6, LOG = 20;
ll L[mxi + 1], R[mxi + 1], t[mxi + 1], trs[MAX + 1], up[MAX + 1][LOG + 1];
ll tin[MAX + 1], tout[MAX + 1], val[MAX + 1];
vi g[MAX + 1];
ll nxt = 0, timer = 0;

int build(int v, int l, int r) {
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) {
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);
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]; }

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() {
int n, m, a, b, k, common, pr;
cin >> n >> m;
vi compr;
compr.pb(-INF);

for (int i = 1; i <= n; i++) {
cin >> val[i];
compr.pb(val[i]);
}

sort(compr.begin(), compr.end());
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());
}

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;
}
}
```

</CPPSection>
</LanguageSection>
Loading