QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#355942 | #7305. Lowest Common Ancestor | 8BQube# | WA | 34ms | 8412kb | C++20 | 2.8kb | 2024-03-17 14:06:51 | 2024-03-17 14:06:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define pb push_back
#define ALL(v) v.begin(), v.end()
const int N = 200005;
template<class T>
struct BIT {
int n;
T bit[N], org[N];
void init(int _n) {
n = _n;
fill_n(bit + 1, n, 0);
fill_n(org + 1, n, 0);
}
void modify(int x, T v) {
for (org[x] += v; x <= n; x += x & -x)
bit[x] += v;
}
T query(int x) {
T res = 0;
for (; x; x -= x & -x)
res += bit[x];
return res;
}
};
BIT<int> counts;
BIT<ll> weighted;
struct Heavy_light_Decomposition {
int n, ulink[N], deep[N], mxson[N], w[N], pa[N];
int t, pl[N], data[N], mx[N];
vector<int> G[N];
void init(int _n) {
n = _n, t = 0;
for (int i = 1; i <= n; ++i)
G[i].clear(), mxson[i] = 0, mx[i] = 0;
}
void add_edge(int a, int b) {
G[a].pb(b);
G[b].pb(a);
}
void dfs(int u, int f, int d) {
w[u] = 1, pa[u] = f, deep[u] = d++;
for (auto &i : G[u])
if (i != f) {
dfs(i, u, d), w[u] += w[i];
if (w[mxson[u]] < w[i]) mxson[u] = i;
}
}
void cut(int u, int link) {
pl[u] = ++t, ulink[u] = link;
mx[link] = max(mx[link], pl[u]);
if (!mxson[u]) return;
cut(mxson[u], link);
for (auto i : G[u])
if (i != pa[u] && i != mxson[u])
cut(i, i);
}
void build() {
dfs(1, 0, 1);
cut(1, 1);
counts.init(n);
weighted.init(n);
}
void modify(int u) {
while (u) {
counts.modify(pl[u], 1);
weighted.modify(pl[u], data[u]);
u = pa[ulink[u]];
}
}
ll query(int u) {
ll res = 0, prv = 0;
while (u) {
int boss = ulink[u];
ll below = counts.query(pl[mx[boss]]) - counts.query(pl[u]) + counts.org[u];
res += (below - prv) * data[u];
res += weighted.query(pl[u] - 1) - weighted.query(pl[boss] - 1);
prv = counts.query(pl[mx[boss]]) - counts.query(pl[boss] - 1);
u = pa[boss];
}
return res;
}
} hld;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
while (cin >> n) {
hld.init(n);
for (int i = 1; i <= n; ++i)
cin >> hld.data[i];
for (int i = 2; i <= n; ++i) {
int p;
cin >> p;
hld.add_edge(p, i);
}
hld.build();
for (int i = 2; i <= n; ++i) {
hld.modify(i - 1);
cout << hld.query(i) << "\n";
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 8320kb
input:
3 1 2 3 1 1 5 1 2 3 4 5 1 2 2 1
output:
1 2 1 3 5 4
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 34ms
memory: 8412kb
input:
13 887 4555 8570 5485 8611 9500 295 2499 83 4959 9772 8620 3825 4 4 1 3 7 12 2 1 9 5 8 12 16 3405 9213 9243 8188 2733 1333 1614 4907 4256 7506 4228 1286 6121 6155 4082 8084 1 12 9 4 2 13 3 8 9 7 11 15 8 1 10 5 7183 1481 9468 8242 1044 1 5 5 1 3 5758 3693 1694 1 2 20 6683 4426 5616 8166 810 4265 3000...
output:
887 14942 6372 9457 21897 22192 24396 7900 -965 30812 48895 67105 3405 6810 7865 18775 14690 11829 16165 17467 5473 16057 17343 22457 22893 26543 -1195 7183 14366 16454 16454 5758 9451 6683 16725 31181 24891 29156 35244 35244 41742 37300 51071 58569 53411 77189 93562 100245 102242 78218 126886 88723...
result:
wrong answer 2nd numbers differ - expected: '6372', found: '14942'