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 ...
随机推荐
- OC基础-day05
#pragma mark - Day05_01_NSObject类 NSObject类 1). NSObject是Foundation框架中的1个类. 在这个类中有1个类方法,叫做new 这个方法的作 ...
- 小白偶遇Sublime Text 3
sublime text3号称神一样的编辑器,主要归功于它丰富的插件所带来的可扩展性.以前曾经抱着玩一玩的心态下载了sublime ,没有插件的sublime 很快被我扔到一边.在用过很多的编辑器后, ...
- 浅谈iOS开发的协议(protocol)和代理(delegate)
协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...
- NSdate 时间格式
NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y 年 M 年 ...
- 【BZOJ3527】【FFT】力
[问题描述]给出n个数qi,给出Fj的定义如下:令Ei=Fi/qi.试求Ei.[输入格式]输入文件force.in包含一个整数n,接下来n行每行输入一个数,第i行表示qi.[输出格式]输出文件forc ...
- bootstrap中的动态加载出来的图片轮播中的li标签中的class="active"的动态添加移除
//该方法是在slide改变时立即触发该事件, $('#myCarousel').on('slide.bs.carousel', function () { $("#myCarousel o ...
- Android 下拉刷新控件Android-PullToRefresh
需要用到一个开源库 Android-PullToRefresh https://github.com/chrisbanes/Android-PullToRefresh ---------------- ...
- 【pyhton】import math与import cmath
import math与import cmath分别代表导入math模块和复数math模块 还有一种导入方式是 from math import sqrt 从math中单独导入sqrt 直接可以用sq ...
- Uninstall office15 click-to-run extensibility Component
Summary : Uninstall office15 click-to-run extensibility Component,How to resolve Uninstall office15 ...
- IIs工作原理
http://www.cnblogs.com/szhy222/archive/2008/07/14/1242576.html 问题: HTTP.SYS 的内置驱动程序 IIS 工作者进程