N - 通貨変換ネットワーク / Currency Conversion Network Editorial by admin
gpt-5.5-highOverview
This problem asks us to find the result of sequentially composing affine transformations along the path between two cities in a forest where edges are dynamically added, deleted, and updated.
We can process all operations efficiently by using a Link-Cut Tree (which can handle dynamic trees) and representing each edge as a node.
Analysis
Since the graph is always a forest, the path between any two cities in the same connected component is uniquely determined.
In the query Q x y s, we apply the transformations of each edge along the path in order.
Applying affine transformations
\[ f(t) = a_f t + b_f \]
and
\[ g(t) = a_g t + b_g \]
in the order of “first \(f\), then \(g\)” yields:
\[ g(f(t)) = a_g(a_f t + b_f) + b_g \]
Since this simplifies to:
\[ (a_g a_f,\ a_g b_f + b_g) \]
This composition satisfies the associative property, allowing us to aggregate the transformations along the path.
A naive approach of searching for the path using DFS/BFS for each Q operation would take \(O(N)\) in the worst case per query.
Since \(N,Q \leq 5 \times 10^4\), the overall time complexity would be \(O(NQ)\), which is too slow.
Additionally, while a Union-Find (Disjoint Set Union) can check connectivity, it cannot handle edge deletions C.
Therefore, we use a Link-Cut Tree, which can handle edge addition, deletion, and path aggregation all in \(O(\log N)\).
Algorithm
1. Representing Edges as Nodes
A Link-Cut Tree is fundamentally a data structure that manages “values on nodes”. In this problem, the exchange rates are associated with the “edges”.
Thus, we add each exchange booth as a new node, representing an edge \(u-v\) as:
\[ u - e - v \]
- Let city nodes have the identity transformation \(t \mapsto t\).
- Let edge nodes \(e\) hold the currency exchange transformations.
This way, when we traverse the nodes on the path from city \(x\) to city \(y\) in order, only the edge nodes will have meaningful transformations, allowing us to obtain the overall composition.
2. Values Stored in Each Node
Each node in the Link-Cut Tree will store the following:
valF: The transformation when traversing this node in the forward direction.valB: The transformation when traversing this node in the backward direction.sumF: The composed transformation when viewed from left to right in the splay tree.sumB: The composed transformation when viewed from right to left in the splay tree.
For city nodes, since it is always
\[ t \mapsto t \]
we store:
\[ (1,0) \]
For edge nodes, for an edge created by the operation:
L u v a b c d
we store:
- Transformation from the \(u\)-side to the \(v\)-side: \((a,b)\)
- Transformation from the \(v\)-side to the \(u\)-side: \((c,d)\)
3. Composition of Affine Transformations
In the code, the affine transformation is represented as:
struct Affine {
long long a, b;
};
concat_aff(f, g) returns the composition of applying “first \(f\), then \(g\)”.
When
\[ f(t) = a_f t + b_f \]
\[ g(t) = a_g t + b_g \]
we have
\[ g(f(t)) = a_g a_f t + a_g b_f + b_g \]
so we write:
Affine concat_aff(const Affine& f, const Affine& g) {
return Affine(
g.a * f.a % MOD,
(g.a * f.b + g.b) % MOD
);
}
4. Aggregation in the Link-Cut Tree
For a splay tree node x, the composition when traversing in the order of the left subtree, x itself, and then the right subtree is:
sumF = left.sumF -> valF[x] -> right.sumF
This is calculated in the code as follows:
t[x].sumF = concat_aff(concat_aff(t[l].sumF, t[x].valF), t[r].sumF);
In the reverse direction, i.e., traversing from right to left, we have:
sumB = right.sumB -> valB[x] -> left.sumB
so:
t[x].sumB = concat_aff(concat_aff(t[r].sumB, t[x].valB), t[l].sumB);
5. Reversal Processing
In a Link-Cut Tree, we may reverse the direction of a path for the makeroot operation.
When a path is reversed:
- The left and right children are swapped.
- The forward and backward transformations are swapped.
sumFandsumBare also swapped.
Thus, we handle reversal as follows:
swap(t[x].ch[0], t[x].ch[1]);
swap(t[x].valF, t[x].valB);
swap(t[x].sumF, t[x].sumB);
t[x].rev = !t[x].rev;
This ensures that the correct transformations are maintained even when the orientation in the Link-Cut Tree changes.
6. Processing Each Operation
L u v a b c d
Create a new edge node id.
Connect them in the Link-Cut Tree to form:
u - id - v
We perform the connection as follows:
link(u, id);
link(id, v);
Then, set the transformations on this edge node:
orient_set(id, u, v, Affine(a, b), Affine(c, d));
Additionally, store the mapping from the city pair to the edge node ID in an unordered_map so we can look it up later for deletion or updates.
C u v
Delete the exchange booth directly connecting cities \(u\) and \(v\).
Find the corresponding edge node id and cut the connections on both sides as follows:
cut(eu[id], id);
cut(ev[id], id);
U u v a b c d
Find the edge node id and update its stored affine transformations.
Care must be taken here. The order of the arguments u, v in U might be different from when the edge was created.
Therefore, in the code, we save the creation-time orientation eu[id] -> ev[id] and always apply:
orient_set(id, eu[id], ev[id], Affine(a, b), Affine(c, d));
Q x y s
First, check if \(x\) and \(y\) are connected.
if (!connected(x, y)) {
cout << -1 << '\n';
}
If they are connected, make \(x\) the root and expose the path to \(y\) in the Link-Cut Tree.
Affine f = path_sum(x, y);
This f is the composed transformation of the entire path from \(x\) to \(y\).
Finally, output:
\[ f(s) = f.a \times s + f.b \]
cout << (f.a * s + f.b) % MOD << '\n';
Complexity
- Time Complexity: \(O(Q \log (N+Q))\)
- Space Complexity: \(O(N+Q)\)
Since each operation only calls Link-Cut Tree operations like link, cut, makeroot, and access a constant number of times, they can be processed in amortized \(O(\log (N+Q))\) time.
Key Implementation Details
Composition of affine transformations is non-commutative.
That is, changing the order of application changes the result, so we must maintain bothsumFandsumB.The orientation of an edge is fixed when it is created.
Sinceu, vinU u v ...orC u vmay arrive in the reverse order of their creation, we save the endpoints at creation timeeu[id], ev[id]for each edge node.Edges should be searchable using the city pairs.
In the code, to treat \((u,v)\) and \((v,u)\) identically, we construct a key with the smaller index first.In the reversal process of the Link-Cut Tree, make sure to swap not only the children but also
valF/valBandsumF/sumB. Forgetting this will lead to incorrect answers for queries traversing in the reverse direction.Source Code
#include <bits/stdc++.h>
using namespace std;
static const long long MOD = 998244353;
struct Affine {
long long a, b;
Affine(long long a_ = 1, long long b_ = 0) : a(a_), b(b_) {}
};
Affine concat_aff(const Affine& f, const Affine& g) {
return Affine(g.a * f.a % MOD, (g.a * f.b + g.b) % MOD);
}
struct Node {
int ch[2];
int fa;
bool rev;
Affine valF, valB;
Affine sumF, sumB;
Node() {
ch[0] = ch[1] = fa = 0;
rev = false;
valF = valB = sumF = sumB = Affine();
}
};
struct LinkCutTree {
vector<Node> t;
vector<int> stk;
LinkCutTree(int n = 0) : t(n + 1) {
stk.reserve(n + 1);
}
void init_node(int x) {
t[x] = Node();
}
bool is_root(int x) {
int f = t[x].fa;
return f == 0 || (t[f].ch[0] != x && t[f].ch[1] != x);
}
void pull(int x) {
int l = t[x].ch[0], r = t[x].ch[1];
t[x].sumF = concat_aff(concat_aff(t[l].sumF, t[x].valF), t[r].sumF);
t[x].sumB = concat_aff(concat_aff(t[r].sumB, t[x].valB), t[l].sumB);
}
void apply_rev(int x) {
if (!x) return;
swap(t[x].ch[0], t[x].ch[1]);
swap(t[x].valF, t[x].valB);
swap(t[x].sumF, t[x].sumB);
t[x].rev = !t[x].rev;
}
void push(int x) {
if (!t[x].rev) return;
apply_rev(t[x].ch[0]);
apply_rev(t[x].ch[1]);
t[x].rev = false;
}
void rotate(int x) {
int y = t[x].fa;
int z = t[y].fa;
int dx = (t[y].ch[1] == x);
int b = t[x].ch[dx ^ 1];
if (!is_root(y)) {
if (t[z].ch[0] == y) t[z].ch[0] = x;
else t[z].ch[1] = x;
}
t[x].fa = z;
t[x].ch[dx ^ 1] = y;
t[y].fa = x;
t[y].ch[dx] = b;
if (b) t[b].fa = y;
pull(y);
pull(x);
}
void splay(int x) {
stk.clear();
int y = x;
stk.push_back(y);
while (!is_root(y)) {
y = t[y].fa;
stk.push_back(y);
}
for (int i = (int)stk.size() - 1; i >= 0; --i) push(stk[i]);
while (!is_root(x)) {
int y = t[x].fa;
int z = t[y].fa;
if (!is_root(y)) {
bool zigzig = ((t[y].ch[0] == x) == (t[z].ch[0] == y));
if (zigzig) rotate(y);
else rotate(x);
}
rotate(x);
}
}
int access(int x) {
int last = 0;
for (int y = x; y; y = t[y].fa) {
splay(y);
t[y].ch[1] = last;
if (last) t[last].fa = y;
pull(y);
last = y;
}
splay(x);
return last;
}
void makeroot(int x) {
access(x);
apply_rev(x);
}
int findroot(int x) {
access(x);
while (true) {
push(x);
if (!t[x].ch[0]) break;
x = t[x].ch[0];
}
splay(x);
return x;
}
bool connected(int x, int y) {
if (x == y) return true;
return findroot(x) == findroot(y);
}
void link(int x, int y) {
makeroot(x);
t[x].fa = y;
}
void cut(int x, int y) {
makeroot(x);
access(y);
if (t[y].ch[0] == x && t[x].ch[1] == 0) {
t[y].ch[0] = 0;
t[x].fa = 0;
pull(y);
}
}
void orient_set(int edge, int u, int v, const Affine& f, const Affine& b) {
makeroot(u);
access(v);
splay(edge);
t[edge].valF = f;
t[edge].valB = b;
pull(edge);
}
Affine path_sum(int x, int y) {
makeroot(x);
access(y);
return t[y].sumF;
}
};
struct CustomHash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15ULL;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long edge_key(int u, int v) {
if (u > v) swap(u, v);
return (1LL * u << 32) | v;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
LinkCutTree lct(N + Q + 5);
vector<int> eu(N + Q + 5), ev(N + Q + 5);
unordered_map<long long, int, CustomHash> mp;
mp.reserve(Q * 2 + 10);
int tot = N;
for (int qi = 0; qi < Q; ++qi) {
char op;
cin >> op;
if (op == 'L') {
int u, v;
long long a, b, c, d;
cin >> u >> v >> a >> b >> c >> d;
int id = ++tot;
lct.init_node(id);
eu[id] = u;
ev[id] = v;
lct.link(u, id);
lct.link(id, v);
lct.orient_set(id, u, v, Affine(a, b), Affine(c, d));
mp[edge_key(u, v)] = id;
} else if (op == 'C') {
int u, v;
cin >> u >> v;
long long key = edge_key(u, v);
int id = mp[key];
mp.erase(key);
lct.cut(eu[id], id);
lct.cut(ev[id], id);
} else if (op == 'U') {
int u, v;
long long a, b, c, d;
cin >> u >> v >> a >> b >> c >> d;
int id = mp[edge_key(u, v)];
lct.orient_set(id, eu[id], ev[id], Affine(a, b), Affine(c, d));
} else if (op == 'Q') {
int x, y;
long long s;
cin >> x >> y >> s;
if (!lct.connected(x, y)) {
cout << -1 << '\n';
} else {
Affine f = lct.path_sum(x, y);
cout << (f.a * s + f.b) % MOD << '\n';
}
}
}
return 0;
}
This editorial was generated by gpt-5.5-high.