#include <bits/stdc++.h>
using namespace std;
int const maxn = 1000010;
int const smaxn = 2005;//smaxn = sqrt(maxn)
char ch[maxn];
int sum[maxn];
intn,m,num;//大块的个数
struct ListBlock{//块状数组的每一个小块
int size;//每个小块的大小
char data[smaxn];
void push_back(char c){
size ++;
data[size] = c;
}
void insert(int pos,char c){
for(int i = size + 1; i > pos; i --)
data[i] = data[i - 1];
data[pos] = c;
size ++;
}
char getData(int pos){
return data[pos];
}
};
ListBlock block[smaxn];
void maintain(){
//此处size是直接调用了ListBlock中、
//变量size不是调用了系统的函数
for(int i = 1; i <= num; i ++)
sum[i] = sum[i - 1] + block[i].size;
}
void init(){
//n+m整个块状数组最大的大小
//n初始状态块状数组的大小
num = sqrt((n + m)*1.0) + 1;
for(int i = 0; i < n; i ++)
block[i/num + 1].push_back(ch[i]);
maintain();
}
char query(int pos,int number){
return block[number].getData(pos);
}
void insert(int pos,int number,char c){
block[number].insert(pos,c);
}
char myQuery(int pos){
int p = lower_bound(sum + 1,sum + 1+ num,pos) - sum;
return query(pos - sum[p - 1],p );
}
void myInsert(char c,int pos){
int p = lower_bound(sum + 1,sum + 1+ num,pos) - sum;
insert(pos - sum[p - 1] ,p,c);
maintain();
}
int main(){
scanf("%s",ch);
n = strlen(ch);
scanf("%d",&m);
init();
char s[3];
char c[3];
int p;
for(int i = 0 ; i < m; i ++){
scanf("%s",s);
if(s[0] == 'Q'){
scanf("%d",&p);
printf("%c\n",myQuery(p));
}
else{
scanf("%s%d",c,&p);
myInsert(c[0], p );
}
}
return 0;
}
POJ2887块状数组/块状链表
Sample Input Sample Output
ab a
7 d
Q 1 e
I c 2
I d 4
I e 2
Q 5
I f 1
Q 3