codeforces Round 223 div1 A Sereja and Prefixes

codeforces  Round 223  div1  A Sereja and Prefixes


这题只需注意到只会复制前10^5的数 ,就不难了 ,可是比赛时我写错了,硬是没找出来。。

只需存储前10^5个数即可 ,分成多个区间存储,平均每查询一个数基本上位O(1) , 整体复杂度为O(n).


我的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int maxn = 100050;

LL s[maxn+10] , begin_s[maxn] ,end_s[maxn] , l[maxn] , c[maxn] ,ss[maxn+10] , n , m , now;
//s存前10^5个数 , 依次为区间的开头位置, 结束位置的下一位置 , 该区间循环节长度 , 循环次数, 非复制的是添加到末尾的数。
int main()
{
    now = 0 ;
    LL cnt = 0 , type , x ,tl ,tc;
    cin>>n;
    while(n--){
        cin>>type;
        if(type == 1){
            cin>>x;
            if(now < maxn) s[now] = x;
            begin_s[cnt] = now , end_s[cnt] = now+1 , l[cnt] = -1 ,c[cnt] = -1 , ss[cnt] = x; 
                                     // -1 为特殊标记
            now++ , cnt++;
        }
        else{
            cin>>tl>>tc;
            begin_s[cnt] = now , end_s[cnt] = now + tl * tc , l[cnt] = tl ,c[cnt] = tc;
            LL p = now , i=0 , tn = tl * tc;
            for(i=0;i<tn && p<maxn ; p++ , i++) {
                s[p] = s[i%tl];  // 原写成: s[p] = s[i]  ,诶
            }
            now = end_s[cnt++];
        }
    }

    cin>>m;
    LL p1 = 0 , q = 0;
    while(m--){
        cin>>q ; q--;
        while(end_s[p1] <= q) p1++;
        if(q<maxn) cout<<s[q];
        else if(l[p1] == -1){
            cout<<ss[p1];
        }
        else{
            LL tmp = q - begin_s[p1] ;
            q = tmp % l[p1];
            cout<<s[q];
        }
        if(m) cout<<" ";
    }
    cout<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值