POJ 3580 SuperMemo

POJ 3580 SuperMemo

裸Splay区间操作: 内存池+区间加减+区间翻转+插入+删除+维护最值

SuperMemo
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8552   Accepted: 2801
Case Time Limit: 2000MS

Description

Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1,A2, … An}. Then the host performs a series of operations and queries on the sequence which consists:

  1. ADD x y D: Add D to each number in sub-sequence {Ax … Ay}. For example, performing “ADD 2 4 1″ on {1, 2, 3, 4, 5} results in {1, 3, 4, 5, 5}
  2. REVERSE x y: reverse the sub-sequence {Ax … Ay}. For example, performing “REVERSE 2 4″ on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
  3. REVOLVE x y T: rotate sub-sequence {Ax … AyT times. For example, performing “REVOLVE 2 4 2″ on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
  4. INSERT x P: insert P after Ax. For example, performing “INSERT 2 4″ on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
  5. DELETE x: delete Ax. For example, performing “DELETE 2″ on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
  6. MIN x y: query the participant what is the minimum number in sub-sequence {Ax … Ay}. For example, the correct answer to “MIN 2 4″ on {1, 2, 3, 4, 5} is 2

To make the show more interesting, the participant is granted a chance to turn to someone else that means when Jackson feels difficult in answering a query he may call you for help. You task is to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson whenever he calls.

Input

The first line contains (≤ 100000).

The following n lines describe the sequence.

Then follows M (≤ 100000), the numbers of operations and queries.

The following M lines describe the operations and queries.

Output

For each “MIN” query, output the correct answer.

Sample Input

5
1 
2 
3 
4 
5
2
ADD 2 4 1
MIN 4 5

Sample Output

5

Source

[Submit]   [Go Back]   [Status]   [Discuss]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=220000;
const int INF=0x3f3f3f3f;
#define Key_Value ch[ch[root][1]][0]

int ch[maxn][2],rev[maxn],add[maxn],sz[maxn],pre[maxn],key[maxn],minn[maxn];
int root,tot1;
int s[maxn],tot2;

int n,q,a[maxn];

void NewNode(int& x,int father,int k)
{
    if(tot2) x=s[tot2--];
    else x=++tot1;

    ch[x][0]=ch[x][1]=rev[x]=add[x]=0;
    sz[x]=1; pre[x]=father; key[x]=minn[x]=k;
}

void Erase(int r)
{
    if(r)
    {
        s[++tot2]=r;
        Erase(ch[r][0]);
        Erase(ch[r][1]);
    }
}

void Upd_Rev(int x)
{
    if(!x) return ;
    swap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}

void Upd_Add(int x,int d)
{
    if(!x) return ;
    key[x]+=d; minn[x]+=d;
    add[x]+=d;
}

void Push_Up(int x)
{
    sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+1;
    minn[x]=key[x];
    if(ch[x][0]) minn[x]=min(minn[x],minn[ch[x][0]]);
    if(ch[x][1]) minn[x]=min(minn[x],minn[ch[x][1]]);
}

void Push_Down(int x)
{
    if(rev[x])
    {
        Upd_Rev(ch[x][0]); Upd_Rev(ch[x][1]);
        rev[x]=0;
    }
    if(add[x])
    {
        Upd_Add(ch[x][0],add[x]); Upd_Add(ch[x][1],add[x]);
        add[x]=0;
    }
}

void Build(int& x,int l,int r,int fa)
{
    if(l>r) return ;
    int mid=(l+r)/2;
    NewNode(x,fa,a[mid]);
    Build(ch[x][0],l,mid-1,x);
    Build(ch[x][1],mid+1,r,x);
    Push_Up(x);
}

void Init()
{
    root=tot1=tot2=0;
    ch[root][0]=ch[root][1]=pre[root]=sz[root]=0;
    minn[root]=key[root]=INF;
    NewNode(root,0,INF);
    NewNode(ch[root][1],root,INF);
    Build(Key_Value,1,n,ch[root][1]);
    Push_Up(ch[root][1]);
    Push_Up(root);
}

void Rotate(int x,int kind)
{
    int y=pre[x];
    Push_Down(y);
    Push_Down(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    pre[y]=x;
    ch[x][kind]=y;
    Push_Up(y);
}

void Splay(int r,int goal)
{
    Push_Down(r);
    while(pre[r]!=goal)
    {
        if(pre[pre[r]]==goal)
        {
            Push_Down(pre[r]);
            Push_Down(r);
            Rotate(r,ch[pre[r]][0]==r);
        }
        else
        {
            Push_Down(pre[pre[r]]);
            Push_Down(pre[r]);
            Push_Down(r);
            int y=pre[r];
            int kind=(ch[pre[y]][0]==y);
            if(ch[y][kind]==r) Rotate(r,!kind);
            else Rotate(y,kind);
            Rotate(r,kind);
        }
    }
    Push_Up(r);
    if(goal==0) root=r;
}

int Get_Kth(int r,int k)
{
    Push_Down(r);
    int t=sz[ch[r][0]]+1;
    if(k==t) return r;
    if(t<k) return Get_Kth(ch[r][1],k-t);
    else return Get_Kth(ch[r][0],k);
}

int Get_Min(int r)
{
    Push_Down(r);
    while(ch[r][0])
    {
        r=ch[r][0];
        Push_Down(r);
    }
    return r;
}

int Get_Max(int r)
{
    Push_Down(r);
    while(ch[r][1])
    {
        r=ch[r][1];
        Push_Down(r);
    }
    return r;
}

///.......do..it........

void ADD(int l,int r,int d)
{
    Splay(Get_Kth(root,l),0);
    Splay(Get_Kth(root,r+2),root);
    Upd_Add(Key_Value,d);
    Push_Up(ch[root][1]);
    Push_Up(root);
}

void REVERSE(int l,int r)
{
    Splay(Get_Kth(root,l),0);
    Splay(Get_Kth(root,r+2),root);
    Upd_Rev(Key_Value);
    Push_Up(ch[root][1]);
    Push_Up(root);
}

void DELETE(int p)
{
    Splay(Get_Kth(root,p),0);
    Splay(Get_Kth(root,p+2),root);
    Erase(Key_Value);
    pre[Key_Value]=0;
    Key_Value=0;
    Push_Up(ch[root][1]);
    Push_Up(root);
}

void INSERT(int p,int v)
{
    Splay(Get_Kth(root,p+1),0);
    Splay(Get_Kth(root,p+2),root);
    NewNode(Key_Value,ch[root][1],v);
    Push_Up(ch[root][1]);
    Push_Up(root);
}

int MIN(int l,int r)
{
    Splay(Get_Kth(root,l),0);
    Splay(Get_Kth(root,r+2),root);
    return minn[Key_Value];
}

void REVOLVE(int l,int r,int t)
{
    int len=r-l+1;
    t=(t%len+len)%len;
    if(t==0) return ;
    int c=r-t+1;
    ///[l..c-1] and [c..r]
    Splay(Get_Kth(root,c),0);
    Splay(Get_Kth(root,r+2),root);
    int temp=Key_Value;
    Key_Value=0;
    Push_Up(ch[root][1]);
    Push_Up(root);

    Splay(Get_Kth(root,l),0);
    Splay(Get_Kth(root,l+1),root);
    Key_Value=temp;
    pre[Key_Value]=ch[root][1];
    Push_Up(ch[root][1]);
    Push_Up(root);
}
/****************Debug*********************/

void showit(int x)
{
    if(x)
    {
        Push_Down(x);
        showit(ch[x][0]);
        printf("结点: %2d key: %2d 左儿子: %2d 右儿子: %2d 父结点: %2d sz: %2d min: %2d \n",
               x,key[x],ch[x][0],ch[x][1],pre[x],sz[x],minn[x]);
        showit(ch[x][1]);
    }
}

void Debug()
{
    cout<<"---------------------------\n";
    cout<<"root: "<<root<<endl;
    showit(root);
}


int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++) scanf("%d",a+i);
        Init();
       // Debug();
        scanf("%d",&q);
        char op[100]; int x,y,z;
        while(q--)
        {
            scanf("%s",op);
            if(strcmp(op,"ADD")==0)
            {
                scanf("%d%d%d",&x,&y,&z);
                ADD(x,y,z);
            }
            else if(strcmp(op,"REVERSE")==0)
            {
                scanf("%d%d",&x,&y);
                REVERSE(x,y);
            }
            else if(strcmp(op,"REVOLVE")==0)
            {
                 scanf("%d%d%d",&x,&y,&z);
                 REVOLVE(x,y,z);
            }
            else if(strcmp(op,"INSERT")==0)
            {
                scanf("%d%d",&x,&y);
                INSERT(x,y);
            }
            else if(strcmp(op,"DELETE")==0)
            {
                scanf("%d",&x);
                DELETE(x);
            }
            else if(strcmp(op,"MIN")==0)
            {
                scanf("%d%d",&x,&y);
                printf("%d\n",MIN(x,y));
            }
           // Debug();
        }
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119239.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DropDownList绑定数据库「建议收藏」

    DropDownList绑定数据库「建议收藏」//获取文本this.DropDownList1.SelectedItem.Text;本类内使用protectedvoidPage_Load(objectsender,EventArgse)   {       this.DropDownList1.Items.Clear();       this.DropDownList1.DataSource=DbHelperSQL.Query(“select*fromauthors”).Tables[0];       this.Drop

    2022年10月8日
    3
  • tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞

    tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞文章目录第一天IDOR漏洞是什么?通常出现的地方查询get请求post的表单的值cookies挑战初探挑战的问题第一天货物系统出现了问题,让我们想办法进行修复!IDOR漏洞是什么?InsecureDirectObjectReference,不安全的直接对象引用,是一种权限控制类漏洞,类似于越权漏洞吧,就是用户访问到了自己不应该访问的信息,比如我只能查看我自己的资料,但我可以通过修改一些参数访问其他人的资料。通常出现的地方查询get请求post的表单的值这里用户的id被隐藏了,如果修

    2022年6月11日
    36
  • python数字转字符串固定位数_python-将String转换为64位整数映射字符以自定…「建议收藏」

    python数字转字符串固定位数_python-将String转换为64位整数映射字符以自定…「建议收藏」您将4个不同“数字”的字符串解释为数字,因此以4为基数.如果您有一串实际数字,范围为0-3,则可以让int()真正快速地生成一个整数.defseq_to_int(seq,_m=str.maketrans(‘ACGT’,’0123′)):returnint(seq.translate(_m),4)上面的函数使用str.translate()用匹配的数字替换4个字符中的每个字符(我使用静态s…

    2022年9月25日
    2
  • 用pycharm打包py程序_怎么打包python文件

    用pycharm打包py程序_怎么打包python文件使用Pycharm和Anancoda打包应用程序平时我们在运行Python项目时都需要在Pycharm中运行,因为里面或者Anancoda中已经导入了相应的包,库,配好了对应的环境。一般运行时不是太方便,特别是在给用户使用时,这就显得很麻烦了。所以我们需要将其单独打包出来并且能够运行。本文我以Pycharm和Anancoda打包我里面的一个计算器应用程序。开始吧!1.进入自己创建的环境安装pyinstaller首先进入命令行,win+r后输入cmd进入命令行。之后输入命令进入到自己创建的环境中,我

    2022年8月29日
    3
  • python3实现网络字节序和ipv4、ipv6互转[通俗易懂]

    文章目录1.前言2.什么是是网络字节序3.ipv4和ipv6简介4.转换5.参考文献1.前言2.什么是是网络字节序3.ipv4和ipv6简介4.转换5.参考文献[1][2]

    2022年4月11日
    326
  • 自我学习总结之——NFV

    自我学习总结之——NFVNFV–DFC1.什么是NFV?网络功能虚拟化NFV(NetworkFunctionsVirtualization)在NFV出现之前设备的专业化很突出,具体设备都有其专门的功能实现,而之后设备的控制平面与具体设备进行分离,不同设备的控制平面基于虚拟机,虚拟机基于云操作系统,这样当企业需要部署新业务时只需要在开放的虚拟机平台上创建相应的虚机,然后在虚拟机上安装相应功能的软件包即可。这种方式…

    2025年10月9日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号