Cerinta completa

  • Declare a 2-dimensional array, , with empty arrays, all zero-indexed.
  • Declare an integer, , and initialize it to 0.

You need to process two types of queries:

  1. Query:

    • Compute .
    • Append the integer to .
  2. Query:

    • Compute .
    • Set .
    • Store the new value of in an answers array.

Notes:
is the bitwise XOR operation, which corresponds to the ^ operator in most languages. Learn more about it on Wikipedia.
is the modulo operator.
– Finally, is the number of elements in .

Function Description

Complete the function with the following parameters:
: the number of empty arrays to initialize in
: 2-D array of integers

Returns

  • : the results of each type 2 query in the order they are presented

Input Format

The first line contains two space-separated integers, , the size of to create, and , the number of queries, respectively.
Each of the subsequent lines contains a query string, .

Constraints

  • It is guaranteed that query type will never query an empty array or index.

Sample Input

STDIN    Function
-----    --------
2 5      size of arr[] n = 2, size of queries[] q = 5
1 0 5    queries = [[1,0,5],[1,1,7],[1,0,3],[2,1,0],[2,1,1]]
1 1 7
1 0 3
2 1 0
2 1 1

Sample Output

7
3

Explanation

Initial Values:


= [ ]
= [ ]

Query 0: Append to .

= [5]
= [ ]

Query 1: Append to .
= [5]
= [7]

Query 2: Append to .

= [5, 3]
= [7]

Query 3: Assign the value at index of to . Store in your answer array.

= [5, 3]
= [7]

Query 4: Assign the value at index of to . Store in your answer array.

= [5, 3]
= [7]

Return your answer array [7, 3]. The code stub prints its elements on separate lines.


Limbajul de programare folosit: cpp14

Cod:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int N = 0, Q = 0;
    int lastans = 0;
    int type, x, y;
    int seq = 0;
    int pos;
    
    cin >> N >> Q;
    
    vector < vector <int>> a(N);
    
    for (int i = 0; i < Q; i ++) {
        cin >> type >> x >> y;
        seq = ((x ^ lastans) % N);
       // cout << seq << endl;
        if (type == 1) {
            a[seq].push_back(y);
           // cout << (seq) << " <-- " << y << endl;
        }
        else if (type == 2) {
            pos = (y % a[seq].size());
            //cout << "pos " << pos << endl;
            lastans = a[seq][pos];
            cout << lastans << endl;
        }
        
    }
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464651313

Link challenge: https://www.hackerrank.com/challenges/dynamic-array/problem

Dynamic Array