#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
using namespace std;
const int mxN = 5e5+5;
const int mxL = 2e5+5;
int n, val[mxN], sub[mxN], tin[mxN], tout[mxN], tour[mxN], Time, cur; ll ans;
vector <int> valN[mxN], ch[mxN];
int newNode() {
return ++cur;
}
struct fenwickTree {
int f[mxL];
void upd(int id, int val) {
for(; id<mxL; id+=id&-id) f[id]+=val;
}
int get(int id) {
int res = 0;
for(; id>0; id-=id&-id) res+=f[id];
return res;
}
} fwt;
struct reverseFenwickTree {
int f[mxL];
void upd(int id, int val) {
for(; id>0; id-=id&-id) f[id]+=val;
}
int get(int id) {
int res = 0;
for(; id<mxL; id+=id&-id) res+=f[id];
return res;
}
} rfwt;
void input(int node) {
int x; cin >> x;
if(x == 0) {
int lfN = newNode();
input(lfN);
int rtN = newNode();
input(rtN);
ch[node].eb(lfN);
ch[node].eb(rtN);
}
else {
val[node] = x;
}
}
void pre_dfs(int u) {
tin[u] = ++Time;
tour[Time] = u;
sub[u] = 1;
for(int v:ch[u]) {
pre_dfs(v);
sub[u] += sub[v];
}
tout[u] = Time;
}
void dfs(int u) {
int bigCh = -1;
for(int v:ch[u]) if(bigCh == -1 || sub[v]>sub[bigCh]) bigCh=v;
for(int v:ch[u]) if(v != bigCh) {
dfs(v);
FOR(i,tin[v],tout[v]){
if(val[tour[i]]) {
fwt.upd(val[tour[i]], -1);
rfwt.upd(val[tour[i]], -1);
}
}
}
if(bigCh != -1) dfs(bigCh);
/// main solve
ll res=0, rres=0;
for(int v:ch[u]) if(v != bigCh) {
FOR(i,tin[v],tout[v]){
if(val[tour[i]]) {
res+=fwt.get(val[tour[i]]-1);
rres+=rfwt.get(val[tour[i]]+1);
}
}
FOR(i,tin[v],tout[v]){
if(val[tour[i]]) {
fwt.upd(val[tour[i]], 1);
rfwt.upd(val[tour[i]], 1);
}
}
}
ans += min(res, rres);
if(val[u]) {
fwt.upd(val[u], 1);
rfwt.upd(val[u], 1);
}
}
int main() {
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> n;
cur = 1; input(1);
pre_dfs(1);
dfs(1);
cout << ans;
}
Compilation message
rot.cpp: In function 'int main()':
rot.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33104 KB |
Output is correct |
2 |
Correct |
6 ms |
33104 KB |
Output is correct |
3 |
Correct |
6 ms |
33272 KB |
Output is correct |
4 |
Correct |
6 ms |
33104 KB |
Output is correct |
5 |
Correct |
5 ms |
33104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33104 KB |
Output is correct |
2 |
Correct |
6 ms |
33104 KB |
Output is correct |
3 |
Correct |
6 ms |
33104 KB |
Output is correct |
4 |
Correct |
7 ms |
33104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33356 KB |
Output is correct |
2 |
Correct |
7 ms |
33340 KB |
Output is correct |
3 |
Correct |
6 ms |
33104 KB |
Output is correct |
4 |
Correct |
5 ms |
33360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33616 KB |
Output is correct |
2 |
Correct |
8 ms |
33360 KB |
Output is correct |
3 |
Correct |
7 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
34516 KB |
Output is correct |
2 |
Correct |
14 ms |
33748 KB |
Output is correct |
3 |
Correct |
34 ms |
34384 KB |
Output is correct |
4 |
Correct |
13 ms |
34128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
35152 KB |
Output is correct |
2 |
Correct |
34 ms |
36536 KB |
Output is correct |
3 |
Correct |
37 ms |
38036 KB |
Output is correct |
4 |
Correct |
36 ms |
37772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
42712 KB |
Output is correct |
2 |
Correct |
46 ms |
40008 KB |
Output is correct |
3 |
Correct |
57 ms |
38224 KB |
Output is correct |
4 |
Correct |
43 ms |
37200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
38224 KB |
Output is correct |
2 |
Correct |
63 ms |
39496 KB |
Output is correct |
3 |
Correct |
62 ms |
42692 KB |
Output is correct |
4 |
Correct |
62 ms |
42436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
43080 KB |
Output is correct |
2 |
Correct |
115 ms |
41812 KB |
Output is correct |
3 |
Correct |
91 ms |
42056 KB |
Output is correct |
4 |
Correct |
107 ms |
41288 KB |
Output is correct |
5 |
Correct |
181 ms |
39896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
42064 KB |
Output is correct |
2 |
Correct |
103 ms |
47696 KB |
Output is correct |
3 |
Correct |
127 ms |
45384 KB |
Output is correct |
4 |
Correct |
91 ms |
48712 KB |
Output is correct |
5 |
Correct |
216 ms |
40936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
41944 KB |
Output is correct |
2 |
Correct |
97 ms |
43088 KB |
Output is correct |
3 |
Correct |
170 ms |
41032 KB |
Output is correct |
4 |
Correct |
125 ms |
42068 KB |
Output is correct |
5 |
Correct |
99 ms |
48968 KB |
Output is correct |
6 |
Correct |
214 ms |
41156 KB |
Output is correct |