#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=2e5+5, inf=1e6;
ll n, a[nx], u, v, in[nx], out[nx], rin[nx], t, rt, used[nx], pa[nx], dp[nx], cnt, k[nx];
vector<ll> d[nx], c[nx];
struct node
{
ll mx, idx, lz;
node(): mx(0), idx(0), lz(0){}
node(ll mx, ll idx, ll lz): mx(mx), idx(idx), lz(lz){}
friend node operator+(const node &a, const node &b)
{
if (a.mx>b.mx) return a;
else return b;
}
};
struct segtree
{
node d[4*nx];
void pushlz(ll l, ll r, ll i)
{
d[i].mx+=d[i].lz;
ll md=(l+r)/2;
if (l!=r) d[2*i].lz+=d[i].lz, d[2*i+1].lz+=d[i].lz;
d[i].lz=0;
}
void build(ll l, ll r, ll i)
{
if (l==r) return d[i]=node(k[l], l, 0), void();
ll md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
d[i]=d[2*i]+d[2*i+1];
}
void update(ll l, ll r, ll i, ll ql, ll qr, ll vl)
{
pushlz(l, r, i);
if (qr<l||r<ql) return;
if (ql<=l&&r<=qr) return d[i].lz+=vl, pushlz(l, r, i), void();
ll md=(l+r)/2;
update(l, md, 2*i, ql ,qr, vl);
update(md+1, r, 2*i+1, ql, qr, vl);
d[i]=d[2*i]+d[2*i+1];
}
void show(int l, int r, int i)
{
pushlz(l, r, i);
cout<<"debugseg "<<l<<' '<<r<<' '<<d[i].mx<<' '<<d[i].idx<<'\n';
if (l==r) return;
int md=(l+r)/2;
show(l, md, 2*i);
show(md+1, r, 2*i+1);
d[i]=d[2*i]+d[2*i+1];
}
} s;
void dfs(ll u, ll p)
{
pa[u]=p;
in[u]=++t;
rin[t]=u;
for (auto v:d[u]) if (v!=p) dfs(v, u);
out[u]=t;
}
void updateoutrange(ll l, ll r, ll vl)
{
if (l!=1) s.update(1, n, 1, 1, l-1, vl);
if (r!=n) s.update(1, n, 1, r+1, n, vl);
}
void solve(ll u)
{
used[u]=1;
if (u!=pa[u]&&!used[pa[u]])
{
s.update(1, n, 1, in[u], out[u], -inf);
c[u].push_back(rin[s.d[1].idx]);
solve(rin[s.d[1].idx]);
s.update(1, n, 1, in[u], out[u], inf);
}
for (auto v:d[u])
{
if (used[v]) continue;
updateoutrange(in[v], out[v], -inf);
//cout<<"edge "<<u<<' '<<v<<' '<<in[v]<<' '<<out[v]<<'\n';
//s.show(1, n, 1);
c[u].push_back(rin[s.d[1].idx]);
solve(rin[s.d[1].idx]);
//cout<<"debug "<<u<<' '<<s.d[1].idx<<'\n';
updateoutrange(in[v], out[v], inf);
}
}
void dfs2(int u)
{
for (auto v:c[u]) dfs2(v), dp[u]=max(dp[u], dp[v]+abs(v-u));
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (ll i=1; i<=n; i++) cin>>a[i];
for (int i=1; i<=n; i++) if (a[i]==n) rt=i;
for (ll i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
dfs(rt, rt);
for (int i=1; i<=n; i++) k[in[i]]=a[i];
s.build(1, n, 1);
solve(rt);
/*
for (int i=1; i<=n; i++)
{
cout<<"c "<<i<<':';
for (auto x:c[i]) cout<<x<<' ';
cout<<'\n';
}*/
dfs2(rt);
cout<<dp[rt];
}
Compilation message
Main.cpp: In member function 'void segtree::pushlz(long long int, long long int, long long int)':
Main.cpp:30:12: warning: unused variable 'md' [-Wunused-variable]
30 | ll md=(l+r)/2;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
6 ms |
35420 KB |
Output is correct |
12 |
Correct |
6 ms |
35420 KB |
Output is correct |
13 |
Correct |
7 ms |
35512 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
6 ms |
35548 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
6 ms |
35420 KB |
Output is correct |
12 |
Correct |
6 ms |
35420 KB |
Output is correct |
13 |
Correct |
7 ms |
35512 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
6 ms |
35548 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
10 ms |
36956 KB |
Output is correct |
19 |
Correct |
17 ms |
30812 KB |
Output is correct |
20 |
Correct |
14 ms |
30716 KB |
Output is correct |
21 |
Correct |
10 ms |
30296 KB |
Output is correct |
22 |
Correct |
12 ms |
30044 KB |
Output is correct |
23 |
Correct |
12 ms |
30040 KB |
Output is correct |
24 |
Correct |
10 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
6 ms |
35420 KB |
Output is correct |
12 |
Correct |
6 ms |
35420 KB |
Output is correct |
13 |
Correct |
7 ms |
35512 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
6 ms |
35548 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
10 ms |
36956 KB |
Output is correct |
19 |
Correct |
17 ms |
30812 KB |
Output is correct |
20 |
Correct |
14 ms |
30716 KB |
Output is correct |
21 |
Correct |
10 ms |
30296 KB |
Output is correct |
22 |
Correct |
12 ms |
30044 KB |
Output is correct |
23 |
Correct |
12 ms |
30040 KB |
Output is correct |
24 |
Correct |
10 ms |
30044 KB |
Output is correct |
25 |
Incorrect |
8 ms |
29272 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
6 ms |
35420 KB |
Output is correct |
12 |
Correct |
6 ms |
35420 KB |
Output is correct |
13 |
Correct |
7 ms |
35512 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
6 ms |
35548 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
10 ms |
36956 KB |
Output is correct |
19 |
Correct |
17 ms |
30812 KB |
Output is correct |
20 |
Correct |
14 ms |
30716 KB |
Output is correct |
21 |
Correct |
10 ms |
30296 KB |
Output is correct |
22 |
Correct |
12 ms |
30044 KB |
Output is correct |
23 |
Correct |
12 ms |
30040 KB |
Output is correct |
24 |
Correct |
10 ms |
30044 KB |
Output is correct |
25 |
Correct |
210 ms |
94792 KB |
Output is correct |
26 |
Correct |
213 ms |
90960 KB |
Output is correct |
27 |
Correct |
199 ms |
91032 KB |
Output is correct |
28 |
Correct |
219 ms |
62196 KB |
Output is correct |
29 |
Correct |
192 ms |
63572 KB |
Output is correct |
30 |
Correct |
194 ms |
63184 KB |
Output is correct |
31 |
Correct |
192 ms |
63740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
35752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
35420 KB |
Output is correct |
2 |
Correct |
6 ms |
35420 KB |
Output is correct |
3 |
Correct |
6 ms |
35520 KB |
Output is correct |
4 |
Correct |
6 ms |
35420 KB |
Output is correct |
5 |
Correct |
7 ms |
35472 KB |
Output is correct |
6 |
Correct |
7 ms |
35424 KB |
Output is correct |
7 |
Correct |
6 ms |
35420 KB |
Output is correct |
8 |
Correct |
6 ms |
35420 KB |
Output is correct |
9 |
Correct |
6 ms |
35416 KB |
Output is correct |
10 |
Correct |
9 ms |
35420 KB |
Output is correct |
11 |
Correct |
6 ms |
35420 KB |
Output is correct |
12 |
Correct |
6 ms |
35420 KB |
Output is correct |
13 |
Correct |
7 ms |
35512 KB |
Output is correct |
14 |
Correct |
8 ms |
35420 KB |
Output is correct |
15 |
Correct |
6 ms |
35548 KB |
Output is correct |
16 |
Correct |
6 ms |
35420 KB |
Output is correct |
17 |
Correct |
6 ms |
35420 KB |
Output is correct |
18 |
Correct |
10 ms |
36956 KB |
Output is correct |
19 |
Correct |
17 ms |
30812 KB |
Output is correct |
20 |
Correct |
14 ms |
30716 KB |
Output is correct |
21 |
Correct |
10 ms |
30296 KB |
Output is correct |
22 |
Correct |
12 ms |
30044 KB |
Output is correct |
23 |
Correct |
12 ms |
30040 KB |
Output is correct |
24 |
Correct |
10 ms |
30044 KB |
Output is correct |
25 |
Incorrect |
8 ms |
29272 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |