题目连接

http://poj.org/problem?id=3580

SuperMemo

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

6
1
7
8
5
2
9
8
ADD 1 3 2
INSERT 4 7
REVERSE 1 7
MIN 3 5
DEL 2
DEL 3
REVOLVE 1 5 3
MIN 1 3

Sample Output

5
3

伸展树模板题,几乎所有的操作都在里面了。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<set>
using std::set;
using std::min;
using std::pair;
using std::swap;
using std::vector;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1 << 17;
const int INF = ~0u >> 1;
int arr[N];
struct Node {
bool rev;
int dat, s, mval, delta;
Node *fa, *ch[2];
inline void set(int _dat, int _s, Node *p) {
rev = delta =0;
dat = mval = _dat, s = _s;
fa = ch[0] = ch[1] = p;
}
inline bool dir() const {
return this == fa->ch[0];
}
inline void link(Node *x, bool d) {
ch[d] = x, x->fa = this;
}
inline void push_up() {
s = ch[0]->s + ch[1]->s + 1;
mval = min(dat, min(ch[0]->mval, ch[1]->mval));
}
inline void update() {
if(!s) return;
rev ^= 1;
swap(ch[0], ch[1]);
}
inline void add(int v) {
if(!s) return;
delta += v;
mval += v;
dat += v;
}
inline void push_down() {
if(delta) {
ch[0]->add(delta);
ch[1]->add(delta);
delta = 0;
}
if(rev) {
ch[0]->update();
ch[1]->update();
rev ^= 1;
}
}
};
#define RRL root->ch[1]->ch[0]
struct SplayTree {
int top;
Node *root, *tail;
Node *null, stack[N], *pool[N >> 1];
inline void init(int n) {
top = 0, tail = &stack[0];
null = tail++;
null->set(INF, 0, NULL);
root = newNode(INF);
root->link(newNode(INF), 1);
Node *x = built(1, n);
root->ch[1]->link(x, 0);
root ->ch[1]->push_up();
root->push_up();
splay(x, null);
}
inline Node *newNode(int v) {
Node *p = !top ? tail++ : pool[--top];
p->set(v, 1, null);
return p;
}
inline Node *built(int l, int r) {
if(l > r) return null;
int mid = (l + r) >> 1;
Node *p = newNode(arr[mid]);
p->ch[0] = built(l, mid - 1);
if(p->ch[0]->s) p->ch[0]->fa = p;
p->ch[1] = built(mid + 1, r);
if(p->ch[1]->s) p->ch[1]->fa = p;
p->push_up();
return p;
}
inline void rotate(Node *&x, bool d) {
Node *y = x->fa;
y->push_down(), x->push_down();
y->ch[!d] = x->ch[d];
if(x->ch[d]->s) x->ch[d]->fa = y;
x->fa = y->fa;
if(y->fa->s) y->fa->ch[!y->dir()] = x;
x->ch[d] = y;
y->fa = x, y->push_up();
if(y == root) root = x;
}
inline void splay(Node *x, Node *f) {
for(;x->fa !=f ;x->push_down()) {
if(x->fa->fa == f) {
rotate(x, x->dir());
} else {
bool d = x->fa->dir();
if(d == x->dir()) rotate(x->fa, d), rotate(x, d);
else rotate(x, !d), rotate(x, d);
}
}
x->push_up();
}
inline Node *select(Node *x, int k) {
for(int t = 0; x->s; ) {
x->push_down();
t = x->ch[0]->s;
if(t == k) break;
if(k < t) x = x->ch[0];
else k -= t + 1, x = x->ch[1];
}
return x;
}
inline void get_range(int l, int r) {
splay(select(root, l - 1), null);
splay(select(root, r + 1), root);
}
inline void add(int l, int r, int v) {
get_range(l, r);
RRL->add(v);
splay(RRL, null);
}
inline void insert(int k, int v) {
splay(select(root, k), null);
splay(select(root, k + 1), root);
root->ch[1]->link(newNode(v), 0);
root->ch[1]->push_up();
root->push_up();
splay(RRL, null);
}
inline void reverse(int l, int r) {
get_range(l, r);
RRL->update();
}
inline void revolve(int l, int r, int k) {
int len = (r - l + 1);
k = (k % len + len) % len;
if(!k) return;
if(1 == k) {
erase(r);
insert(l - 1, pool[--top]->dat);
} else {
splay(select(root, r - k + 1), null);
splay(select(root, r + 1), root);
splay(select(root, l - 1), root);
splay(select(root, r), root->ch[1]);
Node *x = root->ch[0]->ch[1];
root->ch[0]->ch[1] = null;
root->ch[0]->push_up();
root->ch[1]->ch[0]->link(x, 1);
splay(x, null);
}
}
inline void erase(int k) {
splay(select(root, k), null);
Node *ret = root;
root = root->ch[1];
root->fa = null;
splay(select(root, 0), null);
root->ch[0] = ret->ch[0];
root->ch[0]->fa = root;
root->push_up();
pool[top++] = ret;
}
inline void query(int l, int r) {
get_range(l, r);
printf("%d\n", RRL->mval);
}
}spt;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
char buf[20];
int n, q, a, b, c;
while(~scanf("%d", &n)) {
rep(i, n) scanf("%d", &arr[i + 1]);
spt.init(n);
scanf("%d", &q);
while(q--) {
scanf("%s", buf);
if('D' == buf[2]) {
scanf("%d %d %d", &a, &b, &c);
spt.add(a, b, c);
} else if('S' == buf[2]) {
scanf("%d %d", &a, &b);
spt.insert(a, b);
} else if('V' == buf[2] && 'E' == buf[3]) {
scanf("%d %d", &a, &b);
spt.reverse(a, b);
} else if('V' == buf[2] && 'O' == buf[3]) {
scanf("%d %d %d", &a, &b, &c);
spt.revolve(a, b, c);
} else if('L' == buf[2]) {
scanf("%d", &a);
spt.erase(a);
} else if('N' == buf[2]) {
scanf("%d %d", &a, &b);
spt.query(a, b);
}
}
}
return 0;
}

