답안 #1105905

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1105905 2024-10-28T10:00:36 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
231 ms 42700 KB
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define task "task"

const int ar = 4e5 + 5;
const ll mod = 1e9 + 7;
int n;
int cur;
vector<int> ad[ar];
void input(int p)
{
    int x;
    cin >> x;
    int tmp = (x == 0) ? ++cur : x;
    int C = cur;
    if (x == 0)
    {
        input(C);
        input(C);
    }
    if (p != 0) ad[p].push_back(tmp);
}
int sz[ar];
int Sz[ar];
int timedfs = 0;
int in[ar], out[ar], node[ar];
void dfs(int u, int p)
{
    sz[u] = 1;
    in[u] = ++timedfs;
    node[timedfs] = u;
    bool ok = false;
    for (auto v : ad[u])
    {
        if (v == p) continue;
        ok = true;
        dfs(v, u);
        Sz[u] += Sz[v];
    }
    if (!ok) Sz[u]++;
    out[u] = timedfs;
}
int bit[ar];
void update(int u, int v)
{
    if (u == 0)
    {
        cout << 0;
        exit(0);
    }
    while(u <= n)
    {
        bit[u] += v;
        u += u & (-u);
    }
}
int get(int u)
{
    int ans = 0;
    while(u > 0)
    {
        ans += bit[u];
        u -= u & (-u);
    }
    return ans;
}
ll res[ar];
void hld(int u, int p)
{
    int bigc = 0;
    int _sz = 0;
    for (auto v : ad[u])
    {
        if (v == p) continue;
        if (_sz < Sz[v])
        {
            _sz = Sz[v];
            bigc = v;
        }
    }
    for (auto v : ad[u])
    {
        if (v == p || v == bigc) continue;
        hld(v, u);
        res[u] += res[v];
        for (int i = in[v]; i <= out[v]; i++)
        {
            int uu = node[i];
            if (uu > n) continue;
            update(uu, -1);
        }
    }
    if (bigc) hld(bigc, u), res[u] += res[bigc];
    ll add = 0;
    for (auto v : ad[u])
    {
        if (v == p || v == bigc) continue;
        for (int i = in[v]; i <= out[v]; i++)
        {
            int uu = node[i];
            if (uu > n) continue;
            if (bigc)
            {
                add += (bigc == ad[u][0]) ? get(n) - get(uu) : get(uu - 1);
            }
        }
        for (int i = in[v]; i <= out[v]; i++)
        {
            int uu = node[i];
            if (uu > n) continue;
            update(uu, 1);
        }
    }
    if (u <= n) update(u, 1);
    if (bigc)
    {
        add = min(add, 1ll * Sz[ad[u][0]] * Sz[ad[u][1]] - add);
    }
    res[u] += add;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if (fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> n;
    cur = n;
    input(0);
    dfs(n + 1, 0);
    hld(n + 1, 0);
    cout << res[n + 1];
}



Compilation message

rot.cpp: In function 'int main()':
rot.cpp:132:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18000 KB Output is correct
2 Correct 4 ms 18084 KB Output is correct
3 Correct 4 ms 18000 KB Output is correct
4 Correct 5 ms 18068 KB Output is correct
5 Correct 4 ms 18000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 18000 KB Output is correct
2 Correct 3 ms 18000 KB Output is correct
3 Correct 4 ms 18100 KB Output is correct
4 Correct 4 ms 18000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18000 KB Output is correct
2 Correct 5 ms 18000 KB Output is correct
3 Correct 4 ms 18000 KB Output is correct
4 Correct 4 ms 18000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 18512 KB Output is correct
2 Correct 6 ms 18428 KB Output is correct
3 Correct 5 ms 18512 KB Output is correct
4 Correct 6 ms 18768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 20048 KB Output is correct
2 Correct 11 ms 20776 KB Output is correct
3 Correct 24 ms 21328 KB Output is correct
4 Correct 10 ms 21840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 22352 KB Output is correct
2 Correct 33 ms 24648 KB Output is correct
3 Correct 38 ms 26972 KB Output is correct
4 Correct 32 ms 26704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 33872 KB Output is correct
2 Correct 41 ms 29256 KB Output is correct
3 Correct 47 ms 26440 KB Output is correct
4 Correct 39 ms 25168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 26148 KB Output is correct
2 Correct 52 ms 28748 KB Output is correct
3 Correct 54 ms 33864 KB Output is correct
4 Correct 54 ms 33352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 32840 KB Output is correct
2 Correct 91 ms 31564 KB Output is correct
3 Correct 91 ms 32272 KB Output is correct
4 Correct 100 ms 30924 KB Output is correct
5 Correct 151 ms 28832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 30792 KB Output is correct
2 Correct 101 ms 40868 KB Output is correct
3 Correct 129 ms 37448 KB Output is correct
4 Correct 114 ms 42568 KB Output is correct
5 Correct 231 ms 30032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 30328 KB Output is correct
2 Correct 117 ms 33352 KB Output is correct
3 Correct 154 ms 30144 KB Output is correct
4 Correct 131 ms 31472 KB Output is correct
5 Correct 122 ms 42700 KB Output is correct
6 Correct 221 ms 30264 KB Output is correct