We are given a problem with n planets (vertices), q "plans" which are essentially directed edges with costs. There are three types:
-
From v to u with cost w (single edge).
-
From v to any planet in range [l, r] with cost w. So from a specific node v, we can go to all nodes in the interval [l, r] with same cost w.
-
From any planet in range [l, r] to planet v with cost w. So from any node in [l, r] we can go to v with cost w.
We need to compute the shortest distances from a source s to all nodes, with these edges. Since there are up to 10^5 plans, but each type 2/3 can represent many edges (O(n) each). However, we cannot create all edges explicitly. We need to use segment tree or some data structure to efficiently handle range-to-point or point-to-range edges.
This is a classic problem of shortest path with range edges. Usually we build a segment tree over the indices to transform range edges into O(log n) edges per query.
Standard approach: create two segment trees: one for outgoing edges (we call it seg_out) and one for incoming edges (seg_in). But careful: In many solutions, we construct a graph with 2*4n nodes representing intervals. There is known technique for problems like "you can go from node u to all nodes in range [l, r]" etc. We can create a segment tree that allows representing range transitions with log factor.
Typically: Build two segment trees: one for "downward" edges from segment tree nodes to actual vertices (or leaves) for outgoing edges, and another for "upward" edges for incoming edges. But there is more than one variation.
We need to support:
-
Type1: single edge v -> u cost w.
-
Type2: from v to all nodes in [l, r] cost w.
-
Type3: from all nodes in [l, r] to v cost w.
Standard technique: Use a segment tree where each node represents an interval of original vertices. Then we add edges from the node v to the segment tree nodes that cover [l, r] (in type2) and from segment tree nodes covering [l, r] to node v (in type3). However, we need to ensure the edges have the correct cost. Usually we create two segment tree graphs: one "outgoing" tree where each tree node can be reached from original vertices (as a representation of intervals for outgoing edges), and one "incoming" tree where each tree node can go to original vertices (representing intervals for incoming edges). Then we add zero-cost edges within each tree to allow traversal.
A known implementation is as follows:
-
We construct a graph with 5n nodes: n original vertices, plus 2*2n? Actually typical: build two segment trees each with 4n nodes, but we can share? We can build two trees: one for "range out" and one for "range in". Let's denote:
-
For type2 (point to range): we want to go from v to any node in [l, r]. We can add edge from v to certain segment tree nodes that cover [l, r] in the "in" tree? Wait we need to map.
Common approach in problems like "Dijkstra with Segment Tree" (e.g., Codeforces problem "Greg and Friends" or "Travel"?) There's a problem "Shortest Path with Range Edges" and typical solution: create two segment trees: tree A (call it "OUT") where from each segment tree node we have edges to its children with cost 0 (or perhaps from parent to children?), but we need to be careful about direction. Actually, we need two directed trees: one where the edges go from the root to leaves (allowing you to go from a segment node to the actual leaf nodes), and another where edges go from leaves to root (allowing you to go from actual leaves to segment nodes). But we need both: type2: from a point v to nodes in a range [l, r]. That suggests we need to go from v (point) to some segment tree nodes that cover [l, r], and then from those nodes to the actual target vertices. So we need a structure that allows: from point v to segment tree nodes (which represent intervals), and then from segment tree nodes to the leaves (actual vertices). But we also need type3: from any node in [l, r] to point v. That suggests we need to go from leaves to segment tree nodes, and then from segment tree nodes to point v.
Thus, we typically create a graph with two "layers": one for moving from a point to an interval (using a segment tree where each node is reachable from the leaves? Actually we need to decide directions.
We can define two segment trees:
-
Tree A (I'll call it "downward tree" or "out tree") where we have edges from each tree node to its two children with cost 0. This allows traveling from a node representing an interval down to the leaves (the actual vertices). So if we are at a tree node, we can reach any leaf in its subtree at zero cost.
-
Tree B (I'll call it "upward tree") where we have edges from each child to its parent with cost 0. This allows traveling from leaves up to nodes representing intervals.
But to use them for type2 and type3, we also need edges between original vertices and the segment tree nodes. Typically, we also have edges from each original vertex to its corresponding leaf in the upward tree? Let's recall standard construction.
Better to think: we have n original vertices (1..n). We add additional nodes representing intervals (segment tree nodes). We have two copies of each segment tree node: one for "outgoing" and one for "incoming"? Or we can use the same node but careful with edges.
I recall a known Codeforces problem: "Pashmak and Graph" maybe? Actually problem: "DZY Loves Fibonacci" no. There is a problem called "Roads in Berland" no.
Let's derive.
We need to add edges of type:
-
point -> point: easy.
-
point -> range: from v to each node in [l, r] with cost w. So we can add an edge from v to a structure that then can reach all nodes in [l, r] with cost 0. So v should have a directed edge to some "proxy" nodes representing [l, r], and from those proxies we can go to each node in [l, r] at zero cost. The proxies can be segment tree nodes that cover the interval. And we also need zero-cost edges within the segment tree to reach the leaves. So we need a structure where from a segment tree node, we can go to its children (i.e., downward) to eventually reach leaves. That's one direction. So if we add an edge from v to a segment tree node X with cost w, then from X we can go (via zero-cost edges) to all leaves (original vertices) in the interval represented by X. That yields edges v -> any leaf in interval of X. So we need a segment tree where edges point from parent to children (downward). This is like a tree for "range output". Let's call this tree T_out. In T_out, each node corresponds to an interval. It has edges to its children (cost 0). So if we are at a node, we can reach any leaf in its subtree at zero cost. Then leaves of T_out are the original vertices? Possibly we could have the original vertices be the leaves of T_out. But we also need to have original vertices as separate nodes to start from? Typically, we set the leaf nodes of the segment tree to be the original vertices (so the leaf i represents planet i). But then we have the segment tree nodes as additional nodes. So the graph includes the segment tree nodes and the original vertices as the same as leaves? Actually, we can make the segment tree leaves be the original vertices. That is common: we build a segment tree over the indices, and each leaf corresponds to an original vertex. Then we have internal nodes representing intervals. So total nodes = up to 4n. Then we add zero-cost edges:
-
For each internal node, add edges from it to its left child with cost 0, and to its right child with cost 0 (downward). That allows us to go from any internal node to any leaf in its interval at zero cost. However, we also need to be able to go from a leaf to internal nodes for type3 (range -> point). For type3, we need to go from any leaf in [l, r] to point v. So we need a way to go from leaves to some proxy nodes representing intervals. That would require edges from leaves up to internal nodes (i.e., upward edges). So we need to also have edges from children to parent (upward) with cost 0. But if we add both directions (downward and upward) in the same tree, then a leaf could go up to an internal node, then from that internal node to another leaf? That would create unintended edges (like from any leaf to any leaf via up then down). That might be okay if we only use the appropriate edges for the purpose, but we need to ensure we don't inadvertently create edges that change the graph. Actually, it's fine; it's just adding zero-cost connections, but they might allow paths that we don't intend? However, the graph is directed. If we add both parent->child and child->parent edges, then it's essentially bidirectional with zero cost, which might allow moving between leaves arbitrarily. But we don't want that because then from any leaf you could go to any other leaf at zero cost, which is not allowed. So we cannot have both sets of edges in the same tree because they would connect all leaves. So we need separate trees for upward and downward directions.
Thus, a typical solution uses two segment trees: one for "outgoing" edges (point to range) and one for "incoming" edges (range to point). In each tree, we only have directed edges in one direction: in the "outgoing" tree, edges go from parent to children (so you can go from a node to its leaves). In the "incoming" tree, edges go from children to parent (so you can go from a leaf up to a node representing an interval). Then, we connect the two trees via the leaves: we add edges from the leaf in the outgoing tree to the leaf in the incoming tree (or maybe they are the same original vertices, but we need to unify them). Actually, we can treat the original vertices as separate nodes and connect both trees to them.
Another common approach: Build a graph with 2 segment trees: a "range" tree and a "reverse" tree. Then we have original vertices as separate nodes, and we connect them to the leaves of both trees with zero-cost edges? Let's think.
We have original vertices (1..n). We want to support:
-
Type2: v (original) -> [l, r] (original) with cost w. So we need path: v -> some proxy node(s) -> original nodes in [l, r]. The proxy nodes should represent intervals, and from those proxies we can reach all original nodes in that interval via zero-cost directed edges. So we need a set of nodes (interval nodes) that have outgoing edges to original nodes. So we build a segment tree (call it "downward tree") where each internal node has edges to its children (cost 0). The leaves of this tree are the original nodes. So from an internal node, you can go to any leaf in its interval at zero cost. Then for type2, we add an edge from original node v to each segment tree node that exactly covers [l, r] (i.e., the O(log n) nodes that partition [l, r]) with cost w. That's it. Because from v we go to those nodes (cost w), then from those nodes down to leaves (original nodes in [l, r]) at zero cost. That works.
-
Type3: [l, r] (original) -> v (original) with cost w. This is symmetric: we need a way to go from any original node in [l, r] to v via cost w. So we need a structure where original nodes can go up to some interval nodes (with zero cost) and then from those interval nodes we have an edge to v (with cost w). So we need a segment tree where edges go from children to parent (upward) with zero cost, so that from any leaf (original) we can go up to any ancestor node (which represents an interval containing that leaf). The leaves are original nodes. So we build a second segment tree (call it "upward tree") with edges from each node to its parent (or from children to parent) with cost 0. Then for type3, we add edges from the segment tree nodes that exactly cover [l, r] (the same decomposition) to original node v with cost w. Then from any original x in [l, r], you can go up to the segment tree nodes that cover [l, r] (actually, you go from x leaf up to ancestors that include x; but we need to go to those specific nodes that represent the interval [l, r]. To get from x to those nodes, we need a path: from x (leaf) up the tree to the ancestors; but we need to reach exactly the nodes that partition [l, r]. Since x is within [l, r], those partition nodes are ancestors of the leaves? Actually, the segment tree nodes that partition [l, r] are nodes whose intervals are subsets of [l, r] and cover [l, r]. For any leaf x in [l, r], there is a unique path from leaf to root. Some of those partition nodes might be ancestors of x, but not necessarily all? Let's think: In a segment tree, to represent a query interval [l, r], we break it into O(log n) nodes whose intervals are disjoint and cover [l, r]. Each such node is not necessarily an ancestor of every leaf in its interval? Actually, each node's interval covers a set of leaves. For a leaf within that node's interval, the leaf is in the subtree of that node, so that node is an ancestor of the leaf. Yes, in a standard segment tree, each node corresponds to an interval of indices. For any leaf i, the set of nodes on the path from the root to leaf i are exactly those nodes whose intervals contain i. So if a node's interval is a subset of [l, r] and used to cover [l, r], then that node's interval is fully inside [l, r], and any leaf in that node's interval is also in [l, r]. And that node is an ancestor of each leaf in its interval. Therefore, from leaf x (original node) we can go up the upward tree (via child->parent edges) to reach that node. So we need edges from children to parents in the upward tree. So we build a tree where each node (except root) has an edge to its parent (cost 0). Then from leaf we can climb to any ancestor at zero cost. Then we add edges from the covering nodes to target v with cost w. So the path: original x (in [l,r]) -> (upwards) to covering node (cost 0) -> edge to v (cost w). That yields x->v with cost w. Good.
Now we also have type1 edges: v -> u. That can be directly added as original to original edge.
Additionally, we need to allow paths that combine these structures: e.g., from original to original via multiple steps that may involve going through segment tree nodes. But we have both trees. However, note that the two segment trees are separate: the "downward tree" only provides edges from interval nodes to leaves (downward) and from original leaves to interval nodes? Actually, in the downward tree we only have edges from parents to children. But we also might need to go from original leaf to some interval node in the downward tree for type2? No, for type2 we directly add edge from original v to interval nodes. So we need edges from original v to some nodes in the downward tree. That's fine: we can add edges from original node (which is also a leaf of downward tree) to the interval nodes? Actually, the downward tree's leaves are the original nodes, but we also have internal nodes. In type2, we need to add an edge from original v to those interval nodes. So we need to allow edges from original v (a leaf of downward tree) to some internal node of downward tree. That is allowed: we just add an edge between nodes in the graph. So original nodes are present as leaves of the downward tree, but we can also treat them as separate nodes? Typically we unify: the original vertices are exactly the leaves of both segment trees? But we have two segment trees; they cannot share the same leaf nodes because the leaves represent the same original vertices but if we treat them as distinct copies, we need to connect them. Common approach: we have three sets of nodes: original nodes (1..n), and then two segment tree structures each with its own internal nodes and leaves. But we can avoid duplication by having the original nodes be leaves in both trees, but then both trees would share the same leaf node IDs. That would mean the two trees are not separate; they are intertwined. But can we have a node that has both parent->child edges (in downward tree) and child->parent edges (in upward tree) at the same time? That would cause the problem mentioned: leaf node would have both incoming from parent and outgoing to parent? Actually, if we combine the trees into one set of nodes, we would have both directions of edges between parents and children. That would create cycles of zero cost and connect all leaves. Let's examine: In a combined tree, we would have edges from parent to child (downward) and child to parent (upward). That would allow moving from any leaf up to the root and down to any other leaf, giving zero-cost connections between any two leaves. That is not intended. So we must keep the two trees separate, i.e., the segment tree nodes used for downward edges and those for upward edges must be distinct sets, even though they represent the same intervals conceptually. The leaves also need to be distinct: we have original nodes and also leaf nodes in each tree. But we can connect original nodes to the leaves of the trees. Actually, many solutions use this approach: create a graph with 5n nodes: original nodes (n), plus two segment trees each of size 4n? But we need to connect them.
Better: Let's design as follows:
-
We have a set of original vertices: 1..n.
-
Build a segment tree (call it "Tree1") for point-to-range (type2). In Tree1, the leaves are not original vertices but we link them to original vertices with zero-cost edges? Actually, we can make Tree1 leaves correspond to the original vertices? But then we cannot also have Tree2 leaves correspond to the same vertices because that would merge. However, we can keep them separate: We can build Tree1 with its own leaf nodes (say, 1..n shifted), but then we need zero-cost edges from Tree1 leaves to original vertices (or from original to Tree1 leaves) for transitions. But let's think: In type2, we need to go from original v to some internal nodes of Tree1 (the covering nodes) and then from those nodes down to original vertices (in [l, r]). If Tree1 leaves are not the original vertices, we need to eventually get to original vertices. So we need edges from Tree1 leaves to original vertices (or vice versa). But if we make Tree1 leaves directly represent original vertices (i.e., leaf i corresponds to planet i), then that would be simpler. But then Tree2 would also need leaf i to represent planet i to allow going from original to tree2. But if we use the same leaf node for both trees, then edges from child to parent (in tree2) and parent to child (in tree1) would exist simultaneously on the same node, causing bidirectional zero edges. However, maybe we can avoid that by not having both sets of edges on the same node: we could treat Tree1 as a directed acyclic graph where edges go from parent to child, and Tree2 as another directed acyclic graph where edges go from child to parent. If both trees share the same node objects, then a node (like leaf i) would have parent->child edges (from its parent in Tree1) and child->parent edges (to its parent in Tree2) but these are different parents? Actually, the parent of leaf i in Tree1 is the same as parent in Tree2 if they share the same tree structure? Typically we build two separate segment trees with identical structure but separate node IDs. So the leaf node in Tree1 is a distinct node from the leaf node in Tree2. This way, there is no connection between them unless we add explicit edges. So we can have two independent segment trees: TreeOut (for downward) and TreeIn (for upward). Their leaves correspond to the same original indices, but they are different nodes. Then we add edges from each leaf of TreeOut to the corresponding original vertex (or maybe the original vertex is a separate node) and from each original vertex to each leaf of TreeIn? Or we can avoid original vertices by using the leaves as the original vertices themselves, but then we have two copies of each original vertex, which is problematic because we need to differentiate when starting from s. Typically, we designate the original vertices as a separate set and connect them to both trees.
There are multiple designs; we need to be consistent.
A classic solution for this type of problem (like Codeforces 786B - Legacy) uses two segment trees: one for outgoing range edges and one for incoming range edges. In that problem, there are three types: 1) v -> u; 2) v -> [l, r]; 3) [l, r] -> v. The typical approach is:
-
Build two segment trees: tree1 (call it "out tree") with edges directed from parent to child (cost 0). The leaves of this tree correspond to the actual vertices (1..n). So the leaf nodes are the vertices themselves. Then tree2 (call it "in tree") also with leaves as vertices, but edges directed from child to parent (cost 0). However, if we use the same vertex IDs for leaves in both trees, then a vertex would have both parent->child and child->parent edges, causing the problem. Wait, the classic solution actually creates extra nodes: they build a segment tree with 4n nodes, and then they create a second segment tree with another 4n nodes. They also have the original vertices (1..n) as separate nodes? Actually, in many implementations, they assign IDs: 1..n are the original vertices. Then they build two segment trees, each with 2 * (some power) nodes, but the leaves of these trees are not the original vertices; instead, they connect the leaves to the original vertices with zero-cost edges. For example, see the solution for CF 786B: they build a segment tree for outgoing edges (range to point) and another for incoming edges. Let me recall: In that problem, there are three types: 1) add edge u->v w; 2) add edge u->[l,r] w; 3) add edge [l,r]->v w. The standard solution:
-
They have two segment trees: tree_out and tree_in, each of size 4n. The leaves of tree_out and tree_in correspond to the indices 1..n, but they are separate nodes. They add edges from the leaves of tree_out to the actual vertices (or the vertices are the leaves? Actually, often they treat the actual vertices as leaves of both trees, but to avoid mixing directions, they create two copies of the segment tree: one where edges go from parent to child (out tree) and one where edges go from child to parent (in tree). However, they still use the same set of leaf nodes? Let me think: In CF 786B, typical code:
They build a graph with total nodes = (n * something). They allocate base = n+1? Actually, I recall one implementation: they build a segment tree with nodes numbered from n+1 to n+4*n. They set up edges:
- For each node i in the segment tree (which covers some interval), they add edges:
- From i to its left child (i<<1) with weight 0.
- From i to its right child (i<<1|1) with weight 0.
(This is for the "out" tree)
- And they also build another tree for "in" edges, where they add edges:
- From left child to i with weight 0.
- From right child to i with weight 0.
But then they need to connect the leaves to the original vertices. Usually, they set the leaves of the segment trees to be the original vertices themselves: i.e., for the out tree, the leaf corresponding to index x is the node x (original vertex). But if they also use the same leaf for the in tree, then the node x would have both outgoing edges to its children (but leaf has no children) and incoming edges from its children? Actually, leaf nodes have no children. So in the out tree, for a leaf node (original vertex), there are no edges from it to children because it's a leaf. But there are edges from its parent to it (since parent->child edges). In the in tree, there would be edges from the leaf to its parent (child->parent). So a leaf node x would have incoming edge from its parent in the out tree (since parent->child) and outgoing edge to its parent in the in tree (child->parent). So there is no direct zero-cost cycle between different leaves because to go from leaf x to leaf y, you would need to go: x -> parent_in (in tree) up to root? Actually, with these edges, leaf x can go up in the in tree (via child->parent edges) to some ancestor, then from that ancestor, can we go down to another leaf? Not directly because the ancestor is part of the in tree, and from an in tree node, we have only edges from its children to it (child->parent), and also maybe we can connect out tree? But there is no edge from an in tree node to an out tree node unless we explicitly add them. Typically, to allow transitions from in tree to out tree, we connect corresponding leaves: for each leaf (original vertex) we add a zero-weight edge from the leaf in the in tree to the leaf in the out tree? Or something like that. Actually, let's recall the typical construction:
In many solutions for CF 786B, they build two segment trees: one called "SEG_OUT" where edges go from parent to child, and one called "SEG_IN" where edges go from child to parent. However, they both share the same set of nodes? Actually, they often use only one segment tree, but they add both types of edges and then also add edges from leaves to a "virtual node" or something? Let's check known editorial.
The problem is similar to "Legacy" (CF 786B). Yes, this is exactly that problem! Indeed, the problem description is identical: Rick and Morty, portals, plans. So it's CF 786B. Let me verify: CF 786B "Legacy" has exactly the same statements: n, q, s, three types of edges. So it's the same problem. So we can implement the known solution.
The editorial for 786B: Use a segment tree to transform range edges into O(log n) edges. Standard approach: Build two segment trees: one for outgoing and one for incoming. Or use a single segment tree but with two types of edges.
Let me recall typical solution details:
We have n vertices (1..n). We build a segment tree with about 4n nodes, but we will assign each node an ID. The root represents the whole range [1, n]. The leaves correspond to individual indices; but instead of using the original vertex IDs for leaves, we create new IDs for the leaves, but we can also make the original vertices be the leaves. However, we need two trees, or one tree with extra edges.
One common method: Build a graph with (n + 8*n) nodes. Create two segment trees: "tree1" for point-to-range (type2) and "tree2" for range-to-point (type3). In both trees, the leaves are the original vertices. But to avoid cycles, we must separate the trees or use zero-weight edges between the trees appropriately.
Better: Use a single segment tree but with two sets of edges: we create edges from parent to child with weight 0 (downward) for "out" function, and also edges from child to parent with weight 0 (upward) for "in" function. Then we treat the original vertices as leaves. But then the graph will have both directions between parents and children, which creates zero-weight cycles and potentially allows moving between any leaves at zero cost? Let's test: Suppose we have node representing interval [1,2] as parent, children [1,1] and [2,2] (leaves). Edges: parent->left (0), parent->right (0) for out; left->parent (0), right->parent (0) for in. Then from leaf 1 we can go leaf1 -> parent (0) -> leaf2 (0), so indeed we can go from leaf1 to leaf2 at zero cost. This is not intended, because we didn't add any plan that allows free teleportation. So we must avoid that. Therefore, we need to separate the two trees, so that the edges for "out" and "in" are on different copies of the segment tree.
Thus, we need two copies: one for "out" (downward edges) and one for "in" (upward edges). And then we connect the leaves of these trees appropriately to the original vertices, or we treat the original vertices as a third set? Let's think.
Standard solution from known codes: They build two segment trees: tree1 (for range out) and tree2 (for range in). They assign node IDs: for tree1, base id = n+1? Actually, I've seen code where they allocate a segment tree with size 4n, and they assign each node an ID starting from n+1. Then they also have tree2 with IDs starting from n+4n+1? Not sure.
Let's derive from scratch.
We need to construct a directed graph with vertices such that:
-
Original vertices: 1..n.
-
For type2 (v -> [l, r] with weight w): we want to add edges from v to each u in [l, r] with weight w. Instead, we will add edges from v to some "interval nodes" that cover [l, r] (like segment tree nodes) with weight w, and then from those interval nodes to each actual vertex in that interval with weight 0. So we need a set of nodes (call them "out_nodes") that have a directed path of weight 0 to each original vertex in their interval. So we can build a tree (segment tree) where each node corresponds to an interval, and there are directed edges from each node to its children with weight 0. The leaves of this tree are the original vertices (so that from a leaf, we are at the original vertex). However, if the leaf is the original vertex itself, then from an internal node, we can go down to the leaf (original vertex) via zero edges. So that works. But then, note: we also need to add edges from original vertices to some internal nodes (for type2). That's fine: we can add edges from original vertex v (which is also a leaf) to the internal nodes covering [l, r]. So type2 is okay.
-
For type3 ([l, r] -> v with weight w): we want to add edges from each u in [l, r] to v with weight w. So we need a structure where from any original vertex in [l, r], we can go via zero edges to some "interval node" that represents that interval, and then from that interval node to v with weight w. So we need a tree where there are directed edges from children to parent (upward) with weight 0. The leaves again are the original vertices. Then from a leaf, we can go up to any ancestor (which corresponds to an interval containing that leaf) at zero cost. Then we add edges from those covering nodes (the interval nodes) to v with weight w. So type3 works.
But if we use the same set of nodes for both structures (i.e., the same segment tree with both downward and upward edges), we get the zero-cost path between leaves as described. So we must separate them: we need one copy of the segment tree for downward edges (call it "down_tree") and another copy for upward edges (call it "up_tree"). But then leaves in both trees are original vertices? They both need to be connected to original vertices. If we treat the original vertices as the leaves of both trees, then we have two trees that share leaf nodes. But leaf nodes are the same original vertices. That means each original vertex will have incoming edges from its parent in the down_tree? Actually, in down_tree, edges are from parent to child, so leaf (original) receives edges from its parent. In up_tree, edges are from child to parent, so leaf (original) has outgoing edges to its parent. So the leaf node would have: from down_tree parent to leaf (cost 0) and from leaf to up_tree parent (cost 0). There is no direct edge from up_tree parent to down_tree leaf or vice versa that would allow leaf to leaf zero cost? Let's check: Suppose we have leaf x (original). In down_tree, there is a path from some internal node A (covering interval) down to x with cost 0. In up_tree, there is a path from x up to some internal node B with cost 0. However, there is no edge from an up_tree internal node to a down_tree internal node or leaf (other than possible connections via original vertices). Could we go from leaf y to leaf x via zero cost? Starting from y (original) we can go up in up_tree to some internal node, but then to get to x we need to go down in down_tree. But there is no edge from up_tree internal node to down_tree internal node. So unless we add some connection, they are separate. So it's safe to share the same leaf nodes if we only have one set of original vertices, and we have two separate trees attached to them: one with parent->child edges (down_tree), and one with child->parent edges (up_tree). The leaf (original) is the point where the two trees meet. But is there any unintended zero-cost path from leaf x to leaf y? From x, we can go up in up_tree to some ancestor (cost 0). From that ancestor, we cannot go to any down_tree node because we only have up_tree edges (child->parent) and no edges from up_tree nodes to down_tree nodes. Unless we add edges that connect up_tree nodes to down_tree nodes for the queries, but we only add edges from up_tree nodes to original vertex v (type3) and from original vertex v to down_tree nodes (type2). Actually, in type3, we add edges from up_tree nodes (covering [l,r]) to original vertex v. That edge goes from up_tree node to original v. Then from original v, we could go down in down_tree to any leaf? But original v is a leaf of down_tree; from v (leaf) we can only go upward? Wait, in down_tree, edges are parent->child, so from leaf v we cannot go to its parent (since direction is from parent to child, not child to parent). So from v we cannot go up to internal nodes of down_tree. So v can only be a source of edges to down_tree nodes if we explicitly add edges from v to down_tree nodes (type2). But v itself as a leaf cannot travel up the down_tree. So if we have a path that goes from x up in up_tree to some node A, then from A to v (original) via type3 edge, then from v we could potentially go to some down_tree node via type2 edge? But that would require an edge from v to down_tree node, which exists only if there is a type2 plan from v to somewhere. That's not generic; it's only if such a plan exists. So it's fine.
Thus, it seems safe to have the original vertices be the leaves of both trees, as long as the trees have directed edges only one way: down_tree edges go from parent to child; up_tree edges go from child to parent. The two trees are separate except that they share the leaf nodes (original vertices). The leaf nodes themselves have no outgoing edges in down_tree and no incoming edges in up_tree (except those we add). Actually, they have incoming edges from down_tree parents (since parent->child) and outgoing edges to up_tree parents (since child->parent). But they don't have edges in the opposite direction in those trees. So a leaf can receive from down_tree parents, and can send to up_tree parents. That's okay.
Now, the graph we will build: total nodes = n (original) + number of nodes in down_tree + number of nodes in up_tree. Since we need segment trees covering ranges [1, n], we can use a typical segment tree representation with about 4n nodes each? Actually, if we use a segment tree of size 4n, we have 4n internal nodes. But in many implementations, they use two segment trees each with 2 * (next power of two) nodes, but total nodes 2 * (4n) = 8n + n = 9n approx. That's fine (since n up to 1e5, 9e5 nodes, edges O((q + n) log n) which is okay).
Alternatively, we can build the trees recursively and assign IDs accordingly.
Let's define:
- We have original nodes: ids 1..n.
- For down_tree: we need nodes for each segment tree node (including leaves). But if we use original nodes as leaves, we don't need extra leaf nodes; we can treat the original nodes as the leaves of the segment tree. However, building a segment tree where leaves are original nodes means that each internal node's children could be either another internal node or an original node. But we need to assign IDs to internal nodes. So we can assign internal node IDs starting from n+1. And for each internal node, we add edges: from node to its left child with weight 0, and from node to its right child with weight 0. The left and right child could be either internal nodes (if not leaf) or original nodes (if leaf). That works because we have IDs for children.
Similarly, for up_tree: we need internal nodes (IDs starting from maybe n + size_down_tree + 1). And we add edges: from left child to node with weight 0, from right child to node with weight 0. Again, children can be internal or original nodes.
Thus, we need to build two segment trees over the same range [1, n], sharing the same leaf IDs (original nodes). However, the internal node IDs are separate.
Now we need to handle the queries:
-
Type1: v -> u with weight w. Add edge from v (original) to u (original) with weight w.
-
Type2: v -> [l, r] with weight w. This requires adding edges from v (original) to each node in the decomposition of [l, r] in the down_tree. Because from those nodes, via zero edges we can reach all leaves in that interval. So we need to get the O(log n) segment tree nodes that exactly cover [l, r] in the down_tree, and for each such node id (internal node), add edge v -> node_id with weight w.
-
Type3: [l, r] -> v with weight w. This requires adding edges from each node in the decomposition of [l, r] in the up_tree to v (original) with weight w. Because from any leaf in [l, r], they can go up (via zero edges) to those covering nodes, and then from those nodes to v. So we add edge from node_id (up_tree internal node) to v with weight w.
Now, we also need to ensure that from an original vertex we can use both structures appropriately. For example, if we have a path that goes original -> down_tree node (via type2) -> ... -> original (via down_tree zero edges). That's fine. Also, original -> up_tree node? There's no direct need, but if we have type3, original vertices can go to up_tree nodes via zero edges (since they are children of up_tree nodes? Actually, in up_tree, edges are from child to parent. So from original (leaf) we can go to its parent (up_tree internal node) with zero cost? Yes, because up_tree edges are from child to parent. So original vertices have outgoing zero-weight edges to their parent in up_tree. That is necessary for type3 to work: from any leaf in [l, r] we need to be able to go up to the covering nodes. Since covering nodes are ancestors of the leaf, we need edges from leaf to parent, and then from parent to grandparent, etc. So we must add zero-weight edges from each leaf (original) to its parent in up_tree, and similarly from each internal node in up_tree to its parent. So in up_tree construction, we add for each node (including leaves? Leaves are original nodes) edges: from left child to node (0), from right child to node (0). For leaf (original) as child, that edge is from original to its parent. So that's included.
Similarly, in down_tree, we need zero-weight edges from parent to child: for each internal node, edges: from node to left child (0) and from node to right child (0). That includes edges from internal nodes to original nodes (if child is leaf). So original nodes receive incoming zero-weight edges from their ancestors in down_tree. That's needed to reach them from covering nodes.
Now, note: In down_tree, from an original vertex, can we go to its parent? No, because edges are only from parent to child. So original cannot go up in down_tree. That's fine.
In up_tree, from an original vertex, we can go up to parent etc., but cannot go down (since edges are child->parent only). So the two trees are separate in direction.
Now, we also need to consider if there is any path that uses both trees: For instance, starting from original s, we might want to use a type3 plan (range->point) to go to some node, but to get into that plan we need to be in the range. Actually, to use a type3 plan (range->point), you need to be at a vertex in [l, r]. From that vertex, you can go up in up_tree to covering nodes, then take the edge to v. So it's fine.
Similarly, to use a type2 plan, you need to be at v (point) and then go to covering nodes in down_tree via the added edge, then down to any vertex in [l, r] via zero edges. So fine.
Now, we also need to consider that the graph may have paths that combine type2 and type3, e.g., from s to some a via type2, then from a to b via type3, etc. That will work because after a type2, we end at an original vertex (since down_tree zero edges lead to original leaf). Then from that original vertex we can go up in up_tree (if needed) to use a type3 plan. So that's allowed.
Thus, the construction is sound.
Now we need to implement:
- Build down_tree: assign each segment tree node an ID. Since we have original ids 1..n, we can assign down_tree internal nodes ids starting from n+1. But we need to know the total number of nodes to allocate arrays for adjacency list. Let's compute maximum nodes: We have original: n. Down_tree: we need up to 4n nodes (since typical segment tree uses 4*size). Up_tree: another 4n nodes. So total nodes <= n + 8n = 9n = 9e5, which is okay.
But careful: The down_tree internal nodes might be up to 4n, but some of them may not be used? We'll allocate for simplicity an array of size 4 * n + 5 for each tree.
However, we must be careful: The leaves are original nodes, so when building the tree, we don't create new leaf nodes; we just reference original IDs. That means the tree internal nodes have IDs > n, but the children could be either internal node IDs or original IDs (if child is leaf). For internal node, its left and right children could be original nodes if the segment length is 1? Actually, in typical segment tree implementation, we recursively build nodes; if the segment length is 1, that node would be a leaf and we would normally assign it the original id. But if we assign internal node ids from n+1 upward, we need to know when to stop. Alternative: Build two separate trees each with its own nodes including leaves, and then connect leaves to original vertices with zero-cost edges. This might be simpler: we create a tree with 4n nodes (including leaves) but these leaf nodes are not original vertices; they are separate. Then we add zero-cost edges between leaf nodes of the tree and original vertices. That adds 2n edges (for each leaf to original and maybe original to leaf) depending on direction. This approach avoids mixing original vertices as leaves and simplifies the tree building. However, it adds more nodes and edges but still within limits.
Let's examine both.
Approach 1 (original as leaves): In the down_tree, we need to assign internal node IDs, and for leaf ranges we need to treat the node as the original vertex ID. That means when we build the tree recursively, for a leaf (l == r), we don't create a new node; we just use the original id l. But then the internal node's children are either internal node IDs or original IDs. This is doable. For up_tree similarly.
We'll need to know for each internal node its children ids (which may be original or internal). We can store the children ids during construction.
The advantage: fewer nodes (n + 2 * (number of internal nodes)). But we must handle the tree structure carefully.
Approach 2 (separate leaf nodes): We create two full segment trees each with 4n nodes, where leaf nodes are new IDs, and we connect each leaf to the corresponding original vertex with zero-cost edge in the appropriate direction. For down_tree: we need from the internal node eventually to original vertex. So if we have leaf nodes in down_tree that are separate, we add edge from leaf node to original vertex with weight 0 (or from internal to child etc.) Actually, to enable from an internal node to reach original vertices, we can have edges: from internal node to its children (0), and the leaves of down_tree then have edges to the corresponding original vertices (0). So from an internal node, you go down to leaf, then to original. That adds an extra hop but okay. For up_tree: we need from original vertices to be able to go up to internal nodes. So we add edges from original vertices to leaf nodes of up_tree (0), and from leaf nodes up to parent (0). That works.
Which is simpler to code? Possibly the separate leaf approach, because we can build a standard segment tree where each node has an ID, and the leaves are new nodes. Then we add the zero-cost connections from leaves to original (or vice versa) as needed. However, we need to be careful about direction and ensure we don't accidentally create zero-cost cycles through the original vertex linking the two trees. Because if we add edges from down_tree leaf to original (0) and from original to up_tree leaf (0), then from an up_tree internal node we can go down? Let's test: Suppose we have down_tree leaf Ld connected to original O (Ld -> O, cost 0). And up_tree leaf Lu connected from O (O -> Lu? Actually, we need original to go to up_tree leaf? In up_tree, we need from original to be able to go up to internal nodes. So we add edge from original O to up_tree leaf Lu with cost 0? But then from O we can go to Lu, then via up_tree edges (child->parent) go up to internal node. That's fine. So we would have O -> Lu (0). So then from Ld (down_tree leaf) we can go to O (0), then to Lu (0), then up in up_tree to some internal node, then maybe from that internal node we could go to some other node via plan edges? But that's okay. Could it create a zero-cost cycle? Possibly if there is also an edge from up_tree leaf to O? That would be a cycle: O -> Lu -> ... -> O. But we don't have Lu -> O; we have O -> Lu. So no cycle. However, could there be a path from down_tree internal node to up_tree internal node that leads to zero-cost between two different original vertices? For example, from original A we want to go to original B at zero cost without any plan. Is that possible? Let's see: Starting from A (original). To go to B, we could go A -> (some edge) -> ??? Without any plan, the only edges are the zero-cost edges we add for tree connections. For down_tree: we have internal nodes -> children (0) and leaf -> original (0). There is no edge from original to down_tree internal node except those added via type2 plans. For up_tree: we have original -> up_tree leaf (0) and child->parent (0). So from A, we can go A -> up_tree leaf (0) -> up_tree parent (0) -> ... -> up_tree root (0). From up_tree root, can we go to down_tree? No direct edge. Unless there is a plan that adds edge from up_tree node to something (type3 adds edges from up_tree node to original v). That would be from up_tree node to original v with weight w. That's not zero cost unless w=0 (but w >=1). So not zero. Also could go from up_tree leaf to original? We didn't add that edge. So no zero-cost path from A to B unless there is a plan. So it's safe.
But we must also connect leaves of down_tree to original? The direction should be from down_tree leaf to original, because from an internal node we go down to leaf and then to original. So we need leaf -> original (0). For up_tree, we need original -> leaf (0) because from original we need to go up. So direction: down_tree leaf -> original; up_tree original -> leaf. That seems consistent.
We also need to connect internal nodes to children in down_tree: from parent to left child (0), parent to right child (0). For up_tree: from left child to parent (0), right child to parent (0).
Now, for type2: we need to add edge from original v to the segment tree nodes that cover [l, r] in down_tree. Since down_tree internal nodes can reach leaves (and then original) via zero edges, we add edge from v (original) to each covering node (internal node) with weight w. Note: could we also add edge from v to leaf nodes? In decomposition of [l, r], we only get internal nodes, but if [l, r] is a single point, the decomposition might return that leaf node? In segment tree query, when we have a point, the standard decomposition returns the leaf node representing that point. But in our separate leaf approach, leaf nodes are separate from original vertices. So if [l, r] is a point, we would add edge from v to that leaf node. That's fine because leaf node can go to original u via zero edge (leaf->original). So we need to include leaves as well. So our query function for range [l, r] should return all nodes (including leaves) that exactly cover the range. Typically, in a segment tree, we recursively traverse and when a node's segment is fully inside the range, we take that node. This includes leaves when the range is exactly a point. So that works.
Similarly for type3: we need to add edge from up_tree covering nodes (including leaves) to original v with weight w.
Now, we must ensure that from the source s (original) we can start. Since we have original vertices as separate nodes, we just start Dijkstra from s.
Now, we need to compute shortest distances from s to all original vertices.
Graph size: Let's compute approximate number of nodes.
- Original: n.
- Down_tree: we need nodes for each segment tree node. If we build a segment tree over range [1, n], typically we allocate an array of size 4n. For each node, we need a unique ID. So down_tree nodes count: up to 4n (including leaves). Actually, if we use 1-indexed, root=1, then left child = 2id, right = 2id+1, which for a full binary tree of size next power of two, total nodes up to 22^ceil(log2(n)) - 1 <= 4n. So we can use this numbering directly without separate ID mapping? But note: we need to avoid conflict with original IDs (1..n). So we can shift the tree IDs by an offset. For down_tree, we can assign IDs starting from n+1. But if we use the usual segment tree indexing (root=1, children 2,3,...), then the IDs will be from 1 to 4n, which conflict with original IDs (1..n). So we need to map them to unique IDs. Approach: we can keep the original IDs 1..n, and for the down_tree, we assign IDs from n+1 to n + size_down_tree. We'll need to manage the tree structure: we can't use the implicit 2id, 2id+1 because those IDs would overlap with original. Instead, we can build the trees with explicit node objects, or we can allocate arrays of size 4n and then add an offset to convert to global ID. Another common trick: use two segment trees with root indices 1 and another starting from 4n+1? Actually, we can treat the segment tree node indices as separate from original. For down_tree, we can use indices from 1 to 4n (or maybe 2 * NODE?) but we need to make sure they don't conflict with original nodes. So we could allocate global IDs: original: 1..n. Then down_tree nodes: n+1 .. n + M where M = 4n. Up_tree nodes: n+M+1 .. n+2M. But we need to maintain the tree structure: we can assign IDs to nodes during building, ensuring left/right children have consistent IDs.
Simpler: Use recursive build functions that return the node ID for each segment. We'll maintain two arrays: down_id[node] for segment tree node (by the index in the tree structure) but we want to assign global IDs. Alternatively, we can predefine a static numbering scheme: For down_tree, we can use root=1 (global) but we will shift: let base_down = n (or n+1) ??? Actually, we can decide that original IDs are from 1 to n, and we will use IDs from n+1 to n+4n for down_tree, and from n+4n+1 to n+8n for up_tree. But then we need to know the children IDs for each node. If we build the tree recursively, we can assign IDs sequentially. For example, we can have a function build_down(l, r) that returns the node ID. For leaf (l==r), we could choose to not create a new node; instead use original ID l. But if we want to keep separate leaf nodes, we will create a new node even for leaf. That might be simpler: we create a new node for each segment tree node, including leaves. Then we add edge from leaf node to original l (0). So we don't use original IDs as leaves. This avoids mixing. Then we can assign IDs to tree nodes arbitrarily, as long as they are unique. We can allocate an array for the tree with up to 4n nodes. We can use a static index for tree nodes: we can number them from 1 to M (M=4*n) for down_tree, but that would conflict with original IDs if we also use original IDs 1..n. So we need to offset. We could start original IDs at 1, and tree node IDs at n+1. But if we use a recursive build that uses a global counter starting from n+1, that's fine.
So, let's adopt approach: separate leaf nodes. Build two segment trees: down_tree and up_tree. Each tree will have its own nodes, with IDs assigned from a global counter.
Let totalNodes = n + 2 * (4n) (worst-case). But 4n could be up to 400k for n=100k, so 800k + 100k = 900k, which is okay.
We need to store adjacency list for each node. 900k nodes, edges: each tree has O(4n) edges (parent-child edges) which is about 24n = 8n? Actually each tree has about 2*(number of internal nodes) edges (each internal node has 2 edges to children). So ~22n? Actually typical segment tree has about 2N-1 nodes, but with array size 4n we may allocate many nodes not used. But edges are added only for nodes that exist. We'll allocate for up to 4n nodes per tree, and add edges for each node that has children. So edges from tree building: O(4n). Then queries: each type2 or type3 adds O(log n) edges. So total edges O((n + q) log n) which is okay.
Implementation Steps:
-
Read n, q, s.
-
Determine the maximum number of nodes needed. We'll allocate adjacency list for up to n + 8*n + 5. But we need to know exactly how many nodes we will create. We'll build both trees and assign IDs sequentially.
We'll have a global variable cnt = n (since original nodes are 1..n). Then we build down_tree: recursively create nodes, each new node gets ID = ++cnt. We'll also need to store for each node (representing a segment) its node ID. We'll need to be able to query for range covering: given [l, r], we want to collect the IDs of down_tree nodes that exactly cover that range. So we need a function query_down(l, r, node_segment_range, node_id) that traverses the tree and returns a list of node IDs. But to do that efficiently during Dijkstra? We'll call this function for each query of type2 and type3. Since q up to 1e5, and each query adds O(log n) edges, we can process each query online by traversing the tree and adding edges. So we need the tree structure stored: for each node (by its global ID) we need its segment boundaries? Or we can store the segment tree in a static array with indices representing node positions in the tree (like typical segment tree with root=1, children 2i, 2i+1). That would be easier for range queries. But we also need to map these indices to global IDs. We can decide to use the static indexing for each tree separately, and then map to global IDs via an offset.
Simpler: Use static segment tree representation for each tree, where we pre-allocate arrays of size 4n, and use the index within that tree as the node number, but then we add an offset to get global ID. For down_tree, we can say global ID = down_offset + idx, where idx is from 1 to 4n (or up to 4n). For up_tree, global ID = up_offset + idx. Then we can easily compute children: left child = 2idx, right = 2*idx+1. Then the range queries can be implemented recursively using these indices, and we can add edges using the global IDs.
This is easier:
- Let down_offset = n (or n+1?) Actually original IDs are 1..n. So we can set down_offset = n+1? But then we need to be careful with zero-based. We'll set base IDs: original nodes: 1..n.
- For down_tree, we will have nodes numbered from n+1 to n + down_size, where down_size is the number of nodes we allocate. We can allocate a segment tree with size = 4n, but we may not use all. We can just use indices 1..(4n) for the tree nodes, and then define global_id_down(idx) = idx + down_offset, where down_offset = n. That would make global IDs from n+1 to n+4n. But then original nodes 1..n and down nodes n+1..n+4n. However, note that idx 0 might be unused; we'll start idx from 1.
- For up_tree, we set up_offset = n + 4*n (or n+down_size). So global_id_up(idx) = idx + up_offset.
But careful: 4n could be up to 400k, n is 100k, sum n+4n = 500k, plus another 4*n = 400k -> 900k, which is fine.
But we need to ensure that down_size is at most 4n, and same for up. So we can set tree size = 4 * n (maybe 4 * n + 5). We'll allocate arrays of that size for each tree (like seg_down[4n+5]) to store maybe nothing else needed? Actually we only need the tree structure to know the intervals. For range queries, we need to know the segment [L,R] for each node. We can compute that on the fly during recursion using the node index and the stored segment boundaries? Typically, we can write a recursive function that takes node index, its segment l, r, and the query range, and adds edges accordingly. We don't need to store l,r for each node; we can compute them recursively.
Thus, we can implement:
-
define two functions: add_down_edge(from_node, to_node, w) which adds a directed edge with weight w. But for type2, we need to add edges from original v to down_tree nodes covering [l,r]. So we will have a function range_add_down(v, l, r, w) that traverses the down_tree (starting from root, segment [1,n]), and when node segment fully inside [l,r], we add edge from v (original) to global_id_down(node_index) with weight w.
-
Similarly for type3: range_add_up(l, r, v, w) that traverses the up_tree, and for each node fully inside [l,r], we add edge from global_id_up(node_index) to v with weight w.
Now, we also need to add internal edges for the trees:
-
For down_tree: for each node (except leaves) we add edges from this node to its left child and right child with weight 0. Leaves are nodes representing a single index. For leaf nodes, we add edge from leaf node (down_tree leaf) to the corresponding original vertex (index) with weight 0. This allows from any internal node to eventually reach original vertices.
-
For up_tree: for each node (except leaves) we add edges from left child to node and from right child to node with weight 0. For leaf nodes, we add edge from original vertex to leaf node with weight 0. This allows original vertices to go up to any ancestor.
But careful: In up_tree, the edges direction: child -> parent. So from leaf (up_tree leaf) we need to go to its parent? Actually leaf is a child of its parent. So we need edge from leaf (up_tree leaf) to its parent with weight 0. That is covered by the general rule: for each node (including leaves as children), we add edge from child to parent. But we need to have the leaf nodes defined. So for up_tree, we will add edges: for each node (except root), we add edge from this node to its parent? That's easier: when building the tree, we can add edges after constructing the children: we know parent id, left child id, right child id. So we can add edge from left child to parent (0) and from right child to parent (0). That includes leaf children. So we don't need a separate edge from original to leaf; instead, we have leaf nodes (up_tree leaves) and we need to connect original vertices to these leaves? Wait, the up_tree leaf nodes represent a single index, but they are separate from original vertices. To allow an original vertex to go up the up_tree, we need a path from original to the leaf node (since the leaf node is the entry point to the tree). The up_tree edges are from child to parent. So from original vertex, we need to be able to reach the leaf node (child) first, then go up. So we need an edge from original vertex to the up_tree leaf node with weight 0. That's an extra edge. Similarly, for down_tree, from leaf node we need to go to original vertex. So we need edge from down_tree leaf node to original vertex with weight 0.
Thus, we need to add these connections.
But we could also combine the leaf and original to be the same node, which would avoid these extra edges and reduce node count. However, for simplicity of implementation, separate leaf nodes are okay, and we just add 2n extra edges (n for down_tree leaf->original, n for up_tree original->leaf). That's fine.
Now, we also need to add edges for type1: original v -> original u.
Now, after building the graph, we run Dijkstra from source s (original node s) and compute distances to all original nodes. Distances may be large (up to 1e9 * edges) so use 64-bit.
If unreachable, output -1.
Implementation details:
- Determine the total number of nodes: n + down_size + up_size. Since down_size = up_size = 4*n (we'll allocate that many). We'll have global arrays for adjacency