您的位置:首页 > 其它

Poj 3580-SuperMemo Splay

2016-03-15 17:12 501 查看
题目:http://poj.org/problem?id=3580

SuperMemo

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 13105Accepted: 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

POJ Founder Monthly Contest – 2008.04.13, Yao Jinyu

题意:给定一个序列,每次执行一个操作,对于每个min输出即可。
题解:
好个码农题。。。
Splay处理一下区间翻转,区间加上k,区间左右移动(循环序列),区间查询。。。
记得开long long。。。。。。
代码:

#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=0,fh=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');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+1;
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=1;
if(l<f)tree[last].left=now;
else tree[last].right=now;
}
LL mid=(l+r)/2;
now=mid;
Build(l,mid-1,mid);Build(mid+1,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]!=0)
{
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]=0;
}
if(rev[x]!=0)
{
rev[l]^=1;rev[r]^=1;rev[x]^=1;
swap(tree[x].left,tree[x].right);
}
}
LL Find(LL root,LL rank)
{
Pushdown(root);
if(tree[tree[root].left].size+1==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-1);
}
int main()
{
LL n,m,i,rt,SIZE,l,r,add,x,y,z,L,R,T,X,P;
char fh[8];
n=read();
tree[0].val=INF;a[0]=tree[0].mn=INF;
tree[1].val=INF;tree[n+2].val=INF;
tree[1].mn=INF;tree[n+2].mn=INF;
a[1]=INF;a[n+2]=INF;
for(i=2;i<=n+1;i++)a[i]=read(),tree[i].val=tree[i].mn=INF;
Build(1,n+2,0);
SIZE=n+2;rt=(1+n+2)/2;
m=read();
for(i=1;i<=m;i++)
{
scanf("\n%s",fh);
if(fh[0]=='A')
{
l=read();r=read();add=read();
x=Find(rt,l);y=Find(rt,r+2);
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[0]=='R')
{
if(fh[3]=='E')
{
l=read();r=read();
x=Find(rt,l);y=Find(rt,r+2);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
rev[z]^=1;
}
else
{
l=read();r=read();T=read();
L=l;R=r;
T=(T%(r-l+1)+(r-l+1))%(r-l+1);
if(T==0)continue;
l=r-T+1;
x=Find(rt,l);y=Find(rt,r+2);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
father[z]=0;tree[y].left=0;
Pushup(y);Pushup(x);
x=Find(rt,L);y=Find(rt,L+1);
Splay(x,rt);Splay(y,tree[x].right);
father[z]=y;tree[y].left=z;
Pushup(y);Pushup(x);
}
}
else if(fh[0]=='I')
{
X=read();P=read();
x=Find(rt,X+1);y=Find(rt,X+2);
Splay(x,rt);Splay(y,tree[x].right);
tree[y].left=++SIZE;tree[SIZE].val=P;
father[SIZE]=y;tree[SIZE].size=1;
tree[SIZE].mn=P;
Pushup(y);Pushup(x);
}
else if(fh[0]=='D')
{
X=read();
x=Find(rt,X);y=Find(rt,X+2);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;tree[y].left=0;
tree[z].size=0;father[z]=0;
//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+2);
Splay(x,rt);Splay(y,tree[x].right);
z=tree[y].left;
printf("%lld\n",tree[z].mn);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: