# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
47384 | Just_Solve_The_Problem | Construction of Highway (JOI18_construction) | C++11 | 444 ms | 4288 KiB |
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 pb push_back
#define eb emplace_back
#define ll long long
#define pii pair < int, int >
#define fr first
#define sc second
#define mk make_pair
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define ok puts("ok");
#define whatis(x) cerr << #x << " = " << x << endl;
#define pause system("pause");
#define random rand() ^ (rand() << 5)
const int N = (int)1e5 + 7;
const int inf = (int)1e9 + 7;
struct fenwick {
int tree[N];
fenwick() {
memset(tree, 0, sizeof tree);
}
void upd(int pos, int val) {
for (; pos < N; pos += pos & -pos) {
tree[pos] += val;
}
}
int get(int r) {
int res = 0;
for (; r > 0; r -= r & -r) {
res += tree[r];
}
return res;
}
}tr;
int n;
int c[N], b[N];
pii ar[N];
int pr[N];
vector < int > gr[N];
void dfs(int v) {
for (int to : gr[v]) {
pr[to] = v;
dfs(to);
}
}
main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
b[i] = c[i];
}
if (n > 4000)
return 0;
sort(b + 1, b + n + 1);
int m = unique(b + 1, b + n + 1) - b - 1;
for (int i = 1; i <= n; i++) {
c[i] = lower_bound(b + 1, b + m + 1, c[i]) - b;
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
gr[u].pb(v);
ar[i] = mk(u, v);
}
dfs(1);
for (int i = 1; i < n; i++) {
int ans = 0;
tr = fenwick();
int v = ar[i].fr;
while (v != 0) {
ans += tr.get(c[v] - 1);
tr.upd(c[v], 1);
c[v] = c[ar[i].sc];
v = pr[v];
}
printf("%d\n", ans);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |