hdu 3487 Play with Chain —— splay区间翻转模板

博客围绕钻石链操作问题展开,包含CUT和FLIP两种操作。给出输入输出格式及示例,还提到利用Splay树解决问题,通过将特定节点放到根节点及右子节点,处理区间操作,最后用dfs输出结果。

Problem Description
YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.

FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8

He wants to know what the chain looks like after perform m operations. Could you help him?

Input
There will be multiple test cases in a test data.
For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b // Means a FLIP operation, 1 ≤ a < b ≤ n.
The input ends up with two negative numbers, which should not be processed as a case.

Output
For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.

Sample Input
8 2
CUT 3 5 4
FLIP 2 6
-1 -1

Sample Output
1 4 3 7 6 2 5 8

我们只需要把比最小的小1的放到根节点,比最大的大1的放到根节点的右子节点,这样根节点的右子节点的左子树就是这一段区间,然后z也是这样操作,就可以把这一段区间放到z后面,最后dfs输出

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define keytree ch[ch[root][1]][0]
#define L(x) ch[x][0]
#define R(x) ch[x][1]
#define N 300010
int ch[N][2],pre[N],cnt[N],size[N],val[N],small[N],rev[N],key[N];
int tot,root;
int a[N],n;
void newnode(int &u,int fa,int w,int KEY)
{
    u=++tot;
    ch[u][0]=ch[u][1]=rev[u]=0;
    pre[u]=fa;size[u]=1;
    val[u]=small[u]=w;
    key[u]=KEY;
}
void up(int u)
{
    size[u]=1+size[L(u)]+size[R(u)];
    small[u]=min(val[u],min(small[L(u)],small[R(u)]));
}
void down(int u)
{
    if(rev[u])
    {
        if(L(u))rev[L(u)]^=1;
        if(R(u))rev[R(u)]^=1;
        swap(L(u),R(u));
        rev[u]=0;
    }
}
void rotate(int u,int kind)//kind表示u在fa的哪一边
{
    int fa=pre[u];
    down(fa);down(u);
    ch[fa][kind]=ch[u][!kind];
    pre[ch[u][!kind]]=fa;
    if(pre[fa])ch[pre[fa]][ch[pre[fa]][1]==fa]=u;
    pre[u]=pre[fa];
    ch[u][!kind]=fa;
    pre[fa]=u;
    up(fa);up(u);
}
void splay(int u,int goal)
{
    int fa,kind;
    down(u);
    while(pre[u]!=goal)
    {
        if(pre[pre[u]]==goal)
        {
            down(pre[u]);down(u);
            rotate(u,R(pre[u])==u);
        }
        else
        {
            fa=pre[u];
            down(pre[u]);down(fa);down(u);
            kind=R(pre[fa])==fa;
            if(ch[fa][kind]!=u)//不在同一侧
            {
                rotate(u,!kind);
                rotate(u,kind);
            }
            else
            {
                rotate(fa,kind);
                rotate(u,kind);
            }
        }
    }
    up(u);
    if(goal==0)root=u;
}
int getkth(int u,int k)//第k个键值的点的编号
{
    down(u);
    int s=size[L(u)]+1;
    if(s==k) return u;
    if(s>k) return getkth(L(u),k);
    else return getkth(R(u),k-s);
}
int find(int u,int x)//查找键值为x的点的编号
{                    // 有反转标记时不可用
    down(u);
    if(key[u]==x)return u;
    if(key[u]>x)
    {
        if(!L(u))return -1;
        return find(L(u),x);
    }
    if(key[u]<x)
    {
        if(!R(u))return -1;
        return find(R(u),x);
    }
}
int getpre(int u)
{
    down(u);u=L(u);down(u);
    while(R(u))
    {
        u=R(u);
        down(u);
    }
    return u;
}
int getnext(int u)
{
    down(u);u=R(u);down(u);
    while(L(u))
    {
        u=L(u);
        down(u);
    }
    return u;
}
void del(int x)//删除编号为x的节点
{
    if(cnt[x]>1)
    {
        cnt[x]--;
        return ;
    }
    splay(x,0);
    if(L(root))
    {
        int p=getpre(x);
        splay(p,root);
        R(p)=R(root);
        pre[R(root)]=p;
        root=p;
        pre[p]=0;
        up(root);
    }
    else
    {
        root=R(root);
        pre[root]=0;
    }
}
void build(int &u,int l,int r,int fa)//按pos为键值
{                        //val为数的大小 a存数的大小
    if(l>r)return ;
    int mid=(l+r)>>1;
    newnode(u,fa,a[mid],mid);
    build(L(u),l,mid-1,u);
    build(R(u),mid+1,r,u);
    up(u);
}
void init()
{
    root=tot=0;
    L(root)=R(root)=pre[root]=size[root]=rev[root]=0;
    val[root]=small[root]=N;
    newnode(root,0,N,0);
    newnode(R(root),root,N,N);
    build(keytree,1,n,R(root));
    up(R(root));
    up(root);
}
int getmin(int u,int x)//得到最小值的相对位置
{
    down(u);
    if(val[u]==x) return 1+size[L(u)];
    if(small[L(u)]==x) return getmin(L(u),x);
    if(small[R(u)]==x) return size[L(u)]+1+getmin(R(u),x);
}
//--------------------------------------------------基本操作
int sum=0;
void dfs(int root)
{
    if(root==0)
        return ;
    down(root);
    dfs(ch[root][0]);
    if(key[root]!=0&&key[root]!=N)
    {
        sum++;
        if(sum<n)
            printf("%d ",key[root]);
        else
            printf("%d\n",key[root]);
    }
    dfs(ch[root][1]);
}
int main()
{
    int m;
    while(~scanf("%d%d",&n,&m))
    {
        if(n<0&&m<0)
            break;
        for(int i=1;i<=n;i++)
            a[i]=i;
        char s[5];
        sum=0;
        init();
        int l,r,z;
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d",&l,&r,&z);
                int pos=getkth(root,l);
                splay(pos,0);
                pos=getkth(root,r+2);
                splay(pos,root);
                int now=keytree;
                keytree=0;
                pos=getkth(root,z+1);
                splay(pos,0);
                pos=getkth(root,z+2);
                splay(pos,root);
                keytree=now;
                pre[now]=ch[root][1];
            }
            else
            {
                scanf("%d%d",&l,&r);
                int pos=getkth(root,l);
                splay(pos,0);
                pos=getkth(root,r+2);
                splay(pos,root);
                rev[keytree]^=1;
            }
        }
        dfs(root);
    }
    return 0;
}
标题基于SpringBoot的马术俱乐部管理系统设计与实现AI更换标题第1章引言介绍马术俱乐部管理系统的研究背景、意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义阐述马术俱乐部管理系统对提升俱乐部管理效率的重要性。1.2国内外研究现状分析国内外马术俱乐部管理系统的发展现状及存在的问题。1.3研究方法以及创新点概述本文采用的研究方法,包括SpringBoot框架的应用,以及系统的创新点。第2章相关理论总结和评述与马术俱乐部管理系统相关的现有理论。2.1SpringBoot框架理论介绍SpringBoot框架的基本原理、特点及其在Web开发中的应用。2.2数据库设计理论阐述数据库设计的基本原则、方法以及在管理系统中的应用。2.3马术俱乐部管理理论概述马术俱乐部管理的基本理论,包括会员管理、课程安排等。第3章系统设计详细描述马术俱乐部管理系统的设计方案,包括架构设计、功能模块设计等。3.1系统架构设计给出系统的整体架构,包括前端、后端和数据库的交互方式。3.2功能模块设计详细介绍系统的各个功能模块,如会员管理、课程管理、预约管理等。3.3数据库设计阐述数据库的设计方案,包括表结构、字段设计以及数据关系。第4章系统实现介绍马术俱乐部管理系统的实现过程,包括开发环境、编码实现等。4.1开发环境搭建介绍系统开发所需的环境,包括操作系统、开发工具等。4.2编码实现详细介绍系统各个功能模块的编码实现过程。4.3系统测试与调试阐述系统的测试方法、测试用例以及调试过程。第5章系统应用与分析呈现马术俱乐部管理系统的应用效果,并进行性能分析。5.1系统应用情况介绍系统在马术俱乐部中的实际应用情况。5.2系统性能分析从响应时间、并发处理能力等方面对系统性能进行分析。5.3用户反馈与改进收集用户反馈,提出系统改进建议。第6章结论与展望总结马术俱乐部管理系统的设计与实现成果,并展望未来的研究
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值