hdu3487 splay树

告别代码恐惧!用KRobot图形化编程,10分钟搞定Arduino巡线小车(附完整接线图) 本文介绍如何使用KRobot图形化编程工具快速搭建Arduino巡线小车,无需编写代码。从硬件准备到逻辑构建,详细讲解传感器数据处理、电机控制优化及高级功能扩展,帮助教育工作者和创客爱好者10分钟内完成项目。 阅读详情

Play with Chain

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5426    Accepted Submission(s): 2185


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


/*
hdu3487 splay树
cut是将一串数字复制到另外一个位置,flip是将一串数字逆序
cut直接进行一次删除插入即可,flip则是给个转换标记然后更新
hhh-2016-02-20 06:50:15
*/

#include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long  double ld;
#define key_value ch[ch[root][1]][0]
const int maxn = 300010;

int ch[maxn][2];
int rev[maxn],a[maxn];
int pre[maxn],key[maxn],siz[maxn];
int root,TOT,cnt,n;

void push_up(int r)
{
    int lson = ch[r][0],rson = ch[r][1];
    siz[r] = siz[lson] + siz[rson] + 1;
}

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

void push_down(int r)
{
    if(rev[r])
    {
        update_rev(ch[r][0]);
        update_rev(ch[r][1]);
        rev[r] = 0;
    }
}

void inOrder(int r)
{
    if(!r)return;
    push_down(r);
    inOrder(ch[r][0]);
    if(cnt >=1 && cnt <= n)
    {
        printf("%d",key[r]);
        if(cnt < n)printf(" ");
        else printf("\n");
    }
    cnt++;
    inOrder(ch[r][1]);
}

void debug()
{
    cnt = 0;
    inOrder(root);
}

void NewNode(int &r,int far,int k)
{
    r = ++TOT;
    pre[r] = far;
    ch[r][0] = ch[r][1] = 0;
    key[r] = k;
    siz[r] = 1;
    rev[r] = 0;
}


void rotat(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];
    ch[x][kind] = y;
    pre[y] = x;
    push_up(y);
}

void build(int &x,int l,int r,int far)
{
    if(l > r) return ;
    int mid = (l+r) >>1;
    NewNode(x,far,mid);
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    push_up(x);
}

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);
            rotat(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)
            {
                rotat(r,!kind);
                rotat(r,kind);
            }
            else
            {
                rotat(y,kind);
                rotat(r,kind);
            }
        }
    }
    push_up(r);
    if(goal == 0)
        root = r;
}

int get_kth(int r,int k)
{
    push_down(r);
    int t = siz[ch[r][0]]+1;
    if(t == k) return r;
    if(t > k) return get_kth(ch[r][0],k);
    else return  get_kth(ch[r][1],k-t);
}


int CUT(int l,int r,int b)
{
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    int tmp = key_value;
    pre[key_value] = 0;
    key_value = 0;
    push_up(ch[root][1]);
    push_up(root);

    splay(get_kth(root,b+1),0);
    splay(get_kth(root,b+2),root);
    pre[tmp] = ch[root][1];
    key_value = tmp;
    push_up(ch[root][1]);
    push_up(root);
    //debug();
}

void Reverse(int l,int r)
{
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    update_rev(key_value);
    push_up(ch[root][1]);
    push_up(root);
    //debug();
}


void ini(int n)
{
    TOT = root = 0;
    ch[root][0] = ch[root][1] = pre[root] = siz[root] = 0;
    rev[root] = 0;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
    build(key_value,1,n,ch[root][1]);
    //inOrder(root);
     //printf("\n");
    push_up(ch[root][1]);
    push_up(root);
}

int main()
{
    int q;
    while(scanf("%d%d",&n,&q) != EOF)
    {

        if(n == -1 && q == -1)
            break;
        ini(n);
        for(int i =1; i <= q; i++)
        {
            char qry[10];
            scanf("%s",qry);
            int a,b,len;
            if(qry[0] == 'C')
            {
                scanf("%d%d%d",&a,&len,&b);
                CUT(a,len,b);
            }
            else
            {
                scanf("%d%d",&a,&b);
                Reverse(a,b);
            }
        }
        cnt = 0;
        inOrder(root);
    }
    return 0;
}




IgH EtherCAT 从入门到精通:第 19 章 主站状态机(FSM)深度解析 文章摘要:IgH EtherCAT主站采用多层次有限状态机(FSM)架构实现总线管理。核心是Master FSM,负责总线扫描、从站配置和邮箱通信,通过函数指针状态机模式实现异步非阻塞操作。系统包含多个嵌套FSM:Slave FSM管理单个从站请求,Slave Scan FSM处理拓扑变化扫描,Slave Config FSM配置从站状态转换,Change FSM执行AL状态切换。这种设计通过状态函数指针转换实现复杂协议交互,各FSM协同工作完成EtherCAT网络的初始化、配置和实时通信,支持热插拔和错误 阅读详情

