Poj 3580-SuperMemo Splay
题目:http://poj.org/problem?id=3580
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 13105 | Accepted: 4104 | |
| 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
Source
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define MAXM 100010
#define INF 1e9
#define LL long long
struct node
{
LL left,right,mn,val,size;
}tree[MAXN+MAXM];
LL a[MAXN+MAXM],father[MAXN+MAXM],rev[MAXN+MAXM],tag[MAXN+MAXM];
LL read()
{
LL s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Pushup(LL x)
{
LL l=tree[x].left,r=tree[x].right;
tree[x].size=tree[l].size+tree[r].size+;
tree[x].mn=min(min(tree[l].mn,tree[r].mn),tree[x].val);
}
void Build(LL l,LL r,LL f)
{
if(l>r)return;
LL now=l,last=f;
if(l==r)
{
tree[now].val=tree[now].mn=a[l];father[now]=last;
tree[now].size=;
if(l<f)tree[last].left=now;
else tree[last].right=now;
}
LL mid=(l+r)/;
now=mid;
Build(l,mid-,mid);Build(mid+,r,mid);
father[now]=last;tree[now].val=a[mid];
Pushup(now);
if(mid<f)tree[last].left=now;
else tree[last].right=now;
}
/*void Pushup(int x)
{
int l=tree[x].left,r=tree[x].right;
tree[x].size=tree[l].size+tree[r].size+1;
tree[x].mn=min(min(tree[l].mn,tree[r].mn),tree[x].val);
}*/
void rotate(LL x,LL &root)
{
LL y=father[x],z=father[y];
if(y==root)root=x;
else
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
Pushup(y);Pushup(x);
}
void Splay(LL x,LL &root)
{
while(x!=root)
{
int y=father[x],z=father[y];
if(y!=root)
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x,root);
else rotate(y,root);
}
rotate(x,root);
}
}
void Pushdown(LL x)
{
LL l=tree[x].left,r=tree[x].right;
if(tag[x]!=)
{
tag[l]+=tag[x];tag[r]+=tag[x];
tree[l].val+=tag[x];tree[r].val+=tag[x];
tree[l].mn+=tag[x];tree[r].mn+=tag[x];
tag[x]=;
}
if(rev[x]!=)
{
rev[l]^=;rev[r]^=;rev[x]^=;
swap(tree[x].left,tree[x].right);
}
}
LL Find(LL root,LL rank)
{
Pushdown(root);
if(tree[tree[root].left].size+==rank)return root;
else if(rank<=tree[tree[root].left].size)return Find(tree[root].left,rank);
else return Find(tree[root].right,rank-tree[tree[root].left].size-);
}
int main()
{
LL n,m,i,rt,SIZE,l,r,add,x,y,z,L,R,T,X,P;
char fh[];
n=read();
tree[].val=INF;a[]=tree[].mn=INF;
tree[].val=INF;tree[n+].val=INF;
tree[].mn=INF;tree[n+].mn=INF;
a[]=INF;a[n+]=INF;
for(i=;i<=n+;i++)a[i]=read(),tree[i].val=tree[i].mn=INF;
Build(,n+,);
SIZE=n+;rt=(+n+)/;
m=read();
for(i=;i<=m;i++)
{
scanf("\n%s",fh);
if(fh[]=='A')
{
l=read();r=read();add=read();
x=Find(rt,l);y=Find(rt,r+);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
tag[z]+=add;tree[z].val+=add;tree[z].mn+=add;
}
else if(fh[]=='R')
{
if(fh[]=='E')
{
l=read();r=read();
x=Find(rt,l);y=Find(rt,r+);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
rev[z]^=;
}
else
{
l=read();r=read();T=read();
L=l;R=r;
T=(T%(r-l+)+(r-l+))%(r-l+);
if(T==)continue;
l=r-T+;
x=Find(rt,l);y=Find(rt,r+);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
father[z]=;tree[y].left=;
Pushup(y);Pushup(x);
x=Find(rt,L);y=Find(rt,L+);
Splay(x,rt);Splay(y,tree[x].right);
father[z]=y;tree[y].left=z;
Pushup(y);Pushup(x);
}
}
else if(fh[]=='I')
{
X=read();P=read();
x=Find(rt,X+);y=Find(rt,X+);
Splay(x,rt);Splay(y,tree[x].right);
tree[y].left=++SIZE;tree[SIZE].val=P;
father[SIZE]=y;tree[SIZE].size=;
tree[SIZE].mn=P;
Pushup(y);Pushup(x);
}
else if(fh[]=='D')
{
X=read();
x=Find(rt,X);y=Find(rt,X+);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;tree[y].left=;
tree[z].size=;father[z]=;
//tree[SIZE].val=INF;tree[SIZE].mn=INF;
Pushup(y);Pushup(x);
}
else
{
l=read();r=read();
x=Find(rt,l);y=Find(rt,r+);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
printf("%lld\n",tree[z].mn);
}
}
fclose(stdin);
fclose(stdout);
return ;
}
Poj 3580-SuperMemo Splay的更多相关文章
- poj 3580 SuperMemo
题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...
- POJ 3580 - SuperMemo - [伸展树splay]
题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...
- Splay树(多操作)——POJ 3580 SuperMemo
相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11309 Accept ...
- POJ 3580 SuperMemo (FHQ_Treap)
题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...
- POJ 3580 SuperMemo 伸展树
题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...
- POJ 3580:SuperMemo(Splay)
http://poj.org/problem?id=3580 题意:有6种操作,其中有两种之前没做过,就是Revolve操作和Min操作.Revolve一开始想着一个一个删一个一个插,觉得太暴力了,后 ...
- 【POJ 3580】SuperMemo Splay
题意 给定$n$个数,$m$个询问,每次在$[L,R]$区间加上一个数,或者反转一个区间$[L,R]$,或者循环右移区间$[L,R]$共$T$次,或者在第$x$个数后插入一个数$p$,或者删除第$x$ ...
- POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11384 Accepted: 3572 Case T ...
随机推荐
- js - SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data jquery-1.9.1.min.js:3:4315
FF中时不时报这个错, 就近段做项目来看, 一般是我通过 jquery获取form中的参数(或直接获取参数,并通过ajax进行异步请求的时候,如果有错,就抱该错误! 而对应的, 如果在 Google ...
- c语言中文件相关操作
一 .首先介绍一下数据文件的类型: 1.二进制文件(映像文件):在内存中以二进制形式存取. 2.文本文件(ascii文件):以ascii码形式存取的文件. 通俗的讲,在Mac下,你把一个文件丢进记事本 ...
- 函数还能这样玩儿~实现类似add(1)(2)(3)的函数
人生的第一份前端工作找到了,感谢大神主子们给半路出家自学的我这么多的机会,很高兴正式踏上客观又乐趣满满的程序员之路,哇咔咔咔. 分享一个准备面试时遇到的一个有趣的问题: 要求实现类似add(1)( ...
- [学习笔记]设计模式之Facade
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Facade(外观)模式定义了一个高层接口,它能为子系统中的一组接口提供一个一致的界面,从而使得这一子系统更加容易使用.欢迎回到时の魔 ...
- html5 拖拽的简要介绍
1,首先,你要告诉计算机那个元素可以拖动,或者是一张图,或者是一个盒子,在标签里面加上 draggable="true" 2,然后,监听该元素被拖动的函数 ondragstart ...
- 转 C#开发微信门户及应用(1)--开始使用微信接口
微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为日常计划的重要事情之一了.本系列文章希望从一个循序渐进的角度上,全面介 ...
- gcc/g++命令认识
gcc & g++是gnu中最主要和最流行的c & c++编译器 . g++用来针对c++的处理命令,以.cpp为后缀,对于c语言后缀名一般为.c.这时候命令换做gcc即可. 下面以T ...
- C语言中”#x“的含义
#x 的含义是给x添加“”,也就是说将字符常量.常量转换为字符串常量
- VS-FluentData 单元测试
1. 使用VS2013建立一个控制台工程: using System; using System.Collections.Generic; using System.Linq; using Syste ...
- deep learning framework(不同的深度学习框架)
常用的deep learning frameworks 基本转自:http://www.codeceo.com/article/10-open-source-framework.html 1. Caf ...