poj3580 SuperMemo (Splay+区间内向一个方向移动)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 13550 | Accepted: 4248 | |
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:
- 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}
- 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}
- REVOLVE x y T: rotate sub-sequence {Ax ... Ay} T times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
- 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}
- DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
- 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 n (n ≤ 100000).
The following n lines describe the sequence.
Then follows M (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
题意:给你n个数组成的一个序列,让你写一个数据结构支持下列操作:1.区间[x,y]增加c 2.区间[x,y]翻转 3.区间[x,y]向右移动c次,如1 2 3 4 5向右移动2次就变成4 5 1 2 3。4.在第x个数后插入c 5.删除第x个数 6.求出区间[x,y]内的最小值。
思路:要维护rev[],mx[],add[]分别表示旋转标记,最小值以及成段增加的标记,然后这题和其他题不用的地方在于多了一个区间内移动的操作,我们要先求出c被y-x+1除后的余数,如果为0就不变,如果不为0,那么画图可以看出这个操作等价于把[y-c+1,y]移到[x,y-c]这个区间的前面,然后就可以直接做了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define Key_value ch[ch[rt][1]][0]
#define maxn 1000010
int n;
int cnt,rt;
int pre[maxn],ch[maxn][2],sz[maxn],rev[maxn],zhi[maxn],mx[maxn],add[maxn];
int b[maxn],tot2;//内存池和容量
int a[maxn];
void update_rev(int x)
{
if(!x)return; //!!!
rev[x]^=1;
swap(ch[x][0],ch[x][1]);
}
void update_add(int x,int value)
{
zhi[x]+=value;
add[x]+=value;
mx[x]+=value;
}
void pushdown(int x)
{
if(rev[x]){
update_rev(ch[x][0]);
update_rev(ch[x][1]);
rev[x]=0;
}
if(add[x]){
update_add(ch[x][0],add[x]);
update_add(ch[x][1],add[x]);
add[x]=0;
}
}
void pushup(int x)
{
int t=zhi[x];
if(ch[x][0])t=min(t,mx[ch[x][0] ]);
if(ch[x][1])t=min(t,mx[ch[x][1] ]);
mx[x]=t;
sz[x]=sz[ch[x][0] ]+sz[ch[x][1] ]+1;
}
void Treavel(int x)
{
if(x)
{
pushdown(x);
Treavel(ch[x][0]);
printf("结点:%2d: 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d zhi = %2d minx=%2d add=%2d\n",x,ch[x][0],ch[x][1],pre[x],sz[x],zhi[x],mx[x],add[x]);
Treavel(ch[x][1]);
}
}
void debug()
{
printf("root:%d\n",rt);
Treavel(rt);
}
void newnode(int &x,int father,int value)
{
if(tot2)x=b[tot2--];
else x=++cnt;
pre[x]=father;ch[x][0]=ch[x][1]=0;sz[x]=1;rev[x]=0;zhi[x]=value;mx[x]=value;add[x]=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);
pushup(x);
}
void init()
{
cnt=rt=tot2=0;
pre[0]=ch[0][0]=ch[0][1]=sz[0]=rev[0]=zhi[0]=mx[0]=add[0]=0;
newnode(rt,0,-1);
newnode(ch[rt][1],rt,-1);
build(Key_value,1,n,ch[rt][1]);
pushup(ch[rt][1]);
pushup(rt);
}
void rotate(int x,int p)
{
int y=pre[x];
pushdown(y);pushdown(x);
ch[y][!p]=ch[x][p];
pre[ch[x][p] ]=y;
if(pre[y])ch[pre[y] ][ch[pre[y] ][1]==y ]=x;
pre[x]=pre[y];
ch[x][p]=y;
pre[y]=x;
pushup(y);pushup(x);
}
void splay(int x,int goal)
{
pushdown(x);
while(pre[x]!=goal){
if(pre[pre[x] ]==goal){
pushdown(pre[x]);pushdown(x);
rotate(x,ch[pre[x]][0]==x);
}
else{
int y=pre[x];int z=pre[y];
pushdown(z);pushdown(y);pushdown(x);
int p=ch[pre[y] ][0]==y;
if(ch[y][p]==x )rotate(x,!p);
else rotate(y,p);
rotate(x,p);
}
}
if(goal==0)rt=x;
pushup(x);
}
int get_kth(int x,int k)
{
int i,j;
pushdown(x);
int t=sz[ch[x][0] ]+1;
if(t==k)return x;
if(t<k)return get_kth(ch[x][1],k-t);
return get_kth(ch[x][0],k);
}
void erase(int x)
{
if(x==0)return; //!!!
b[++tot2]=x;
erase(ch[x][0]);
erase(ch[x][1]);
}
void Add(int x,int y,int c)
{
splay(get_kth(rt,x),0);
splay(get_kth(rt,y+2),rt);
update_add(Key_value,c);
pushup(ch[rt][1]);
pushup(rt);
}
void Reverse(int x,int y)
{
splay(get_kth(rt,x),0);
splay(get_kth(rt,y+2),rt);
update_rev(Key_value);
pushup(ch[rt][1]);
pushup(rt);
}
void Insert(int x,int value)
{
int i,j;
splay(get_kth(rt,x+1),0);
splay(get_kth(rt,x+2),rt);
newnode(Key_value,ch[rt][1],value);
pushup(ch[rt][1]);
pushup(rt);
}
void Rotate(int x,int y,int c)
{
int len=y-x+1;
c=(c%len+len)%len;
if(c==0)return;
splay(get_kth(rt,y-c+1),0);
splay(get_kth(rt,y+2 ),rt);
int tmp=Key_value;
Key_value=0;
pushup(ch[rt][1]);
pushup(rt);
splay(get_kth(rt,x),0 );
splay(get_kth(rt,x+1),rt);
Key_value=tmp;
pre[tmp]=ch[rt][1];
pushup(ch[rt][1]);
pushup(rt);
}
void Delete(int pos,int tot)
{
splay(get_kth(rt,pos),0);
splay(get_kth(rt,pos+tot+1),rt);
erase(Key_value);
Key_value=0;
pushup(ch[rt][1]);
pushup(rt);
}
int Get_min(int x,int y)
{
splay(get_kth(rt,x),0);
splay(get_kth(rt,y+2),rt);
return mx[Key_value];
}
int main()
{
int m,i,j,pos,tot,c,d,e,f;
char s[10];
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
init();
//debug();
for(i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='A'){
scanf("%d%d%d",&c,&d,&e);
Add(c,d,e);
}
if(s[0]=='R' && s[3]=='E'){
scanf("%d%d",&c,&d);
Reverse(c,d);
}
if(s[0]=='R' && s[3]=='O'){
scanf("%d%d%d",&c,&d,&e);
Rotate(c,d,e);
}
if(s[0]=='I'){
scanf("%d%d",&c,&d);
Insert(c,d);
}
if(s[0]=='D'){
scanf("%d",&c);
Delete(c,1);
}
if(s[0]=='M'){
scanf("%d%d",&c,&d);
printf("%d\n",Get_min(c,d));
//printf("1\n");
}
//debug();
}
}
return 0;
}
poj3580 SuperMemo (Splay+区间内向一个方向移动)的更多相关文章
- POJ3580 SuperMemo splay伸展树,区间操作
题意:实现一种数据结构,支持对一个数列的 6 种操作:第 x 个数到第 y 个数之间的数每个加 D:第 x 个数到第 y 个数之间全部数翻转:第 x 个数到第 y 个数之间的数,向后循环流动 c 次, ...
- 【BZOJ1895】Pku3580 supermemo Splay
[BZOJ1895]Pku3580 supermemo Description 给出一个初始序列fA1;A2;:::Ang,要求你编写程序支持如下操作: 1. ADDxyD:给子序列fAx:::Ayg ...
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
- Splay(区间翻转)&树套树(Splay+线段树,90分)
study from: https://tiger0132.blog.luogu.org/slay-notes P3369 [模板]普通平衡树 #include <cstdio> #inc ...
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- splay模板三合一 luogu2042 [NOI2005]维护数列/bzoj1500 [NOI2005]维修数列 | poj3580 SuperMemo | luogu3391 【模板】文艺平衡树(Splay)
先是维修数列 题解看这里,但是我写的跑得很慢 #include <iostream> #include <cstdio> using namespace std; int n, ...
- BZOJ 2631: tree [LCT splay区间]
2631: tree Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 3854 Solved: 1292[Submit][Status][Discus ...
- 「BZOJ1251」序列终结者 (splay 区间操作)
题面: 1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5367 Solved: 2323[Submit][Status][D ...
随机推荐
- node.js中使用http-proxy-middleware请求转发给其它服务器
var express = require('express');var proxy = require('http-proxy-middleware'); var app = express(); ...
- dig的安装和使用
-bash: dig: command not found 解决办法: yum -y install bind-utils dig www.baid bu.com 查看a记录 dig www.ba ...
- leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)
题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...
- 【一天一个知识点系列】- Http之状态码
状态码 简介 HTTP 状态码负责表示客户端 HTTP 请求的返回结果. 标记服务器端的处理是否正常. 通知出现的错误等工作 作用及类别 作用:状态码告知从服务器端返回的请求结果 状态码的类别 注意: ...
- C# 中的动态类型
翻译自 Camilo Reyes 2018年10月15日的文章 <Working with the Dynamic Type in C#> [1] .NET 4 中引入了动态类型.动态对象 ...
- ORACLE 归档日志打开关闭方法(转载)
一 设置为归档方式 1 sql> archive log list; #查看是不是归档方式 2 sql> alter system set log_archive_start=true s ...
- 全栈性能测试修炼宝典-JMeter实战笔记(三)
JMeter体系结构 简介 JMeter是一款开源桌面应用软件,可用来模拟用户负载来完成性能测试工作. JMeter体系结构 X1~X5是负载模拟的一个过程,使用这些组件来完成负载的模拟 Y1:包含的 ...
- NodeJS连接MongoDB数据库
NodeJS连接MongoDB数据库 连接数据库的js文件[我将其命名为(connect.js)] // 引入mongoose第三方模块 const mongoose = require('mongo ...
- Windows server 2008常用优化设置
1. 如何取消开机按 CTRL+ALT+DEL登录? 控制面板→管理工具→本地安全策略→本地策略→安全选项→交互式登录:无须按CTRL+ALT+DEL→启用. 2. 如何取消关机时出现的关机理由选择项 ...
- Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf/
Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf ...