Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
Sereja takes a blank piece of paper. Then he starts writing out the sequence in 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 times).
A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
The first line contains integer m (1 ≤ m ≤ 105) — the number of stages to build a sequence.
Next 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 is never larger than the current length of the sequence.
The next line contains integer 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 from the beginning to the end of the sequence.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the 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
1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4
题目链接:http://codeforces.com/contest/380/problem/A
题目的意思是说给出m次操作,分别有1:在序列最后加上x;2:复制1~x,y次,添加在序列尾部。然后给出n次查询,问说在第k[i]个位置上的数是多少。
因为x不会大于10^5,就是说复制的部分最多就10^5,那么久可以在一开始将前10^5个数记录下来,后面只需要遍历n的同时移动m即可
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std;
const int N=100005;
struct node{
int s;
LL x,y;
}xin[N];
int n,m,num[N],ans[N],c,cnt;
LL k[N];
void handle(){
c=0;
for(int i=0;i<m;i++){
if(xin[i].s==1){
num[c++]=xin[i].x;
if(c>=100000)
return;
}
else{
for(LL t=0;t<xin[i].y;t++){
for (LL j=0;j<xin[i].x;j++){
num[c++]=num[j];
if(c>=100000)
return;
}
}
}
}
}
void solve(){
int i=0,j=0;
LL p=0;
while(i<n){
while(j<m){
if(xin[j].s==1){
p++;
j++;
if(p==k[i]){
ans[i]=xin[j-1].x;
i++;
if(i>=n)
return;
break;
}
}
else{
LL q=p;
p+=xin[j].x*xin[j].y;
if(p>=k[i]){
while(i<n){
if(k[i]<=p){
ans[i]=num[(k[i]-q-1)%xin[j].x%100000];
i++;
if(i>=n)
return ;
}
else{
j++;
break;
}
}
}
else
j++;
}
}
}
}
int main(){
scanf("%d",&m);
for(int i=0;i<m;i++){
scanf("%d",&xin[i].s);
if(xin[i].s==1)
cin>>xin[i].x;
else
cin>>xin[i].x>>xin[i].y;
}
handle();
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>k[i];
solve();
printf("%d",ans[0]);
for(int i=1;i<n;i++)
printf(" %d",ans[i]);
printf("\n");
return 0;
}