답안 #287870

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
287870 2020-09-01T05:31:44 Z zecookiez Tree Rotations (POI11_rot) C++17
100 / 100
168 ms 17656 KB
#include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int len(const C&c){return int(c.size());}

const int MAXN = 200005;
int N, id, cur, arr[MAXN], L[2*MAXN], R[2*MAXN], LL[2*MAXN], RR[2*MAXN], bit[MAXN];
long long ans;

void update(int pos, int val){
    for(int i = pos; i <= N; i += i & -i) bit[i] += val;
}
int query(int pos){
    int tot = 0; for(int i = pos; i > 0; i &= i - 1) tot += bit[i];
    return tot;
}

void read_tree(int node){
    int A; cin >> A;
    L[node] = cur;
    if(A != 0){
        arr[cur++] = A; R[node] = cur; return;
    }
    LL[node] = ++id; read_tree(id);
    RR[node] = ++id; read_tree(id);
    R[node] = cur;
}

void solve(int id){
    if(R[id] - L[id] == 1){
        update(arr[L[id]], 1); return;
    }
    int SZ, left = LL[id], right = RR[id];
    long long cur, case1 = 0, case2 = 0;
    int A = L[left], B = R[left], C = L[right], D = R[right];
    if(B - A <= D - C){
        solve(left); for(int i = A; i < B; ++i) update(arr[i], -1);
        solve(right); SZ = D - C;
        for(int i = A; i < B; ++i){
            cur = query(arr[i] - 1);
            case1 += cur; case2 += SZ - cur;
        }
        for(int i = A; i < B; ++i) update(arr[i], 1);
    } else {
        solve(right); for(int i = C; i < D; ++i) update(arr[i], -1);
        solve(left); SZ = B - A;
        for(int i = C; i < D; ++i){
            cur = query(arr[i] - 1);
            case1 += cur; case2 += SZ - cur;
        }
        for(int i = C; i < D; ++i) update(arr[i], 1);
    }
    ans += min(case1, case2);
}

int main(){
    cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N; read_tree(0); solve(0); cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 768 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 2 ms 768 KB Output is correct
4 Correct 2 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1664 KB Output is correct
2 Correct 6 ms 896 KB Output is correct
3 Correct 19 ms 1536 KB Output is correct
4 Correct 6 ms 1408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 2304 KB Output is correct
2 Correct 22 ms 3728 KB Output is correct
3 Correct 23 ms 4960 KB Output is correct
4 Correct 25 ms 4992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 9352 KB Output is correct
2 Correct 32 ms 6912 KB Output is correct
3 Correct 39 ms 5248 KB Output is correct
4 Correct 32 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 5248 KB Output is correct
2 Correct 44 ms 6524 KB Output is correct
3 Correct 45 ms 9592 KB Output is correct
4 Correct 44 ms 9336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 10104 KB Output is correct
2 Correct 85 ms 10232 KB Output is correct
3 Correct 71 ms 10488 KB Output is correct
4 Correct 84 ms 9592 KB Output is correct
5 Correct 130 ms 8464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 8696 KB Output is correct
2 Correct 84 ms 14456 KB Output is correct
3 Correct 94 ms 14200 KB Output is correct
4 Correct 76 ms 17144 KB Output is correct
5 Correct 154 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 92 ms 8568 KB Output is correct
2 Correct 80 ms 11768 KB Output is correct
3 Correct 117 ms 9848 KB Output is correct
4 Correct 94 ms 10360 KB Output is correct
5 Correct 74 ms 17656 KB Output is correct
6 Correct 168 ms 9976 KB Output is correct