相关推荐

软件工程导论实践:从需求到架构的机票预订系统全流程解析

本文深入解析机票预订系统从需求分析到架构设计的全流程实践,涵盖业务流程、功能建模、模块化设计、性能优化等关键环节。通过实际项目经验,分享软件工程导论在复杂系统开发中的应用技巧,包括数据流图绘制、E-R图设计、微服务架构及高并发处理等实战策略,助力开发者构建高效可靠的机票预订系统。

weixin_29202213的博客 412

HDU 3487 Play with Chain (splay tree)

Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2783    Accepted Submission(s): 1141 Problem Description YaoYao is fond of pl...

weixin_33897722的博客 202

CEH Practical 实战考试真题与答案

CEH Practical 是 EC-Council 推出的Certified Ethical Hacker(CEH)认证项目中的一项高级动手实践考试。它不同于传统的理论考试,侧重于在真实环境中检验考生的实操能力。

sgafdgdagd的博客 966

HDU-3487 Play with Chain (splay好题 带翻转 cut-link操作)

Play with ChainTime Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7092    Accepted Submission(s): 2812Problem DescriptionYaoYao is fond of playing ...

hmc0411的博客 1365

免费 AIS船舶轨迹预测 数据集 分享 (可用于长周期轨迹预测)

论文中使用的数据由提供。有关预处理步骤的详细信息,请参阅。

qq_43747519的博客 1万+

模型训练篇 | 手把手教你使用ResNet训练自己数据集和推理,ResNet50通用物体分类识别,含完整代码和数据集,全网最详细教程

手把手教你使用ResNet训练自己数据集和推理,ResNet50通用物体分类识别,含完整代码和数据集,全网最详细教程。本篇文章将带你学习 ResNet50 模型构建、ResNet50训练自己数据集、数据集处理、推理和验证、模型剪枝与模型改进等。 本篇教程适配各种分类识别任务,可以说是一个通用的教程,只需修改自己的数据集即可完成自己领域的任务和系统实现。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 719

HDU 3487 Play with Chain(伸展树基本操作)

题意:给定一数组,完成两种操作:CUT a b c 将区间[a,b]的数删除,并接到新数列的第c个数后面FLIP a b 将区间[a,b]的数翻转顺序输出最终的序列 题解:      伸展树的基本操作。#include&lt;bits/stdc++.h&gt; using namespace std; #define keyv ch[ch[root][1]][0] #define ddd put...

sgh666666的博客 302

小卫星多普勒频偏MATLAB代码

多普勒频偏是由于发射机和接收机之间的相对运动而引起的频率变化。对于小卫星,由于其速度和高度的变化,多普勒频偏会变得更加明显。本文将探讨小卫星多普勒频偏的原理、影响因素以及补偿方法。多普勒频偏的原理当发射机和接收机之间存在相对运动时,接收到的信号频率会发生变化。这种频率变化称为多普勒频偏。其原理可以描述如下:设发射机和接收机之间的距离为 d,发射信号的频率为 f0,发射机和接收机的相对速度为 v。则接收到的信号频率 f 为:其中 c 为光速。

qq_59747472的博客 243

HDU 3487. Play with Chain

HDU 3487. Play with Chain Play with ChainTime Limit: 6000/2000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7099 Accepted Submission(s): 2815Problem Descrip

Felix-Lee的博客 1万+

Autodesk Revit软件二次开发:Revit二次开发中的插件设计与实现

在 Revit 中,族是构成建筑模型的基本单元,可以是门窗、家具等。通过自定义族,可以创建符合特定需求的建筑构件。参数化设计允许族的尺寸、形状等属性根据参数变化而变化。使用 Revit API 创建自定义族涉及多个步骤,包括定义族类型、设置参数、创建几何形状等。// 创建族文档// 定义参数Parameter param = family.FamilyManager.AddParameter("Width", BuiltInParameterGroup.PG_LENGTH, "宽度");

kkchenjj的博客 2229

【数据集】全球首个10米分辨率精细分类土地覆盖数据集GLC_FCS10

【数据集】全球首个10米分辨率精细分类土地覆盖数据集GLC_FCS10

WW、forever的博客 1567

Play with Chain

题目描述 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 wi...

weixin_30306905的博客 210

开关电源和线性电源Multisim电路仿真实验汇总——硬件工程师笔记

