# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
894955 | therealpain | Deblo (COCI18_deblo) | C++17 | 1087 ms | 13908 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define getbit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
#define TASK ""
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const long long inf = 1e18;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
int n;
int a[N];
vector <int> adj[N];
namespace subtask1 {
int d[1005];
void dfs(int u, int p) {
for (int v : adj[u]) {
if (v == p) continue;
d[v] = (d[u] ^ a[v]);
dfs(v, u);
}
}
void solve() {
long long ans = 0;
for (int i = 1; i <= n; ++i) {
memset(d, 0, sizeof d);
d[i] = a[i];
dfs(i, i);
for (int j = i; j <= n; ++j) {
ans = ans + d[j];
//cout << i << ' ' << j << ' ' << d[j] << '\n';
}
}
cout << ans;
}
};
namespace subtask3 {
const int LOG = 19;
int pref[N];
long long ans;
void solve() {
for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] ^ a[i];
//for (int i = 1; i <= n; ++i) cout << pref[i] << " \n"[i == n];
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
int lo = j, hi = n, res = j;
while (lo <= hi) {
int mid = lo + hi >> 1;
if ((pref[j] ^ pref[i - 1]) == (pref[mid] ^ pref[i - 1])) {
res = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
ans += 1LL * (res - j + 1) * (pref[j] ^ pref[i - 1]);
//cout << i << ' ' << j << ' ' << res << ' ' << (pref[j] ^ pref[i - 1]) << '\n';
j = res;
}
}
cout << ans;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (n <= 1000) subtask1::solve();
else subtask3::solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |