# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
47385 | mirbek01 | Construction of Highway (JOI18_construction) | C++17 | 7 ms | 740 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;
const int N = 1e5 + 2;
int n, c[N], p[N], fen[N];
vector <int> vec;
void update(int pos, int val){
for(; pos <= n; pos |= pos + 1)
fen[pos] += val;
}
int get(int pos){
int res = 0;
for(; pos > 0; pos = (pos & (pos + 1)) - 1)
res += fen[pos];
return res;
}
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
scanf("%d", &c[i]);
vec.emplace_back(c[i]);
}
sort(vec.begin(), vec.end());
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
for(int i = 1; i <= n; i ++){
c[i] = lower_bound(vec.begin(), vec.end(), c[i]) - vec.begin() + 1;
}
for(int i = 1; i < n; i ++){
int a, b;
scanf("%d %d", &a, &b);
int v = a, ans = 0;
set <int> st;
while(v){
update(c[v], 1);
ans += get(c[v] - 1);
st.insert(c[v]);
v = p[v];
}
if(st.size() > 100)
assert(0);
v = a;
while(v){
update(c[v], -1);
c[v] = c[b];
v = p[v];
}
printf("%d\n", ans);
p[b] = a;
}
}
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... |