codeforces #233 (Div2) C. Sereja and Prefixes【二分+递归】

C. Sereja and Prefixes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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 %I64d specifier.

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
题意:输入n次操作,每次操作为1时,直接在数组后插入数值,操作为2时,在数组后插入数组前缀长度为L的数组元素,且重复插入time次,再输入m次询问,每次询问为第i(1<= i<= m)个位置的数组值,输出 m 次询问后的数组值。

思路:当输入为1时,直接输出插入值就OK,输入为2时,我们根据需要复制输出的前缀的长度在已实现的数组中找到起始位置,最后递归输出,因此,在读入时,需要存储当前操作的前缀在数组中的起始位置length以及当前操作的前缀的长度,在递归查找中,用二分法找到满足前缀起始位置小于询问位置x的pos,如果pos位置处的操作为2,通过用询问位置repos减去pos处的前缀起始位置,得到需要复制的前缀的起始位置,因为最终我们还是回到最开始的位置进行复制,所以需要mod前缀长度,如下

repos-vis[pos].len)%vis[pos].pre_len+1
最后递归输出即可。

总结:这道题虽然花了一个下午的时间去理解+AC,但是我觉得时间还是很值的,对递归有了更深入的理解,也学习到了小伙伴良好的解题思路,emm,他的代码很简洁,理解了以后简直让人神清气爽~~努力&&加油,以后我也想写这么漂亮的代码~~

#include<bits/stdc++.h>
using namespace std;
#define LL long long 
#define N 100000
#define inf 0x3f3f3f3f
struct node{
	LL flag;
	LL number;
	LL len;
	LL pre_len;
}vis[N+10];

int Solve(LL repos,LL n)//形参repos存储询问的位置值,n为操作次数 
{
	int l,r,pos,x;
	l = 0;
	r = n-1; 
	while(l <= r)
	{
		x = (l+r)/2;//二分 
		if(vis[x].len <= repos)//如果x位置处的前缀起始位置小于询问的位置 
		{
			pos = x;//如果有更优的前缀起始位置,更新pos值 
			l = x +1;//向右移动 
		}
		else 
			r = x-1;// 向左移动 
	}
	if(vis[pos].flag == 1)//直接插入在数组尾部时,输出插入值 
		return vis[pos].number ;
	else
		return Solve((repos-vis[pos].len)%vis[pos].pre_len+1,n);
		//递归查找需要复制的前缀的起始位置 
}

int main()
{
	LL n,m,a,b,c,pos,length;
	cin>>n;
	length = 1;//前缀在数组中的起始位置 
	for(int i = 0; i < n; i ++)
	{
		cin>>a>>b;
		if(a == 1)
		{
			vis[i].flag = 1;
			vis[i].number = b;
			vis[i].len = length;
			length += 1;
		}
		else
		{
			cin>>c;
			vis[i].flag = 2;
			vis[i].len = length;
			vis[i].pre_len = b;//存储第i次操作前缀的长度 
			length += b*c;
		}
	}
	cin>>m;
	for(int i = 0; i < m; i ++)
	{
		cin>>pos;//询问的位置 
		cout<<Solve(pos,n)<<' ';
	}
	cout<<endl;
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值