結果

問題 No.3343 Distance Sum of Large Tree
コンテスト
ユーザー kmjp
提出日時 2025-11-13 23:36:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,037 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 206,960 KB
実行使用メモリ 12,780 KB
最終ジャッジ日時 2025-11-13 23:36:31
合計ジャッジ時間 4,845 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27 WA * 3
権限があれば一括ダウンロードができます

ソースコード

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;
ll A[101010];
ll B[101010];
ll C[101010];
ll P[101010];
vector<int> E[101010];
ll S;
ll NS[202020];

const ll mo=998244353;
ll ret=0;

void dfs(int cur) {
	NS[cur]=A[cur];
	ll LS=S-NS[cur];
	
	vector<pair<int,ll>> num;
	FORR(e,E[cur]) {
		dfs(e);
		LS-=NS[e];
		NS[cur]+=NS[e];
		num.push_back({C[e],NS[e]});
		
		//左
		ll lef=C[e]*(C[e]+1)/2;
		ll ri=(A[cur]-1-C[e])*(A[cur]-C[e])/2;
		(ret+=(lef+ri)%mo*(NS[e]%mo))%=mo;
		
	}
	//親方向
	(ret+=LS%mo*(NS[cur]%mo))%=mo;
	//親から左右
	ll lef=B[cur]*(B[cur]+1)/2;
	ll ri=(A[cur]-1-B[cur])*(A[cur]-B[cur])/2;
	(ret+=(lef+ri)%mo*(LS%mo))%=mo;
	
	sort(ALL(num));
	ll numS=0;
	ll distS=0;
	FORR2(a,b,num) {
		b%=mo;
		a%=mo;
		(ret+=b*(numS*a%mo-distS)%mo)%=mo;
		//親方向に向けた移動
		if(LS) (ret+=(LS%mo)*(b%mo)%mo*abs(a-B[cur])%mo)%=mo;
		
		(numS+=b)%=mo;
		(distS+=a*b)%=mo;
	}
	ret=(ret%mo+mo)%mo;
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i];
		S+=A[i];
		__int128 a=A[i]-1;
		ret+=(a*(a+1)*(2*a+1)/6+a*(a+1)/2)/2%mo;
	}
	FOR(i,N-1) {
		cin>>B[i+1];
		B[i+1]--;
	}
	FOR(i,N-1) {
		cin>>C[i+1];
		C[i+1]--;
	}
	FOR(i,N-1) {
		cin>>P[i+1];
		P[i+1]--;
		E[P[i+1]].push_back(i+1);
	}
	dfs(0);
	cout<<ret*2%mo<<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