Submission #893178

# Submission time Handle Problem Language Result Execution time Memory
893178 2023-12-26T15:42:43 Z vjudge1 Tree Rotations (POI11_rot) C++14
100 / 100
267 ms 48248 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'

ll N;
vector<ll> GR[500007];

void read_input() {
    cin >> N;
    ll NodeCnt = N;
    stack<ll> st;
    for(int i = 1, u; i <= 2 * N - 1; ++i) {
        cin >> u;
        if (u == 0) {
            ++NodeCnt;
            if (!st.empty()) {
                GR[st.top()].push_back(NodeCnt);
                if (GR[st.top()].size() == 2) st.pop();
            }
            st.push(NodeCnt);
        }
        else {
            GR[st.top()].push_back(u);
            if (GR[st.top()].size() == 2) st.pop();
        }
    }

//    for(int i = 1; i <= NodeCnt; ++i) {
//        cerr << "i = " << i << endl;
//        for(int c : GR[i]) cerr << c << " ";
//        cerr << endl;
//    }
}

ll sta[500007], fin[500007], stapos[500007], timeDfs = 1, sz[500007];

void preDfs(ll u) {
    sta[u] = timeDfs;
    if (!GR[u].size()) {
        stapos[timeDfs] = u;
        timeDfs++;
        sta[u] = fin[u] = timeDfs - 1;
        sz[u] = 1;
        return;
    }
    preDfs(GR[u][0]);
    preDfs(GR[u][1]);
    fin[u] = timeDfs - 1;
    sz[u] += sz[GR[u][0]] + sz[GR[u][1]];
//    cerr << u << " " << sta[u] << " " << fin[u] << " " << sz[u] << endl;
}

ll DP[500007], fw[500007];

void update(ll id, ll v) {
    for(; id <= N; id += id & (-id)) {
        fw[id] += v;
    }
}

ll getans(ll id) {
    ll ans = 0;
    for(; id > 0; id -= id & (-id)) {
        ans += fw[id];
    }
    return ans;
}

void Add(ll i) {
    update(i, 1);
}

void Del(ll i) {
    update(i, -1);
}

void Dfs(ll u) {
    if (GR[u].size() == 0) {
        Add(u);
        return;
    }

    if (sz[GR[u][0]] > sz[GR[u][1]]) swap(GR[u][0], GR[u][1]);
    Dfs(GR[u][0]);
    for(int i = sta[GR[u][0]]; i <= fin[GR[u][0]]; ++i) Del(stapos[i]);

    Dfs(GR[u][1]);
    ll cnt1 = 0, cnt2 = 0;
    for(int i = sta[GR[u][0]]; i <= fin[GR[u][0]]; ++i) {
        cnt1 += getans(stapos[i]);
        cnt2 += getans(N) - getans(stapos[i]);
//        cerr << i << " " << stapos[i] << " " << getans(stapos[i]) << " " << getans(N) - getans(stapos[i]) << endl;
    }
    for(int i = sta[GR[u][0]]; i <= fin[GR[u][0]]; ++i) {
        Add(stapos[i]);
    }

//    cerr << u << " " << GR[u][0] << " " <<GR[u][1] << " " << cnt1 << " " << cnt2 << endl;
    DP[u] = DP[GR[u][0]] + DP[GR[u][1]] + min(cnt1, cnt2);
}

int main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    if (fopen("FILE.INP", "r")) {
        freopen("FILE.INP", "r", stdin);
        freopen("FILE.OUT", "w", stdout);
    }

    read_input();
    preDfs(N + 1);
//    for(int i = 1; i <= N; ++i) cerr << stapos[i] << " "; cerr << endl;
    Dfs(N + 1);

    cout << DP[N + 1];
}

Compilation message

rot.cpp: In function 'int main()':
rot.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |         freopen("FILE.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:108:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |         freopen("FILE.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21084 KB Output is correct
2 Correct 4 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21080 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 21080 KB Output is correct
2 Correct 4 ms 21084 KB Output is correct
3 Correct 5 ms 21084 KB Output is correct
4 Correct 5 ms 21084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21596 KB Output is correct
2 Correct 6 ms 21340 KB Output is correct
3 Correct 5 ms 21596 KB Output is correct
4 Correct 6 ms 21596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 22620 KB Output is correct
2 Correct 11 ms 21596 KB Output is correct
3 Correct 24 ms 26708 KB Output is correct
4 Correct 9 ms 22364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 29276 KB Output is correct
2 Correct 25 ms 30808 KB Output is correct
3 Correct 30 ms 31936 KB Output is correct
4 Correct 28 ms 32148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 39656 KB Output is correct
2 Correct 44 ms 38332 KB Output is correct
3 Correct 49 ms 36404 KB Output is correct
4 Correct 41 ms 34128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 37456 KB Output is correct
2 Correct 58 ms 38676 KB Output is correct
3 Correct 58 ms 42068 KB Output is correct
4 Correct 56 ms 42836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 166 ms 42576 KB Output is correct
2 Correct 92 ms 41188 KB Output is correct
3 Correct 100 ms 41700 KB Output is correct
4 Correct 103 ms 41812 KB Output is correct
5 Correct 171 ms 39376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 41300 KB Output is correct
2 Correct 108 ms 48112 KB Output is correct
3 Correct 121 ms 44588 KB Output is correct
4 Correct 85 ms 48044 KB Output is correct
5 Correct 193 ms 40016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 42340 KB Output is correct
2 Correct 115 ms 42116 KB Output is correct
3 Correct 157 ms 40272 KB Output is correct
4 Correct 157 ms 41328 KB Output is correct
5 Correct 110 ms 48248 KB Output is correct
6 Correct 267 ms 40164 KB Output is correct