Submission #504109

# Submission time Handle Problem Language Result Execution time Memory
504109 2022-01-09T20:32:46 Z inksamurai Deblo (COCI18_deblo) C++17
45 / 90
90 ms 65540 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>;

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)(30,vi(3)));
	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,25){
			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:97:5: warning: unused variable 't' [-Wunused-variable]
   97 |  ll t=1;
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 3 ms 2112 KB Output is correct
5 Correct 3 ms 1996 KB Output is correct
6 Runtime error 81 ms 65540 KB Execution killed with signal 9
7 Runtime error 77 ms 65540 KB Execution killed with signal 9
8 Runtime error 90 ms 65540 KB Execution killed with signal 9
9 Runtime error 81 ms 65540 KB Execution killed with signal 9
10 Runtime error 82 ms 65540 KB Execution killed with signal 9