题目:

A. 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), liis 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.

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

题意:

  有两种操作:1)在数组后面加上一个x

        2)选在数组前 l 个数 复制 c 次, 放在数组后

  样例:

    1 1  -> [1]

    1 2  -> [1, 2]

    2 2 1 -> [1, 2, {1, 2}]

    1 3  -> [1, 2, {1, 2}, 3]

    2 5 2 ->[1, 2, {1, 2}, 3, {1, 2, 1, 2, 3}, {1, 2, 1, 2, 3}]

    1 4 -> [1, 2, {1, 2}, 3, {1, 2, 1, 2, 3}, {1, 2, 1, 2, 3}, 4]。

题解:

  当我们询问pos的数值的时候,有2种情况:(1)这个位置是通过 1 插入的(实)的数值, (2)这个位置是复制的, 没有实的数值。

  当是实的数值的时候,直接输出。下面就讨论虚的数值。

  询问的位置 pos 。由于是在复制的区域。那个这个指令就有个最后的一个复制的位置temp。

  例如在样例中的 2 5 2 中, temp = 15。

  而这个复制区域的前一个位置就是 temp - l * c (15 - 5 * 2 = 5), 我们需要找的就是pos 对应在前缀的位置 , pos -  (temp - l * c)。

  比如说我们询问第6个位置,那么它对应的位置就是 6 - (15 - 2 * 5) = 1。

  但是我们如果是询问第11 个位置,11 - (15 - 2 * 5 ) = 6。 这时我们就需要 mod 复制的长度 l , 6 % 5 = 1。 所以11位置就对应的是第1号位置。

  就产生一个新的pos' ,查看这个新的pos' 的数值是否是虚的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
struct node
{
int op, l, c;
int x;
LL len;
node(){
x = l = c = ;
len = ;
}
friend bool operator <(node x1, node x2){
return x1.len < x2.len;
}
};
node t[maxn];
LL len, m;
void init(){
len = 1LL;
}
int ans(LL pos)
{
node now; now.len = pos;
LL wei = lower_bound(t, t+m, now) - t;
if(t[wei].len == pos && t[wei].op == ) return t[wei].x;
else{
LL temp = t[wei].len;
temp -= t[wei].c*t[wei].l;
pos -= temp;
pos %= t[wei].l;
if(pos==) pos = t[wei].l;
return ans(pos);
}
}
void solve() {
LL n, op, a, b, pos;
cin >> m;
for(LL i = ;i<m;i++){
cin >> op >> a;
if(op==){
t[i].op = ;
t[i].x = a;
t[i].len = len;
len++;
}
else{
cin >> b;
t[i].op = ;
t[i].l = a;
t[i].c = b;
len+=a*b;
t[i].len = len-;
}
}
cin >> n;
for(LL i = ;i<n;i++){
cin >> pos;
cout << ans(pos) << " ";
}
cout << endl;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();
cin.tie();
init();
solve();
return ;
}

codeforces 380A Sereja and Prefixes (递归)的更多相关文章

  1. Codeforces 380A - Sereja and Prefixes

    原题地址:http://codeforces.com/problemset/problem/380/A 让期末考试整的好久没有写题, 放假之后由于生病也没怎么做,新年的第一场CF也不是那么在状态,只过 ...

  2. Codeforce 380A Sereja and Prefixes【二分】

    题意:定义两种操作 1 a ---- 向序列中插如一个元素a 2 a b ---- 将序列的前a个元素[e1,e2,...,ea]重复b次插入到序列中 经过一列操作后,为处于某个位置p的元素是多少.数 ...

  3. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  4. codeforces 314E Sereja and Squares

    discription Sereja painted n points on the plane, point number i (1 ≤ i ≤ n) has coordinates (i, 0). ...

  5. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  6. codeforces 425C Sereja and Two Sequences(DP)

    题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...

  7. codeforces B. Sereja and Stairs 解题报告

    题目链接:http://codeforces.com/problemset/problem/381/B 题目意思:给定一个m个数的序列,需要从中组合出符合楼梯定义 a1 < a2 < .. ...

  8. codeforces A. Sereja and Bottles 解题报告

    题目链接:http://codeforces.com/problemset/problem/315/A 题目意思:有n个soda bottles,随后给出这n个soda bottles的信息.已知第 ...

  9. codeforces C. Sereja and Swaps

    http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...

随机推荐

  1. 怎么用 pytorch 查看 GPU 信息

    如果你用的 Keras 或者 TensorFlow, 请移步 怎么查看keras 或者 tensorflow 正在使用的GPU In [1]: import torch In [2]: torch.c ...

  2. JavaScript 开发的 睡眠状况自测(SRSS)

    Javascript 开发睡眠状况自测程序,手记!2019.11.13日... <script>//初始化fbox = new Findpair('fbox','output');fbox ...

  3. python字典-基础

    一.解释 像列表一样,“字典”是许多值的集合.但不像列表的下标,字典的索引可以 使用许多不同数据类型,不只是整数.字典的索引被称为“键”,键及其关联的值 称为“键-值”对. 二.列表创建方式 1. I ...

  4. 什么是python??

    python 是一门非常简单易学好用,同时功能强大的编程语言,具有丰富和强大的库,开发效率特别高.它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起. pyth ...

  5. 关于 Python 程序的运行方面,有什么手段能提升性能?

    1.使用多进程,充分利用机器的多核性能2.对于性能影响较大的部分代码,可以使用 C 或 C++编写3.对于 IO 阻塞造成的性能影响,可以使用 IO 多路复用来解决4.尽量使用 Python 的内建函 ...

  6. P4514 上帝造题的七分钟(二维树状数组)

    P4514 上帝造题的七分钟 二维树状数组 差分维护区间加法,区间求和 #include<cstdio> int read(){ ,f=; ') f=f&&(c!='-') ...

  7. RMAN备份与恢复 —— 参数文件还原

         在RMAN用语中,“还原”与“恢复”具有不同的含义,还原(restore)是指访问先前生成的备份集,从中得到一个或多个对象,然后再磁盘上的某个位置还原这些对象.还原与恢复时分离的.恢复(re ...

  8. 省流量 转:http://www.wtoutiao.com/p/T3b8it.html

    前言:“客户端上传时间戳”的玩法,你玩过么?一起聊聊时间戳的奇技淫巧! 缘起:无线时代,流量敏感.APP在登录后,往往要向服务器同步非常多的数据,很费流量,技术上有没有节省流量的方法呢?这是本文要讨论 ...

  9. [转载]企业级应用架构(NHibernater+Spring.Net+MVC3)

    本人已经从事公司两套这类架构系统的开发工作啦!对于这套架构,我惊叹不已!BPS和CMS系统都是采用这套架构.但本人也同时渐渐发现了这套架构有诸多 不足之处,于是本人利用闲暇时光进一步改进了这套架构.新 ...

  10. openstack stein部署手册 9. neutron

    # 安装程序包 yum -y install openstack-neutron-linuxbridge ebtables ipset # 变更配置文件 mv /etc/neutron/neutron ...