結果

問題 No.1215 都市消滅ビーム
ユーザー HIR180HIR180
提出日時 2020-08-30 17:25:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,851 bytes
コンパイル時間 4,407 ms
コンパイル使用メモリ 262,968 KB
実行使用メモリ 98,392 KB
最終ジャッジ日時 2024-11-15 13:38:15
合計ジャッジ時間 176,525 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
85,868 KB
testcase_01 AC 37 ms
84,468 KB
testcase_02 AC 38 ms
89,252 KB
testcase_03 AC 38 ms
83,976 KB
testcase_04 AC 37 ms
87,388 KB
testcase_05 AC 38 ms
87,376 KB
testcase_06 AC 38 ms
87,632 KB
testcase_07 AC 43 ms
85,692 KB
testcase_08 AC 39 ms
85,540 KB
testcase_09 AC 42 ms
83,748 KB
testcase_10 AC 38 ms
82,576 KB
testcase_11 AC 42 ms
85,888 KB
testcase_12 AC 39 ms
87,152 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 561 ms
42,292 KB
testcase_20 TLE -
testcase_21 AC 394 ms
42,148 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 5,870 ms
45,028 KB
testcase_26 TLE -
testcase_27 TLE -
testcase_28 AC 5,493 ms
43,712 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 AC 3,690 ms
43,976 KB
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 AC 37 ms
41,692 KB
testcase_41 AC 36 ms
93,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}

int n, k, c[100005], d[100005];
vector<int>edge[1000005];
ll sum[2][100005];
int vt[2][100005];



#define SZ 100005
int up[20][SZ],dep[SZ];
struct dbling{
	bool ready = 0;
	void dfs(int v,int u,int d){
		up[0][v] = u; dep[v] = d;
		rep(i,edge[v].size()){
			if(edge[v][i] == u) continue;
			dfs(edge[v][i], v, d+1);
		}
	}
	void prepare(){
	    dfs(1,-1,0);
		rep(j,19){
			rep(i,SZ){
				if(up[j][i] == -1) up[j+1][i] = up[j][i];
				else up[j+1][i] = up[j][up[j][i]];
			}
		}
		ready = 1;
	}
	int get(int a,int b){
		if(dep[a] > dep[b]) swap(a,b);
		int D = dep[b]-dep[a];
		rep(i,20){
			if(((D>>i)&1)) b = up[i][b];
		}
		if(a == b) return a;
		for(int i=19;i>=0;i--){
			if(up[i][a] != up[i][b]){
				a = up[i][a];
				b = up[i][b];
			}
		}
		return up[0][a];
	}
	int dist(int a,int b){
		int c = get(a,b);
		return dep[a]+dep[b]-2*dep[c];
	}
	int dist(int a,int b,int c){
		//assuming c is lca of (a,b)
		return dep[a]+dep[b]-2*dep[c];
	}
	int go_up(int v,int a){
	    if(dep[v] < a) return -1;
	    rep(i,20) if(((a>>i)&1)) v = up[i][v];
	    return v;
	}
	//a ---- b
	//と並べたときのx番目 (1-indexed)
	//存在しない場合は-1を返す
	//verified : opencup Warsaw I
	int v_on_path(int a,int b,int x){
		int c = get(a,b);
		int d = dist(a,b,c);
		if(x <= 0 || x > d+1) return -1;
		if(x <= dep[a]-dep[c]+1){
			return go_up(a, x-1);
		}
		else{
			x = d+2-x;
			return go_up(b, x-1);
		}
	}
}kaede;
struct BIT
{
	int bit[200005], sz;
	void init(int nn = 200005){
		sz = nn;
		rep(i, sz) bit[i] = 0;
	}
	int f(int x)
	{
		return x&-x;
	}
	void add(int i,int x)
	{
		for(int s=i;s<sz;s+=f(s)) bit[s]+=x;
	}
	int sum(int i)
	{
		int res=0;
		for(int s=i;s>0;s-=f(s)) res+=bit[s];
		return res;
	}
}shiki;
ll rui[100005];
vector<ll>za;
ll interval(ll num){
	ll ret = 0;
	shiki.init();
	shiki.add(POSL(za, 0)+1, 1);
	//cout << num << endl;
	for(int i=1;i<=k;i++){
		ll x = rui[i]-num;
		int pos = POSL(za, x);
		ret += i-shiki.sum(pos);
		pos = POSL(za, rui[i]) + 1;
		shiki.add(pos, 1);
	}
	//cout << ret << endl;
	return ret;
}
vector<ll>Q[100005];
bool rem[100005];
int sub[100005];
int make(int v,int u){
	int cnt=1;
	for(int i=0;i<edge[v].size();i++){
		int v2=edge[v][i];
		if(rem[v2] || v2==u) continue;
		cnt+=make(v2,v);
	}
	return sub[v]=cnt;
}
P search(int v,int u,int siz){
	//size and index
	P res=mp(INF,-1);
	int s=1,m=0;
	for(int i=0;i<edge[v].size();i++){
		int v2=edge[v][i];
		if(rem[v2] || v2==u) continue;
		res=min(res,search(v2,v,siz));
		m=max(m,sub[v2]);
		s+=sub[v2];
	}
	m=max(m,siz-s);
	res=min(res,mp(m,v));
	return res;
}
ll B, C;
vector<ll>cand, cand2;
void make(int v, int u, int d){
	for(const auto a:Q[v]) cand.pb(a-d);
	rep(i, edge[v].size()){
		if(rem[edge[v][i]] || edge[v][i] == u) continue;
		make(edge[v][i], v, d+1);
	}
}
void make2(int v, int u, int d){
	for(const auto a:Q[v]){
		ll x = B-a+d;
		int k = POSU(cand, x);
		C += shiki.sum(k);
		cand2.pb(a-d);
	}
	rep(i, edge[v].size()){
		if(rem[edge[v][i]] || edge[v][i] == u) continue;
		make2(edge[v][i], v, d+1);
	}
}
void solve(int v){
	make(v,-1);
	int cent=search(v,-1,sub[v]).sc;
	rem[cent]=true;
	cand.clear();
	for(const auto a:Q[cent]) cand.pb(a);
	for(int i=0;i<edge[cent].size();i++)
	{
		if(rem[edge[cent][i]]) continue;
		make(edge[cent][i], cent, 1);
	}
	SORT(cand); ERASE(cand);
	shiki.init(cand.size()+2);
	for(const auto a:Q[cent]){
		ll x = B-a;
		int k = POSU(cand, x);
		C += shiki.sum(k);
		shiki.add(POSL(cand, a)+1, 1);
	}
	for(int i=0;i<edge[cent].size();i++)
	{
		if(rem[edge[cent][i]]) continue;
		cand2.clear();
		make2(edge[cent][i], cent, 1);
		for(const auto a:cand2){
			shiki.add(POSL(cand, a)+1, 1);
		}
	}
	for(int i=0;i<edge[cent].size();i++) if(!rem[edge[cent][i]]) solve(edge[cent][i]);
	rem[cent]=false;
}

