#include <bits/stdc++.h>
#define ll int
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll base = 31;
const ll blocksz = 320;
const ll N = 4e6+8;
ll g[N][2];
ll a[N],tdfs,cnt,n;
long long dp[N], inv1, inv2;
void build_tree(ll u) {
for(ll i = 0; i < 2; i++) {
ll x;
cin >> x;
if(x == 0) {
tdfs++;
g[u][i] = tdfs;
build_tree(tdfs);
}
else {
tdfs++;
g[u][i] = tdfs;
a[tdfs] = x;
cnt++;
}
if(cnt == n) return;
}
}
struct Binary_Indexed_Tree{
ll BIT[N+5];
void update(ll i, ll x){
while(i <= n){
BIT[i] += x;
i += (i&-i);
}
}
ll get(ll i){
ll ans = 0;
while(i){
ans += BIT[i];
i -= (i&-i);
}
return ans;
}
} bit;
ll sz[N];
void dfs_size(ll u, ll p){
sz[u] = 1;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) {
dfs_size(v,u);
sz[u] += sz[v];
}
}
}
void sack_add(ll u, ll p){
if(a[u] != -1) bit.update(a[u],1);
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) sack_add(v,u);
}
}
void sack_rmv(ll u, ll p){
if(a[u] != -1) bit.update(a[u],-1);
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) sack_rmv(v,u);
}
}
void cal_inv(ll u, ll p){
if(a[u] != -1){
inv1 += bit.get(a[u]-1);
inv2 += bit.get(n)-bit.get(a[u]);
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) cal_inv(v,u);
}
}
void sack(ll u, ll p){
ll big = 0;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && sz[v] > sz[big]) big = v;
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
sack(v,u);
dp[u] += dp[v];
sack_rmv(v,u);
}
}
if(big){
sack(big,u);
dp[u] += dp[big];
}
if(a[u] != -1) bit.update(a[u],1);
inv1 = inv2 = 0;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
cal_inv(v,u);
}
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
sack_add(v,u);
}
}
dp[u] += min(inv1,inv2);
}
void solve(){
memset(a,-1,sizeof(a));
memset(g,-1,sizeof(g));
cin >> n;
build_tree(0);
n = tdfs;
dfs_size(1,1);
sack(1,1);
cout << dp[1];
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("test.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; i++){
solve();
// cout << '\n';
}
}
Compilation message
rot.cpp:16:16: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+18' to '2147483647' [-Woverflow]
16 | const ll inf = 2e18;
| ^~~~
rot.cpp: In function 'int main()':
rot.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
48220 KB |
Output is correct |
2 |
Correct |
19 ms |
48220 KB |
Output is correct |
3 |
Correct |
19 ms |
48164 KB |
Output is correct |
4 |
Correct |
19 ms |
48220 KB |
Output is correct |
5 |
Correct |
19 ms |
48160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
48216 KB |
Output is correct |
2 |
Correct |
20 ms |
48220 KB |
Output is correct |
3 |
Correct |
18 ms |
48272 KB |
Output is correct |
4 |
Correct |
20 ms |
48216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48220 KB |
Output is correct |
2 |
Correct |
20 ms |
48216 KB |
Output is correct |
3 |
Correct |
20 ms |
48220 KB |
Output is correct |
4 |
Correct |
20 ms |
48220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
48476 KB |
Output is correct |
2 |
Correct |
19 ms |
48220 KB |
Output is correct |
3 |
Correct |
20 ms |
48472 KB |
Output is correct |
4 |
Correct |
20 ms |
48732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
49244 KB |
Output is correct |
2 |
Correct |
25 ms |
48468 KB |
Output is correct |
3 |
Correct |
38 ms |
48976 KB |
Output is correct |
4 |
Correct |
24 ms |
49244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
49500 KB |
Output is correct |
2 |
Correct |
38 ms |
51028 KB |
Output is correct |
3 |
Correct |
42 ms |
52308 KB |
Output is correct |
4 |
Correct |
42 ms |
52052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
56656 KB |
Output is correct |
2 |
Correct |
49 ms |
53840 KB |
Output is correct |
3 |
Correct |
68 ms |
52276 KB |
Output is correct |
4 |
Correct |
51 ms |
51024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
51704 KB |
Output is correct |
2 |
Correct |
58 ms |
53072 KB |
Output is correct |
3 |
Correct |
62 ms |
56144 KB |
Output is correct |
4 |
Correct |
61 ms |
55888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
56176 KB |
Output is correct |
2 |
Correct |
110 ms |
54672 KB |
Output is correct |
3 |
Correct |
84 ms |
55124 KB |
Output is correct |
4 |
Correct |
108 ms |
54356 KB |
Output is correct |
5 |
Correct |
171 ms |
52816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
54864 KB |
Output is correct |
2 |
Correct |
104 ms |
60240 KB |
Output is correct |
3 |
Correct |
116 ms |
58236 KB |
Output is correct |
4 |
Correct |
87 ms |
61520 KB |
Output is correct |
5 |
Correct |
224 ms |
53432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
54612 KB |
Output is correct |
2 |
Correct |
85 ms |
55784 KB |
Output is correct |
3 |
Correct |
171 ms |
53784 KB |
Output is correct |
4 |
Correct |
113 ms |
54608 KB |
Output is correct |
5 |
Correct |
75 ms |
61520 KB |
Output is correct |
6 |
Correct |
195 ms |
53844 KB |
Output is correct |