# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1162657 | SmuggingSpun | Construction of Highway (JOI18_construction) | C++20 | 227 ms | 1064 KiB |
#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
int n;
namespace sub12{
void solve(){
vector<int>c(n + 1), _c(n);
for(int i = 1; i <= n; i++){
cin >> c[i];
_c[i - 1] = c[i];
}
sort(_c.begin(), _c.end());
for(int i = 1; i <= n; i++){
c[i] = lower_bound(_c.begin(), _c.end(), c[i]) - _c.begin() + 1;
}
vector<int>bit(n + 1);
auto update = [&] (int p){
for(; p <= n; p += p & -p){
bit[p]++;
}
};
auto get = [&] (int p){
int ans = 0;
for(; p > 0; p -= p & -p){
ans += bit[p];
}
return ans;
};
int a, b, ans;
vector<vector<int>>g(n + 1);
vector<int>path;
function<void(int, int)>dfs;
dfs = [&] (int s, int p){
path.emplace_back(s);
for(int& d : g[s]){
if(d != p){
dfs(d, s);
}
}
if(s == a){
for(int i = int(path.size()) - 1; i > -1; i--){
ans += get(c[path[i]] - 1);
update(c[path[i]]);
}
for(int& i : path){
c[i] = c[b];
}
}
path.pop_back();
};
for(int _ = 1; _ < n; _++){
cin >> a >> b;
fill(bit.begin(), bit.end(), ans = 0);
dfs(1, -1);
cout << ans << "\n";
g[a].emplace_back(b);
g[b].emplace_back(a);
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
if(n <= 4000){
sub12::solve();
}
}
컴파일 시 표준 에러 (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... |