题意:在字符串中插入字符,问第i个字符是谁
题解:块状链表,不知为什么splay超时= =!
View Code
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1005000; 7 int bs,top; 8 struct Block 9 { 10 int size,next; 11 char s[3000]; 12 void push_back(char ch) 13 { 14 s[size++]=ch; 15 } 16 void insert(int pos,char ch) 17 { 18 for(int i=size++;i>pos;i--) 19 s[i]=s[i-1]; 20 s[pos]=ch; 21 } 22 }block[3000]; 23 void update(int x) 24 { 25 if(block[x].size<bs*2) 26 return; 27 ++top; 28 int i,j,k=block[x].size; 29 for(i=bs,j=0;i<k;i++,j++) 30 block[top].s[j]=block[x].s[i]; 31 block[top].size=j; 32 block[x].size=bs; 33 block[top].next=block[x].next; 34 block[x].next=top; 35 } 36 int main() 37 { 38 char s[maxn]; 39 gets(s); 40 int len=strlen(s),m; 41 scanf("%d",&m); 42 bs=sqrt((double)(len+m))+1; 43 top=0; 44 block[0].size=0; 45 for(int i=0;i<len;i++) 46 { 47 if(block[top].size==bs) 48 { 49 block[top].next=top+1; 50 block[++top].size=0; 51 } 52 block[top].push_back(s[i]); 53 } 54 block[top].next=-1; 55 while(m--) 56 { 57 char op,ch; 58 int pos,k; 59 scanf(" %c",&op); 60 if(op=='Q') 61 { 62 scanf("%d",&pos); 63 k=0; 64 while(pos>block[k].size) 65 pos-=block[k].size,k=block[k].next; 66 printf("%c\n",block[k].s[pos-1]); 67 } 68 else 69 { 70 scanf(" %c %d",&ch,&pos); 71 k=0; 72 while(block[k].next!=-1&&pos>block[k].size) 73 pos-=block[k].size,k=block[k].next; 74 block[k].insert(min(pos-1,block[k].size+1),ch); 75 update(k); 76 } 77 } 78 return 0; 79 }