Submission #1116323

# Submission time Handle Problem Language Result Execution time Memory
1116323 2024-11-21T13:59:50 Z FubuGold Tree Rotations (POI11_rot) C++14
100 / 100
212 ms 55112 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 500000;

struct BIT {
    vector<int> tree;
    int n;
    void init(int _n) {
        n = _n;
        tree.assign(n+1,0);
    }
    void update(int pos,int val) {
        for (;pos <= n;pos += pos & -pos) tree[pos] += val;
    }
    int query(int pos) {
        int sum = 0;
        for (;pos > 0;pos -= pos & -pos) sum += tree[pos];
        return sum;
    }
    int query(int l,int r) {
        if (l > r) return 0;
        return query(r) - query(l-1);
    }
} tree;

vector<int> adj[MAXN+1];
stack<pair<int,int>> st;
vector<int> ch[MAXN+1];
int val[MAXN+1];
int sz[MAXN+1];
int n;
int cnt;
long long ans = 0;

int cal_sz(int u) {
    if (adj[u].size() == 0) return sz[u] = 1;
    sz[u] += cal_sz(adj[u][0]);
    sz[u] += cal_sz(adj[u][1]);
    return sz[u];
}

void dfs(int u,int keep) {
    if (adj[u].size() == 0) {
        ch[u].push_back(val[u]);
        if (keep) tree.update(val[u],1);
        return;
    }
    int lt = adj[u][0], rt = adj[u][1];
    long long tmp = 0;
    if (sz[lt] < sz[rt]) {
        dfs(lt,0);
        dfs(rt,1);
        ch[u].swap(ch[rt]);
        for (int i=0;i<ch[lt].size();i++) {
            int v = ch[lt][i];
            ch[u].push_back(v);
//            cerr << u << ' ' << v << '\n';
            tmp += tree.query(1,v-1);
        }
        for (int i=0;i<ch[lt].size();i++) tree.update(ch[lt][i],1);
    }
    else {
        dfs(rt,0);
        dfs(lt,1);
        ch[u].swap(ch[lt]);
        for (int i=0;i<ch[rt].size();i++) {
            int v = ch[rt][i];
//            cerr << u << ' ' << v << '\n';
            ch[u].push_back(v);
            tmp += tree.query(v+1,n);
        }
        for (int i=0;i<ch[rt].size();i++) tree.update(ch[rt][i],1);
    }
    tmp = min(tmp,1ll * sz[lt] * sz[rt] - tmp);
    ans += tmp;
    if (!keep) {
        for (int i=0;i<ch[u].size();i++) tree.update(ch[u][i],-1);
    }
}

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    cin >> n;
    tree.init(n);
    for (int i=1;;i++) {
        cin >> val[i];
        if (st.size()) adj[st.top().first].push_back(i);
        if (st.size()) {
            st.top().second++;
            if (st.top().second == 2) st.pop();
        }
        if (val[i] == 0) {
            st.push({i,0});
        }
        else cnt++;
        if (cnt == n) break;
    }
    cal_sz(1);
    dfs(1,1);
    cout << ans;
    return 0;
}

Compilation message

rot.cpp: In function 'void dfs(int, int)':
rot.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for (int i=0;i<ch[lt].size();i++) {
      |                      ~^~~~~~~~~~~~~~
rot.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |         for (int i=0;i<ch[lt].size();i++) tree.update(ch[lt][i],1);
      |                      ~^~~~~~~~~~~~~~
rot.cpp:68:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for (int i=0;i<ch[rt].size();i++) {
      |                      ~^~~~~~~~~~~~~~
rot.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         for (int i=0;i<ch[rt].size();i++) tree.update(ch[rt][i],1);
      |                      ~^~~~~~~~~~~~~~
rot.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for (int i=0;i<ch[u].size();i++) tree.update(ch[u][i],-1);
      |                      ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 27472 KB Output is correct
2 Correct 5 ms 27472 KB Output is correct
3 Correct 5 ms 27472 KB Output is correct
4 Correct 5 ms 27692 KB Output is correct
5 Correct 5 ms 27472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27472 KB Output is correct
2 Correct 5 ms 27472 KB Output is correct
3 Correct 5 ms 27472 KB Output is correct
4 Correct 6 ms 27472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 27728 KB Output is correct
2 Correct 6 ms 27580 KB Output is correct
3 Correct 6 ms 27728 KB Output is correct
4 Correct 5 ms 27832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 28240 KB Output is correct
2 Correct 8 ms 27984 KB Output is correct
3 Correct 8 ms 28240 KB Output is correct
4 Correct 8 ms 28496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 29776 KB Output is correct
2 Correct 13 ms 29008 KB Output is correct
3 Correct 29 ms 31048 KB Output is correct
4 Correct 12 ms 29632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 32600 KB Output is correct
2 Correct 31 ms 34636 KB Output is correct
3 Correct 33 ms 36176 KB Output is correct
4 Correct 34 ms 36168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 42436 KB Output is correct
2 Correct 43 ms 39752 KB Output is correct
3 Correct 49 ms 37892 KB Output is correct
4 Correct 45 ms 36044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 37356 KB Output is correct
2 Correct 54 ms 40012 KB Output is correct
3 Correct 58 ms 44376 KB Output is correct
4 Correct 55 ms 44016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 48628 KB Output is correct
2 Correct 99 ms 46532 KB Output is correct
3 Correct 84 ms 45668 KB Output is correct
4 Correct 103 ms 45632 KB Output is correct
5 Correct 145 ms 46656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 44540 KB Output is correct
2 Correct 100 ms 54300 KB Output is correct
3 Correct 109 ms 51664 KB Output is correct
4 Correct 93 ms 55112 KB Output is correct
5 Correct 182 ms 49732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 45228 KB Output is correct
2 Correct 87 ms 47552 KB Output is correct
3 Correct 144 ms 47724 KB Output is correct
4 Correct 112 ms 47048 KB Output is correct
5 Correct 105 ms 54724 KB Output is correct
6 Correct 212 ms 50628 KB Output is correct