Submission #1020359

# Submission time Handle Problem Language Result Execution time Memory
1020359 2024-07-12T02:03:27 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
1000 ms 54804 KB
#include <iostream>
#include <vector>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int c = 1, a[400005];
vector<int> g[400005];
ordered_set s[400005];
void trya() {
    int h = c, w;
    cin >> w;
    if (w == 0) {
        c++;
        trya();
        c++;
        trya();
        g[h].push_back(h + 1);
        g[h].push_back(h + 2);
    }
    else a[h] = w;
}
long long int dfs(int x) {
    if (g[x].size() == 0) {
        s[x].insert(a[x]);
        return 0;
    }
    long long int res = 0, c = 0, h = 1;
    for (int w : g[x]) {
        c += dfs(w);
        h *= s[w].size();
        int flag = 0;
        if (s[w].size() > s[x].size()) {
            swap(s[w], s[x]);
            flag = 1;
        }
        for (int v : s[w]) {
            if (flag == 1) res += s[x].order_of_key(v);
            else res += s[x].size() - s[x].order_of_key(v);
        }
        for (int v : s[w]) {
            s[x].insert(v);
        }
    }
    return min(res, h - res) + c;
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    trya();
    cout << dfs(1);
}
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 41044 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 41172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 41048 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 41308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 42328 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 43600 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1032 ms 49972 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1056 ms 47444 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 59 ms 51796 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1065 ms 54804 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1054 ms 54356 KB Time limit exceeded
2 Halted 0 ms 0 KB -