답안 #869371

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
869371 2023-11-04T08:21:46 Z MilosMilutinovic Tree Rotations (POI11_rot) C++14
100 / 100
101 ms 30916 KB
#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;

template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}

int readint(){	
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int n,tsz,dfn[400005],dfo[400005],fenw[400005];
vector<int> ch[400005],ord; 
ll ans;

void change(int p,int x){
	for(;p<400005;p+=p&-p) fenw[p]+=x;
}

int query(int p){
	int ret=0;
	for(;p;p-=p&-p) ret+=fenw[p];
	return ret;
}

int make(){
	int x=readint();
	if(x){
		dfn[x]=ord.size();
		dfo[x]=ord.size();
		ord.pb(x);
		return x;
	}
	int nd=++tsz;
	ch[nd].pb(make());
	ch[nd].pb(make());
	dfn[nd]=dfn[ch[nd][0]];
	dfo[nd]=dfo[ch[nd][1]];
	return nd;
}

void dfs(int x,bool ok){
	if(x<=n){
		if(ok) change(x,+1);
		return;
	}
	int szl=dfo[ch[x][0]]-dfn[ch[x][0]];
	int szr=dfo[ch[x][1]]-dfn[ch[x][1]];
	if(szl>szr) swap(szl,szr),swap(ch[x][0],ch[x][1]);
	dfs(ch[x][0],0);
	dfs(ch[x][1],1);
	ll inv=0;
	for(int i=dfn[ch[x][0]];i<=dfo[ch[x][0]];i++){
		inv+=query(ord[i]-1);
	}
	inv=min(inv,(szl+1)*1ll*(szr+1)-inv);
	ans+=inv;
	if(ok) for(int i=dfn[ch[x][0]];i<=dfo[ch[x][0]];i++) change(ord[i],1);
	else for(int i=dfn[ch[x][1]];i<=dfo[ch[x][1]];i++) change(ord[i],-1);
}

int main(){
	n=readint();
	tsz=n;
	int rt=make();
	dfs(rt,0);
	printf("%lld",ans);
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
5 Correct 2 ms 12920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 2 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13144 KB Output is correct
2 Correct 5 ms 13144 KB Output is correct
3 Correct 4 ms 13148 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14168 KB Output is correct
2 Correct 7 ms 13452 KB Output is correct
3 Correct 15 ms 14428 KB Output is correct
4 Correct 6 ms 14172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 15512 KB Output is correct
2 Correct 16 ms 16856 KB Output is correct
3 Correct 19 ms 18220 KB Output is correct
4 Correct 19 ms 18272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 22988 KB Output is correct
2 Correct 26 ms 20460 KB Output is correct
3 Correct 31 ms 18636 KB Output is correct
4 Correct 24 ms 17872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 19156 KB Output is correct
2 Correct 41 ms 20424 KB Output is correct
3 Correct 33 ms 23756 KB Output is correct
4 Correct 34 ms 23544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 79 ms 24772 KB Output is correct
2 Correct 61 ms 23496 KB Output is correct
3 Correct 62 ms 23884 KB Output is correct
4 Correct 60 ms 22976 KB Output is correct
5 Correct 87 ms 22056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 24004 KB Output is correct
2 Correct 61 ms 29636 KB Output is correct
3 Correct 80 ms 27272 KB Output is correct
4 Correct 55 ms 30456 KB Output is correct
5 Correct 101 ms 22728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 24008 KB Output is correct
2 Correct 71 ms 25032 KB Output is correct
3 Correct 98 ms 23272 KB Output is correct
4 Correct 63 ms 24004 KB Output is correct
5 Correct 73 ms 30916 KB Output is correct
6 Correct 96 ms 23216 KB Output is correct