QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#717638 | #9492. 树上简单求和 | Hunster | 0 | 4430ms | 143776kb | C++20 | 4.0kb | 2024-11-06 18:35:04 | 2024-11-06 18:35:04 |
Judging History
answer
#include <bits/stdc++.h>
using LL = long long;
using ULL = unsigned long long;
constexpr int N = 200005;
int n, m;
ULL a[N];
std::vector<int> tree[2][N];
int fa[2][N][20], dep[2][N], dfn[2][N], size[2][N], dfc[2];
template <int o>
void pre_dfs(int u, int p) {
if (o == 1) a[u] += a[p];
size[o][u] = 1;
dfn[o][u] = ++dfc[o];
dep[o][u] = dep[o][p] + 1;
fa[o][u][0] = p;
for (int i = 1; i < 20; i++) fa[o][u][i] = fa[o][fa[o][u][i - 1]][i - 1];
for (int v : tree[o][u]) {
if (v == p) continue;
pre_dfs<o>(v, u);
size[o][u] += size[o][v];
}
}
template <int o>
int lca(int x, int y) {
if (dep[o][x] > dep[o][y]) std::swap(x, y);
for (int i = 19; i >= 0; i--) if (dep[o][fa[o][y][i]] >= dep[o][x]) y = fa[o][y][i];
if (x == y) return x;
for (int i = 19; i >= 0; i--) if (fa[o][x][i] != fa[o][y][i]) x = fa[o][x][i], y = fa[o][y][i];
return fa[o][x][0];
}
constexpr int B = 450;
int tot, key[N], pos[502];
int get_key(int u, int p) {
int max = 0;
for (int v : tree[1][u]) {
if (v == p) continue;
max = std::max(max, get_key(v, u) + 1);
}
if (u == 1 || max == B) {
key[u] = ++tot;
pos[tot] = u;
max = 0;
}
return max;
}
std::vector<std::pair<int, ULL>> h[N], g[N], d[502];
struct Blocks {
ULL h[502];
ULL g[502][502];
int bl[502], br[502], bel[N];
void build() {
for (int i = 1; i <= n; i++) bel[i] = (i + B - 1) / B;
for (int i = 1; i <= n; i++) br[bel[i]] = i;
for (int i = n; i >= 1; i--) bl[bel[i]] = i;
}
void add(int x, ULL v) {
int b = bel[x];
for (int i = b; i <= bel[n]; i++) h[i] += v;
for (int i = x; i <= br[b]; i++) g[b][i - bl[b] + 1] += v;
}
ULL query(int x) {
int b = bel[x];
return h[b - 1] + g[b][x - bl[b] + 1];
}
};
Blocks blocks;
ULL ans[N];
int tag[N], val[N];
void dfs(int u, int p) {
val[u] = val[p] + tag[u];
for (int v : tree[0][u]) {
if (v == p) continue;
dfs(v, u);
}
}
int main() {
std::cin >> n >> m;
for (int i = 1; i <= n; i++) std::cin >> a[i];
for (int i = 1; i < n; i++) {
int x, y;
std::cin >> x >> y;
tree[0][x].push_back(y);
tree[0][y].push_back(x);
}
for (int i = 1; i < n; i++) {
int x, y;
std::cin >> x >> y;
tree[1][x].push_back(y);
tree[1][y].push_back(x);
}
pre_dfs<0>(1, 0);
pre_dfs<1>(1, 0);
get_key(1, 0);
for (int i = 1; i <= m; i++) {
int x, y;
ULL k;
std::cin >> x >> y >> k;
int p0 = lca<0>(x, y);
h[i].push_back({x, k});
h[i].push_back({y, k});
h[i].push_back({p0, -k});
if (fa[0][p0][0]) h[i].push_back({fa[0][p0][0], -k});
int p1 = lca<1>(x, y);
g[i].push_back({x, 1});
g[i].push_back({y, 1});
g[i].push_back({p1, -1});
if (fa[1][p1][0]) g[i].push_back({fa[1][p1][0], -1});
ans[i] = a[x] + a[y] - a[p1] - a[fa[1][p1][0]];
}
blocks.build();
for (int i = 1; i <= m; i++) {
for (auto [x, v] : h[i]) blocks.add(dfn[0][x], v);
for (auto [x, v] : g[i]) {
while (!key[x]) {
ans[i] += v * (blocks.query(dfn[0][x] + size[0][x] - 1) - blocks.query(dfn[0][x] - 1));
x = fa[1][x][0];
}
d[key[x]].push_back({i, v});
}
}
for (int i = 1; i <= tot; i++) {
for (int u = pos[i]; u; u = fa[1][u][0]) tag[u] = 1;
dfs(1, 0);
int p = 0;
ULL cur = 0;
for (auto [id, v] : d[i]) {
while (p + 1 <= id) {
p++;
for (auto [x, v] : h[p]) cur += val[x] * v;
}
ans[id] += v * cur;
}
for (int u = pos[i]; u; u = fa[1][u][0]) tag[u] = 0;
}
for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]);
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 11ms
memory: 19236kb
input:
3000 3000 7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...
output:
-6341590215050170492 -79301366137484859 -6778502111225453738 -7158505953357193144 1742468310074622166 -8503908076023457945 3305677510569607477 -705142073284547528 -3462615771656933350 1075081718074605786 6509217537832509095 -1696230445866278503 8569443169249732820 -3971559879410972572 -2835636962904...
result:
wrong answer 1st lines differ - expected: '12105153858659381124', found: '-6341590215050170492'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 1812ms
memory: 130520kb
input:
200000 200000 622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...
output:
9042998055336671259 -6835450584445030474 5835924579879681322 9187084356907537870 -636397663002600543 565636160725988981 837626748701483168 -2387170783879744517 7246210357888652619 7725251776483176497 -1358645631525857679 9042305714006927228 -7539365334493336160 6526772063609981609 515782024564696093...
result:
wrong answer 2nd lines differ - expected: '11611293489264521142', found: '-6835450584445030474'
Subtask #5:
score: 0
Time Limit Exceeded
Test #27:
score: 0
Time Limit Exceeded
input:
200000 200000 1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...
output:
result:
Subtask #6:
score: 0
Wrong Answer
Test #34:
score: 0
Wrong Answer
time: 4430ms
memory: 143776kb
input:
200000 200000 6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...
output:
5519324519442957729 -4983882929317521117 8898301730697138469 4148979398311169421 -3356497222382343549 8628707816538336707 -4579446166671375110 -8150315721267694513 -2792439658300230960 7404566644919251615 -8575867809693751019 6356224262148620783 249874952637342736 9023132497407647441 -42849842200341...
result:
wrong answer 2nd lines differ - expected: '13462861144392030499', found: '-4983882929317521117'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%