Official

N - 通貨変換ネットワーク / Currency Conversion Network Editorial by admin Editorial for printing

gemini-3.5-flash-high

Summary

This problem asks us to find the result of sequentially applying affine transformations \(t \mapsto at + b \pmod{998244353}\) along the path between two specified vertices in a forest where edges are dynamically added, removed, and updated.

Since the structure of the graph changes dynamically, this problem can be solved using a Link-Cut Tree (LCT), which is a data structure capable of efficiently handling dynamic trees.

Analysis

1. Dynamic Forests and Path Queries

If the graph were a static tree, we could compute the composition of affine transformations along the path between two vertices in \(O(\log N)\) or \(O(\log^2 N)\) using doubling or Heavy-Light Decomposition (HLD). However, in this problem, edges are dynamically added (L) and removed (C) via queries. To handle algebraic composition queries on paths in such a “dynamic forest”, a Link-Cut Tree (LCT) is the ideal choice.

2. Managing Edge Attributes (Introduction of Auxiliary Vertices)

LCT is fundamentally a data structure that stores information on “vertices”. On the other hand, the affine transformations in this problem are defined on “edges”. When information is associated with edges like this, a standard technique is to introduce “auxiliary vertices representing the edges”. - When adding an edge between cities \(u\) and \(v\), we create a new edge vertex \(e\). - Then, we connect \(u\) and \(v\) indirectly by linking \(u - e\) and \(e - v\) in the tree. - This allows us to centrally manage the edge information as the value of vertex \(e\) on the LCT.

3. Non-commutativity and Bidirectionality of Affine Transformations

The composition of affine transformations \(g(f(x))\) is order-dependent (non-commutative). Furthermore, depending on the direction of the path traversal, the direction in which we traverse each edge (from \(u \to v\) or \(v \to u\)) differs. Therefore, at each node of the LCT (each node in the Splay trees), we need to maintain the following two composed transformations: - sum_l2r: The composed transformation when traversing the path represented by the subtree from “left to right”. - sum_r2l: The composed transformation when traversing the path represented by the subtree from “right to left”.

When performing the reversal operation (push_reverse) on a Splay tree, we must swap not only the left and right children but also sum_l2r and sum_r2l.

Algorithm

1. Composition of Affine Transformations

Let \(f(x) = ax + b\) and \(g(x) = cx + d\). The composition is \(g(f(x)) = c(ax + b) + d = (ca)x + (cb + d)\). We define this as an Affine struct and simulate composition using the multiplication operator *.

2. Construction of Link-Cut Tree (LCT)

Each node in the LCT will hold the following information: - val_l2r, val_r2l: The transformation held by the node itself (city vertices have the identity mapping \(t \mapsto t\), and edge vertex \(e\) has the specified transformations for both directions). - sum_l2r, sum_r2l: The composed transformation of the entire subtree (path) rooted at that node.

In the pushup function, we compose the transformations of the left subtree (the start of the path), the node itself, and the right subtree (the end of the path) in the correct order.

3. Handling Each Query

  • L u v a b c d (Link): For a newly created edge vertex \(e\), we set val_l2r = Affine(a, b) and val_r2l = Affine(c, d). Then, we perform link(u, e) and link(e, v).
  • C u v (Cut): Identify the edge vertex \(e\) connecting \(u\) and \(v\), and perform cut(u, e) and cut(e, v).
  • U u v a b c d (Update): Update the values of the edge vertex \(e\). During the update, it is necessary to preserve the direction of the edge at the time of its creation (\(p \to q\)). By setting the original starting vertex \(p\) (orig_u) as the root using evert and then performing splay(e), we can safely overwrite the values with the direction on the Splay tree fixed.
  • Q x y s (Query): If \(x\) and \(y\) are in different connected components (find_root(x) != find_root(y)), output -1. If they are in the same connected component, perform evert(x) to make \(x\) the root, then perform access(y) and splay(y) to isolate the path from \(x\) to \(y\) as a single Splay tree. At this point, the sum_l2r stored at the root \(y\) represents the composed transformation of the entire path, so we apply this transformation to the initial value \(s\) and output the result.

