// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebear"
/*END OF TEMPLATE. ICEBEAR AND THE CAT WILL WIN VOI26 */
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2000 + 5;
int n, C[N];
vector<int> G[N], compress;
ii edge[N];
int par[N], ft[N];
void update(int x, int v) {
for(; x <= n; x += x & -x) ft[x] += v;
}
int get(int x) {
int ans = 0;
for(; x; x -= x & -x) ans += ft[x];
return ans;
}
void dfs(int u) {
for(int v : G[u]) if (v != par[u]) {
par[v] = u;
dfs(v);
}
}
void init(void) {
cin >> n;
FOR(i, 1, n) cin >> C[i], compress.pb(C[i]);
sort(all(compress));
compress.resize(unique(all(compress)) - compress.begin());
FOR(i, 1, n) C[i] = upper_bound(all(compress), C[i]) - compress.begin();
FOR(i, 2, n) {
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
edge[i] = mp(u, v);
}
}
void process(void) {
dfs(1);
FOR(i, 2, n) {
memset(ft, 0, sizeof ft);
int u = edge[i].fi;
int ans = 0;
while(u > 0) {
ans += get(C[u] - 1);
update(C[u], +1);
C[u] = C[edge[i].se];
u = par[u];
}
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
construction.cpp: In function 'int main()':
construction.cpp:96:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |