Submission #896213

# Submission time Handle Problem Language Result Execution time Memory
896213 2024-01-01T02:54:13 Z 12345678 Tree Rotations (POI11_rot) C++17
63 / 100
120 ms 20672 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=4e5+5, fx=2e5+5;
int n, cnt, t, x, sz[nx], p[nx], c;
vector<int> d[nx];

struct fenwick
{
    int d[fx];
    void add(int i, int vl)
    {
        while (i<fx) d[i]+=vl, i+=(i&-i);
    }
    int query(int i)
    {
        int res=0;
        while (i>0) res+=d[i], i-=(i&-i);
        return res;
    }
} f;

void dfssz(int u)
{
    sz[u]=1;
    for (auto v:d[u]) dfssz(v), sz[u]+=sz[v];
}

pair<int, int> dfsvl(int u)
{
    if (u<=n) return make_pair(f.query(u-1), f.query(fx-1)-f.query(u));
    auto l=dfsvl(d[u][0]), r=dfsvl(d[u][1]);
    return make_pair(l.first+r.first, l.second+r.second);
}

void dfsrem(int u)
{
    if (u<=n) return f.add(u, -1), void();
    dfsrem(d[u][0]);
    dfsrem(d[u][1]);
}

void dfsadd(int u)
{
    if (u<=n) return f.add(u, 1), void();
    dfsadd(d[u][0]);
    dfsadd(d[u][1]);
}

int solve(int u, bool del)
{
    if (u<=n&&!del) f.add(u, 1);
    if (u<=n) return 0;
    if (sz[d[u][0]]<sz[d[u][1]]) swap(d[u][0], d[u][1]);
    int res=solve(d[u][1], 1)+solve(d[u][0], 0);
    //if (u==8) cout<<f.query(0)<<' '<<f.query(1)<<' '<<f.query(5)<<'\n';
    auto tmp=dfsvl(d[u][1]);
    if (del) dfsrem(d[u][0]);
    else dfsadd(d[u][1]);
    //cout<<u<<' '<<del<<' '<<res<<' '<<tmp.first<<' '<<tmp.second<<' '<<min(tmp.first, tmp.second)<<'\n';
    return res+min(tmp.first, tmp.second);
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>x;
    t=c=n+1;
    while (cnt!=n)
    {
        cin>>x;
        if (!x) d[c].push_back(++t), p[t]=c, c=t;
        else 
        {
            d[c].push_back(x); cnt++;
            while (d[c].size()==2) c=p[c];
        }
    }
    dfssz(n+1);
    cout<<solve(n+1, 0);
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 12740 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 2 ms 12780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 12776 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12888 KB Output is correct
2 Correct 4 ms 12892 KB Output is correct
3 Correct 4 ms 12892 KB Output is correct
4 Correct 4 ms 13144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 13660 KB Output is correct
2 Correct 11 ms 13320 KB Output is correct
3 Correct 22 ms 14076 KB Output is correct
4 Correct 8 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 14428 KB Output is correct
2 Correct 23 ms 15716 KB Output is correct
3 Correct 27 ms 16728 KB Output is correct
4 Correct 25 ms 16480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 19036 KB Output is correct
2 Correct 33 ms 18276 KB Output is correct
3 Correct 44 ms 17184 KB Output is correct
4 Correct 34 ms 16484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 16992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 120 ms 20672 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 90 ms 20448 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 20380 KB Output isn't correct
2 Halted 0 ms 0 KB -