답안 #1070421

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1070421 2024-08-22T14:04:57 Z thdh__ Tree Rotations (POI11_rot) C++17
100 / 100
255 ms 24916 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ff(x,a,b,c) for (auto x=a;x<=b;x+=c)
#define fd(x,a,b,c) for (auto x=a;x>=b;x-=c)
#define int ll

using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

typedef pair<int, int> ii;
const int N = 1e6+5;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;} 

int n, root, sz[N], l[N], r[N], idx = 0, ans, sum, bit[4*N];

//BIT
void update(int u,int val)
{
    for (;u<=n+5;u += u&(-u)) bit[u] += val;
}

int get(int u)
{
    int res = 0;
    for (;u>0;u -= u&(-u)) res += bit[u];
    return res;
}

void reset(int u)
{
    if (u<=n) 
    {
        update(u,-1); return;
    }
    reset(l[u]); reset(r[u]);
}

void push(int u)
{
    if (u<=n) 
    {
        update(u,1); return;
    }
    push(l[u]), push(r[u]);
}

void calc(int u)
{
    if (u<=n) 
    {
        sum += get(n)-get(u); return;
    }
    calc(l[u]); calc(r[u]);
}

void dfs(int u)
{
    if (u<=n) 
    {
        update(u,1); return;
    }
    dfs(r[u]);
    reset(r[u]);
    dfs(l[u]); sum = 0;
    calc(r[u]); push(r[u]);
    ans += min(sum, sz[l[u]]*sz[r[u]]-sum);
}

//idea: dùng fenwick tree để quản lý số inversion
//với mỗi đỉnh dfs ra 2 nhánh, tính minimum inversions
//dfs ra nhánh r[u] trước rồi xóa hết các thao tác trên BIT
//dfs tiếp sang nhánh l[u], rồi calculate số inversion (i,j) sao cho i thuộc nhánh l[u] còn j thuộc nhánh r[u]
//push lại vào nhánh r[u];
//cộng thêm vào ans (có 2 TH: ko đảo thì số inv = sum, có đảo thì số inv = sz[l[u]]*sz[r[u]]-sum)

int init()
{
    int x; cin>>x;
    if (x) 
    {
        sz[x] = 1; return x;
    }
    idx++; int i = idx-1;
    l[i] = init();
    r[i] = init();
    sz[i] += sz[l[i]] + sz[r[i]];
    //cout<<i<<endl<<l[i]<<" "<<r[i]<<endl;
    if (sz[l[i]]<sz[r[i]]) swap(l[i],r[i]);
    return i;
}

void solve()
{
	cin>>n;
    idx = n+1;
    int root = init();
    dfs(root);
    cout<<ans;
}

signed main()
{
	bruh
	//freopen("input.inp","r",stdin);
	//freopen("output.inp","w",stdout);
	int t = 1;
	//cin>>t;
	while (t--)
	{
		solve();
		cout<<"\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 6488 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6608 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 2 ms 604 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1368 KB Output is correct
2 Correct 5 ms 940 KB Output is correct
3 Correct 20 ms 3416 KB Output is correct
4 Correct 4 ms 7000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 2396 KB Output is correct
2 Correct 17 ms 3420 KB Output is correct
3 Correct 25 ms 4444 KB Output is correct
4 Correct 18 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 8540 KB Output is correct
2 Correct 26 ms 9304 KB Output is correct
3 Correct 34 ms 5072 KB Output is correct
4 Correct 22 ms 4184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 13148 KB Output is correct
2 Correct 31 ms 11100 KB Output is correct
3 Correct 31 ms 9820 KB Output is correct
4 Correct 31 ms 17496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 20448 KB Output is correct
2 Correct 72 ms 19796 KB Output is correct
3 Correct 72 ms 20444 KB Output is correct
4 Correct 96 ms 19284 KB Output is correct
5 Correct 145 ms 18244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 19284 KB Output is correct
2 Correct 78 ms 23884 KB Output is correct
3 Correct 94 ms 22352 KB Output is correct
4 Correct 66 ms 24820 KB Output is correct
5 Correct 194 ms 18524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 19028 KB Output is correct
2 Correct 71 ms 20048 KB Output is correct
3 Correct 125 ms 18512 KB Output is correct
4 Correct 94 ms 19292 KB Output is correct
5 Correct 61 ms 24916 KB Output is correct
6 Correct 255 ms 18508 KB Output is correct