結果
| 問題 | No.2342 Triple Tree Query (Hard) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-24 19:50:09 |
| 言語 | C++23(gcc16) (gcc 16.2.0 + boost 1.92.0 + ACL) |
| 結果 |
WA
不安定
|
| 実行時間 | - |
| コード長 | 4,792 bytes |
| 記録 | |
| コンパイル時間 | 5,316 ms |
| コンパイル使用メモリ | 369,420 KB |
| 実行使用メモリ | 23,040 KB |
| 最終ジャッジ日時 | 2026-11-24 19:50:23 |
| 合計ジャッジ時間 | 11,967 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge6_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 2 |
| other | WA * 32 RE * 5 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
static constexpr long long MOD = 998244353;
struct SegmentTree {
int n;
vector<long long> mul, add;
explicit SegmentTree(int n) : n(n), mul(4 * n, 1), add(4 * n, 0) {}
// Apply (x -> a*x + b) after the transform already stored here.
void apply(int node, long long a, long long b) {
mul[node] = a * mul[node] % MOD;
add[node] = (a * add[node] + b) % MOD;
}
void push(int node) {
if (mul[node] == 1 && add[node] == 0) return;
apply(node * 2, mul[node], add[node]);
apply(node * 2 + 1, mul[node], add[node]);
mul[node] = 1;
add[node] = 0;
}
void update(int node, int lo, int hi, int ql, int qr,
long long a, long long b) {
if (ql <= lo && hi <= qr) {
apply(node, a, b);
return;
}
push(node);
int mid = (lo + hi) / 2;
if (ql <= mid) update(node * 2, lo, mid, ql, qr, a, b);
if (qr > mid) update(node * 2 + 1, mid + 1, hi, ql, qr, a, b);
}
void update(int l, int r, long long a, long long b) {
if (l <= r) update(1, 0, n - 1, l, r, a, b);
}
long long query(int node, int lo, int hi, int p) {
if (lo == hi) return (mul[node] + add[node]) % MOD;
push(node);
int mid = (lo + hi) / 2;
if (p <= mid) return query(node * 2, lo, mid, p);
return query(node * 2 + 1, mid + 1, hi, p);
}
long long query(int p) {
return query(1, 0, n - 1, p);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> parent(n, -1);
vector<vector<int>> children(n), adj(n);
for (int v = 1; v < n; ++v) {
cin >> parent[v];
children[parent[v]].push_back(v);
adj[parent[v]].push_back(v);
adj[v].push_back(parent[v]);
}
vector<long long> score(n);
for (auto& x : score) cin >> x;
// Rooted traversal order and depths.
vector<int> depth(n, 0), order;
order.reserve(n);
order.push_back(0);
for (int i = 0; i < (int)order.size(); ++i) {
int v = order[i];
for (int c : children[v]) {
depth[c] = depth[v] + 1;
order.push_back(c);
}
}
// Subtree sizes and heavy child.
vector<int> sub(n, 1), heavy(n, -1);
for (int i = n - 1; i >= 0; --i) {
int v = order[i];
int bestSize = 0;
for (int c : children[v]) {
sub[v] += sub[c];
if (sub[c] > bestSize) {
bestSize = sub[c];
heavy[v] = c;
}
}
}
// Heavy-light positions. Heavy-first ordering also keeps subtrees contiguous.
vector<int> head(n), pos(n);
int timer = 0;
vector<pair<int, int>> chains = {{0, 0}};
while (!chains.empty()) {
auto [start, chainHead] = chains.back();
chains.pop_back();
for (int v = start; v != -1; v = heavy[v]) {
head[v] = chainHead;
pos[v] = timer++;
for (int c : children[v]) {
if (c != heavy[v]) chains.push_back({c, c});
}
}
}
SegmentTree seg(n);
auto applyRange = [&](int l, int r, long long b, long long a) {
seg.update(l, r, a, b);
};
auto applyPath = [&](int u, int v, long long b, long long a) {
while (head[u] != head[v]) {
if (depth[head[u]] < depth[head[v]]) swap(u, v);
applyRange(pos[head[u]], pos[u], b, a);
u = parent[head[u]];
}
if (depth[u] > depth[v]) swap(u, v);
applyRange(pos[u], pos[v], b, a);
};
while (q--) {
string op;
cin >> op;
if (op == "LOOK") {
int v;
cin >> v;
cout << seg.query(pos[v]) << '\n';
} else if (op == "LINEAGE") {
int v;
long long b, a;
cin >> v >> b >> a;
applyRange(pos[v], pos[v] + sub[v] - 1, b, a);
} else if (op == "BRIDGE") {
int u, v;
long long b, a;
cin >> u >> v >> b >> a;
applyPath(u, v, b, a);
} else if (op == "RIPPLE") {
int v, r;
long long b, a;
cin >> v >> r >> b >> a;
queue<tuple<int, int, int>> todo;
todo.push({v, -1, 0});
while (!todo.empty()) {
auto [x, prev, dist] = todo.front();
todo.pop();
seg.update(pos[x], pos[x], a, b);
if (dist == r) continue;
for (int y : adj[x]) {
if (y != prev) todo.push({y, x, dist + 1});
}
}
}
}
return 0;
}