poj 2887 Big String(块状链表)

题目链接:poj 2887 Big String

题意:

让你设计一个数据结构,支持将一个字符插在p位置,询问第p个字符。

题解:

块状链表搞搞就行。

  1 #include<cstdio>
  2 #include<queue>
  3 #include<cmath>
  4 #include<cstring>
  5 #define F(i,a,b) for(int i=(a);i<=(b);++i)
  6 using namespace std;
  7 
  8 const int N=1100000,SQR=1510;
  9 struct Node{
 10     int a[1600],sz,pre,nxt;
 11     void clear(){sz=pre=nxt=0;}
 12     inline void pb(int x){a[++sz]=x;}
 13     inline void ins(int pos,int x){
 14         for(int i=++sz;i>pos;i--)a[i]=a[i-1];
 15         a[pos]=x;
 16     }
 17     void del(int pos){sz--;F(i,pos,sz)a[i]=a[i+1];}
 18 }T[1600];
 19 struct Block{
 20     int head,tail,sz;
 21     queue<int>Q;
 22     void clear(){
 23         head=tail=sz=0;
 24         while(!Q.empty())Q.pop();
 25         F(i,1,SQR)Q.push(i);
 26     }
 27     int nw(){int t=Q.front();Q.pop(),T[t].clear();return t;}
 28     inline int find(int &x){
 29         int i=head;
 30         while(i&&x>T[i].sz)x-=T[i].sz,i=T[i].nxt;
 31         return i==0?(x+=T[tail].sz),tail:i;
 32     }
 33     int& operator[](int x){
 34         int now=find(x);
 35         if(x>T[now].sz)x=0;
 36         return T[now].a[x];
 37     }
 38     void split(int x)
 39     {
 40         int New=nw();
 41         T[New].pre=x,T[New].nxt=T[x].nxt;
 42         T[T[x].nxt].pre=New,T[x].nxt=New;
 43         F(i,SQR/2,T[x].sz)T[New].pb(T[x].a[i]);
 44         T[x].sz=SQR/2-1;
 45         if(x==tail)tail=New;
 46     }
 47     void merge(int x)
 48     {
 49         int nxt=T[x].nxt;
 50         if(nxt&&T[x].sz+T[nxt].sz<SQR)
 51         {
 52             F(i,1,T[nxt].sz)T[x].pb(T[nxt].a[i]);
 53             T[x].nxt=T[nxt].nxt,T[T[nxt].nxt].pre=x;
 54             Q.push(nxt);
 55             if(nxt==tail)tail=x;
 56         }
 57     }
 58     inline void check(int x){
 59         if(T[x].sz>SQR)split(x);
 60         if(T[x].sz<SQR/2)merge(x);
 61     }
 62     inline void pb(int x)
 63     {
 64         if(!tail)head=tail=nw();
 65         if(T[tail].sz>=SQR)
 66         {
 67             int t=nw();T[tail].nxt=t;
 68             T[t].pre=tail,tail=t;
 69         }
 70         T[tail].pb(x),sz++;
 71     }
 72     void ins(int pos,int x,int now=0)
 73     {
 74         sz++;if(pos>sz)pos=sz;
 75         if(!head){head=tail=nw(),T[head].pb(x);return;}
 76         now=find(pos),T[now].ins(pos,x),check(now);
 77     }
 78     void delet(int pos)
 79     {
 80         if(pos>sz)return;
 81         int now=find(pos);
 82         T[now].del(pos),check(now),sz--;
 83     }
 84 }B;
 85 
 86 char s[N];
 87 int n;
 88 int main(){
 89     while(~scanf("%s",s))
 90     {
 91         B.clear();
 92         for(int i=0;s[i];i++)B.pb(s[i]);
 93         scanf("%d",&n);
 94         F(i,1,n)
 95         {
 96             char op[2],str[2];int pos;
 97             scanf("%s",op);
 98             if(*op=='I')
 99             {
100                 scanf("%s%d",str,&pos);
101                 B.ins(pos,*str);
102             }else
103             {
104                 scanf("%d",&pos);
105                 printf("%c\n",B[pos]);
106             }
107         }
108     }
109     return 0;
110 }
View Code

 

posted @ 2017-08-13 20:43  bin_gege  阅读(133)  评论(0编辑  收藏  举报