C. Sereja and Prefixes
time limit per test
memory limit per test
input
output
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current sequence and adds them c times to the end. More formally, if we represent the current sequence as a1, a2, ..., an, then after we apply the described operation, the sequence transforms into a1, a2, ..., an[, a1, a2, ..., al] (the block in the square brackets must be repeated c
A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
Input
m (1 ≤ m ≤ 105)
m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer xi (1 ≤ xi ≤ 105) — the number to add. Type 2 means copying a prefix of length li to the end ci times, in this case the line further contains two integers li, ci(1 ≤ li ≤ 105, 1 ≤ ci ≤ 104), li is the length of the prefix, ci is the number of copyings. It is guaranteed that the length of prefix li
n (1 ≤ n ≤ 105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1
%lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Output
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
Examples
input
6 1 1 1 2 2 2 1 1 3 2 5 2 1 4 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
output
1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
struct Node {
int t, l, c;
ll k;
}node[maxn];
int d[maxn];
int main() {
//freopen("in.txt", "r", stdin);
int n, cnt = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d%d", &node[i].t, &node[i].l);
if(node[i].t == 2){
scanf("%d", &node[i].c);
for(int j = 0; j < node[i].c && cnt < 100001; j++) {
for(int h = 1; h <= node[i].l && cnt < 100001; h++) {
d[cnt++] = d[h];
}
}
node[i].k = (ll)node[i].l * node[i].c;
}
else {
if(cnt < 100001)
d[cnt++] = node[i].l;
node[i].k = 1;
}
node[i].k += node[i-1].k;
}
int m, first = 0;
ll a;
scanf("%d", &m);
while(m--) {
scanf("%I64d", &a);
int l = 1, r = n+1;
while(l < r) {
int mid = (l + r) >> 1;
if(node[mid].k >= a)
r = mid;
else
l = mid + 1;
}
if(first)
putchar(' ');
first = 1;
if(node[l].t == 1)
printf("%d", node[l].l);
else {
ll s = a - node[l-1].k;
s %= node[l].l;
if(s == 0)
s = node[l].l;
printf("%d", d[s]);
}
}
puts("");
return 0;
}