Submission #996666

# Submission time Handle Problem Language Result Execution time Memory
996666 2024-06-11T04:12:23 Z berWoW Tree Rotations (POI11_rot) C++14
100 / 100
389 ms 43088 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // namespace __gnu_pbds
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;

typedef long long ll;

typedef __gnu_pbds::tree<
int,
__gnu_pbds::null_type,
less<int>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
ordered_set;

const int MAX_N = 2e5 + 10;

int n;
ll inv[MAX_N];
ordered_set trunks[MAX_N];

int count_inv() {
    int p;
    scanf("%d", &p);
    if (p != 0) { trunks[p].insert(p); return p; }

    int l = count_inv(), r = count_inv();

    if (trunks[l].size() < trunks[r].size()) swap(l, r);

    ll inv_l_r = 0, inv_r_l = 0, i = 0;

    for (auto it = trunks[r].begin(); it != trunks[r].end(); it++) {
        trunks[l].insert(*it);
        ll pos = trunks[l].order_of_key(*it);
        inv_l_r += trunks[l].size() - pos - 1;
        inv_r_l += pos - i++;
    }

    trunks[r].clear();

    inv[l] = inv[l] + inv[r] + min(inv_l_r, inv_r_l);

    return l;
}

int main() {
    scanf("%d", &n);
    printf("%lld\n", inv[count_inv()]);
}

Compilation message

rot.cpp: In function 'int count_inv()':
rot.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |     scanf("%d", &p);
      |     ~~~~~^~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 15964 KB Output is correct
2 Correct 10 ms 15960 KB Output is correct
3 Correct 10 ms 15964 KB Output is correct
4 Correct 9 ms 15972 KB Output is correct
5 Correct 9 ms 15960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15964 KB Output is correct
2 Correct 8 ms 15964 KB Output is correct
3 Correct 9 ms 15964 KB Output is correct
4 Correct 9 ms 15964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16220 KB Output is correct
2 Correct 10 ms 16220 KB Output is correct
3 Correct 9 ms 16220 KB Output is correct
4 Correct 8 ms 16220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 16732 KB Output is correct
2 Correct 12 ms 16332 KB Output is correct
3 Correct 11 ms 16732 KB Output is correct
4 Correct 10 ms 16712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 18012 KB Output is correct
2 Correct 22 ms 16728 KB Output is correct
3 Correct 53 ms 18260 KB Output is correct
4 Correct 18 ms 17752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 19792 KB Output is correct
2 Correct 52 ms 22100 KB Output is correct
3 Correct 59 ms 23888 KB Output is correct
4 Correct 49 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 29784 KB Output is correct
2 Correct 71 ms 27984 KB Output is correct
3 Correct 89 ms 23124 KB Output is correct
4 Correct 66 ms 21844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 23164 KB Output is correct
2 Correct 113 ms 25052 KB Output is correct
3 Correct 100 ms 32560 KB Output is correct
4 Correct 115 ms 32592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 242 ms 34492 KB Output is correct
2 Correct 174 ms 29524 KB Output is correct
3 Correct 175 ms 31568 KB Output is correct
4 Correct 179 ms 30548 KB Output is correct
5 Correct 332 ms 30032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 29524 KB Output is correct
2 Correct 169 ms 41812 KB Output is correct
3 Correct 229 ms 34136 KB Output is correct
4 Correct 150 ms 43088 KB Output is correct
5 Correct 389 ms 32596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 244 ms 33080 KB Output is correct
2 Correct 182 ms 30224 KB Output is correct
3 Correct 293 ms 29780 KB Output is correct
4 Correct 289 ms 34900 KB Output is correct
5 Correct 184 ms 41044 KB Output is correct
6 Correct 319 ms 32716 KB Output is correct