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 ...
随机推荐
- c语言学习之基础知识点介绍(一):输出语句和变量简单介绍
本系列是为了学习ios做准备的,也能作为c语言入门的教程看看. c语言的程序结构: 1.顺序结构:自上而下依次执行. 2.分支结构:程序有选择的执行某段代码或者不执行某段代码. 3.循环结构:程序循环 ...
- What's DB2 模式?
近期负责一个银行方面的项目,需要用到DB2实现多数据库版本切换.初步接触DB2,对于它的管理工具(IBM DATA STUDIO)虽然与ORACLE\MSSQL大同小异,但还是有些东西不一样的.比如什 ...
- mysql的sql分页函数limit使用
My sql数据库最简单,是利用mysql的LIMIT函数,LIMIT [offset,] rows从数据库表中M条记录开始检索N条记录的语句为: SELECT * FROM 表名称 LIMIT M, ...
- ios专题 - GCD(1)
什么是GCD? Grand Central Dispatch或者GCD,是一套低层API,提供了一种新的方法来进行并发程序编写.从基本功能上讲,GCD有点像 NSOperationQueue,他们都允 ...
- BitMap(比特位)
所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 腾讯面试的时候,让写了一个BitMap ...
- [Machine Learning] Probabilistic Graphical Models:一、Introduction and Overview(2、Factors)
一.什么是factors? 类似于function,将一个自变量空间投影到新空间.这个自变量空间叫做scope. 二.例子 如概率论中的联合分布,就是将不同变量值的组合映射到一个概率,概率和为1. 三 ...
- [Machine Learning] Probabilistic Graphical Models:一、Introduction and Overview(1、Overview and Motivation)
一.PGM用来做什么 1. 医学诊断:从各种病症分析病人得了什么病,该用什么手段治疗 2. 图像分割:从一张百万像素级的图片中分析每个像素点对应的是什么东西 两个共同点:(1)有非常多不同的输入变 ...
- mysql备份和恢复
摘自:http://safe.it168.com/a2009/1108/805/000000805490.shtml 要备份数据库" phpbb_db_backup " #mysq ...
- Windows下面对环境变量的操作
如何在cmd命令行中查看.修改.删除与添加环境变量:首先明确一点:所有的在cmd命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改.也就是说当关闭此cmd命令行窗口后,将不再起作用.永久性修改 ...
- ExecuteReader
最近在做winform的编程,想到一真没有使用过ExecuteReader.可能以前以后它的用户不大,或者 不大好用,故没有用过.今天在这里将学习记录写下来,供读者参考: 1.MSDN上说:Sends ...