# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
636523 |
2022-08-29T13:09:00 Z |
vovamr |
Deblo (COCI18_deblo) |
C++17 |
|
107 ms |
30368 KB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 1e5 + 5;
const int D = 23;
int a[N], sz[N], xr[N];
ve<int> gr[N];
int cnt[N][D];
ll ans = 0;
inline void dfs(int v, int p) {
if (v == p) xr[v] = a[v];
sz[v] = 1;
for (auto &to : gr[v]) {
if (to == p) continue;
xr[to] = xr[v] ^ a[to];
dfs(to, v); sz[v] += sz[to];
}
for (int b = 0; b < D; ++b) {
ll cnt1 = 0;
for (auto &to : gr[v]) {
if (to == p) continue;
cnt1 += cnt[to][b];
}
ll cnt0 = 0;
for (auto &to : gr[v]) {
if (to == p) continue;
cnt0 += sz[to] - cnt[to][b];
}
for (auto &to : gr[v]) {
if (to == p) continue;
ll ways = 0;
if (a[v] >> b & 1 ^ 1) {
ways += cnt[to][b] * 1ll * (cnt0 - (sz[to] - cnt[to][b]));
ways += (sz[to] - cnt[to][b]) * 1ll * (cnt1 - cnt[to][b]);
ans += ways * (1ll << b);
}
else {
ways += cnt[to][b] * 1ll * (cnt1 - cnt[to][b]);
ways += (sz[to] - cnt[to][b]) * 1ll * (cnt0 - (sz[to] - cnt[to][b]));
ans += ways * (1ll << b);
}
}
int pr = (v == p ? 0 : xr[p]);
for (auto &to : gr[v]) {
if (to == p) continue;
ll ways = 0;
ways += cnt[to][b] * 1ll * (pr >> b & 1 ^ 1);
ways += (sz[to] - cnt[to][b]) * 1ll * (pr >> b & 1);
ans += ways * (1ll << (b + 1));
}
}
for (auto &to : gr[v]) {
if (to == p) continue;
for (int b = 0; b < D; ++b) cnt[v][b] += cnt[to][b];
}
for (int b = 0; b < D; ++b) cnt[v][b] += xr[v] >> b & 1;
ans += 2 * a[v];
}
inline void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
int v, u;
cin >> v >> u, --v, --u;
gr[v].pb(u), gr[u].pb(v);
}
dfs(0, 0);
cout << ans / 2;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
Compilation message
deblo.cpp: In function 'void dfs(int, int)':
deblo.cpp:62:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
62 | if (a[v] >> b & 1 ^ 1) {
| ~~~~~~~~~~^~~
deblo.cpp:80:40: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
80 | ways += cnt[to][b] * 1ll * (pr >> b & 1 ^ 1);
| ~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2828 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
81 ms |
30368 KB |
Output is correct |
7 |
Correct |
77 ms |
30192 KB |
Output is correct |
8 |
Correct |
77 ms |
20152 KB |
Output is correct |
9 |
Correct |
81 ms |
19064 KB |
Output is correct |
10 |
Correct |
107 ms |
17868 KB |
Output is correct |