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>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
int C[100000];
vi adjList[100000];
int parent[100000][17],order[100000];
int depth[100000];
int disc[100000],fin[100000],inv[100000],num = 0;
int doDFS(int u,int d) {
int i;
depth[u] = d,disc[u] = num++,inv[num-1] = u;
for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1);
fin[u] = num;
return 0;
}
int good[100000],pref[100000];
vpii chains;
vi sorted;
int bit[100001],bit2[100001];
int update(int u,int N) {
int i;
for (i = disc[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? -1:1;
for (i = fin[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? 1:-1;
good[u] ^= 1;
return 0;
}
int query(int u) {
int i,sum = 0;
for (i = disc[u]+1; i > 0; i -= i & (-i)) sum += bit[i];
return sum;
}
int main() {
int i;
int N,A,B;
scanf("%d",&N);
for (i = 0; i < N; i++) scanf("%d",&C[i]);
parent[0][0] = -1;
for (i = 0; i < N-1; i++) {
scanf("%d %d",&A,&B);
A--,B--;
adjList[A].pb(B),parent[B][0] = A;
order[i] = B;
}
doDFS(0,0);
int j,k;
for (i = 1; (1 << i) < N; i++) {
for (j = 0; j < N; j++) {
if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1];
else parent[j][i] = -1;
}
}
int logn = i;
for (i = 0; i < N; i++) pref[i] = -1;
for (i = 0; i < N-1; i++) {
int u = order[i];
while (u != 0) {
int v = pref[parent[u][0]];
if (v != -1) update(v,N);
pref[parent[u][0]] = u,update(u,N);
int w = parent[u][0];
for (j = logn-1; j >= 0; j--) {
if ((parent[w][j] != -1) && (query(w) == (query(parent[w][j])+(1 << j)))) w = parent[w][j];
}
if (v != -1) C[v] = C[w];
chains.pb(mp(C[w],depth[u]-depth[w])),sorted.pb(C[w]);
u = w;
}
LLI ans = 0;
sort(sorted.begin(),sorted.end());
sorted.resize(unique(sorted.begin(),sorted.end())-sorted.begin());
for (j = 0; j < chains.size(); j++) {
int p = lower_bound(sorted.begin(),sorted.end(),chains[j].first)-sorted.begin();
for (k = p; k > 0; k -= k & (-k)) ans += (LLI) chains[j].second*bit2[k];
for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second;
}
printf("%lld\n",ans);
C[0] = C[order[i]];
for (j = 0; j <= sorted.size(); j++) bit2[j] = 0;
chains.clear(),sorted.clear();
}
return 0;
}
Compilation message (stderr)
construction.cpp: In function 'int doDFS(int, int)':
construction.cpp:18:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1);
~~^~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:78:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < chains.size(); j++) {
~~^~~~~~~~~~~~~~~
construction.cpp:81:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second;
~~^~~~~~~~~~~~~~~~
construction.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j <= sorted.size(); j++) bit2[j] = 0;
~~^~~~~~~~~~~~~~~~
construction.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
construction.cpp:42:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&C[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&A,&B);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |