Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 930173a

Browse files
another problem
1 parent cc66612 commit 930173a

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

‎tree/2-path-queries-iterative.cpp‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
5+
const int maxn = 2e5+5;
6+
7+
int n, q, tmr, a[maxn], tour[maxn], tin[maxn], dep[maxn], par[maxn], head[maxn], sz[maxn], st[4*maxn];
8+
vector<int> vs[maxn];
9+
10+
void dfs(int v) {
11+
sz[v] = 1;
12+
for(int i = 0, e; i<int(vs[v].size()); i++) {
13+
e = vs[v][i];
14+
if (e == par[v]) {
15+
swap(vs[v][i--], vs[v].back());
16+
vs[v].pop_back();
17+
continue;
18+
}
19+
par[e] = v;
20+
dep[e] = dep[v] + 1;
21+
dfs(e);
22+
sz[v] += sz[e];
23+
if (sz[e] > sz[vs[v][0]]) swap(vs[v][i], vs[v][0]);
24+
}
25+
}
26+
27+
void hld(int v) {
28+
tour[tmr] = a[v];
29+
tin[v] = tmr++;
30+
for(int e : vs[v]) {
31+
head[e] = e == vs[v][0] ? head[v] : e;
32+
hld(e);
33+
}
34+
}
35+
36+
//Time limit is very tight for recursive segment tree
37+
//Passes easily with iterative though.
38+
39+
void build() {
40+
for(int i = 0; i<n; i++) st[n + i] = tour[i];
41+
for(int i = n-1; i>0; i--) st[i] = max(st[i<<1], st[(i<<1)|1]);
42+
}
43+
44+
void upd(int i, int v) {
45+
for(st[i+=n] = v; i > 1; i >>= 1) st[i>>1] = max(st[i], st[i^1]);
46+
}
47+
48+
int query(int l, int r) {
49+
int res = -1;
50+
for(l += n, r += n+1; l < r; l >>= 1, r >>= 1) {
51+
if (l&1) res = max(res, st[l++]);
52+
if (r&1) res = max(res, st[--r]);
53+
}
54+
return res;
55+
}
56+
57+
int path_query(int u, int v) {
58+
int res = -1;
59+
while(head[u] != head[v]) {
60+
if (dep[head[u]] < dep[head[v]]) swap(u, v);
61+
res = max(res, query(tin[head[u]], tin[u]));
62+
u = par[head[u]];
63+
}
64+
if (dep[u] < dep[v]) swap(u, v);
65+
return max(res, query(tin[v], tin[u]));
66+
}
67+
68+
int main(){
69+
scanf("%d %d", &n, &q);
70+
for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
71+
for(int i = 1, x, y; i<n; i++) {
72+
scanf("%d %d", &x, &y);
73+
vs[x].push_back(y);
74+
vs[y].push_back(x);
75+
}
76+
dfs(1);
77+
hld(1);
78+
build();
79+
for(int i = 0, t, x, y; i<q; i++) {
80+
scanf("%d %d %d", &t, &x, &y);
81+
if (t == 1) upd(tin[x], y);
82+
else printf("%d\n", path_query(x, y));
83+
}
84+
return 0;
85+
}
86+

‎tree/2-path-queries-recursive.cpp‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
using ll = long long;
4+
5+
const int maxn = 2e5+5;
6+
7+
int n, q, tmr, a[maxn], tour[maxn], tin[maxn], dep[maxn], par[maxn], head[maxn], sz[maxn], st[4*maxn];
8+
vector<int> vs[maxn];
9+
10+
void dfs(int v) {
11+
sz[v] = 1;
12+
for(int i = 0, e; i<int(vs[v].size()); i++) {
13+
e = vs[v][i];
14+
if (e == par[v]) {
15+
swap(vs[v][i--], vs[v].back());
16+
vs[v].pop_back();
17+
continue;
18+
}
19+
par[e] = v;
20+
dep[e] = dep[v] + 1;
21+
dfs(e);
22+
sz[v] += sz[e];
23+
if (sz[e] > sz[vs[v][0]]) swap(vs[v][i], vs[v][0]);
24+
}
25+
}
26+
27+
void hld(int v) {
28+
tour[tmr] = a[v];
29+
tin[v] = tmr++;
30+
for(int e : vs[v]) {
31+
head[e] = e == vs[v][0] ? head[v] : e;
32+
hld(e);
33+
}
34+
}
35+
36+
37+
void build(int pos = 1, int l = 0, int r = n-1) {
38+
if (l == r) { st[pos] = tour[l]; return; }
39+
int mid = (l+r) >> 1, child = pos << 1;
40+
build(child, l, mid);
41+
build(child|1, mid+1, r);
42+
st[pos] = max(st[child], st[child|1]);
43+
}
44+
45+
//Cheesy query parameters as global variables to squeeze
46+
//recursive segtree into the time limit. Passes with 0.97s.
47+
//Just use iterative segment tree instead honestly.
48+
49+
int qi, ql, qr, qv;
50+
void upd(int pos = 1, int l = 0, int r = n-1) {
51+
if (l == r) { st[pos] = qv; return; }
52+
int mid = (l+r) >> 1, child = pos << 1;
53+
if (qi <= mid) upd(child, l, mid);
54+
else upd(child|1, mid+1, r);
55+
st[pos] = max(st[child], st[child|1]);
56+
}
57+
58+
int query(int pos = 1, int l = 0, int r = n-1) {
59+
if (r < ql or l > qr) return -1;
60+
if (ql <= l && r <= qr) return st[pos];
61+
int mid = (l+r) >> 1, child = pos << 1;
62+
return max(query(child, l, mid), query(child|1, mid+1, r));
63+
}
64+
65+
int path_query(int u, int v) {
66+
int res = -1;
67+
while(head[u] != head[v]) {
68+
if (dep[head[u]] < dep[head[v]]) swap(u, v);
69+
ql = tin[head[u]], qr = tin[u];
70+
res = max(res, query());
71+
u = par[head[u]];
72+
}
73+
if (dep[u] < dep[v]) swap(u, v);
74+
ql = tin[v], qr = tin[u];
75+
return max(res, query());
76+
}
77+
78+
int main(){
79+
scanf("%d %d", &n, &q);
80+
for(int i = 1; i<=n; i++) scanf("%d", &a[i]);
81+
for(int i = 1, x, y; i<n; i++) {
82+
scanf("%d %d", &x, &y);
83+
vs[x].push_back(y);
84+
vs[y].push_back(x);
85+
}
86+
dfs(1);
87+
hld(1);
88+
build();
89+
for(int i = 0, t, x, y; i<q; i++) {
90+
scanf("%d %d %d", &t, &x, &y);
91+
if (t == 1) qi = tin[x], qv = y, upd();
92+
else printf("%d\n", path_query(x, y));
93+
}
94+
return 0;
95+
}
96+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /