您的位置:首页 > 其它

POJ 3580 SuperMemo

2017-04-10 21:17 204 查看
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
这就是一道splay模板题,不过把各种操作综合在一起时,还是很麻烦啦。
比如说加法标记,就要像线段树一样,先更新了自己,再下传给儿子,更新他们。
虽说这样,还是拍了一晚上,qwqqq

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=200005;
const int inf=1e9+7;
int n,m,rt,cnt,a
,val
,ch
[2],fa
,sz
,mn
,add
,rev
;
void pushup(int x)//¸üÐÂsize
{
sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
mn[x]=val[x];
if(ch[x][0])
mn[x]=min(mn[x],mn[ch[x][0]]);
if(ch[x][1])
mn[x]=min(mn[x],mn[ch[x][1]]);
}
void pushdown(int x)//±ê¼ÇÏ´«
{
if(add[x])
{
if(ch[x][0])
add[ch[x][0]]+=add[x],mn[ch[x][0]]+=add[x],val[ch[x][0]]+=add[x];
if(ch[x][1])
add[ch[x][1]]+=add[x],mn[ch[x][1]]+=add[x],val[ch[x][1]]+=add[x];
add[x]=0;
}
if(rev[x])
{
rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
}
int build(int l,int r,int f)//½¨Ê÷
{
if(l>r)
return 0;
int mid=(l+r)/2;
int x=++cnt;
val[x]=a[mid];
ch[x][0]=build(l,mid-1,x);
ch[x][1]=build(mid+1,r,x);
fa[x]=f;
pushup(x);
return x;
}
void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],lc=(ch[y][1]==x),rc=(lc^1);
if(y==k)
k=x;
else
ch[z][ch[z][1]==y]=x;
fa[x]=z,fa[y]=x,fa[ch[x][rc]]=y;
ch[y][lc]=ch[x][rc],ch[x][rc]=y;
pushup(y),pushup(x);
}
void splay(int x,int &k)//½«xÐýתµ½k
{
while(x!=k)
{
int y=fa[x],z=fa[y];
if(y!=k)
{
if((ch[y][0]==x)^(ch[z][0]==y))
rotate(x,k);
else
rotate(y,k);
}
rotate(x,k);
}
}
int fnd(int x,int rnk)//ÒÑÖªÅÅÃû£¬ÕÒ±àºÅ
{
pushdown(x);
int lc=ch[x][0],rc=ch[x][1];
if(rnk<=sz[lc])
return fnd(lc,rnk);
if(rnk>sz[lc]+1)
return fnd(rc,rnk-sz[lc]-1);
return x;
}
void split(int x,int y)//·ÖÀëÇø¼ä[x,y]
{
splay(x,rt),splay(y,ch[rt][1]);
}
int main()
{
scanf("%d",&n);
for(int i=2;i<=n+1;i++)
scanf("%d",&a[i]);
a[1]=a[n+2]=inf;
rt=build(1,n+2,0);
int x,y,z;
char s[11];
scanf("%d",&m);
while(m--)
{
scanf("%s",s);
if(s[0]=='A')//Çø¼ä¼Ó
{
scanf("%d%d%d",&x,&y,&z);
int p=fnd(rt,x),q=fnd(rt,y+2);
split(p,q);
add[ch[q][0]]+=z,mn[ch[q][0]]+=z,val[ch[q][0]]+=z;
}
if(s[0]=='R'&&s[3]=='E')//Çø¼ä·´×ª
{
scanf("%d%d",&x,&y);
int p=fnd(rt,x),q=fnd(rt,y+2);
split(p,q);
rev[ch[q][0]]^=1;
}
if(s[0]=='R'&&s[3]=='O')//Çø¼äÓÒÒÆ
{
scanf("%d%d%d",&x,&y,&z);
z%=(y-x+1);
int nr=y-z,sz=nr-x+1;//ÐÂÇø¼äµÄÓҶ˵ã
int p=fnd(rt,x),q=fnd(rt,nr+2);
split(p,q);
int t=ch[q][0];
ch[q][0]=0;
pushup(q),pushup(p);
p=fnd(rt,y+1-sz),q=fnd(rt,y+2-sz);
split(p,q);
ch[q][0]=t,fa[t]=q;
pushup(q),pushup(p);
}
if(s[0]=='I')//²åÈë
{
scanf("%d%d",&x,&y);
int p=fnd(rt,x+1),q=fnd(rt,x+2);
split(p,q);
z=++cnt;
val[z]=mn[z]=y;
fa[z]=q;
sz[z]=1;
ch[q][0]=z;
pushup(q),pushup(p);
}
if(s[0]=='D')//ɾ³ý
{
scanf("%d",&x);
int p=fnd(rt,x),q=fnd(rt,x+2);
split(p,q);
ch[q][0]=0;
pushup(q),pushup(p);
}
if(s[0]=='M')//²éѯ×îСֵ
{
scanf("%d%d",&x,&y);
int p=fnd(rt,x),q=fnd(rt,y+2);
split(p,q);
printf("%d\n",mn[ch[q][0]]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  平衡树