This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)1e5 + 5;
vector<int> adj[MAXN];
deque<pii> T[MAXN];
int group[MAXN];
int boss[MAXN];
int arr[MAXN];
int sub[MAXN];
int par[MAXN];
int h[MAXN];
pii e[MAXN];
int n, m;
void dfs(int v, int pr = -1) {
sub[v] = 1;
for (int to : adj[v]) {
if (to != pr) {
h[to] = h[v] + 1;
par[to] = v;
dfs(to, v);
sub[v] += sub[to];
}
}
}
void dec(int v, int pr = -1) {
if (!boss[m]) {
boss[m] = v;
}
group[v] = m;
T[m].pb(mp(arr[v], 1));
int mx = -1;
for (int to : adj[v]) {
if (to != pr) {
if (mx == -1 || sub[to] > sub[mx]) {
mx = to;
}
}
}
if (mx != -1) {
dec(mx, v);
}
for (int to : adj[v]) {
if (to != pr && to != mx) {
++m;
dec(to, v);
}
}
}
void gather(vector<pii> &V, int col, int cnt, int x) {
int _cnt = cnt;
vector<pii> V2;
while (!T[col].empty()) {
if (T[col].front().se > cnt) {
V2.pb(mp(T[col].front().fi, cnt));
T[col][0].se -= cnt;
break;
}
else {
V2.pb(T[col].front());
cnt -= T[col].front().se;
T[col].pop_front();
}
}
while (!V2.empty()) {
V.pb(V2.back());
V2.pop_back();
}
T[col].push_front(mp(x, _cnt));
}
int query(int u, int x) {
vector<pii> V;
while (u != 0) {
int my = group[u];
int cnt = h[u] - h[boss[my]] + 1;
gather(V, my, cnt, x);
u = par[boss[my]];
}
ll ans = 0;
for (int i = 0; i < V.size(); ++i) {
for (int j = i + 1; j < V.size(); ++j) {
if (V[i].fi < V[j].fi) {
ans += V[i].se * 1ll * V[j].se;
}
}
}
return ans;
}
void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i]);
}
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d %d", &u, &v);
e[i] = mp(u, v);
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
dec(1);
for (int i = 1; i < n; ++i) {
int u = e[i].fi, v = e[i].se;
printf("%d\n", query(u, arr[v]));
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message (stderr)
construction.cpp: In function 'int query(int, int)':
construction.cpp:128:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < V.size(); ++i) {
~~^~~~~~~~~~
construction.cpp:129:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = i + 1; j < V.size(); ++j) {
~~^~~~~~~~~~
construction.cpp: In function 'void solve()':
construction.cpp:140:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:143:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
construction.cpp:148:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |