POJ 3580 SuperMemo(splay成段更新、区间最小值、反转、插入和删除、区间搬移)

本文主要是介绍POJ 3580 SuperMemo(splay成段更新、区间最小值、反转、插入和删除、区间搬移),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

SuperMemo
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 5839
Accepted: 1884
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, {A1A2, ... 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都是伸展树比较经典的操作。
特别的是其中的循环右移操作。循环右移[l,r] T次,其实就是把区间[l,r-T]放在[r-T+1,r]后面。就是区间搬移。但是T必须先对长度取模
狂斌大神模板

/** 给定一个数列a1,a2,...an* 进行以下6种操作* ADD x y D :给第x个数到第y个数加D(增加一个add进行延迟标记)* REVERSE x y :反转[x,y]之间的数(伸展树经典操作)* REVOLVE x y T:循环右移T次(先把T对长度进行取模,然后就相当于把[y-T+1,y]放在[x,y-T]前面)* INSERT x P:在第x个数后面插入P (经典的插入)* DELETE x:删除第x个数(删除操作)* MIN x y:查询[x,y]之间最小的数(标记)** 需要的操作:反转、删除、插入、查询区间最小值、成段更新、区间搬移(循环右移转化为区间搬移)* 需要的变量:pre,ch,key,size,add,rev,m(最小值)*/#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define Key_value ch[ch[root][1]][0]
#define Key_Value ch[ch[root][1]][0]
const int MAXN=200010;
const int INF=0x3f3f3f3f;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN],add[MAXN],rev[MAXN],m[MAXN];
int root,tot1;
int s[MAXN],tot2;//内存池、内存池容量
int a[MAXN];
int n,q;void NewNode(int &r,int father,int k)
{if(tot2)r=s[tot2--];else r=++tot1;ch[r][0]=ch[r][1]=0;pre[r]=father;size[r]=1;add[r]=rev[r]=0;key[r]=m[r]=k;
}
void Update_Rev(int r)
{if(!r)return;swap(ch[r][0],ch[r][1]);rev[r]^=1;
}
void Update_Add(int r,int ADD)
{if(!r)return;add[r]+=ADD;key[r]+=ADD;m[r]+=ADD;
}
void Push_Up(int r)
{size[r]=size[ch[r][0]]+size[ch[r][1]]+1;m[r]=key[r];if(ch[r][0])m[r]=min(m[r],m[ch[r][0]]);if(ch[r][1])m[r]=min(m[r],m[ch[r][1]]);
}
void Push_Down(int r)
{if(rev[r]){Update_Rev(ch[r][0]);Update_Rev(ch[r][1]);rev[r]=0;}if(add[r]){Update_Add(ch[r][0],add[r]);Update_Add(ch[r][1],add[r]);add[r]=0;}
}
void Build(int &x,int l,int r,int father)
{if(l>r)return;int mid=(l+r)/2;NewNode(x,father,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]=size[root]=add[root]=rev[root]=pre[root]=0;m[root]=INF;//这个不用也可以,如果在push_up那判断了的话,否则需要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];ch[x][kind]=y;pre[y]=x;Push_Up(y);
}
//Splay调整
void Splay(int r,int goal)
{Push_Down(r);while(pre[r]!=goal){if(pre[pre[r]]==goal){//这题有反转操作,需要先push_down,在判断左右孩子Push_Down(pre[r]);Push_Down(r);Rotate(r,ch[pre[r]][0]==r);}else{//这题有反转操作,需要先push_down,在判断左右孩子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);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=size[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 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;
}
//下面是操作了
void ADD(int l,int r,int D)
{Splay(Get_Kth(root,l),0);Splay(Get_Kth(root,r+2),root);Update_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);Update_Rev(Key_value);Push_Up(ch[root][1]);Push_Up(root);
}
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;//将区间[c,r]放在[l,c-1]前面Splay(Get_Kth(root,c),0);Splay(Get_Kth(root,r+2),root);int tmp=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=tmp;pre[Key_value]=ch[root][1];//这个不用忘记Push_Up(ch[root][1]);Push_Up(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,ch[root][1],P);Push_Up(ch[root][1]);Push_Up(root);
}
void erase(int r)//回收内存
{if(r){s[++tot2]=r;erase(ch[r][0]);erase(ch[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;Push_Up(ch[root][1]);Push_Up(root);
}
int Query_Min(int l,int r)
{Splay(Get_Kth(root,l),0);Splay(Get_Kth(root,r+2),root);return m[Key_value];
}
int main()
{//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);char op[20];int x,y,z;while(scanf("%d",&n)==1){for(int i=1;i<=n;i++)scanf("%d",&a[i]);Init();scanf("%d",&q);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{scanf("%d%d",&x,&y);printf("%d\n",Query_Min(x,y));}}}return 0;
}

这篇关于POJ 3580 SuperMemo(splay成段更新、区间最小值、反转、插入和删除、区间搬移)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/860706

相关文章

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

docker如何删除悬空镜像

《docker如何删除悬空镜像》文章介绍了如何使用Docker命令删除悬空镜像,以提高服务器空间利用率,通过使用dockerimage命令结合filter和awk工具,可以过滤出没有Tag的镜像,并将... 目录docChina编程ker删除悬空镜像前言悬空镜像docker官方提供的方式自定义方式总结docker

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

Python脚本实现自动删除C盘临时文件夹

《Python脚本实现自动删除C盘临时文件夹》在日常使用电脑的过程中,临时文件夹往往会积累大量的无用数据,占用宝贵的磁盘空间,下面我们就来看看Python如何通过脚本实现自动删除C盘临时文件夹吧... 目录一、准备工作二、python脚本编写三、脚本解析四、运行脚本五、案例演示六、注意事项七、总结在日常使用

Git中恢复已删除分支的几种方法

《Git中恢复已删除分支的几种方法》:本文主要介绍在Git中恢复已删除分支的几种方法,包括查找提交记录、恢复分支、推送恢复的分支等步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录1. 恢复本地删除的分支场景方法2. 恢复远程删除的分支场景方法3. 恢复未推送的本地删除分支场景方法4. 恢复

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

Oracle数据库使用 listagg去重删除重复数据的方法汇总

《Oracle数据库使用listagg去重删除重复数据的方法汇总》文章介绍了在Oracle数据库中使用LISTAGG和XMLAGG函数进行字符串聚合并去重的方法,包括去重聚合、使用XML解析和CLO... 目录案例表第一种:使用wm_concat() + distinct去重聚合第二种:使用listagg,