답안 #1020388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020388 2024-07-12T03:17:30 Z Thanhs Tree Rotations (POI11_rot) C++17
100 / 100
197 ms 65536 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
#define pb push_back
 
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x,y) x=min((x),(y))
#define setmax(x,y) x=max((x),(y))
#define sqr(x) (x)*(x)
#define fi first
#define se second
 
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l,int r){return l+((hdp()%(r-l+1))+r-l+1)%(r-l+1);}
 
const int N = 4e5 + 5;
const int mod = 998244353;
const int SQ = 450;
const int inf = 1e9;
const int bound = 1e7;
 
vector<int> g[N];
int n,m,c[N],sz[N],par[N];
vector<int> V[N];
stack<int> st;
long long dp[N];
 
int bit[N];
void upd(int i)
{
    for(;i<=n;i+=i&-i) 
    {
        st.push(i);
        bit[i]++;
    }
}
int get(int i)
{
    int res=0;
    for(;i;i-=i&-i) res+=bit[i];
    return res;
}
 
void dfs(int u=1,int p=0,bool keep=0)
{
    int bc=0;
    for(int v:g[u]) if(v!=p&&sz[v]>sz[bc]) bc=v;
    for(int v:g[u]) if(v!=p&&v!=bc) dfs(v,u,0);
    if(bc)
    {
        dfs(bc,u,1);
        swap(V[u],V[bc]);
        dp[u]=dp[bc];
    }
    for(int v:g[u]) if(v!=p&&v!=bc) dp[u]+=dp[v];
    long long sum1=0,sum2=0,xx=V[u].size();
    for(int v:g[u]) if(v!=bc) for(auto t:V[v])
    {
        V[u].pb(t);
        int x=get(t);
        sum1+=x;
        sum2+=xx-x;
    }
    dp[u]+=min(sum1,sum2);
    for(int v:g[u]) if(v!=bc) for(auto t:V[v]) upd(t);
    if(c[u])
    {
        V[u].pb(c[u]);
        upd(c[u]);
    }
    if(!keep) 
    {
        while(!st.empty())
        {
            bit[st.top()]=0;
            st.pop();
        }
    }
}
 
void dfs_sz(int u=1,int p=0)
{
    sz[u]=1;
    for(auto v:g[u]) if(v!=p)
    {
        dfs_sz(v,u);
        sz[u]+=sz[v];
    }
}
 
signed main()
{
    if (fopen("in.txt", "r")) {
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    }
    fastIO
    cin>>n;
    int u=0,v=0;
    for(int m=1;m<2*n;m++)
    {
        cin>>c[m];
        g[u].pb(m);
        par[m]=u;
        if(c[m]) 
        {
            while(g[u].size()==2) u=par[u];
        }
        else u=m;
    }
    dfs_sz();
    dfs();
    cout<<dp[1];
}

Compilation message

rot.cpp: In function 'int main()':
rot.cpp:103:13: warning: unused variable 'v' [-Wunused-variable]
  103 |     int u=0,v=0;
      |             ^
rot.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen("in.txt","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
rot.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen("out.txt","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19288 KB Output is correct
2 Correct 11 ms 19292 KB Output is correct
3 Correct 10 ms 19292 KB Output is correct
4 Correct 11 ms 19292 KB Output is correct
5 Correct 10 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19292 KB Output is correct
2 Correct 10 ms 19292 KB Output is correct
3 Correct 10 ms 19176 KB Output is correct
4 Correct 11 ms 19192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 19292 KB Output is correct
2 Correct 11 ms 19280 KB Output is correct
3 Correct 11 ms 19236 KB Output is correct
4 Correct 13 ms 19300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 20312 KB Output is correct
2 Correct 17 ms 19804 KB Output is correct
3 Correct 13 ms 20316 KB Output is correct
4 Correct 13 ms 20312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 22364 KB Output is correct
2 Correct 18 ms 21436 KB Output is correct
3 Correct 33 ms 24400 KB Output is correct
4 Correct 18 ms 22280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 27852 KB Output is correct
2 Correct 38 ms 29692 KB Output is correct
3 Correct 43 ms 32696 KB Output is correct
4 Correct 41 ms 32964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 43540 KB Output is correct
2 Correct 57 ms 39116 KB Output is correct
3 Correct 62 ms 36040 KB Output is correct
4 Correct 50 ms 33740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 38664 KB Output is correct
2 Correct 69 ms 40832 KB Output is correct
3 Correct 72 ms 47052 KB Output is correct
4 Correct 76 ms 46716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 155 ms 56008 KB Output is correct
2 Correct 129 ms 51820 KB Output is correct
3 Correct 105 ms 50800 KB Output is correct
4 Correct 120 ms 50320 KB Output is correct
5 Correct 159 ms 50888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 113 ms 52160 KB Output is correct
2 Correct 115 ms 63944 KB Output is correct
3 Correct 134 ms 60104 KB Output is correct
4 Correct 139 ms 65232 KB Output is correct
5 Correct 193 ms 55916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 134 ms 53300 KB Output is correct
2 Correct 116 ms 55116 KB Output is correct
3 Correct 185 ms 54068 KB Output is correct
4 Correct 130 ms 53628 KB Output is correct
5 Correct 112 ms 65536 KB Output is correct
6 Correct 197 ms 57016 KB Output is correct