Submission #504116

# Submission time Handle Problem Language Result Execution time Memory
504116 2022-01-09T20:36:59 Z inksamurai Deblo (COCI18_deblo) C++17
90 / 90
108 ms 55816 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (ll)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define crep(i,x,n) for(ll i=x;i<n;i++)
#define drep(i,n) for(ll i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _34raRxL ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;

void prll(){
	cout<<"\n";
}
template<class te,class ...ti>
void prll(const te&v, const ti&...nv) { 
	cout<<v;
	if(sizeof...(nv)){
		cout<<" ";
		prll(nv...);
	}
}

using tpii=pair<ll,pair<ll,ll>>;
using pii=pair<ll,ll>;
using vi=vector<ll>;
using vll=vector<long long>;

ll cnt[100011][23][2];

void slv(){
	ll n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	vec(vi) adj(n);
	rep(i,n-1){
		ll u,v;
		cin>>u>>v;
		u--,v--;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	// vec(vec(vi)) cnt(n,vec(vi)(22,vi(2)));
	vi pxor(n,-1);
	ll ans=0;
	auto dfs=[&](auto self,ll v,ll par)->void{
		if(par!=-1){
			pxor[v]=pxor[par];
		}
		ll x=a[v];
		ans+=x;
		pxor[v]^=x;
		for(auto u : adj[v]){
			if(u!=par){
				self(self,u,v);
			}
		}
		rep(j,22){
			for(auto u : adj[v]){
				if(u==par){
					continue;
				}
				if(x&(1ll<<j)){
					// [0,0],[1,1]
					ans+=(1ll<<j)*cnt[u][j][0]*cnt[v][j][0];
					ans+=(1ll<<j)*cnt[u][j][1]*cnt[v][j][1];
				}else{
					// [0,1],[1,0]
					ans+=(1ll<<j)*cnt[u][j][0]*cnt[v][j][1];
					ans+=(1ll<<j)*cnt[u][j][1]*cnt[v][j][0];
				}
				if((pxor[v]^x)&(1ll<<j)){
					// if(v==0 and u==1){
					// 	prll(pxor[x],"\n");
					// }
					ans+=(1ll<<j)*cnt[u][j][0];
				}else{
					ans+=(1ll<<j)*cnt[u][j][1];
				}
				cnt[v][j][0]+=cnt[u][j][0];
				cnt[v][j][1]+=cnt[u][j][1];
			}
			cnt[v][j][bool(pxor[v]&(1ll<<j))]++;
		}
	};
	dfs(dfs,0,-1);
	prll(ans,"\n");
}

int main(){
_34raRxL;
	ll t=1;
	// cin>>t;
	slv();
//	
	return 0;
}

Compilation message

deblo.cpp: In function 'int main()':
deblo.cpp:99:5: warning: unused variable 't' [-Wunused-variable]
   99 |  ll t=1;
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 716 KB Output is correct
5 Correct 1 ms 716 KB Output is correct
6 Correct 90 ms 55816 KB Output is correct
7 Correct 88 ms 55796 KB Output is correct
8 Correct 81 ms 46016 KB Output is correct
9 Correct 87 ms 44924 KB Output is correct
10 Correct 108 ms 43920 KB Output is correct