本文系统介绍了稳压电源的基础知识与应用实验。首先阐述了稳压电源的定义与类型,包括线性稳压电源(如LM7805/7905)和开关稳压电源(降压/升压/升降压式)的工作原理、参数特点及优劣势。随后详细讲解了各类稳压电源的实验设计,如线性稳压电源的电路实现、开关稳压电源的PWM控制方法等。最后汇总了硬件工程师相关的笔试面试资源,涵盖器件知识、电路仿真、企业真题等内容。全文结合理论分析与Multisim仿真,为电子工程学习者提供了稳压电源的实用技术参考。

XU157303764的博客 1638

【splay树】hdu 3487

可以作为模板 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define LL

leolin_的专栏 846

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Delegatecall存储碰撞漏洞详解:从EVM存储布局到DeFi安全审计

在智能合约开发中,delegatecall是一把双刃剑:它让代理合约升级和库函数复用成为可能,却也因代码执行与存储上下文的分离,成为存储碰撞漏洞的温床。理解EVM的存储布局——每个变量所占据的槽位、mapping的哈希计算规则——是识别这类风险的前提。当不同合约对同一物理存储槽赋予不同语义时,一次看似普通的调用就可能改写代理合约的owner、implementation等关键状态,导致权限被接管。这类攻击不依赖价格预言机或闪电贷,只需精确命中存储槽位即可得手,历史上Cream Finance、Hundred

weixin_34335458的博客 191

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

hdu 3487 Play with Chain (Splay树) 区间切割 插入 翻转

hdu 3487 Play with Chain (Splay树) 区间切割 插入 翻转 关键注意:down 和 up find,kth,next,pre, RTO等当有flip时,都要down 切割之后,up 插入之后,修改pre 指针: //#pragma comment(linker, "/STACK:1024000000,1024000000") #includ

冰 1065

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

hdu 3487 Play with Chain

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 题目思路:splay,区间旋转和区间转移,区间转移的时候要注意向上更新,旋转的时候要注意rev要异或。 #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; #include&lt;string&g...

java开发指南博客 【转载】 99

当初学 Splay树 所看过的,凭理解打过的代码

HDU 1890   这是我第一次接触Splay树时,所接触的代码。 那位大牛的博客我现在找不到了#。#。抱歉ing..   思路就是区间翻转,主要是讲n个数通过树的中序访问保存下来。 这样旋转的时候就不会出现问题   代码:   // // 1890.cpp // ACM_HDU // // Created by ipqhjjybj on 13-8-27. // Co

I will Do iT @ hlyblog.net 1630

HDU 3487 Play With Chain Splay树 区间处理

Splay进行区间处理 还没完全搞懂..http://acm.hdu.edu.cn/showproblem.php?pid=3487#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #incl

Katapeltes 395

Splay树(区间添加删除 | 区间翻转)——HDU 3487 Play with Chain

Splay树——HDU 3487 Play with Chain

大田二十 1742

splay经典题目举例

数据结构专攻告一段落吧,把这段时间和以前做过的splay的题目拿出来晒晒,没题都写了一下简单的解题报告,用白色的字体,每道题的做法最好要多花时间琢磨琢磨,splay的题目主要就是中间过程的处理上比较麻烦。为了方便,我对区间操作都是将l-1结点splay到根,r+1结点splay到

5761

Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的q操作,最后输出操作后的序列: 分析:首先建立初始化的树结构之前,先给n设置个极限范围0和n+1,防止特殊情况下越界,如果反转[a,b],则先把第a-1个数放在根节点,把b+1结点的数放在根节点...

weixin_30457065的博客 168

HDU 3487 Play with Chain

HDU_3487 拿这个题目练了一下splay的区间的切割,也就是删除、移动、添加一个连续的区间。 #include<stdio.h>#include<string.h>#define MAXD 300010int N, M, T, node, size[MAXD], rev[MAXD], left[MAXD], right[MAXD], pre[MAXD]...

weixin_30641999的博客 81

splay区间翻转及删除区间,平移区间

研究了很长时间,花费了一个下午debug,终于调出来了 hdu 3487 play with chain http://acm.hdu.edu.cn/showproblem.php?pid=3487 两个操作(指针实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置. flip l r 把[l,r]翻转(lazy标记,每次交换左右节点)

nikelong的博客 3508

2018年7月30日暑假训练日记

上午:Splay HDU 3436 Queue-jumpers (Splay tree) Splay的三大操作,查询某个点的位置,查询第k个位置,和旋转点到队首。 这里离散化比较六,把没有用到的区间压缩成了一个点 HDU 3487 Play with Chain (Splay tree) 这就是上次没做出来的题目的升级版,加了一个反转操作,果然是个splay的模板题,由于操作l,r,所以...

m0_37772713的博客 277
上一篇: poj3580 splay树 REVOVLE循环
下一篇: hdu 1890 splay树
Tyuio28
博客等级 码龄11年 12粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值