1. 程式人生 > >LOJ#6282. 數列分塊入門 6

LOJ#6282. 數列分塊入門 6

div max pro 數據 tdi 輸出格式 https 鏈表 單點

內存限制:256 MiB時間限制:500 ms標準輸入輸出 題目類型:傳統評測方式:文本比較 上傳者: hzwer 提交提交記錄統計討論測試數據

題目描述

給出一個長為 nnn 的數列,以及 nnn 個操作,操作涉及單點插入,單點詢問,數據隨機生成。

輸入格式

第一行輸入一個數字 nnn。

第二行輸入 nnn 個數字,第 i 個數字為 aia_ia?i??,以空格隔開。

接下來輸入 nnn 行詢問,每行輸入四個數字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔開。

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

opt=1\mathrm{opt} = 1opt=1,表示詢問 ara_ra?r?? 的值(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,2?31??others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans2?31??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; }

LOJ#6282. 數列分塊入門 6