POJ 3580 SuperMemo (Sply经典操作:成段更新、区间最小值、反转、插入和删除、区间搬移)

题目链接

题目大意

分析

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define bit(x) (1<<(x))
using namespace std;
typedef long long LL;
const double pi=4*atan(1.0);
const int INF=0x3f3f3f3f;
const double eps=1e-6;
const int MAXN=100010;
const int MAXM=2*MAXN;
int a[MAXN],n;
struct SplayTree
{
    #define Key_value (nt[nt[root][1]][0])
    int nt[MAXN][2],pre[MAXN],val[MAXN],Size[MAXN],add[MAXN],rev[MAXN],minv[MAXN];
    int root,tot1;
    int s[MAXN],tot2;///内存池 内存池容量 有删除操作要这样写
    void NewNode(int &r,int fa,int k)
    {
        if(tot2)r=s[tot2--];
        else r=++tot1;
        nt[r][0]=nt[r][1]=0;
        pre[r]=fa;
        Size[r]=1;
        add[r]=rev[r]=0;
        val[r]=minv[r]=k;
    }
    void Update_Rev(int r)///添加反转标记
    {
        if(!r)return;
        swap(nt[r][0],nt[r][1]);
        rev[r]^=1;
    }
    void Update_Add(int r,int ADD)///添加增量标记
    {
        if(!r)return;
        add[r]+=ADD;
        val[r]+=ADD;
        minv[r]+=ADD;
    }
    void PushUp(int r)
    {
        Size[r]=Size[nt[r][0]]+Size[nt[r][1]]+1;
        minv[r]=val[r];
        if(nt[r][0])minv[r]=min(minv[r],minv[nt[r][0]]);
        if(nt[r][1])minv[r]=min(minv[r],minv[nt[r][1]]);
    }
    void PushDown(int r)
    {
        if(rev[r])
        {
            Update_Rev(nt[r][0]);
            Update_Rev(nt[r][1]);
            rev[r]=0;
        }
        if(add[r])
        {
            Update_Add(nt[r][0],add[r]);
            Update_Add(nt[r][1],add[r]);
            add[r]=0;
        }
    }
    void Build(int &x,int l,int r,int fa)///对序列建平衡二叉树
    {
        if(l>r)return;
        int mid=(l+r)>>1;
        NewNode(x,fa,a[mid]);
        Build(nt[x][0],l,mid-1,x);
        Build(nt[x][1],mid+1,r,x);
        PushUp(x);
    }
    void Init()
    {
        root=tot1=tot2=0;
        nt[root][0]=nt[root][1]=Size[root]=add[root]=rev[root]=pre[root]=0;
        minv[root]=INF;///这个不用也可以,如果在PushUp那判断了的话,否则需要
        NewNode(root,0,INF);
        NewNode(nt[root][1],root,INF);
        Build(Key_value,1,n,nt[root][1]);
        PushUp(nt[root][1]);
        PushUp(root);
    }
    void Rotate(int x,int kind)///旋转
    {
        int y=pre[x];
        PushDown(y);
        PushDown(x);
        nt[y][!kind]=nt[x][kind];
        pre[nt[x][kind]]=y;
        if(pre[y])
            nt[pre[y]][nt[pre[y]][1]==y]=x;
        pre[x]=pre[y];
        nt[x][kind]=y;
        pre[y]=x;
        PushUp(y);
    }
    void Splay(int r,int goal) ///Splay调整
    {
        PushDown(r);
        while(pre[r]!=goal)
        {
            if(pre[pre[r]]==goal)
                Rotate(r,nt[pre[r]][0]==r);
            else
            {
                int y=pre[r];
                int kind=(nt[pre[y]][0]==y);
                if(nt[y][kind]==r)
                {
                    Rotate(r,!kind);
                    Rotate(r,kind);
                }
                else
                {
                    Rotate(y,kind);
                    Rotate(r,kind);
                }
            }
        }
        PushUp(r);
        if(goal==0)root=r;
    }
    int Get_Kth(int r,int k)///返回以r为根的子树中第k个结点编号
    {
        PushDown(r);
        int t=Size[nt[r][0]]+1;
        if(t==k)return r;
        if(t>k)return Get_Kth(nt[r][0],k);
        else return Get_Kth(nt[r][1],k-t);
    }
    int Get_Min(int r)///返回以r为根的子树的最左边的端点编号
    {
        PushDown(r);
        while(nt[r][0])
        {
            r=nt[r][0];
            PushDown(r);
        }
        return r;
    }
    int Get_Max(int r)///返回以r为根的子树的最右边的端点编号
    {
        PushDown(r);
        while(nt[r][1])
        {
            r=nt[r][1];
            PushDown(r);
        }
        return r;
    }
    ///下面是本题用到的操作
    void ADD(int l,int r,int v)///a[l]~a[r]+=v
    {
        Splay(Get_Kth(root,l),0);
        Splay(Get_Kth(root,r+2),root);
        Update_Add(Key_value,v);
        PushUp(nt[root][1]);
        PushUp(root);
    }
    void Reverse(int l,int r)///将区间反转
    {
        Splay(Get_Kth(root,l),0);
        Splay(Get_Kth(root,r+2),root);
        Update_Rev(Key_value);
        PushUp(nt[root][1]);
        PushUp(root);
    }
    void Revolve(int l,int r,int T)///a[l]~a[r]循环右移T位
    {
        int len=r-l+1;
        T%=len;
        if (T==0) return;
        int c=r-T;///将区间[l,c]与[c+1,r]调换位置 处理方法为将[c+1,r]这棵子树插入到l-1与l两结点中间
        Splay(Get_Kth(root,c+1),0);
        Splay(Get_Kth(root,r+2),root);///将[c+1,r]对应的子树旋转到关键位置
        int temp=Key_value;///保存这棵子树的根节点编号
        Key_value=0;///删除这棵子树
        PushUp(nt[root][1]);
        PushUp(root);

        Splay(Get_Kth(root,l),0);
        Splay(Get_Kth(root,l+1),root);
        Key_value=temp;
        pre[Key_value]=nt[root][1];///添加子树时这句不能忘记
        PushUp(nt[root][1]);
        PushUp(root);
    }
    void Insert(int x,int P)///在第x个数后面插入P
    {
        Splay(Get_Kth(root,x+1),0);
        Splay(Get_Kth(root,x+2),root);
        NewNode(Key_value,nt[root][1],P);
        PushUp(nt[root][1]);
        PushUp(root);
    }
    void Erase(int r)///回收内存
    {
        if(r)
        {
            s[++tot2]=r;
            Erase(nt[r][0]);
            Erase(nt[r][1]);
        }
    }
    void Delete(int x)///删除第x个数
    {
        Splay(Get_Kth(root,x),0);
        Splay(Get_Kth(root,x+2),root);
        Erase(Key_value);
        pre[Key_value]=0;
        Key_value=0;
        PushUp(nt[root][1]);
        PushUp(root);
    }
    int Query_Min(int l,int r)///查询区间最小值
    {
        Splay(Get_Kth(root,l),0);
        Splay(Get_Kth(root,r+2),root);
        return minv[Key_value];
    }
    /**以下为Debug部分 用于输出整个序列*/
    void middle_order(int r)///Splay的中序遍历即为维护的序列
    {
        if (r)
        {
            PushDown(r);///这句话不能少
            middle_order(nt[r][0]);
            printf("%d ",val[r]);
            middle_order(nt[r][1]);
        }
    }
    void Test()
    {
        Splay(Get_Min(root),0);
        Splay(Get_Max(root),root);
        middle_order(Key_value);///两个外加端点的中间即为维护序列
        printf("\n");
    }
}st;
int main()
{
    char op[20];
    int x,y,z,q;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        st.Init();
        //st.Test();
        scanf("%d",&q);
        while(q--)
        {
            scanf("%s",op);
            if(strcmp(op,"ADD")==0)
            {
                scanf("%d%d%d",&x,&y,&z);
                st.ADD(x,y,z);
                //st.Test();
            }
            else if(strcmp(op,"REVERSE")==0)
            {
                scanf("%d%d",&x,&y);
                st.Reverse(x,y);
                //st.Test();
            }
            else if(strcmp(op,"REVOLVE")==0)
            {
                scanf("%d%d%d",&x,&y,&z);
                st.Revolve(x,y,z);
                //st.Test();
            }
            else if(strcmp(op,"INSERT")==0)
            {
                scanf("%d%d",&x,&y);
                st.Insert(x,y);
                //st.Test();
            }
            else if(strcmp(op,"DELETE")==0)
            {
                scanf("%d",&x);
                st.Delete(x);
                //st.Test();
            }
            else
            {
                scanf("%d%d",&x,&y);
                printf("%d\n",st.Query_Min(x,y));
            }
        }
    }

    return 0;
}
/*
5
1
2
3
4
5
2
ADD 2 4 1
MIN 4 5
*/

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值