Complexity

  • Time Complexity: \(O(Q \log(N + Q))\) Each operation on the LCT (link, cut, evert, access, splay) runs in \(O(\log V)\) amortized time. Here, \(V\) is the number of vertices, which is at most \(N + Q\) including the auxiliary vertices. In addition, we use std::map to manage the correspondence of edges, which takes \(O(\log Q)\) for search, insertion, and deletion. Therefore, the complexity per query is \(O(\log(N + Q))\), and the overall complexity of \(O(Q \log(N + Q))\) is fast enough.

  • Space Complexity: \(O(N + Q)\) Since the number of LCT nodes and the edge information stored in the std::map are at most \(N + Q\), the memory usage scales linearly.

Implementation Details

  • Note on Array Sizes: The problem constraints are \(N, Q \le 5 \times 10^4\). Since auxiliary vertices (edge vertices) are added with each query, the number of LCT nodes can reach up to \(N + Q \approx 10^5\). The size 30005 of the tree array in the provided code is too small for these constraints. To prevent Runtime Errors (RE), it must be set to a sufficiently large size, such as 100005 or more.

  • Controlling Direction with evert: Since affine transformations are non-commutative, it is extremely important to ensure consistency regarding which vertex is the “start (left)” and which is the “end (right)” during U and Q queries. Use evert appropriately to fix the direction of the path before performing operations.

    Source Code

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

const int MOD = 998244353;

struct Affine {
    long long a, b;
    Affine() : a(1), b(0) {}
    Affine(long long a, long long b) : a(a % MOD), b(b % MOD) {
        if (this->a < 0) this->a += MOD;
        if (this->b < 0) this->b += MOD;
    }
    Affine operator*(const Affine& other) const {
        return Affine((a * other.a) % MOD, (a * other.b + b) % MOD);
    }
    long long eval(long long x) const {
        return (a * x + b) % MOD;
    }
};

struct Node {
    int left = 0, right = 0, parent = 0;
    bool rev = false;
    Affine val_l2r, val_r2l;
    Affine sum_l2r, sum_r2l;
} tree[30005];

void push_reverse(int x) {
    if (!x) return;
    swap(tree[x].left, tree[x].right);
    swap(tree[x].val_l2r, tree[x].val_r2l);
    swap(tree[x].sum_l2r, tree[x].sum_r2l);
    tree[x].rev = !tree[x].rev;
}

void pushdown(int x) {
    if (!x) return;
    if (tree[x].rev) {
        int l = tree[x].left, r = tree[x].right;
        if (l) push_reverse(l);
        if (r) push_reverse(r);
        tree[x].rev = false;
    }
}

void pushup(int x) {
    if (!x) return;
    int l = tree[x].left, r = tree[x].right;
    
    tree[x].sum_l2r = tree[x].val_l2r;
    if (l) tree[x].sum_l2r = tree[x].sum_l2r * tree[l].sum_l2r;
    if (r) tree[x].sum_l2r = tree[r].sum_l2r * tree[x].sum_l2r;
    
    tree[x].sum_r2l = tree[x].val_r2l;
    if (r) tree[x].sum_r2l = tree[x].sum_r2l * tree[r].sum_r2l;
    if (l) tree[x].sum_r2l = tree[l].sum_r2l * tree[x].sum_r2l;
}

bool is_root(int x) {
    int p = tree[x].parent;
    return tree[p].left != x && tree[p].right != x;
}

void rotate(int x) {
    int y = tree[x].parent, z = tree[y].parent;
    int k = (tree[y].right == x);
    if (!is_root(y)) {
        if (tree[z].left == y) tree[z].left = x;
        else tree[z].right = x;
    }
    tree[x].parent = z;
    if (k == 0) {
        tree[y].left = tree[x].right;
        if (tree[x].right) tree[tree[x].right].parent = y;
        tree[x].right = y;
    } else {
        tree[y].right = tree[x].left;
        if (tree[x].left) tree[tree[x].left].parent = y;
        tree[x].left = y;
    }
    tree[y].parent = x;
    pushup(y);
    pushup(x);
}

