LOJ#6282. 数列分块入门 6

内存限制:256 MiB时间限制:500 ms标准输入输出
题目类型:传统评测方式:文本比较
上传者: hzwer

题目描述

给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及单点插入,单点询问,数据随机生成。

输入格式

第一行输入一个数字 nnn。

第二行输入 nnn 个数字,第 i 个数字为 aia_iai​​,以空格隔开。

接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔开。

若 opt=0\mathrm{opt} = 0opt=0,表示在第 lll 个数字前插入数字 rrr (ccc 忽略)。

若 opt=1\mathrm{opt} = 1opt=1,表示询问 ara_rar​​ 的值(lll 和 ccc 忽略)。

输出格式

对于每次询问,输出一行一个数字表示答案。

样例

样例输入

4
1 2 2 3
0 1 3 1
1 1 4 4
0 1 2 2
1 1 2 4

样例输出

2
3

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤100000,−231≤others 1 \leq n \leq 100000, -2^{31} \leq \mathrm{others}1n100000,231​​others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans231​​1。

 

用vector维护块状链表

数据是随机的,所以不用重构

只不过速度倒数第一

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int MAXN=1e5+10;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
vector<int>v[MAXN];
int a[MAXN],belong[MAXN],block;
int main()
{
    int N=read();block=sqrt(N);
    for(int i=1;i<=N;i++) a[i]=read(),belong[i]=(i-1)/block+1;    
    for(int i=1;i<=N;i++) v[belong[i]].push_back(a[i]);
    for(int i=1;i<=N;i++)
    {
        int opt=read(),l=read(),r=read(),c=read();
        if(opt==0)
        {
            for(int i=1;i<=belong[N];i++)
            {
                if(l<=v[i].size()) {v[i].insert(v[i].begin()+l-1,r);break;}
                else l-=v[i].size();
            }
        }
        else
        {
            for(int i=1;i<=belong[N];i++)
            {
                if(r<=v[i].size()) {printf("%d\n",v[i][r-1]);break;}    
                else r-=v[i].size();
            }
        }
    }
    return 0;
}

 

 

 

posted @ 2018-02-13 10:58  自为风月马前卒  阅读(338)  评论(0编辑  收藏  举报

Contact with me