【JZOJ4778】数列编辑器

Description

这里写图片描述

Solution

只要发现询问的k是在光标前,这题就没有问题了。

剩下的就是用双向链表维护,维护前缀和即可。

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define N 1000100
#define ll long long
using namespace std;
int a[N],nx[N],ls[N],num=0;
ll s[N],sm[N];
int b[N];
int main()
{
    freopen("editor.in","r",stdin);
    freopen("editor.out","w",stdout);
    int Q;
    cin>>Q;
    int c=0,p=0;
    nx[0]=N-99;
    ls[N-99]=0;
    ls[0]=-1;
    b[0]=0;
    sm[0]=-2147483647;
    while(Q--)
    {
        char ch[3];
        int x;
        scanf("%s",ch);
        if(ch[0]=='I')
        {
            scanf("%d",&x);
            a[++num]=x;
            ls[nx[p]]=num;
            nx[num]=nx[p];
            nx[p]=num;
            ls[num]=p;
            b[num]=b[ls[num]]+1;
            p=nx[p];
            int t=b[p];
            s[t]=s[t-1]+x;
            sm[t]=max(sm[t-1],s[t]);
        }
        else if(ch[0]=='D')
        {
            ls[nx[p]]=ls[p];
            nx[ls[p]]=nx[p];
            p=ls[p];
        }
        else if(ch[0]=='L')
        {
             if(ls[p]!=-1) p=ls[p];
        }
        else if(ch[0]=='R')
        {
            if(nx[p]!=N-99)
            {
                p=nx[p];
                b[p]=b[ls[p]]+1;
                int t=b[p];
                s[t]=s[t-1]+a[p];
                sm[t]=max(sm[t-1],s[t]);
            }
        }
        else
        {
            scanf("%d",&x);
            printf("%d\n",sm[x]);
        }
    }
}
posted @ 2016-11-21 19:00  sadstone  阅读(57)  评论(0编辑  收藏  举报