QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100

#7815. Struct

Statistiques

In high-level languages like C++, in addition to basic types such as int and float, one can usually define custom structure types. In this problem, you need to simulate a structure definition method similar to that of a high-level language and calculate the corresponding memory usage and other information.

In this language, there are 4 basic types: byte, short, int, and long, which occupy 1, 2, 4, and 8 bytes of space, respectively.

When defining a structure type, you need to provide the type name and its members, where each member must be given its type and name in order. The type can be a basic type or a previously defined structure type. Note that defining a structure type does not define specific elements, so it does not occupy memory.

When defining an element, you need to provide the element's type and name. Elements will occupy memory according to the following rules: All members within an element will be arranged in memory in the order given at the time of definition; the same applies to members that are of a structure type. To ensure the efficiency of memory access, the address occupied by an element must satisfy the alignment rule, meaning the size of any type and the starting address of an element of that type must be aligned to an integer multiple of the alignment requirement for that type. Specifically: For basic types: The alignment requirement is equal to the size of the space it occupies. For example, the int type needs to be aligned to 4 bytes, and others follow the same rule. For structure types: The alignment requirement is equal to the maximum value of the alignment requirements of its members. For example, a structure type containing int and short needs to be aligned to 4 bytes.

The following is an example (written in C++ format):

struct d {
    short a;
    int b;
    short c;
};
d e;

This code defines the structure type d and the element e. Element e contains three members e.a, e.b, and e.c, which occupy addresses $0 \sim 1$, $4 \sim 7$, and $8 \sim 9$ respectively. Since type d needs to be aligned to 4 bytes, e occupies addresses $0 \sim 11$, with a size of 12 bytes.

You need to process $n$ operations, each being one of the following four types:

  1. Define a structure type. Specifically, given a positive integer $k$ and strings $s, t_1, n_1, \dots, t_k, n_k$, where $k$ represents the number of members in the type, $s$ represents the type name, $t_1, t_2, \dots, t_k$ represent the type of each member in order, and $n_1, n_2, \dots, n_k$ represent the name of each member in order. You need to output the size and alignment requirement of this structure type, separated by a space.
  2. Define an element. Specifically, given strings $t$ and $n$ representing the type and name of the element, respectively. All defined elements will be arranged in order starting from memory address 0, and must satisfy the address alignment rules. You need to output the starting address of the newly defined element.
  3. Access an element. Specifically, given a string $s$ representing the accessed element. Similar to C++ and other languages, the . operator is used to access members of a structure type. For example, a.b.c means a is a defined element, it is a structure type, it has a member named b, which is also a structure type, and it has a member named c. You need to output the starting address of the innermost accessed element.
  4. Access a memory address. Specifically, given a non-negative integer $addr$ representing the accessed address, you need to determine whether a basic type element occupies this address. If so, output the element in the same format as the access element in operation 3; otherwise, output ERR.

Input

Read data from the file struct.in. The first line contains a positive integer $n$, representing the number of operations. The following lines describe each operation, with the first integer $op$ on each line representing the operation type: If $op = 1$, first input a string $s$ and a positive integer $k$, representing the type name and the number of members. Then, for each of the next $k$ lines, input two strings $t_i$ and $n_i$, representing the type and name of each member in order. If $op = 2$, input two strings $t$ and $n$, representing the type and name of the element. If $op = 3$, input a string $s$, representing the accessed element. If $op = 4$, input a non-negative integer $addr$, representing the accessed address.

Output

Output to the file struct.out. Output $n$ lines, representing the results of each operation in order, as described in the problem statement.

Examples

Input 1

5
1 a 2
short aa
int ab
1 b 2
long bb
2 b x
3 x.ba.ab
4 10

Output 1

8 4
16 8
0
4
x.bb

Note

In the structure type a, the int member aa occupies addresses $0 \sim 3$, and the short member ab occupies addresses $4 \sim 5$. Since its alignment requirement is 4 bytes, its size is 8 bytes. Similarly, the size of structure type b can be calculated as 16 bytes, with an alignment requirement of 8 bytes.

Example 2

See struct/struct2.in and struct/struct2.ans in the contestant directory.

Note 2

In the second operation 4, the accessed memory address happens to be in a "hole" left for address alignment, so no basic type element occupies it.

Example 3

See struct/struct3.in and struct/struct3.ans in the contestant directory.

Constraints

For all data, $1 \le n \le 100$, $1 \le k \le 100$, $0 \le addr \le 10^{18}$. All defined structure type names, member names, and element names consist of at most 10 lowercase letters and are not byte, short, int, or long (i.e., they do not share names with basic types). All defined structure type names and element names are unique, and member names within the same structure are unique. However, different structures may have the same member names, and a member name within a structure may be the same as a defined structure or element name. It is guaranteed that all operations comply with the specifications and requirements described in the problem, i.e., structure definitions will not contain non-existent types, and there will be no access to non-existent elements or members. It is guaranteed that the size of any structure and the highest memory address occupied by defined elements do not exceed $10^{18}$.

Formal Definitions

The formal definition for the alignment requirement and size of a structure type is as follows: Suppose the structure has $k$ members with sizes $s_1, \dots, s_k$ and alignment requirements $a_1, \dots, a_k$. The alignment requirement of the structure is $a = \max\{a_1, \dots, a_k\}$. Let the address offsets for these members be $o_1, \dots, o_k$: $o_1 = 0$; For $i = 2, \dots, k$, $o_i$ is the smallest value such that $o_{i-1} + s_{i-1} \le o_i$ and $a_i$ divides $o_i$. The size $s$ of the structure is the smallest value such that $o_k + s_k \le s$ and $a$ divides $s$.

The formal definition for memory layout when defining an element is as follows: Suppose the $i$-th defined element has size $s_i$, alignment requirement $a_i$, and starting address $b_i$. $b_1 = 0$; for $i \ge 2$, $b_i$ is the smallest value such that $b_{i-1} + s_{i-1} \le b_i$ and $a_i$ divides $b_i$.

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.