SuperMemo
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6841   Accepted: 2268
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

Source

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/28 19:39:45
File Name :F:\2013ACM练习\专题学习\splay_tree_2\POJ3580.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
/*
* 给定一个数列:a1,a2,.... an
* 进行以下6种操作:
* ADD x y D : 给第x个数到第y个数加D
* REVERSE x y : 反转[x,y]
* REVOVLE x y T : 对[x,y]区间的数循环右移T次
* (先把T对长度取模,然后相当于把[y-T+1,y]放到[x,y-T] 的前面)
* INSERT x P : 在第x个数后面插入P
* DELETE x : 删除第x个数
* MIN x y : 查询[x,y]之间的最小的数
*/
#define Key_value ch[ch[root][1]][0]
const int MAXN = ;
const int INF = 0x3f3f3f3f;
int pre[MAXN],ch[MAXN][],root,tot1,size[MAXN];
int key[MAXN],rev[MAXN],m[MAXN],add[MAXN];
int s[MAXN],tot2;
int a[MAXN];
int n;
void NewNode(int &r,int father,int k)
{
if(tot2) r = s[tot2--];
else r = ++tot1;
pre[r] = father;
ch[r][] = ch[r][] = ;
key[r] = k;
m[r] = k;
rev[r] = add[r] = ;
size[r] = ;
}
void Update_Rev(int r)
{
if(!r)return;
swap(ch[r][],ch[r][]);
rev[r] ^= ;
}
void Update_Add(int r,int D)
{
if(!r)return;
m[r] += D;
key[r] += D;
add[r] += D;
}
void push_up(int r)
{
size[r] = size[ch[r][]] + size[ch[r][]] + ;
m[r] = min(key[r],min(m[ch[r][]],m[ch[r][]]));
}
void push_down(int r)
{
if(rev[r])
{
Update_Rev(ch[r][]);
Update_Rev(ch[r][]);
rev[r] = ;
}
if(add[r])
{
Update_Add(ch[r][],add[r]);
Update_Add(ch[r][],add[r]);
add[r] = ;
}
}
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l+ r)/;
NewNode(x,father,a[mid]);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
push_up(x);
}
void Init()
{
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = pre[root] = ;
rev[root] = add[root] = ;
m[root] = INF;
size[root] = ;
NewNode(root,,-);
NewNode(ch[root][],root,-);
for(int i = ;i < n;i++)
scanf("%d",&a[i]);
Build(Key_value,,n-,ch[root][]);
push_up(ch[root][]);
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]][]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
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);
Rotate(r,ch[pre[r]][]==r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
push_up(r);
if(goal == ) root = r;
} int Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][]] + ;
if(t == k) return r;
if(t > k) return Get_kth(ch[r][],k);
else return Get_kth(ch[r][],k-t);
} void ADD(int x,int y,int D)
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,y+),root);
Update_Add(Key_value,D);
push_up(ch[root][]);
push_up(root);
}
void Reverse(int x,int y)
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,y+),root);
Update_Rev(Key_value);
push_up(ch[root][]);
push_up(root);
}
void REVOLVE(int x,int y,int T)
{
int len = y - x + ;
T = ( T%len + len )%len;
Splay(Get_kth(root,y-T+),);
Splay(Get_kth(root,y+),root);
int tmp = Key_value;
Key_value = ;
push_up(ch[root][]);
push_up(root);
Splay(Get_kth(root,x),);
Splay(Get_kth(root,x+),root);
Key_value = tmp;
pre[tmp] = ch[root][];
push_up(ch[root][]);
push_up(root);
}
void INSERT(int x,int P)
{
Splay(Get_kth(root,x+),);
Splay(Get_kth(root,x+),root);
NewNode(Key_value,ch[root][],P);
push_up(ch[root][]);
push_up(root);
}
void erase(int r)
{
if(!r)return;
s[++tot2] = r;
erase(ch[r][]);
erase(ch[r][]);
}
void DELETE(int x)
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,x+),root);
erase(Key_value);
pre[Key_value] = ;
Key_value = ;
push_up(ch[root][]);
push_up(root);
}
int MIN(int x,int y)
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,y+),root);
return m[Key_value];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d",&n) == )
{
Init();
int x,y,z;
char op[];
int q;
scanf("%d",&q);
while(q--)
{
scanf("%s",op);
if(strcmp(op,"ADD") == )
{
scanf("%d%d%d",&x,&y,&z);
ADD(x,y,z);
}
else if(strcmp(op,"REVERSE") == )
{
scanf("%d%d",&x,&y);
Reverse(x,y);
}
else if(strcmp(op,"REVOLVE") == )
{
scanf("%d%d%d",&x,&y,&z);
REVOLVE(x,y,z);
}
else if(strcmp(op,"INSERT") == )
{
scanf("%d%d",&x,&y);
INSERT(x,y);
}
else if(strcmp(op,"DELETE") == )
{
scanf("%d",&x);
DELETE(x);
}
else if(strcmp(op,"MIN") == )
{
scanf("%d%d",&x,&y);
printf("%d\n",MIN(x,y));
}
}
}
return ;
}

