Submission #952277

# Submission time Handle Problem Language Result Execution time Memory
952277 2024-03-23T13:31:04 Z serifefedartar Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 65536 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define LOGN 20
const ll MOD = 998244353;
const ll MAXN = 2e5 + 100;

vector<vector<int>> graph;
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> st[2*MAXN];
int val[2*MAXN], now = 0;
ll ans = 0;

void construct_tree(int from) {
    now++;
    cin >> val[now];
    graph[from].push_back(now);
    if (val[now] == 0) {
        int q = now;
        construct_tree(q);
        construct_tree(q);
    } else
        st[now].insert(val[now]);
}

void dfs(int node, int parent) {
    for (auto u : graph[node]) {
        if (u == parent)
            continue;
        dfs(u, node);
    }

    if (graph[node].size() != 2)
        return ;
    
    int x = graph[node][0];
    int y = graph[node][1];
    if (st[y].size() > st[x].size())
        swap(x, y);

    ll all = st[x].size() * 1LL * st[y].size();
    ll nw = 0;
    for (auto u : st[y])
        nw += st[x].order_of_key(u);
    for (auto u : st[y])
        st[x].insert(u);
    swap(st[x], st[node]);
    ans += min(nw, all - nw);
}

int main() {
    fast
    int leaves; 
    cin >> leaves;

    graph = vector<vector<int>>(2*leaves, vector<int>());
    construct_tree(0);
    dfs(0, 0);
    cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 33112 KB Output is correct
2 Correct 19 ms 33116 KB Output is correct
3 Correct 18 ms 33116 KB Output is correct
4 Correct 18 ms 33112 KB Output is correct
5 Correct 18 ms 33116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 33168 KB Output is correct
2 Correct 18 ms 33116 KB Output is correct
3 Correct 24 ms 33112 KB Output is correct
4 Correct 18 ms 33116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 33372 KB Output is correct
2 Correct 20 ms 33408 KB Output is correct
3 Correct 20 ms 33328 KB Output is correct
4 Correct 26 ms 33452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 34644 KB Output is correct
2 Correct 30 ms 34652 KB Output is correct
3 Correct 289 ms 35168 KB Output is correct
4 Correct 400 ms 34968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1032 ms 37576 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 200 ms 55376 KB Output is correct
2 Execution timed out 1075 ms 45112 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1052 ms 62324 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1047 ms 51152 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 111 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1069 ms 62848 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1059 ms 63132 KB Time limit exceeded
2 Halted 0 ms 0 KB -