Submission #287849

# Submission time Handle Problem Language Result Execution time Memory
287849 2020-09-01T04:36:23 Z zecookiez Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 20600 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 * 2;
int N, inp[MAXN], sz[MAXN], L[MAXN], R[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;
}
 
int read_tree(int id){
    cin >> inp[id];
    if(inp[id] != 0){
        L[id] = R[id] = id;
        sz[id] = 1;
        return id;
    }
    int left  = read_tree(id + 1);
    int right = read_tree(left + 1);
    sz[id] = sz[left] + sz[right];
    L[id] = left; return R[id] = right;
}
 
vector<int> solve(int id){
    int A = inp[id];
    if(A != 0){
        update(A, 1);
        return {A};
    }
    vector<int> left, right;
    if(sz[L[id]] <= sz[R[id]]){
        left = solve(id + 1);
        for(auto i : left) update(i, -1);
        right = solve(L[id] + 1);
    } else {
        left = solve(L[id] + 1);
        for(auto i : left) update(i, -1);
        right = solve(id + 1);
    }
    int cur, case1 = 0, case2 = 0;
    for(auto i : left){
        cur = query(i - 1);
        case1 += cur; case2 += len(right) - cur;
    }
    ans += min(case1, case2);
    for(auto i : left) update(i, 1), right.push_back(i);
    return right;
}
 
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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory 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 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 1152 KB Output is correct
2 Correct 5 ms 640 KB Output is correct
3 Correct 134 ms 1304 KB Output is correct
4 Correct 186 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1095 ms 3232 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 2888 KB Output is correct
2 Execution timed out 1086 ms 6648 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 20600 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 5556 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1035 ms 16508 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1088 ms 11524 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1097 ms 10872 KB Time limit exceeded
2 Halted 0 ms 0 KB -