답안 #1020696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1020696 2024-07-12T08:42:19 Z cnn008 Tree Rotations (POI11_rot) C++17
63 / 100
47 ms 28580 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=2e5+5;
const int mod=1e9+7;
 
int n,node=1,a[N],sz[N],cur,leaf[N];
ll ans[N];
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);
			ans[u]+=ans[v];
		}
	}
	assert(big);
	dfs(big);
	cur=0;
	ans[u]+=ans[big];
	for(auto v:g[u]) if(v!=big) add(v),upd(v),small=v;
	ans[u]+=min(cur,leaf[big]*leaf[small]-cur);
	//cebug(u,cur);
	//cout << u << " " << ans[u] << " " << a[big] << " " << a[small] << "\n";
}
void sol(){
	fw.init(N);
	cin>>n;
	input();
	pre_dfs(1);
	dfs(1);
	cout<<ans[1];
}
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 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9560 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9564 KB Output is correct
2 Correct 3 ms 9820 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 9820 KB Output is correct
2 Correct 4 ms 9564 KB Output is correct
3 Correct 4 ms 9820 KB Output is correct
4 Correct 4 ms 9908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10844 KB Output is correct
2 Correct 9 ms 10124 KB Output is correct
3 Correct 29 ms 10584 KB Output is correct
4 Correct 7 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 11612 KB Output is correct
2 Correct 27 ms 12980 KB Output is correct
3 Correct 30 ms 14308 KB Output is correct
4 Correct 34 ms 14164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 19060 KB Output is correct
2 Correct 42 ms 16300 KB Output is correct
3 Correct 47 ms 14444 KB Output is correct
4 Correct 43 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 29 ms 27152 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 30 ms 26820 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 31 ms 28580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 31 ms 28156 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -