답안 #952272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
952272 2024-03-23T13:20:11 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[3*MAXN];
int val[3*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>>(3*leaves, vector<int>());
    construct_tree(0);
    dfs(0, 0);
    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 47440 KB Output is correct
2 Correct 28 ms 47448 KB Output is correct
3 Correct 30 ms 47452 KB Output is correct
4 Correct 27 ms 47440 KB Output is correct
5 Correct 27 ms 47452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47444 KB Output is correct
2 Correct 26 ms 47372 KB Output is correct
3 Correct 26 ms 47448 KB Output is correct
4 Correct 27 ms 47452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 47708 KB Output is correct
2 Correct 28 ms 47708 KB Output is correct
3 Correct 29 ms 47700 KB Output is correct
4 Correct 34 ms 47704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 49476 KB Output is correct
2 Correct 37 ms 48976 KB Output is correct
3 Correct 295 ms 49268 KB Output is correct
4 Correct 378 ms 49452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1103 ms 52304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 139 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 98 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 49 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 37 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -