luogu#P9448. [ICPC2021 WF] Splitstream
[ICPC2021 WF] Splitstream
题目描述
A splitstream system is an acyclic network of nodes that processes finite sequences of numbers. There are two types of nodes (illustrated in Figure J.1):
- A split node takes a sequence of numbers as input and distributes them alternatingly to its two outputs. The first number goes to output , the second to output , the third to output , the fourth to output , and so on, in this order. </li>
- A merge* node takes two sequences of numbers as input and merges them alternatingly to form its single output. The output contains the first number from input , then the first from input , then the second from input , then the second from input , and so on. If one of the input sequences is shorter than the other, then the remaining numbers from the longer sequence are simply transmitted without being merged after the shorter sequence has been exhausted. </li>
Figure J.1: Illustration of how split and merge nodes work.
The overall network has one input, which is the sequence of positive integers . Any output of any node can be queried. A query will seek to identify the number in the sequence of numbers for a given output and a given . Your task is to implement such queries efficiently.
输入格式
The first line of input contains three integers , , and , where () is the length of the input sequence, () is the number of nodes, and () is the number of queries.
The next lines describe the network, one node per line. A split node has the format , where , and identify its input, first output and second output, respectively. A merge node has the format , where , and identify its first input, second input and output, respectively. Identifiers , and are distinct positive integers. The overall input is identified by , and the remaining input/output identifiers form a consecutive sequence beginning at . Every input identifier except appears as exactly one output. Every output identifier appears as the input of at most one node.
Each of the next lines describes a query. Each query consists of two integers and , where () is a valid output identifier and () is the index of the desired number in that sequence. Indexing in a sequence starts with .
输出格式
For each query and output one line with the number in the output sequence identified by , or if there is no element with that index number.
题目大意
题意
有一个有 个节点的传输数字序列的网络,其中有两种节点:拆分节点和合并节点。拆分节点会将输入序列中的数字交替插入两个输出序列中,合并节点会交替将两个输入序列中的数字插入输出序列中。例如:
拆分得 和 。
和 合并得 。
在网络中,除 号外每一个结点的每一个输入端都连接着另一个节点的输出端, 号节点的输入端为总输入端,每一个输出端不一定连接着另一个节点的输入端。每一个输出端都有着从 开始的正整数编号。
将一个数字序列 从 号节点的输入端输入网络,你需要求出编号为 的输出端输出的序列中的第 个数字。
输入格式
第一行三个整数 。分别表示结点的个数、输入序列为 、查询的次数。
接下来 行,每行一个字母开头,接着是三个整数 。若字母为 ,则表示这是一个拆分节点, 是它的输入端所连接的输出端编号, 是它的两个输出端的编号;若字母为 ,则表示这是一个合并节点, 是它的两个输入端所连接的输出端的编号, 是它的输出端编号。
再接下来 行,每行两个整数 ,意义如题意中所述。
输出格式
对于每一次询问,输出其对应的回答。若答案不存在,输出 none
。
200 2 2
S 1 2 3
M 3 2 4
4 99
4 100
100
99
100 3 6
S 1 4 2
S 2 3 5
M 3 4 6
6 48
6 49
6 50
6 51
6 52
5 25
47
98
49
51
53
100
2 3 3
S 1 2 3
S 3 4 5
M 5 2 6
3 1
5 1
6 2
2
none
none