Mapmaker

Interview

Time limit1sMemory limit128 MB

Summary
Given array declarations with bounds and element sizes, compute each reference's physical address with the row-major address formula.
Level

Medium4 of 10

Topics
Array, Math, Implementation, Simulation
Solved
No attempts yet

Problem

The Cybersoft Computer Company (a leader in programming languages) has hired you to work on a new programming language named A--. Your job is the array-mapping part of the language: given an array reference such as x[5, 6], you must map it to an actual physical address. To do this, write a program that reads several array declarations and references and prints the physical address of each reference. Every physical address must be printed as a base-10 integer.

The physical address of an array reference A[i1,i2,…,iD]A[i_1, i_2, \ldots, i_D] is given by

C0+C1i1+C2i2+⋯+CDiD,C_0 + C_1 i_1 + C_2 i_2 + \cdots + C_D i_D,

where the constants C0,…,CDC_0, \ldots, C_D are defined as follows:

  • BB = base address of the array
  • DD = number of dimensions of the array
  • LdL_d = lower bound of dimension dd
  • UdU_d = upper bound of dimension dd
  • CDC_D = size of one array element, in bytes
  • Cd=Cd+1 (Ud+1−Ld+1+1)C_d = C_{d+1}\,(U_{d+1} - L_{d+1} + 1) for 1≤d<D1 \le d < D
  • C0=B−C1L1−C2L2−⋯−CDLDC_0 = B - C_1 L_1 - C_2 L_2 - \cdots - C_D L_D

Input

The first line contains two positive integers NN and RR: NN is the number of arrays that are declared, and RR is the number of array references whose addresses must be computed. The next NN lines each declare one array, and the following RR lines each contain one array reference.

Each array-declaration line contains, in order: the array name (at most 10 characters), a positive integer giving the base address of the array, a positive integer giving the size in bytes of each array element, and DD, the number of dimensions (1≤D≤101 \le D \le 10). These are followed, on the same line, by DD pairs of integers giving the lower and upper bounds of dimensions 1,…,D1, \ldots, D, respectively.

Each array-reference line contains the array name followed by the integer indices i1,i2,…,iDi_1, i_2, \ldots, i_D, where DD is the number of dimensions of that array.

Output

For each reference, print one line containing the reference together with its physical address, formatted exactly as follows:

  1. Print the array name.
  2. Print a left square bracket [.
  3. Print the index values, separating each consecutive pair of indices by a comma and a single space.
  4. Print a right square bracket, a space, an equals sign, and another space (] = ).
  5. Print the physical address.

Examples2

  1. Example 1

    Input
    3 4
    ONE    1500 2 2 0 3 1 5
    TWO    2000 4 3 1 4 0 5 5 10
    THREE  3000 1 1 1 9
    ONE    2 4
    THREE  7
    TWO    2 0 6
    TWO    3 3 9
    
    Expected output
    ONE[2, 4] = 1526
    THREE[7] = 3006
    TWO[2, 0, 6] = 2148
    TWO[3, 3, 9] = 2376
    
  2. Example 2

    Input
    1 1
    A 100 4 1 0 9
    A 5
    
    Expected output
    A[5] = 120