This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define task "C"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, a[N], head[N], sz[N], dep[N], bit[N], par[N], cur = 0;
vi g[N], com;
vii edge, rollback;
deque<ii> seg[N];
void upd(int p, int v)
{
for (; p <= n; p += p & -p) bit[p] += v;
}
int query(int p)
{
int res = 0;
for (; p; p -= p & -p) res += bit[p];
return res;
}
void dfs(int u, int p)
{
par[u] = p;
dep[u] = dep[p] + 1;
sz[u] = 1;
for (int v : g[u]) if (v != p)
{
dfs(v, u);
sz[u] += sz[v];
}
}
void hld(int u, int p, int h)
{
head[u] = h;
int mx = 0, big = -1;
for (int v : g[u]) if (v != p && mx < sz[v]) mx = sz[v], big = v;
if (big != -1) hld(big, u, h);
for (int v : g[u]) if (v != p && v != big) hld(v, u, v);
}
void solve(int id, int col, int len)
{
if (seg[id].empty())
{
seg[id].push_front({col, len});
return;
}
vii tem;
int num = 0;
while (1)
{
ii it = seg[id].front();
seg[id].pop_front();
num += it.S;
if (num >= len || seg[id].empty())
{
if (num - len > 0) seg[id].push_front({it.F, num - len});
tem.eb(it.F, it.S - max(0, (num - len)));
seg[id].push_front({col, len});
break;
}
tem.eb(it);
}
Rep(i, SZ(tem) - 1, 0)
{
//cerr << tem[i].F << ' ' << tem[i].S << '\n';
rollback.eb(tem[i]);
upd(tem[i].F, tem[i].S);
cur += tem[i].S * query(tem[i].F - 1);
}
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n;
rep(i, 1, n) cin >> a[i], com.eb(a[i]);
sort(all(com));
com.erase(unique(all(com)), com.end());
rep(i, 1, n) a[i] = lower_bound(all(com), a[i]) - com.begin() + 1;
rep(i, 2, n)
{
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
edge.eb(u, v);
}
dfs(1, 0);
hld(1, 0, 1);
seg[1].eb(a[1], 1);
for (ii E : edge)
{
int u = E.S;
while (u)
{
solve(head[u], a[E.S], dep[u] - dep[head[u]] + 1);
u = par[head[u]];
}
cout << cur << '\n';
//for (ii v : seg[1]) cout << '(' << v.F << ", " << v.S << "), ";
//cout << '\n';
for (ii v : rollback) upd(v.F, -v.S);
cur = 0;
rollback.clear();
rep(i, 1, n) assert(bit[i] == 0);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |