SuperMemo

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

思路:

\(FHQ\_treap\ or\ Splay板题\)

每个节点需保存当前节点值,子树最小值,反转标记,增加标记。

对于操作\(4.5,\)直接插入/删除

对于操作\(1,\)先提取区间\([l,r]\),再打上增加标记

对于操作\(2,\)先提取区间\([l,r]\),再打上翻转标记

对于操作\(3,\)先提取区间\([l,r]\),再提取并交换区间\([l,r-T\%(r-l+1)-l+1]\)和\([r-T\%(r-l+1)-l+2,r]\)

对于操作\(6,\)先提取区间\([l,r]\),返回最小值

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define read read1<int>()
Type inline T read1()
{
T t=0;
bool ty=0;
char k;
do k=getchar(),(k=='-')&&(ty=1);while('0'>k||k>'9');
do t=(t<<3)+(t<<1)+(k^'0'),k=getchar();while('0'<=k&&k<='9');
return ty?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
template<typename T,typename _T>
class FHQ_treap_map
{
private:
int seed,space;
public:
int Rand(){return seed=seed*33457+52331314;}
class node
{
public:
node *l,*r;
T k;_T v;bool fl;
int cnt,si,q,ad;
node(){l=r=NULL;cnt=si=1;fl=ad=0;}
node(T k1,_T v1,int q1){l=r=NULL;cnt=si=1;k=k1;v=v1;q=q1;fl=ad=0;}
}*root;
class Pair
{
public:
node *l,*r;
Pair(){}
Pair(node *a,node *b){l=a;r=b;}
};
private:
_T value(node *n){return n?n->v:1<<30;}
void pushup(node *n){n->si=size(n->l)+size(n->r)+n->cnt;n->v=min(value(n->l),min(value(n->r),n->k));}
void turn(node *n){if(n)n->fl^=1;}
void ad(node *n,int m){if(n)n->ad+=m,n->k+=m,n->v+=m;}
void pushdown(node *n){if(n->fl)turn(n->l),turn(n->r),swap(n->l,n->r);ad(n->l,n->ad),ad(n->r,n->ad),n->ad=n->fl=0;}
int size(node *n){return n?n->si:0;}
Pair split(node *n,int ra)
{
if(!n)return Pair(NULL,NULL);
Pair tem;
pushdown(n);
if(ra<=size(n->l))
{
tem=split(n->l,ra);
n->l=tem.r;
tem.r=n;
}
else
{
tem=split(n->r,ra-size(n->l)-n->cnt);
n->r=tem.l;
tem.l=n;
}
pushup(n);
return tem;
}
Pair _split(node *n,T v)
{
if(!n)return Pair(NULL,NULL);
Pair tem;
pushdown(n);
if(v<n->k)
{
tem=_split(n->l,v);
n->l=tem.r;
tem.r=n;
}
else
{
tem=_split(n->r,v);
n->r=tem.l;
tem.l=n;
}
pushup(n);
return tem;
}
Pair split_(node *n,T v)
{
if(!n)return Pair(NULL,NULL);
Pair tem;
pushdown(n);
if(v<=n->k)
{
tem=split_(n->l,v);
n->l=tem.r;
tem.r=n;
}
else
{
tem=split_(n->r,v);
n->r=tem.l;
tem.l=n;
}
pushup(n);
return tem;
}
node* merge(node *l,node *r)
{
if(!l or !r)return l?l:r;
pushdown(l);
pushdown(r);
node* re;
if(l->q<r->q)l->r=merge(l->r,r),re=l;
else r->l=merge(l,r->l),re=r;
pushup(re);
return re;
}
node* findmin(node* n){while(n->l)n=n->l;return n;}
node* findmax(node* n){while(n->r)n=n->r;return n;}
public:
FHQ_treap_map(){root=NULL;seed=4088;}
void insert(T ke,_T va)
{
++space;
Pair tem=_split(root,ke);
root=merge(merge(tem.l,new node(ke,va,Rand())),tem.r);
}
void _insert(int n,T ke,_T va)
{
++space;
Pair tem=split(root,n);
root=merge(merge(tem.l,new node(ke,va,Rand())),tem.r);
}
void del(T ke)
{
--space;
Pair tem=_split(root,ke),tem1=split_(tem.l,ke);
root=merge(tem1.l,tem.r);
}
void _del(int n)
{
--space;
Pair tem=split(root,n),tem1=split(tem.l,n-1);
root=merge(tem1.l,tem.r);
}
T upper(T ke)
{
Pair tem=_split(root,ke);
T tans=findmin(tem.r)->k;
merge(tem.l,tem.r);
return tans;
}
T lower(T ke)
{
Pair tem=split_(root,ke);
T tans=findmax(tem.l)->k;
merge(tem.l,tem.r);
return tans;
}
int findrank(T ke)
{
Pair tem=split_(root,ke);
int tans=size(tem.l)+1;
merge(tem.l,tem.r);
return tans;
}
T findwho(int n)
{
for(node* i=root;i;)
if(n<=size(i->l))i=i->l;
else if(size(i->l)+i->cnt<n)n-=size(i->l)+i->cnt,i=i->r;
else return i->k;
return 0;
}
bool find(T n)
{
for(node* i=root;i;)
if(i->k==n)return 1;
else if(i->k>n)i=i->l;
else i=i->r;
return 0;
}
_T operator [] (const T n)
{
for(node* i=root;i;)
if(i->k==n)return i->v;
else if(i->k>n)i=i->l;
else i=i->r;
return root->v;
}
_T query(int l,int r)
{
Pair tem=split(root,l-1),tem1=split(tem.r,r-l+1);
_T va=value(tem1.l);
merge(merge(tem.l,tem1.l),tem1.r);
return va;
}
void add(int l,int r,int x)
{
Pair tem=split(root,l-1),tem1=split(tem.r,r-l+1);
tem1.l->k+=x;tem1.l->ad+=x;pushdown(tem1.l);
pushup(tem1.l);
merge(merge(tem.l,tem1.l),tem1.r);
}
void jump(int l,int r)
{
Pair tem=split(root,l-1),tem1=split(tem.r,r-l+1);
tem1.l->fl^=1;
merge(merge(tem.l,tem1.l),tem1.r);
}
void rev(int l,int r,int k)
{
k%=r-l+1;
if(!k)return;
Pair tem=split(root,l-1),tem1=split(tem.r,r-l+1),tem2=split(tem1.l,(r-k)-l+1);
merge(merge(tem.l,merge(tem2.r,tem2.l)),tem1.r);
}
};
FHQ_treap_map<int,int>tr;
char str[10];
int main()
{
int s=read,x,y;
for(int i=0;i++^s;)tr._insert(i-1,x,x=read);
for(int m=read;m--;)
{
scanf("%s",str);
switch(*str)
{
case 'A':x=read,y=read,tr.add(x,y,read);break;
case 'R':
{
if(*(str+3)=='E')x=read,tr.jump(x,read);
else x=read,y=read,tr.rev(x,y,read);
break;
}
case 'I':x=read,tr._insert(x,y,y=read);break;
case 'D':tr._del(read);break;
default:x=read,printf("%d\n",tr.query(x,read));
}
}
return 0;
}

SuperMemo的更多相关文章

  1. Supermemo背单词7周年纪念

    从2007年2月1日开始,用Supermemo背单词7周年了,在2013年11月21日将单词表Reset,重新开始Review以前背过的单词,并慢慢加入听写VOA时遇到的生词.

  2. poj 3580 SuperMemo

    题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...

  3. BZOJ1895: Pku3580 supermemo

    1895: Pku3580 supermemo Time Limit: 15 Sec  Memory Limit: 64 MBSubmit: 77  Solved: 47[Submit][Status ...

  4. 【POJ3580】【splay版】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  5. 【POJ3580】【块状链表】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  6. 平衡树(Splay):Splaytree POJ 3580 SuperMemo

    SuperMemo         Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...

  7. POJ3580 SuperMemo

    Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to pl ...

  8. POJ 3580 - SuperMemo - [伸展树splay]

    题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...

  9. POJ 3580 SuperMemo (splay tree)

    SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6841   Accepted: 2268 Case Ti ...

  10. 【BZOJ1895】Pku3580 supermemo Splay

    [BZOJ1895]Pku3580 supermemo Description 给出一个初始序列fA1;A2;:::Ang,要求你编写程序支持如下操作: 1. ADDxyD:给子序列fAx:::Ayg ...

随机推荐

  1. C#中的Json序列化

    核心思想: 利用nuget下载“Newtonsoft.Json”包,调用其中的方法可以json化各种对象.反序列化调用“JsonConvert.DeserializeObject<DataTab ...

  2. LearnOpenGL笔记(1)搭建环境

    之前有写过Unity Shader,但不过是东拼西凑,对其中的原理可以说完全不清楚,现在准备好好从opengl开始,学习这基础又重要的内容. LearnOpenGL CN是一个超超超炒鸡好的openG ...

  3. 北理工机器人队RM视觉组学习参考汇总(持续更新中)

    欢迎大家有意加入北理工机器人队参与到视觉组的工作中.在大家能够正式作为队员参与到视觉组的准备工作之前,北理机器人队需要对各位进行培训.这篇文章主要面向有志于参加机器人队视觉组的同学.同时,欢迎所有对相 ...

  4. 基于vue+springboot+docker网站搭建【一】 前言

      前言 开一个系列记录下一次从0开始搭建一个网站的过程.前后端项目都是在github找的开源项目,主要用于练习部署. 前端:vue.js 后端: spring-boot 搭建环境:centOS7.6 ...

  5. http 307重定向

    刚才在做hexo页面优化,发现了本地测试返回http 307.以前没见过这个响应码,于是做一下调研. 相关文章: hexo页面优化 http 307 在rfc规范中,http 307 Temporar ...

  6. sql脚本来获取数据库中的所有表结构了

    sql脚本来获取数据库中的所有表结构了,代码如下: use AdventureWorks2008 go SELECT (case when a.colorder=1 then d.name else ...

  7. 搜索引擎框架之ElasticSearch基础详解(非原创)

    文章大纲 一.搜索引擎框架基础介绍二.ElasticSearch的简介三.ElasticSearch安装(Windows版本)四.ElasticSearch操作客户端工具--Kibana五.ES的常用 ...

  8. Semaphore的简介及应用场景

    Semaphore是一个计数信号量,常用于限制可以访问某些资源(物理或逻辑的)线程数目. 常用函数: 信号量的构造函数 非公平: public Semaphore(int permits);//per ...

  9. MySQL Backup--使用mysqldump依次备份所有数据库

    某些场景下需要将数据库分开备份,有些场景又需要将所有数据库合在一起备份,特此整理此备份脚本 #!/bin/bash ##======================================== ...

  10. Nginx 反向代理功能-实现Nginx tcp负载均衡

    Nginx 反向代理功能-实现Nginx tcp负载均衡 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.