ll essence(ll num){
	B = 2LL*num;
	ll ret = 0;
	rep(i, 100005) Q[i].clear();
	for(int i=1;i<=n;i++){
		Q[vt[0][i]].pb(2LL*sum[0][i]+dep[vt[0][i]]);
		Q[vt[1][i]].pb(2LL*sum[1][i]+dep[vt[1][i]]);
	}
	//repn(i, n) for(auto a:Q[i]) cout << i << " " << a << endl;
	C = 0; solve(1); ret += C; 
	rep(i, 100005) Q[i].clear();
	for(int i=1;i<=n;i++){
		Q[vt[0][i]].pb(2LL*sum[0][i]+dep[vt[0][i]]);
	}
	C = 0; solve(1); ret -= C; 
	rep(i, 100005) Q[i].clear();
	for(int i=1;i<=n;i++){
		Q[vt[1][i]].pb(2LL*sum[1][i]+dep[vt[1][i]]);
	}
	C = 0; solve(1); ret -= C; 
	return ret;
}
int main(){
	scanf("%d%d", &n, &k);
	repn(i, k){
		scanf("%d", &c[i]);
	}
	repn(i, k){
		scanf("%d", &d[i]); rui[i] = rui[i-1] + d[i];
	}
	rep(i, k+1) za.pb(rui[i]);
	SORT(za); ERASE(za);
	rep(i, n-1){
		int a, b; scanf("%d%d", &a, &b);
		edge[a].pb(b);
		edge[b].pb(a);
	}
	kaede.prepare();
	vector<ll>A, B;
	repn(i, k){
		sum[0][i] = sum[0][i-1] + d[i];
		if(i == 1) vt[0][i] = c[i];
		else vt[0][i] = kaede.get(vt[0][i-1], c[i]);
		A.pb(sum[0][i]+dep[vt[0][i]]);
		//cout << sum[0][i] << " " << vt[0][i] << endl;
	}
	repn(i, k){
		sum[1][i] = sum[1][i-1] + d[k+1-i];
		if(i == 1) vt[1][i] = c[k+1-i];
		else vt[1][i] = kaede.get(vt[1][i-1], c[k+1-i]);
		B.pb(sum[1][i]+dep[vt[1][i]]);
	}
	SORT(A); SORT(B);
	ll num = 1LL*k*(k+1)/2+1;
	num = (num+1)/2;
	//(lb, ub]
	ll lb = -2e15, ub = 2e15;
	while(ub-lb > 1){
		ll mid = (lb+ub)/2;
		//1, 何もしない
		ll cnt = 1;
		//k*k
		//if(mid == 3) cout << essence(mid) << endl;
		cnt += essence(mid);
		//2*k
		cnt += POSU(A, mid) + POSU(B, mid);
		//all
		if(sum[0][k]+dep[vt[0][k]] <= mid) cnt -= k;
		cnt -= interval(mid - sum[0][k] - dep[vt[0][k]]);
		assert(cnt >= 0);
		if(cnt < num) lb = mid; else ub = mid;
	}
	printf("%lld\n", ub);
}
0