Submission #1020367

# Submission time Handle Problem Language Result Execution time Memory
1020367 2024-07-12T02:16:39 Z adaawf Tree Rotations (POI11_rot) C++17
91 / 100
361 ms 65536 KB
#include <iostream>
#include <vector>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
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], l[400005], r[400005];
ordered_set s[400005];
void trya() {
    int h = c, w;
    cin >> w;
    if (w == 0) {
        l[h] = ++c;
        trya();
        r[h] = ++c;
        trya();
    }
    else a[h] = w;
}
long long int dfs(int x) {
    if (l[x] == 0) {
        s[x].insert(a[x]);
        return 0;
    }
    long long int res = 0, c = 0, h = 1;
    {
        int w = l[x];
        c += dfs(w);
        h *= s[w].size();
        int flag = 0;
        if (s[w].size() > s[x].size()) {
            s[x].swap(s[w]);
            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);
        }
        s[w].clear();
    }
    {
        int w = r[x];
        c += dfs(w);
        h *= s[w].size();
        int flag = 0;
        if (s[w].size() > s[x].size()) {
            s[x].swap(s[w]);
            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);
        }
        s[w].clear();
    }
    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 Correct 22 ms 31576 KB Output is correct
2 Correct 26 ms 31788 KB Output is correct
3 Correct 29 ms 31716 KB Output is correct
4 Correct 22 ms 31708 KB Output is correct
5 Correct 30 ms 31672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 31572 KB Output is correct
2 Correct 28 ms 31580 KB Output is correct
3 Correct 23 ms 31676 KB Output is correct
4 Correct 22 ms 31576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 31824 KB Output is correct
2 Correct 25 ms 31832 KB Output is correct
3 Correct 22 ms 31868 KB Output is correct
4 Correct 22 ms 31836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 32348 KB Output is correct
2 Correct 25 ms 31940 KB Output is correct
3 Correct 24 ms 32604 KB Output is correct
4 Correct 23 ms 32732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 34396 KB Output is correct
2 Correct 36 ms 32604 KB Output is correct
3 Correct 66 ms 34384 KB Output is correct
4 Correct 33 ms 34176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 36176 KB Output is correct
2 Correct 65 ms 39252 KB Output is correct
3 Correct 74 ms 42068 KB Output is correct
4 Correct 68 ms 42068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 50768 KB Output is correct
2 Correct 85 ms 46928 KB Output is correct
3 Correct 106 ms 41292 KB Output is correct
4 Correct 84 ms 39252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 40788 KB Output is correct
2 Correct 121 ms 43480 KB Output is correct
3 Correct 109 ms 52564 KB Output is correct
4 Correct 127 ms 52696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 53332 KB Output is correct
2 Correct 196 ms 49232 KB Output is correct
3 Correct 188 ms 51164 KB Output is correct
4 Correct 197 ms 49232 KB Output is correct
5 Correct 361 ms 48464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 48500 KB Output is correct
2 Correct 194 ms 65536 KB Output is correct
3 Correct 235 ms 57072 KB Output is correct
4 Runtime error 160 ms 65536 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 236 ms 52048 KB Output is correct
2 Correct 216 ms 51672 KB Output is correct
3 Correct 277 ms 48724 KB Output is correct
4 Correct 244 ms 53844 KB Output is correct
5 Correct 197 ms 64672 KB Output is correct
6 Correct 311 ms 51956 KB Output is correct