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 mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
ll _mergeSort(int arr[], int temp[], int left, int right);
ll mergee(int arr[], int temp[], int left, int mid, int right);
ll mergeSort(int arr[], int array_size) {
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
ll _mergeSort(int arr[], int temp[], int left, int right) {
ll mid, inv_count = 0;
if (right > left) {
mid = (right + left) / 2;
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
inv_count += mergee(arr, temp, left, mid + 1, right);
}
return inv_count;
}
ll mergee(int arr[], int temp[], int left, int mid, int right) {
int i, j, k;
ll inv_count = 0;
i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
const int MAXN = 1e5 + 10;
int n;
int c[MAXN];
int p[MAXN];
void solve() {
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> c[i];
p[1] = -1;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
p[v] = u;
int x = u;
veci inv_u;
while(x != -1) inv_u.pb(c[x]), x = p[x];
reverse(all(inv_u));
int cntinv[inv_u.sz];
///cout << u << " : ";
for(int j = 0; j < inv_u.sz; ++j) cntinv[j] = inv_u[j];
///for(auto it : inv_u) cout << it << ' ';
///cout << '\n';
cout << mergeSort(cntinv, inv_u.sz) << '\n';
x = u;
while(x != -1) c[x] = c[v], x = p[x];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}
Compilation message (stderr)
construction.cpp: In function 'void solve()':
construction.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | for(int j = 0; j < inv_u.sz; ++j) cntinv[j] = inv_u[j];
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |