QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#469764 | #7563. Fun on Tree | duckindog | WA | 803ms | 37124kb | C++23 | 2.2kb | 2024-07-09 23:38:51 | 2024-07-09 23:38:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200'000 + 10;
int n, q;
int a[N];
int par[N];
vector<pair<int, int>> ad[N];
int d[N], st[N], rvs[N], ed[N], it;
void dfs0(int u) {
st[u] = ++it; rvs[it] = u;
for (const auto& [v, w] : ad[u]) {
d[v] = d[u] + w;
par[v] = u;
dfs0(v);
}
ed[u] = it;
}
pair<int, int> f[N << 2];
int lazy[N << 2];
void build(int s, int l, int r) {
if (l == r) {
f[s].first = d[rvs[l]] - a[rvs[l]];
f[s].second = -rvs[l];
return;
}
int mid = l + r >> 1;
build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
void push(int s) {
f[s << 1].first += lazy[s]; f[s << 1 | 1].first += lazy[s];
lazy[s << 1] += lazy[s]; lazy[s << 1 | 1] += lazy[s];
lazy[s] = 0;
}
void update(int s, int l, int r, int u, int v, int x) {
if (v < l || u > r) return;
if (u <= l && r <= v) {
f[s].first += x;
lazy[s] += x;
return;
}
push(s);
int mid = l + r >> 1;
update(s << 1, l, mid, u, v, x); update(s << 1 | 1, mid + 1, r, u, v, x);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
pair<int, int> query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return {-1'000'000'000'000'000, 0};
if (u <= l && r <= v) return f[s];
push(s);
int mid = l + r >> 1;
return max(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int v = 2; v <= n; ++v) {
int u, w; cin >> u >> w;
ad[u].push_back({v, w});
}
dfs0(par[1] = 1);
build(1, 1, n);
while (q--) {
int x, y, v; cin >> x >> y >> v;
int start = x;
update(1, 1, n, st[y], ed[y], -v);
pair<int, int> answer = query(1, 1, n, st[x], ed[x]);
answer.first -= d[x];
while (true) {
int u = par[x];
if (x == 1) break;
auto ret = max(query(1, 1, n, st[u], st[x] - 1), query(1, 1, n, ed[x] + 1, ed[u]));
ret.first += d[start] - d[u];
answer = max(answer, ret);
x = u;
}
cout << -answer.second << " " << answer.first << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11864kb
input:
6 6 1 1 4 5 1 4 1 5 2 0 3 2 4 1 5 6 3 2 -100000 1 2 100000 1 1 0 2 2 66 3 1 5 4 4 -3
output:
6 100005 6 10 6 10 1 4 1 -1 1 1
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 13904kb
input:
5 6 -10 0 2 -4 8 1 7 1 1 2 2 2 -2 1 1 100 2 1 -100 1 1 0 4 3 10 2 5 3 5 2 2
output:
4 -87 1 17 4 13 1 19 1 17 1 15
result:
ok 6 lines
Test #3:
score: 0
Accepted
time: 1ms
memory: 9824kb
input:
6 3 0 0 0 0 0 0 1 10 1 10 1 -100 4 10 4 11 1 1 0 4 1 0 1 4 1000
output:
2 10 6 11 2 10
result:
ok 3 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 13960kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Wrong Answer
time: 803ms
memory: 37124kb
input:
200000 200000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 ...
output:
119017 15000000000 120167 17000000000 119017 15000000000 119017 15000000000 120167 17000000000 120167 15000000000 120167 16000000000 119017 17000000000 119017 16000000000 119017 12000000000 119017 17000000000 120167 16000000000 120167 14000000000 120167 17000000000 120167 18000000000 120167 16000000...
result:
wrong answer 18664th lines differ - expected: '186683 18000000000', found: '120167 18000000000'