zoukankan      html  css  js  c++  java
  • BZOJ1507: [NOI2003]Editor

    1507: [NOI2003]Editor

    Time Limit: 5 Sec  Memory Limit: 162 MB

    Description

    Input

    输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作。其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它们(如果难以理解这句话,可以参考样例)。 除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 这里我们有如下假定:  MOVE操作不超过50000个,INSERT和DELETE操作的总个数不超过4000,PREV和NEXT操作的总个数不超过200000。  所有INSERT插入的字符数之和不超过2M(1M=1024*1024),正确的输出文件长度不超过3M字节。  DELETE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作必然不会试图把光标移动到非法位置。  输入文件没有错误。 对C++选手的提示:经测试,最大的测试数据使用fstream进行输入有可能会比使用stdio慢约1秒。

    Output

    输出文件editor.out的每行依次对应输入文件中每条GET指令的输出。

    Sample Input

    15
    Insert 26
    abcdefghijklmnop
    qrstuv wxy
    Move 15
    Delete 11
    Move 5
    Insert 1
    ^
    Next
    Insert 1
    _
    Next
    Next
    Insert 4
    ./.
    Get 4
    Prev
    Insert 1
    ^
    Move 0
    Get 22

    Sample Output

    ./.
    abcde^_^f./.ghijklmno
    模板题。花样过。
    Splay:
      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<cstdlib>
      7 #include<vector>
      8 using namespace std;
      9 typedef long long ll;
     10 typedef long double ld;
     11 typedef pair<int,int> pr;
     12 const double pi=acos(-1);
     13 #define rep(i,a,n) for(int i=a;i<=n;i++)
     14 #define per(i,n,a) for(int i=n;i>=a;i--)
     15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
     16 #define clr(a) memset(a,0,sizeof(a))
     17 #define pb push_back
     18 #define mp make_pair
     19 #define fi first
     20 #define sc second
     21 #define pq priority_queue
     22 #define pqb priority_queue <int, vector<int>, less<int> >
     23 #define pqs priority_queue <int, vector<int>, greater<int> >
     24 #define vec vector
     25 ld eps=1e-9;
     26 ll pp=1000000007;
     27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
     28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
     29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
     30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
     31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
     32 ll read(){ ll ans=0; char last=' ',ch=getchar();
     33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
     34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
     35 if(last=='-')ans=-ans; return ans;
     36 }
     37 #define N 2100000
     38 char ch[10],str[N],cht[N];
     39 int k=1,size[N],c[N][3],fa[N],root,num;
     40 void Treaval(int x) {    
     41     if(x) {    
     42         Treaval(c[x][0]);    
     43         printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d cht = %c
    ",x,c[x][0],c[x][1],fa[x],size[x],cht[x]);    
     44         Treaval(c[x][1]);    
     45     }    
     46 }    
     47 void debug() {printf("%d
    ",root);Treaval(root);}    
     48 void update(int x){
     49     size[x]=size[c[x][0]]+size[c[x][1]]+1;
     50 }
     51 void rotate(int x,int &k){
     52     int y=fa[x],z=fa[y],l,r;
     53     if (c[y][0]==x) l=0;else l=1; r=l^1;
     54     if (y==k) k=x;
     55     else { if (c[z][0]==y) c[z][0]=x; else c[z][1]=x; }
     56     fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
     57     c[y][l]=c[x][r]; c[x][r]=y;
     58     update(y); update(x);
     59 }
     60 void splay(int x,int &k){
     61     while (x!=k){
     62         int y=fa[x],z=fa[y];
     63         if (y!=k){
     64             if (c[y][0]==x^c[z][0]==y) rotate(x,k);
     65             else rotate(y,k);
     66         }
     67         rotate(x,k);
     68     }
     69 }
     70 int find(int x,int k){
     71     int t=size[c[k][0]]+1;
     72     if (t==x) return k;
     73     if (t>x) return find(x,c[k][0]);
     74     if (t<x) return find(x-t,c[k][1]);
     75 }
     76 void build(int l,int r,int f,int n){
     77     if (l>r) return;
     78     int mid=(l+r)>>1;
     79     if (l==r) size[mid]=1;
     80     else build(l,mid-1,mid,n),build(mid+1,r,mid,n);
     81     fa[mid]=f; update(mid); cht[mid]=str[mid-num]; if (r-l+1==n) c[f][0]=mid; else c[f][mid>f]=mid;
     82 }
     83 void out(int k){
     84     if (k==0) return;
     85     out(c[k][0]);
     86     printf("%c",cht[k]);
     87     out(c[k][1]);
     88 }
     89 void Ins(){
     90     int n=read();
     91     for (int i=1;i<=n;i++){
     92         char ch_=getchar();
     93         while (ch_<32 || ch_>126) ch_=getchar();
     94         str[i]=ch_;
     95     }
     96     int x=find(k,root),y=find(k+1,root); 
     97     splay(x,root); splay(y,c[x][1]);
     98     build(num+1,num+n,y,n); num+=n;
     99     update(y); update(x);
    100 }
    101 void Del(){
    102     int n=read();
    103     int x=find(k,root),y=find(k+n+1,root);
    104     splay(x,root); splay(y,c[x][1]);
    105     fa[c[y][0]]=0; c[y][0]=0;
    106 }
    107 void Get(){
    108     int n=read();
    109     int x=find(k,root),y=find(k+n+1,root);
    110     splay(x,root); splay(y,c[x][1]);
    111     out(c[y][0]); puts("");
    112 }
    113 int main()
    114 {
    115     int q=read();
    116     root=1; c[1][1]=2; fa[2]=1; 
    117     size[1]=2; size[2]=1; cht[1]=0; cht[2]=0; num=2;
    118     while (q--){
    119         scanf("%s",ch);
    120         if (ch[0]=='M') scanf("%d",&k),k++;
    121         if (ch[0]=='I') Ins();
    122         if (ch[0]=='D') Del();
    123         if (ch[0]=='G') Get();
    124         if (ch[0]=='P') k--;
    125         if (ch[0]=='N') k++;
    126     }
    127     return 0;
    128  } 
    View Code

    分块:

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<cstdlib>
      7 #include<vector>
      8 using namespace std;
      9 typedef long long ll;
     10 typedef long double ld;
     11 typedef pair<int,int> pr;
     12 const double pi=acos(-1);
     13 #define rep(i,a,n) for(int i=a;i<=n;i++)
     14 #define per(i,n,a) for(int i=n;i>=a;i--)
     15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
     16 #define clr(a) memset(a,0,sizeof(a))
     17 #define pb push_back
     18 #define mp make_pair
     19 #define fi first
     20 #define sc second
     21 #define pq priority_queue
     22 #define pqb priority_queue <int, vector<int>, less<int> >
     23 #define pqs priority_queue <int, vector<int>, greater<int> >
     24 #define vec vector
     25 ld eps=1e-9;
     26 ll pp=1000000007;
     27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
     28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
     29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
     30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
     31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
     32 ll read(){ ll ans=0; char last=' ',ch=getchar();
     33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
     34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
     35 if(last=='-')ans=-ans; return ans;
     36 }
     37 #include<queue>
     38 const int N=1<<25; 
     39 const int blo=20000;   
     40 const int num=N/blo*3;
     41 char ch[10];
     42 int m,k;      
     43 char str[N];      
     44 queue<int> q;   
     45 struct node       
     46 {      
     47     char s[blo];    
     48     int nu,next;  
     49 }a[num];    
     50 void find(int &pos,int &now)
     51 {    
     52     for (now=0;a[now].next!=-1&&pos>a[now].nu;now=a[now].next)  pos-=a[now].nu;
     53 }
     54 void devide(int x,int p){
     55     if (a[x].nu==p) return ;
     56     int t=q.front(); q.pop(); a[t].next=a[x].next; a[t].nu=a[x].nu-p;
     57     memcpy(a[t].s,a[x].s+p,a[t].nu);
     58     a[x].next=t; a[x].nu=p;
     59 }
     60 void maintain(int pos)
     61 {    
     62     int t;    
     63     for (;pos!=-1;pos=a[pos].next){
     64       for(t=a[pos].next;t!=-1&&a[pos].nu+a[t].nu<blo;t=a[t].next)    
     65       {    
     66         memcpy(a[pos].s+a[pos].nu,a[t].s,a[t].nu);
     67         a[pos].nu+=a[t].nu; a[pos].next=a[t].next; q.push(t);
     68       }     
     69     }    
     70 } 
     71 void Ins(){
     72     int m=read(),nu=-1,t=m; str[0]=0;
     73     while (t){
     74         char c=getchar();
     75         if (c>=32 && c<=126) { str[++nu]=c; t--;}
     76     }
     77     int now,p=k,i; find(p,now); devide(now,p);
     78     for (i=0;i+blo<=m;i+=blo){
     79         int t=q.front(); q.pop(); a[t].nu=blo; a[t].next=a[now].next;
     80         memcpy(a[t].s,str+i,blo); a[now].next=t; now=t;
     81     }
     82     if (i<m){
     83         int t=q.front(); q.pop(); a[t].nu=m-i; a[t].next=a[now].next;
     84         memcpy(a[t].s,str+i,m-i); a[now].next=t; now=t;
     85     }
     86     maintain(now);
     87 }
     88 void Del(){
     89     int m=read(),i;
     90     int now,p=k; find(p,now); devide(now,p);
     91     for (i=a[now].next;m>a[i].nu;i=a[i].next) m-=a[i].nu;
     92     devide(i,m); i=a[i].next;
     93     for (int t=a[now].next;t!=i;t=a[t].next) q.push(t);
     94     a[now].next=i;
     95     maintain(now);
     96 }
     97 void Get(){
     98     int m=read(),now,p=k,t; find(p,now);
     99     int i=min(m,a[now].nu-p);
    100     memcpy(str,a[now].s+p,i);
    101     for (t=a[now].next;i+a[t].nu<=m;i+=a[t].nu,t=a[t].next)    
    102         memcpy(str+i,a[t].s,a[t].nu);        
    103     if (i<m&&t!=-1)  memcpy(str+i,a[t].s,m-i);    
    104     str[m]=0; puts(str);
    105 }
    106 int main(){
    107     for (int i=1;i<=num;i++) q.push(i);
    108     a[0].nu=0; a[0].next=-1;
    109     int t=read();
    110     while (t--){
    111         scanf("%s",ch);
    112         if (ch[0]=='M') scanf("%d",&k);
    113         if (ch[0]=='I') Ins();
    114         if (ch[0]=='D') Del();
    115         if (ch[0]=='G') Get();
    116         if (ch[0]=='P') k--;
    117         if (ch[0]=='N') k++;
    118     }
    119     return 0;
    120 } 
    121 
    View Code

    rope大法好:

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<cstdlib>
     7 #include<vector>
     8 using namespace std;
     9 typedef long long ll;
    10 typedef long double ld;
    11 typedef pair<int,int> pr;
    12 const double pi=acos(-1);
    13 #define rep(i,a,n) for(int i=a;i<=n;i++)
    14 #define per(i,n,a) for(int i=n;i>=a;i--)
    15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
    16 #define clr(a) memset(a,0,sizeof(a))
    17 #define pb push_back
    18 #define mp make_pair
    19 #define fi first
    20 #define sc second
    21 #define pq priority_queue
    22 #define pqb priority_queue <int, vector<int>, less<int> >
    23 #define pqs priority_queue <int, vector<int>, greater<int> >
    24 #define vec vector
    25 ld eps=1e-9;
    26 ll pp=1000000007;
    27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
    28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
    29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
    30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
    31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
    32 ll read(){ ll ans=0; char last=' ',ch=getchar();
    33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
    34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    35 if(last=='-')ans=-ans; return ans;
    36 }
    37 using namespace __gnu_cxx;  
    38 #define MAXLEN 1224*1024  
    39 #include<ext/rope>  
    40 crope s;  
    41 char tmp[MAXLEN];  
    42 char order[20];  
    43 int now = 0;  
    44 int n;  
    45    
    46 int main()  
    47 {  
    48     scanf("%d", &n);  
    49     for (int i = 1; i <= n ; i++)  
    50     {  
    51         scanf("%s", order);  
    52         int len;  
    53         switch (order[0])  
    54         {  
    55             case 'I':  
    56                 scanf("%d", &len);  
    57                 for (int j = 0; j < len; j++)  
    58                     while ((tmp[j] = getchar()) == '
    ');  
    59                 tmp[len] = '';  
    60                 s.insert(now, tmp);  
    61                 break;  
    62             case 'M':  
    63                 scanf("%d", &now);  
    64                 break;  
    65             case 'D':  
    66                 scanf("%d", &len);  
    67                 s.erase(now, len);  
    68                 break;  
    69             case 'G':  
    70                 scanf("%d", &len);  
    71                 for (int j = now; j <= now + len - 1; j++)  
    72                     putchar(s[j]);  
    73                 putchar('
    ');  
    74                 break;  
    75             case 'P':  
    76                 now--;  
    77                 break;  
    78             case 'N':  
    79                 now++;  
    80                 break;  
    81         }  
    82     }  
    83 }  
    84 
    View Code
  • 相关阅读:
    深入Java集合学习系列:ConcurrentHashSet简单实现
    深入Java集合学习系列:TreeSet 详解
    深入Java集合学习系列:TreeMap实现
    深入Java集合学习系列:ConcurrentHashMap之实现细节
    深入Java集合学习系列:WeakHashMap的实现原理
    深入Java集合学习系列:Hashtable的实现原理
    深入Java集合学习系列:LinkedList的实现原理
    深入Java集合学习系列:ArrayList的实现原理
    深入Java集合学习系列:LinkedHashSet的实现原理
    深入Java集合学习系列:LinkedHashMap的实现原理
  • 原文地址:https://www.cnblogs.com/SXia/p/6963925.html
Copyright © 2011-2022 走看看