QOJ.ac

QOJ

実行時間制限: 1.0 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#17865. 假塑料树

統計

树是一种递归结构,它可以是:

  • :空树记为 $-1$,大小为 $0$。
  • 非空:非空树 $T$ 记为一对树 $(T_1, T_2)$,其中 $T_1$ 称为 $T$ 的左子树,$T_2$ 称为 $T$ 的右子树。如果 $T = (-1, -1)$,则称 $T$ 为叶子。叶子的大小为 $1$,非叶子的树大小为 $|T_1| + |T_2|$,其中 $|T_1|$ 是 $T_1$ 的大小,$|T_2|$ 是 $T_2$ 的大小。

如果一个非空树 $T = (T_1, T_2)$ 是平衡的,则称其为假塑料树(Fake Plastic Tree)。形式化地,如果满足 $|T_1| = |T_2|$ 或 $|T_1| = |T_2| + 1$,则 $T$ 是一棵假塑料树。

在计算机科学中,树通常用作一种数据结构并存储在内存中。起初,内存中没有树,只有一个虚构的空指针(对应于空树 $-1$)。你可以通过将 $T_1$ 和 $T_2$ 设置为空指针或已存在树的指针,在内存中分配一棵新树。然后,通过将 $T = (T_1, T_2)$ 添加到其结构中来扩展内存。注意,指针可以用一个较小的整数表示,从而减少了显式存储整棵树的需要。

形式化地,内存 $M$ 是一个归纳结构,起初仅包含空树 $-1$(即 $M = \{-1\}$)。你可以通过以下操作扩展内存:$M \leftarrow M \cup \{(T_1, T_2)\}$,其中 $T_1 \in M, T_2 \in M$。如果在第 $i$ 个阶段插入了一棵树 $T$,则它的编号为 $i - 1$。对于编号为 $i$ 的树,其子树可以用范围在 $[-1, i - 1]$ 内的整数对表示。

你的任务是构造一个内存 $M$,满足以下条件:

  • $M$ 中的每棵树要么是空树,要么是假塑料树。
  • $M$ 中最多包含 $125$ 棵非空树。
  • 存在一棵树 $T \in M$,满足 $|T| = N$。其中 $N$ 是一个整数,作为输入给出。

输入格式

第一行包含一个整数 $T$,表示测试用例的数量。($1 \le T \le 2000$)

接下来的 $T$ 行中,每行包含一个整数 $N$,表示你的树应该包含的叶子数量。($1 \le N \le 10^{18}$)

输出格式

对于每个测试用例,你应该输出 $V + 2$ 行,其中 $V$ 是 $M$ 中非空树的数量。($1 \le V \le 125$)

第一行,你应该输出一个整数 $V$。

接下来的 $V$ 行中,你应该输出两个空格分隔的整数 $L_i, R_i$,表示编号为 $i$ 的树的左子树和右子树的编号。($-1 \le L_i, R_i \le i - 1$)

在第 $V + 2$ 行,你应该输出 $P$,即包含 $N$ 个节点的树的编号。($0 \le P \le V - 1$)

保证在给定条件下,答案总是存在。

样例

输入 1

4
1
2
3
4

输出 1

1
-1 -1
0
3
-1 -1
-1 -1
0 1
2
3
-1 -1
0 0
1 0
2
5
-1 -1
0 0
0 0
2 1
1 2
3

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.