QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#431734 | #7563. Fun on Tree | pandapythoner | WA | 1726ms | 75412kb | C++20 | 6.7kb | 2024-06-06 00:31:12 | 2024-06-06 00:31:13 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
const ll inf = 1e18;
mt19937 rnd(234);
struct SGT {
int n;
vector<pair<ll, int>> t;
vector<ll> d;
void apply(int v, ll x) {
t[v].first += x;
d[v] += x;
}
void push(int v) {
apply(v + v, d[v]);
apply(v + v + 1, d[v]);
d[v] = 0;
}
void upd(int v) {
t[v] = max(t[v + v], t[v + v + 1]);
}
void build(int v, int tl, int tr, vector<pair<ll, int>>& a) {
if (tl == tr) {
t[v] = a[tl];
return;
}
int tm = (tl + tr) / 2;
build(v + v, tl, tm, a);
build(v + v + 1, tm + 1, tr, a);
upd(v);
}
void build(vector<pair<ll, int>> a) {
n = (int)a.size();
t.assign(4 * n, make_pair(-inf, -1));
d.assign(4 * n, 0);
build(1, 0, n - 1, a);
}
void add(int v, int tl, int tr, int l, int r, ll x) {
if (tr < l or r < tl) {
return;
}
if (l <= tl and tr <= r) {
apply(v, x);
return;
}
int tm = (tl + tr) / 2;
push(v);
add(v + v, tl, tm, l, r, x);
add(v + v + 1, tm + 1, tr, l, r, x);
upd(v);
}
void add(int l, int r, ll x) {
add(1, 0, n - 1, l, r, x);
}
void assign(int v, int tl, int tr, int i, ll x, int j) {
if (tr < i or i < tl) {
return;
}
if (tl == tr) {
t[v] = { x, j };
return;
}
int tm = (tl + tr) / 2;
push(v);
assign(v + v, tl, tm, i, x, j);
assign(v + v + 1, tm + 1, tr, i, x, j);
upd(v);
}
void assign(int i, ll x, int j) {
assign(1, 0, n, i, x, j);
}
pair<ll, int> get(int v, int tl, int tr, int l, int r) {
if (tr < l or r < tl) {
return make_pair(-inf, -1);
}
if (l <= tl and tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
push(v);
return max(get(v + v, tl, tm, l, r), get(v + v + 1, tm + 1, tr, l, r));
}
pair<ll, int> get(int l, int r) {
return get(1, 0, n - 1, l, r);
}
};
struct edge {
int to;
ll w;
};
int n, q;
vector<ll> a;
vector<vector<edge>> g;
vector<int> forth, back;
vector<int> up, h, depth, par, tout;
vector<int> sz;
SGT sgt, sgt_side;
pair<ll, int> get_vertex_value(int v, int baned = -1) {
pair<ll, int> val;
if (baned == -1) {
val = sgt.get(v, tout[v] - 1);
} else {
assert(par[baned] == v);
val = max(sgt.get(v, baned - 1), sgt.get(tout[baned], tout[v] - 1));
}
return make_pair(val.first - depth[v] * 2, val.second);
}
void dfs1(int v) {
sz[v] = 1;
int mx_sz = -1;
int mx_szi = -1;
for (int i = 0; i < (int)g[v].size(); i += 1) {
auto [to, w] = g[v][i];
dfs1(to);
sz[v] += sz[to];
if (sz[to] > mx_sz) {
mx_sz = sz[to];
mx_szi = i;
}
}
if (mx_szi != -1) {
swap(g[v][0], g[v][mx_szi]);
}
}
void dfs2(int v, ll mh, ll mdpth, ll mup, int p) {
forth[v] = back.size();
back.push_back(v);
h[forth[v]] = mh;
depth[forth[v]] = mdpth;
up[forth[v]] = forth[mup];
if (p == -1) {
par[forth[v]] = -1;
} else {
par[forth[v]] = forth[p];
}
for (auto [to, w] : g[v]) {
int toup = mup;
if (to != g[v][0].to) {
toup = to;
}
dfs2(to, mh + 1, mdpth + w, toup, v);
}
tout[forth[v]] = (int)back.size();
}
void reorder() {
sz.resize(n);
dfs1(0);
forth.resize(n);
back.clear();
back.reserve(n);
h.resize(n);
depth.resize(n);
up.resize(n);
par.resize(n);
tout.resize(n);
dfs2(0, 0, 0, 0, -1);
vector<vector<edge>> ng(n);
for (int v = 0; v < n; v += 1) {
for (auto [to, w] : g[v]) {
ng[forth[v]].push_back(edge{ forth[to], w });
}
}
g = ng;
}
void build() {
reorder();
vector<pair<ll, int>> sgt_init(n), sgt_side_init(n, make_pair(-inf, -1));
for (int v = 0; v < n; v += 1) {
sgt_init[v] = make_pair(depth[v] - a[back[v]], -back[v]);
}
sgt.build(sgt_init);
sgt_side.build(sgt_side_init);
for (int v = 0; v < n; v += 1) {
int baned = -1;
if (!g[v].empty()) {
baned = g[v][0].to;
}
auto val = get_vertex_value(v, baned);
sgt_side.assign(v, val.first, val.second);
}
}
void change(int v, ll x) {
v = forth[v];
x = -x;
sgt.add(v, tout[v] - 1, x);
sgt_side.add(v, tout[v] - 1, x);
while (v != -1) {
v = up[v];
v = par[v];
if (v == -1) {
break;
}
assert(!g[v].empty());
int baned = g[v][0].to;
auto val = get_vertex_value(v, baned);
sgt_side.assign(v, val.first, val.second);
}
}
pair<ll, int> get(int root) {
root = forth[root];
int v = root;
pair<ll, int> rs = make_pair(-inf, -1);
auto val = get_vertex_value(root);
rs = max(rs, make_pair(val.first + depth[root], val.second));
while (v != -1) {
val = sgt_side.get(up[v], v - 1);
rs = max(rs, make_pair(val.first + depth[root], val.second));
v = up[v];
if (par[v] == -1) {
break;
}
val = get_vertex_value(par[v], v);
rs = max(rs, make_pair(val.first + depth[root], val.second));
v = par[v];
}
return make_pair(rs.first, -rs.second);
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> q;
a.resize(n);
for (int i = 0; i < n; i += 1) {
cin >> a[i];
}
g.assign(n, vector<edge>());
for (int i = 1; i < n; i += 1) {
int p;
ll w;
cin >> p >> w;
--p;
g[p].push_back(edge{ i, w });
}
build();
for (int itr = 0; itr < q; itr += 1) {
int x, y;
ll val;
cin >> x >> y >> val;
--x;
--y;
change(y, val);
auto [goodness, v] = get(x);
cout << v + 1 << " " << goodness << "\n";
}
return 0;
}
/*
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
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
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: 0ms
memory: 3784kb
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: 0ms
memory: 3600kb
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: 3572kb
input:
2 0 1 1 1 3
output:
result:
ok 0 lines
Test #5:
score: -100
Wrong Answer
time: 1726ms
memory: 75412kb
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:
6 2000000000 176070 3294967296 8 2000000000 5 2000000000 199161 3294967296 1346 1000000000 26 2000000000 5385 0 111069 3294967296 61316 3294967296 1838 0 20700 2000000000 20 0 188651 3294967296 19 -294967296 206 2000000000 1452 1000000000 37 2000000000 88718 3294967296 84804 3294967296 30869 3294967...
result:
wrong answer 1st lines differ - expected: '119017 15000000000', found: '6 2000000000'