• codeforces 380A Sereja and Prefixes (递归)


    题目:

    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' 的数值是否是虚的。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <algorithm>
     6 #include <cmath>
     7 #include <vector>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 using namespace std;
    12 typedef long long LL;
    13 #define ms(a, b) memset(a, b, sizeof(a))
    14 #define pb push_back
    15 #define mp make_pair
    16 const int INF = 0x7fffffff;
    17 const int inf = 0x3f3f3f3f;
    18 const int mod = 1e9+7;
    19 const int maxn = 100000+10;
    20 struct node
    21 {
    22     int op, l, c;
    23     int x;
    24     LL len;
    25     node(){
    26         x = l = c = 0;
    27         len = 0;
    28     }
    29     friend bool operator <(node x1, node x2){
    30         return x1.len < x2.len;
    31     }
    32 };
    33 node t[maxn];
    34 LL len, m;
    35 void init(){
    36     len = 1LL;
    37 }
    38 int ans(LL pos)
    39 {
    40     node now; now.len = pos;
    41     LL wei = lower_bound(t, t+m, now) - t;
    42     if(t[wei].len == pos && t[wei].op == 1)  return t[wei].x;
    43     else{
    44         LL temp = t[wei].len;
    45         temp -= t[wei].c*t[wei].l;
    46         pos -= temp;
    47         pos %= t[wei].l;
    48         if(pos==0)  pos = t[wei].l;
    49         return ans(pos);
    50     }
    51 }
    52 void solve() {
    53     LL n, op, a, b, pos;
    54     cin >> m;
    55     for(LL i = 0;i<m;i++){
    56         cin >> op >> a;
    57         if(op==1){
    58             t[i].op = 1;
    59             t[i].x = a;
    60             t[i].len = len;
    61             len++;
    62         }
    63         else{
    64             cin >> b;
    65             t[i].op = 2;
    66             t[i].l = a;
    67             t[i].c = b;
    68             len+=a*b;
    69             t[i].len = len-1;
    70         }
    71     }
    72     cin >> n;
    73     for(LL i = 0;i<n;i++){
    74         cin >> pos;
    75         cout << ans(pos) << " ";
    76     }
    77     cout << endl;
    78 }
    79 int main() {
    80 #ifdef LOCAL
    81     freopen("input.txt", "r", stdin);
    82 //        freopen("output.txt", "w", stdout);
    83 #endif
    84     ios::sync_with_stdio(0);
    85     cin.tie(0);
    86     init();
    87     solve();
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    vue 的模板编译—ast(抽象语法树) 详解与实现
    Vue 组件(component)之 精美的日历
    nvm 装 nodejs 重启终端失效的解决方法
    vue 2 仿IOS 滚轮选择器 从入门到精通 (一)
    np.stack() 与 tf.stack() 的简单理解
    PHP 之 Ci框架下隐藏index.php
    Boosting 简单介绍
    Adaboost算法流程及示例
    Python 之 解码汉字乱码(如果gbk、utf8都试过不行,可以试试这个)
    Linux 之 tar和nc传文件
  • 原文地址:https://www.cnblogs.com/denghaiquan/p/7202828.html
Copyright © 2020-2023  润新知