結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー 👑 kmjpkmjp
提出日時 2024-02-23 23:16:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 845 ms / 7,000 ms
コード長 2,468 bytes
コンパイル時間 4,170 ms
コンパイル使用メモリ 212,904 KB
実行使用メモリ 55,616 KB
最終ジャッジ日時 2024-02-23 23:16:42
合計ジャッジ時間 26,706 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
24,072 KB
testcase_01 AC 8 ms
24,072 KB
testcase_02 AC 9 ms
24,072 KB
testcase_03 AC 629 ms
35,312 KB
testcase_04 AC 544 ms
33,532 KB
testcase_05 AC 337 ms
28,656 KB
testcase_06 AC 94 ms
25,372 KB
testcase_07 AC 259 ms
27,768 KB
testcase_08 AC 359 ms
31,256 KB
testcase_09 AC 615 ms
34,184 KB
testcase_10 AC 90 ms
25,320 KB
testcase_11 AC 407 ms
31,824 KB
testcase_12 AC 74 ms
25,116 KB
testcase_13 AC 411 ms
31,576 KB
testcase_14 AC 776 ms
37,828 KB
testcase_15 AC 758 ms
35,896 KB
testcase_16 AC 322 ms
28,692 KB
testcase_17 AC 608 ms
34,584 KB
testcase_18 AC 236 ms
27,392 KB
testcase_19 AC 735 ms
37,604 KB
testcase_20 AC 382 ms
31,552 KB
testcase_21 AC 418 ms
31,984 KB
testcase_22 AC 421 ms
31,936 KB
testcase_23 AC 8 ms
24,092 KB
testcase_24 AC 9 ms
24,096 KB
testcase_25 AC 9 ms
24,096 KB
testcase_26 AC 9 ms
24,096 KB
testcase_27 AC 10 ms
24,116 KB
testcase_28 AC 835 ms
37,060 KB
testcase_29 AC 845 ms
36,976 KB
testcase_30 AC 832 ms
37,028 KB
testcase_31 AC 818 ms
38,360 KB
testcase_32 AC 809 ms
38,360 KB
testcase_33 AC 759 ms
55,616 KB
testcase_34 AC 415 ms
39,088 KB
testcase_35 AC 826 ms
37,004 KB
testcase_36 AC 670 ms
55,564 KB
testcase_37 AC 824 ms
37,064 KB
testcase_38 AC 692 ms
55,580 KB
testcase_39 AC 6 ms
24,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
int A[202020],P[202020];
vector<int> As;
ll ret[202020];
vector<int> E[202020];
vector<pair<int,int>> V;

template<class V, int ME> class BIT {
public:
	V bit[1<<ME],val[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
	void set(int e,V v) { add(e,v-val[e]);}
	int lower_bound(V val) {
		V tv=0; int i,ent=0;
		for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
		return ent;
	}
};
BIT<ll,20> num,sum;

ll hoge(int rem) {
	num.add(rem,-1);
	sum.add(rem,-V[rem].first);
	
	
	int tnum=num(N+2);
	int L,R;
	if(tnum%2==1) {
		L=R=num.lower_bound(tnum/2+1);
	}
	else {
		L=num.lower_bound(tnum/2);
		R=num.lower_bound(tnum/2+1);
	}
	ll ret=(sum(N+2)-sum(R-1))-(num(N+2)-num(R-1))*V[R].first;
	ret+=num(L)*V[R].first-sum(L);
	
	if(V[rem].first==V[L].first&&V[rem].first==V[R].first) ret++;
	
	num.add(rem,1);
	sum.add(rem,V[rem].first);
	return ret;
}

void dfs(int cur,int pre) {
	num.add(P[cur],1);
	sum.add(P[cur],A[cur]);
	
	int tnum=num(N+2);
	if(tnum<=2) {
		ret[cur]=-1;
	}
	else {
		int L=num.lower_bound(1);
		int R=num.lower_bound(tnum);
		
		if(L==R) {
			ret[cur]=1;
		}
		else {
			ret[cur]=min(hoge(L),hoge(R));
			
		}
	}
	
	FORR(e,E[cur]) if(e!=pre) dfs(e,cur);
	
	num.add(P[cur],-1);
	sum.add(P[cur],-A[cur]);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	
	FOR(i,N+1) {
		cin>>A[i];
		V.push_back({A[i],i});
	}
	sort(ALL(V));
	FOR(i,N+1) P[V[i].second]=i;
	
	FOR(i,N) {
		cin>>x>>y;
		E[x].push_back(y);
		E[y].push_back(x);
	}
	dfs(0,0);
	FOR(i,N) cout<<ret[i+1]<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0