ADABASET - Ada and Database

Ada the Ladybug has inserted all data from her TODO-list into the database. The database is represented as multidimensional structure with direct indexing. Your task is simple - read the database and tell Ada the data on given indices!

Input

There will be multiple test-cases.

The first line of each test-case contains n integers 1≤ Di ≤ 105 , indicating the size of each dimension. n will be between 2 and 10. Their product won't exceed 106

Each of following D1×D2×...×Dn-1 lines contains Dn integers (where Dn is size of last dimension), all between 0 and 1018.

Last line of each test-case contains 0 to 5 queries. Each query consists of n integers A1, ... ,An, where 1 ≤ Ai ≤ Di (note that array is one-indexed).

Input file will have at most 2*106 integers. There won't be any other lines than those described above.

NOTE: If you are not confident about what number is on index [A1][A2]...[An], imagine input as D1 blocks of D2 blocks of D3 blocks of ... of Dn integers.

Output

For each query, output integer on indices [A1][A2]...[An] on a single line. After each test-case, output line with as many hashes ('#'), as the number of test-case (beginning with 1)

Example Input

2 2
1 2
3 4
1 1 1 2 2 1 2 2
2 2 2
10 9
8 7
6 5
2 1
2 2 2
1 2 6
1 2 3 4 5 6
6 5 4 3 2 1
1 1 1 1 2 1

Example Output

1
2
3
4
#
1
##
1
6
###

Added by:Morass
Date:2017-02-16
Time limit:4s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All

hide comments
2021-04-13 15:41:37
Not very well set Morass' problem. While it's not that easy to think in n dimensions, the statement can be understood with the help of sample cases, but the input format is quite nasty. Without the number of cases/queries, I normally rely on a try/except loop in Python (no EOF signal there), but here it would never actually throw an exception and instead kept waiting for more inputs. Eventually, I've managed to AC using this:

while 1:
____dimensions = [int(x) for x in stdin.readline().split()]
____if len(dimensions) == 0:
________break

I also can't see what 1-indexing adds to this.

Edit: The line of queries can contain 0 queries, are you even serious here? Got AC in CPP after wasting way too much time on this; had to assume there are no extra whitespaces anywhere and if there is 0 queries, their line consists of a single '\n'.

Worst case of input formatting in SPOJ history, perhaps except HXH. Had to pollute an elegant solution with a bunch of bullshit flags and checks to deal with this garbage. Downvoting, what a disappointment considering psetter's excellent reputation.

Last edit: 2021-04-14 14:54:20
2021-04-13 13:07:55
Question is not clear
2019-11-19 14:14:45
I can't understand the problem after reading it for a time of 1 hour. Can anyone help me understanding the problem. @Morass
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.