We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You can do it with one dimensional array. Just need to keep track of each seq length and value in the array. Please refer my solution below:
int N,Q;
cin>>N>>Q; //read no of seq and no of queries
int seq[500002]; //this will store all sequences, seq[0] to seq[N-1] will store the length of each sequence, the values of each seq will be stored seq[N] onwards
int csum[N]={0}; //it will store the cummulative sum of lengths of each seq till seq N i.e csum[0] stores first seq length, csum[1] stores 0th + 1st seq length and so on
int k,inp,curr=N; //curr denotes the point from where we need to fill the current seq values in the array
for(int i=0;i<N;i++){
cin>>k;
seq[i]=k; //store length of current seq
for(int j=0;j<k;j++){
cin>>inp;
seq[curr+j] = inp; //store values of current seq
}
if(i==0) csum[i] = k;
else csum[i] = csum[i-1]+k;
curr +=k; //adjust curr value for next seq
}
for(int x=0;x<Q;x++){
int a,b;
cin>>a>>b;
cout<<seq[N+b+csum[a]-seq[a]]<<endl; //output the value of the element b from sequence a
}
Variable Sized Arrays
You are viewing a single comment's thread. Return to all comments →
You can do it with one dimensional array. Just need to keep track of each seq length and value in the array. Please refer my solution below: