結果

問題 No.2654 [Cherry 6th Tune] Re: start! (Black Sheep)
ユーザー 沙耶花沙耶花
提出日時 2024-02-23 23:50:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 813 ms / 7,000 ms
コード長 2,028 bytes
コンパイル時間 5,492 ms
コンパイル使用メモリ 271,212 KB
実行使用メモリ 47,132 KB
最終ジャッジ日時 2024-02-23 23:51:16
合計ジャッジ時間 25,251 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,384 KB
testcase_01 AC 4 ms
8,384 KB
testcase_02 AC 5 ms
8,384 KB
testcase_03 AC 554 ms
18,556 KB
testcase_04 AC 522 ms
18,216 KB
testcase_05 AC 302 ms
14,440 KB
testcase_06 AC 88 ms
10,304 KB
testcase_07 AC 266 ms
13,472 KB
testcase_08 AC 349 ms
15,076 KB
testcase_09 AC 558 ms
18,848 KB
testcase_10 AC 82 ms
10,304 KB
testcase_11 AC 426 ms
15,764 KB
testcase_12 AC 68 ms
9,920 KB
testcase_13 AC 367 ms
15,440 KB
testcase_14 AC 772 ms
21,524 KB
testcase_15 AC 744 ms
20,840 KB
testcase_16 AC 314 ms
14,468 KB
testcase_17 AC 584 ms
19,264 KB
testcase_18 AC 238 ms
13,040 KB
testcase_19 AC 710 ms
21,220 KB
testcase_20 AC 365 ms
15,384 KB
testcase_21 AC 422 ms
16,084 KB
testcase_22 AC 393 ms
15,836 KB
testcase_23 AC 5 ms
8,384 KB
testcase_24 AC 6 ms
8,384 KB
testcase_25 AC 6 ms
8,384 KB
testcase_26 AC 5 ms
8,384 KB
testcase_27 AC 7 ms
8,512 KB
testcase_28 AC 798 ms
22,360 KB
testcase_29 AC 813 ms
22,232 KB
testcase_30 AC 761 ms
22,232 KB
testcase_31 AC 779 ms
22,360 KB
testcase_32 AC 779 ms
22,232 KB
testcase_33 AC 714 ms
47,132 KB
testcase_34 AC 424 ms
22,960 KB
testcase_35 AC 557 ms
22,360 KB
testcase_36 AC 473 ms
47,132 KB
testcase_37 AC 717 ms
22,360 KB
testcase_38 AC 623 ms
47,132 KB
testcase_39 AC 5 ms
8,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001

long long a[200005];
vector<vector<int>> E;
vector<long long> t;
fenwick_tree<long long> Fc(200005),Fs(200005);
long long ans[200005];
int ma = 200005;
void dfs(int cv,int pv){
	Fc.add(a[cv],1);
	Fs.add(a[cv],t[a[cv]]);
	
	if(Fc.sum(0,200005)<=2)ans[cv] = -1;
	else{
		int mini,maxi;
		{
			int ok = 0,ng = ma;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(Fc.sum(0,mid)==0)ok = mid;
				else ng = mid;
			}
			mini = ok;
		}
		{
			int ok = 0,ng = ma;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(Fc.sum(mid,ma)>0)ok = mid;
				else ng = mid;
			}
			maxi = ok;
		}
		//cout<<cv<<' '<<mini<<' '<<maxi<<endl;
		if(mini==maxi){
			ans[cv] = 1;
		}
		else{
			ans[cv] = Inf64;
			
			rep(_,2){
				Fc.add(mini,-1);
				Fs.add(mini,-t[mini]);
				long long num = Fc.sum(0,ma);
				int ok = ma,ng = 0;
				while(ok-ng>1){
					int mid = (ok+ng)/2;
					if(Fc.sum(0,mid)>=(num+1)/2)ok = mid;
					else ng = mid;
				}
				ok--;
				
				long long tt =0;
				tt += t[ok] * Fc.sum(0,ok);
				tt -= Fs.sum(0,ok);
				tt -= t[ok] * Fc.sum(ok,ma);
				tt += Fs.sum(ok,ma);
				ans[cv] = min(ans[cv],tt);
				Fc.add(mini,1);
				Fs.add(mini,t[mini]);
				swap(mini,maxi);
			}
			
			
		}
	}
	rep(i,E[cv].size()){
		int to = E[cv][i];
		if(to==pv)continue;
		dfs(to,cv);
	}
	Fc.add(a[cv],-1);
	Fs.add(a[cv],-t[a[cv]]);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n;
	cin>>n;
	E.resize(n+1);
	rep(i,n+1)cin>>a[i];
	rep(i,n){
		int u,v;
		cin>>u>>v;
		E[u].push_back(v);
		E[v].push_back(u);
	}

	rep(i,n+1)t.push_back(a[i]);
	sort(t.begin(),t.end());
	t.erase(unique(t.begin(),t.end()),t.end());
	rep(i,n+1){
		a[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),a[i]));
	}
	
	dfs(0,-1);
	for(int i=1;i<=n;i++){
		cout<<ans[i]<<endl;
	}
	return 0;
}
0