void splay(int x) {
    static int stk[30005];
    int top = 0;
    int curr = x;
    stk[top++] = curr;
    while (!is_root(curr)) {
        curr = tree[curr].parent;
        stk[top++] = curr;
    }
    while (top > 0) {
        pushdown(stk[--top]);
    }
    
    while (!is_root(x)) {
        int y = tree[x].parent, z = tree[y].parent;
        if (!is_root(y)) {
            if ((tree[y].left == x) ^ (tree[z].left == y)) rotate(x);
            else rotate(y);
        }
        rotate(x);
    }
}

void access(int x) {
    for (int t = 0; x; t = x, x = tree[x].parent) {
        splay(x);
        tree[x].right = t;
        pushup(x);
    }
}

void evert(int x) {
    access(x);
    splay(x);
    push_reverse(x);
}

int find_root(int x) {
    access(x);
    splay(x);
    while (tree[x].left) {
        pushdown(x);
        x = tree[x].left;
    }
    splay(x);
    return x;
}

void link(int x, int y) {
    evert(x);
    if (find_root(y) != x) {
        tree[x].parent = y;
    }
}

void cut(int x, int y) {
    evert(x);
    access(y);
    splay(y);
    if (tree[y].left == x && tree[x].right == 0) {
        tree[y].left = 0;
        tree[x].parent = 0;
        pushup(y);
    }
}

struct EdgeInfo {
    int id;
    int orig_u;
    int orig_v;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, Q;
    if (!(cin >> N >> Q)) return 0;
    
    for (int i = 1; i <= N; ++i) {
        tree[i].val_l2r = tree[i].val_r2l = tree[i].sum_l2r = tree[i].sum_r2l = Affine(1, 0);
    }
    
    map<pair<int, int>, EdgeInfo> edges;
    int edge_id_counter = N + 1;
    
    auto get_key = [](int u, int v) {
        if (u > v) swap(u, v);
        return make_pair(u, v);
    };
    
    for (int q = 0; q < Q; ++q) {
        char type;
        cin >> type;
        if (type == 'L') {
            int u, v;
            long long a, b, c, d;
            cin >> u >> v >> a >> b >> c >> d;
            int e = edge_id_counter++;
            edges[get_key(u, v)] = {e, u, v};
            tree[e].val_l2r = tree[e].sum_l2r = Affine(a, b);
            tree[e].val_r2l = tree[e].sum_r2l = Affine(c, d);
            link(u, e);
            link(e, v);
        } else if (type == 'C') {
            int u, v;
            cin >> u >> v;
            auto key = get_key(u, v);
            auto it = edges.find(key);
            int e = it->second.id;
            cut(u, e);
            cut(e, v);
            edges.erase(it);
        } else if (type == 'U') {
            int u, v;
            long long a, b, c, d;
            cin >> u >> v >> a >> b >> c >> d;
            auto key = get_key(u, v);
            auto it = edges.find(key);
            int e = it->second.id;
            int orig_u = it->second.orig_u;
            evert(orig_u);
            access(e);
            splay(e);
            tree[e].val_l2r = Affine(a, b);
            tree[e].val_r2l = Affine(c, d);
            pushup(e);
        } else if (type == 'Q') {
            int x, y;
            long long s;
            cin >> x >> y >> s;
            if (find_root(x) != find_root(y)) {
                cout << -1 << "\n";
            } else {
                evert(x);
                access(y);
                splay(y);
                long long ans = tree[y].sum_l2r.eval(s);
                cout << ans << "\n";
            }
        }
    }
    return 0;
}

This editorial was generated by gemini-3.5-flash-high.

posted:
last update: