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;
}