poj 3580 SuperMemo的更多相关文章

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

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

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

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

  3. POJ 3580 SuperMemo (splay tree)

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

  4. Splay树(多操作)——POJ 3580 SuperMemo

    相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11309   Accept ...

  5. POJ 3580 SuperMemo (FHQ_Treap)

    题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...

  6. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  7. POJ 3580:SuperMemo(Splay)

    http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...

  8. 【POJ 3580】SuperMemo Splay

    题意 给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$ ...

  9. 【POJ 3580】 SuperMemo

    [题目链接] 点击打开链接 [算法] 本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉 本题还是 ...

随机推荐

  1. HTTP 500.22 错误解决

    打开网站对应的应用池-->高级设置-->托管管道模式改为classic

  2. C语言,不是从hello world开始

    开始看C语言,主要是复习,所以就没必要从hello world开始了,写点例子熟悉下就好了. 使用公式℃=(5/9)(℉-32)打印下列华氏温度与摄氏温度对照表: #include <stdio ...

  3. SQL函数——LENGTH()和LENGTHB()

    LENGTH()函数是比较简单同时也是非常有用的一个函数,在此小记一下,加深印象以备后用! 1:先上实验的例子——我新建了一张Student表,插入了若干条测试数据,如下图所示: 2:实验LENGTH ...

  4. js控制台调试

    在web编程的过程中js代码出现错误,可以通过console.log()将相关信息输入到控制台进行调试. 清空控制台右击选 Clear console 菜单,或者输入 clear() 都行 控制台相关 ...

  5. C#实现文件下载的几种方法

    //WriteFile实现下载 protected void Button2_Click(object sender, EventArgs e) { /* using System.IO; */ st ...

  6. Mongodb解决不能连接到服务器的错误

    注:这次解决的这个问题的前提是之前打开MongoDB之后,再次使用的时候无法连接了(使用mongod和mongo都不对) 闲话:遇到这种问题真是让人恼火,所以说句sun of beach,好了~爽 正 ...

  7. 【ILSpy反编译】C# 写的程序反编译查看是不是也太容易了点吧,太恐怖了。。。

    最近由于要写一些界面的东西,写了几个月c#(之前一直做c/c++项目),发现c#写界面很方便,效果也不错,在这个过程中也听说c#程序可以很容易被反编译到,但一直也没时间去自己反编译去试着看看,心想就算 ...

  8. 背景图片background-size兼容ie8以下浏览器解决

    背景图片不够大,然后就想到用background-size:100%; 测试浏览器的时候发现ie8以下不兼容,图片会自动填充平铺过去,然后出现背景不好看的现象.解决方法: background-ima ...

  9. shp地图解析(不用AE)

    AE太重型,还收费,如果只是加载地图作为底图,可以用纯C#实现.线类型用得最多,以下是线类型的数据结构: 总体架构 文件头 记录头 记录内容 记录头 记录内容 ............ 记录头 记录内 ...

  10. 二十二、OGNL的一些其他操作

    二十二.OGNL的一些其他操作 投影 ?判断满足条件 动作类代码: ^ $   public class Demo2Action extends ActionSupport {     public ...