POJ 3580 SuperMemo (splay tree)的更多相关文章

  1. 伸展树(Splay Tree)进阶 - 从原理到实现

    目录 1 简介 2 基础操作 2.1 旋转 2.2 伸展操作 3 常规操作 3.1 插入操作 3.2 删除操作 3.3 查找操作 3.4 查找某数的排名.查找某排名的数 3.4.1 查找某数的排名 3 ...

  2. 数据结构(二) --- 伸展树(Splay Tree)

    文章图片和代码来自邓俊辉老师课件 概述 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由丹尼尔·斯立特Daniel Sleator ...

  3. 纸上谈兵:伸展树(splay tree)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们讨论过,树的搜索效率与树的深度有关.二叉搜索树的深度可能为n,这种情况下,每次 ...

  4. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. 平衡树之伸展树(Splay Tree)题目整理

    目录 前言 练习1 BZOJ 3224 普通平衡树 练习2 BZOJ 3223 文艺平衡树 练习3 BZOJ 1588 [HNOI2002]营业额统计 练习4 BZOJ 1208 [HNOI2004] ...

  6. 伸展树(splay tree)

    伸展树的设计思路,鉴于数据访问的局部性(28原则)在实际应用中普遍存在,将按照"最常用者优先"的启发策略.尽管在最坏情况下其单次操作需要 O(n) 时间,但分摊而言仍然 O(log ...

  7. BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1213  Solved: 454[Submit ...

  8. HDU 3436 Queue-jumpers (splay tree)

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. HDU 1890--Robotic Sort(Splay Tree)

    题意:每次找出第i大的数的位置p输出,然后将i~p之间的数反转. 题解:每次把要的区间转成一棵子树,然后更新.因为每次将第i小的数转到了了i,所以k次操作后,可知前k个数一定是最小的那k个数,所以以后 ...

随机推荐

  1. laravel 中provider的理解和使用

    https://segmentfault.com/q/1010000004640866

  2. js实现图片懒加载

    大型购物网站都会采用图片懒加载技术来优化网站首页打开速度,以提高用户体验,那么具体是怎么实现的呢,我们一探究竟. html结构(div包裹一层用来显示背景图片,等待图片加载完成后,显示真实图片) &l ...

  3. 一步一步学习IdentityServer4 (4) 处理特殊需求之-登录等待页面

    用IdentityServer3的时候登录如果采用Post方式大家会发现有中间有一个等待Submit空白页面,界面不友好,现在我想把这个修改自定义的页面Loading 在Identityserver3 ...

  4. 微商城三级分销源码公众号开发 微分销 C#源码

    需要源码,请加QQ:858-048-581 ,可以查看演示 运行环境:vs2012+ sql2008r2 [什么是微分销] 微分销是助力企业进军移动电商,完善分销体系搭建微信分销系统.基于微信平台,搭 ...

  5. 何时调用getView?——从源码的角度给出解答

    先来看ListView类中的makeAndAddView方法: 没有数据变化:从mRecycler中取得可视的view 数据有变化:obtainView /** * 获取视图填充到列表的item中去, ...

  6. scrapy 设置cookie池

    代码已经很详细了,可以直接拿来使用了. 包含了: 从网页获取cookie 存入mongodb 定期删除cookie scrapy中间件对cookie池的取用 #!/usr/bin/python #co ...

  7. 黑马程序员_java基础笔记(06)...集合

    —————————— ASP.Net+Android+IOS开发..Net培训.期待与您交流! —————————— JavaApi(其实就是java给我们提供的已经定义好的对象.工具对象:集合框架) ...

  8. 一台Windows下配置多个Tomcat服务器

    上一篇博客<Windows下配置Tomcat服务器>讲了,如何在一台Windows机器上配置一个Tomcat服务器.这篇介绍一下如何在一台Windows机器上配置多个Tomcat. 第一步 ...

  9. C++ 几种经典的垃圾回收算法

    之前遇到了一篇好文(https://blog.csdn.net/wallwind/article/details/6889917)准备学习一下的,课程繁忙就忘记了,今日得闲,特来补一下. 自己写一遍加 ...

  10. maven 发布jar包到远程仓库

    有的时候我们需要发布一些自己写的相关jar包到maven私服,供项目组使用. 首先在setting.xml文件添加,这里 注意 要保证该账户有发布的权限 <servers> <ser ...