답안 #1020719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020719 2024-07-12T08:49:31 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
184 ms 28500 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef N_N_C
#include "debug.h"
#else
#define cebug(...) "Arya"
#endif
 
#define ll long long
 
const int N=4e5+5;
const int mod=1e9+7;
 
int n,node=1,a[N],sz[N],leaf[N];
ll ans,cur;
vector <int> g[N];
struct BIT{
	#define gb(x) (x&(-x))
   	int n;
   	vector <int> bit;
   	void init(int _n){
   		bit.resize(_n,0);
   		n=_n;
   	}
   	void update(int pos, int val){
   		int i=pos;
      	for (; i<n; i+=gb(i)) bit[i]+=val;
   	}
   	int get(int pos){
    	int ans=0,i=pos;
      	for (; i>=1; i-=gb(i)) ans+=bit[i];
      	return ans;
   	}
}fw;
void input(){
	int x;
	cin>>x;
	a[node]=x;
	if(x) return;
	int nw=node;
	g[nw].push_back(++node);
	input();
	g[nw].push_back(++node);
	input();
}
void pre_dfs(int u){
	if(a[u]) leaf[u]=1;
	sz[u]=1;
	for(auto v:g[u]){
		pre_dfs(v);
		sz[u]+=sz[v];
		leaf[u]+=leaf[v];
	}
}
void add(int u){
	if(a[u]) cur+=fw.get(a[u]-1);
	for(auto v:g[u]) add(v);
}
void del(int u){
	if(a[u]) fw.update(a[u],-1);
	for(auto v:g[u]) del(v);
}
void upd(int u){
	if(a[u]) fw.update(a[u],1);
	for(auto v:g[u]) upd(v);
}
void dfs(int u){
	if(a[u]){
		fw.update(a[u],1);
		return;
	}
	int big=0,small=0;
	for(auto v:g[u]) if(sz[v]>sz[big]) big=v;
	for(auto v:g[u]){
		if(v!=big){
			dfs(v);
			del(v);
		}
	}
	assert(big);
	dfs(big);
	cur=0;
	for(auto v:g[u]) if(v!=big) add(v),upd(v),small=v;
	ans+=min(1ll*cur,1ll*leaf[big]*leaf[small]-cur);
}
void sol(){
	fw.init(N);
	cin>>n;
	input();
	pre_dfs(1);
	dfs(1);
	cout<<ans;
}
signed main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen(".inp", "r", stdin);
    // freopen(".out", "w", stdout);
    int tt=1;
    //cin>>tt; 
    while(tt--){
    	sol();
    }
    cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 4 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 2 ms 14428 KB Output is correct
5 Correct 3 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14428 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 3 ms 14428 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 14680 KB Output is correct
2 Correct 3 ms 14428 KB Output is correct
3 Correct 4 ms 14440 KB Output is correct
4 Correct 3 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14680 KB Output is correct
2 Correct 5 ms 14684 KB Output is correct
3 Correct 4 ms 14684 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 15452 KB Output is correct
2 Correct 10 ms 14940 KB Output is correct
3 Correct 25 ms 15708 KB Output is correct
4 Correct 9 ms 15448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 16472 KB Output is correct
2 Correct 26 ms 17752 KB Output is correct
3 Correct 27 ms 18524 KB Output is correct
4 Correct 29 ms 18540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 22620 KB Output is correct
2 Correct 35 ms 20568 KB Output is correct
3 Correct 48 ms 19036 KB Output is correct
4 Correct 36 ms 18268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 19292 KB Output is correct
2 Correct 47 ms 20316 KB Output is correct
3 Correct 48 ms 22864 KB Output is correct
4 Correct 49 ms 22608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 23644 KB Output is correct
2 Correct 107 ms 22868 KB Output is correct
3 Correct 87 ms 22928 KB Output is correct
4 Correct 95 ms 22320 KB Output is correct
5 Correct 158 ms 21164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 22864 KB Output is correct
2 Correct 92 ms 27540 KB Output is correct
3 Correct 103 ms 25784 KB Output is correct
4 Correct 103 ms 28240 KB Output is correct
5 Correct 184 ms 22096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 23120 KB Output is correct
2 Correct 77 ms 23892 KB Output is correct
3 Correct 147 ms 22364 KB Output is correct
4 Correct 106 ms 23144 KB Output is correct
5 Correct 73 ms 28500 KB Output is correct
6 Correct 182 ms 22356 KB Output is correct