51nod 1422(强行YY)

题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注

沙拉酱非常喜欢数字序列。这正是他要弄一个关于构造序列的算法的原因。

沙拉酱拿了一张白纸。然后他开始用m个步骤来制作一个序列。每一步他要么向这个序列的末尾添加一个数字,要么拿这个序列的开头l个数字,然后在末尾添加c次。对于第二种操作,一般的,如果当前序列是 a1,a2,...,an ,那么经过操作之后序列将变成 a1,a2,...,an[,a1,a2,...,al]  (方括号里面的内容会重复c次)。

一天过去了,沙拉酱也完成了他的序列。现在他想知道某个位置是什么数字。

Input
单组测试数据。
第一行包含一个整数m (1 ≤ m ≤ 10^5),表示构造序列的步骤数目。
接下来m行包含每一个步骤的信息。第一个数字是类型(1或2)。类型1表示在序列后面加一个数字,这种情况下后面会跟一个整数xi (1 ≤ xi ≤ 10^5),表示被加在后面的数字。类型2表示复制一段长度为 li 前缀然后接到后面 ci 次,这种情况下后面会跟两个整数 li, ci(1 ≤ li ≤ 10^5, 1 ≤ ci ≤ 10^4),li 是前缀的长度,ci是复制的次数。输入中保证li不会大于当前序列的长度。

接下来一行包含一个整数n (1 ≤ n ≤ 10^5),表示查询的数量。接下来一行中包含n个正整数,每一个整数表示要查询的位置。题目保证这些数字大小不会超过序列的长度。序列的下标从1开始。
Output
对于每一个查询,输出对应查询位置的数字。两个查询之间用空格分开。具体格式看样例。
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

思路:

维护一个数组。由于这里的m是10^5,所以可以通过这个来处理。维护一个数组,这个数组每个都是一个node值。

struct node
{
    int flag;
    ll l;
    ll r;
    ll st;
    ll num;
};

flag表示是当个点还是循环。l,r表示当前数组第i为保存的是哪一个范围内的信息。st表示如果为循环的时候,前st个。num表示循环的次数。

这时候m次输入的信息都可以用这个来维护。

然后处理每次查询,对于第i次查询,可以先二分存信息的数组,找到哪个位置时l<=x && x <=r(x表示当前询问的位置)。然后当前需要查询的位置x就会更新为一个更加前面

的位置。这里其实就是递归处理。 终止条件,就是找到flag == 0,也就是说找到了这个点。 我觉得这个时间复杂度可以降到log(yy的)。跑出来的时间还是不错的。

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll unsigned long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = 100010;
struct node
{
    int flag;
    ll l;
    ll r;
    ll st;
    ll num;
};
struct qnode
{
    int id;
    ll x;
    ll ans;
}q[MAXN];
node mp[MAXN];
int n,m;
bool cmp1(qnode fa,qnode fb)
{
    return fa.x < fb.x;
}
bool cmp2(qnode fa,qnode fb)
{
    return fa.id < fb.id;
}
int Search(ll x)
{
    int l,r,tm;
    l = 1,r = m;
    while(l <= r){
        tm = (l + r) >> 1;
        if(mp[tm].l <= x && mp[tm].r >= x){
            return tm;
        }
        else if(mp[tm].l > x){
            r = tm - 1;
        }
        else {
            l = tm + 1;
        }
    }
}
ll getans(int x)
{
    int p = Search(x);
    if(mp[p].r == x && mp[p].flag == 0){
        return mp[p].st;
    }
    ll tp = (x - mp[p].l + 1) % mp[p].st;
    if(tp == 0)tp = mp[p].st;
    return getans(tp);
}
void solve()
{
    ll p = 1;
    sort(q+1,q+n+1,cmp1);
    int i = 1;
    while(1){
        if(i > n)break;
        while(q[i].x > mp[p].r){
            p ++;
        }
        if(mp[p].r == q[i].x && mp[p].flag == 0){
            q[i].ans = mp[p].st;
        }
        else {
            int tp = (q[i].x - mp[p].l + 1) % mp[p].st;
            if(tp == 0)tp = mp[p].st;
            q[i].ans = getans(tp);
        }
        i ++;
    }
    sort(q+1,q+n+1,cmp2);
    for(int i = 1; i <= n; i++){
        if(i == 1)cout<<q[i].ans;
        else cout<<' '<<q[i].ans;
    }
    printf("
");
}
int main()
{
    while(cin >>m){
        node tp;
        ll x,y,z;
        ll cnt;
        cnt = 0;
        for(ll i = 1; i <= m; i++){
            cin >>z;
            if(z == 1){
                cin >>y;
                tp.flag = 0;
                tp.l = cnt + 1;
                tp.r = cnt + 1;
                tp.st = y;
                tp.num = 0;
                cnt ++;
                mp[i] = tp;
            }
            else {
                cin >>x >>y;
                tp.flag = 1;
                tp.l = cnt + 1;
                tp.r = 1LL * x * y + cnt;
                tp.st = x;
                tp.num = y;
                cnt += 1LL * x * y;
                mp[i] = tp;
            }
        }
        cin >>n;
        for(int i = 1; i <= n; i++){
            cin >>q[i].x;
            q[i].id = i;
        }
        solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sweat123/p/5504890.html