题目:http://poj/problem?id=3580
题意:
SuperMemo
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:
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 |
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
分析:Splay~
有个循环移位操作之前没见到过,其实用splay也很容易解决。首先将区间提取出来,然后将区间分成两部分,再然后把后区间接到前一个区间前面即可。。。
PS:本地测的时候好像有BUG,不过交上去也AC了。然后又不记得那组数据了。。。希望路过的大牛可以将bug指出来。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MI = ~0u>>1;
#define lson son[rt][0]
#define rson son[rt][1]
#define getPos son[son[root][1]][0]
const int maxn = 111111;
int son[maxn][2],fa[maxn],sz[maxn],val[maxn];
int flip[maxn]; //懒惰标记反转
int Min[maxn]; //区间最小值
int add[maxn]; //懒惰标记,区间需要添加的值
int root,cnt;
int a[maxn],n;
int newnode(int f,int v)
{
++cnt;
son[cnt][0]=son[cnt][1]=0;
fa[cnt]=f;
sz[cnt]=1;
val[cnt]=v;
flip[cnt]=0;
Min[cnt]=v;
add[cnt]=0;
return cnt;
}
void pushdown(int rt)
{
if(add[rt])
{
add[lson]+=add[rt];
add[rson]+=add[rt];
val[lson]+=add[rt];
val[rson]+=add[rt];
Min[lson]+=add[rt];
Min[rson]+=add[rt];
add[rt]=0;
}
if(flip[rt])
{
flip[lson]^=1;
flip[rson]^=1;
swap(lson,rson);
flip[rt]=0;
}
}
void pushup(int rt)
{
sz[rt]=sz[lson]+sz[rson]+1;
Min[rt]=val[rt];
if(lson) Min[rt]=min(Min[rt],Min[lson]);
if(rson) Min[rt]=min(Min[rt],Min[rson]);
}
void debug(int rt)
{
if(rt)
{
pushdown(rt);
debug(lson);
printf("node:%d val:%d Min:%d lson:%d rson:%d\n",rt,val[rt],Min[rt],lson,rson);
debug(rson);
}
}
void Rotate(int r,int kind)
{
int y=fa[r];
pushdown(y);
pushdown(r);
son[y][kind^1]=son[r][kind];
fa[son[r][kind]] = y;
if(fa[y]!=0)
son[fa[y]][son[fa[y]][1]==y] = r;
fa[r]=fa[y];
son[r][kind]=y;
fa[y]=r;
pushup(y);
}
void Splay(int r,int goal)
{
pushdown(r);
while(fa[r]!=goal)
{
if(goal==fa[fa[r]])
Rotate(r,son[fa[r]][0]==r);
else
{
int y=fa[r];
int kind=son[fa[y]][0]==y;
if(son[y][kind]==r)
{
Rotate(r,kind^1);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
if(0==goal)
root=r;
pushup(r);
}
void RotateTo(int k,int goal) //将代表位置k的节点移到goal下面
{
int rt=root;
while(rt)
{
pushdown(rt);
if(sz[lson]+1==k) break;
if(k>sz[lson]+1)
{
k-=(sz[lson]+1);
rt=rson;
}
else rt=lson;
}
Splay(rt,goal);
}
int get_pre(int rt)
{
pushdown(rt);
rt=lson;
pushdown(rt);
while(rson)
{
rt=rson;
pushdown(rt);
}
return rt;
}
int Findm(int pos)
{
int rt=root;
pushdown(rt);
while(rt)
{
if(sz[lson]+1==pos) return rt;
if(pos>sz[lson]+1)
{
pos-=(sz[lson]+1);
rt=rson;
}
else
rt=lson;
pushdown(rt);
}
return -1;
}
void ADD(int L,int R,int x) //operater 1
{
RotateTo(L,0);
RotateTo(R+2,root);
val[getPos]+=x;
Min[getPos]+=x;
add[getPos]+=x;
pushup(son[root][1]);
pushup(root);
}
void REVERSE(int L,int R) //operater 2
{
RotateTo(L,0);
RotateTo(R+2,root);
flip[getPos]^=1;
}
void REVOLVE(int L,int R,int times)
{
int len=R-L+1;
times%=len;
if(times==0) return ;
RotateTo(L,0);
RotateTo(R+2,root);
int leftNum=len-times;
RotateTo(L+leftNum,son[root][1]);
int mind=son[getPos][1];
son[getPos][1]=0;
pushup(getPos);
RotateTo(L+1,son[root][1]);
son[getPos][0]=mind;
fa[mind]=getPos;
pushup(getPos);
}
void INSERT(int pos,int v)
{
RotateTo(pos+1,0);
RotateTo(pos+1+1,root);
int r=newnode(son[root][1],v);
getPos=r;
pushup(son[root][1]);
pushup(root);
}
void DELETE(int pos)
{
int r=Findm(pos);
Splay(r,0);
if(son[r][0] && son[r][1])
{
Splay(get_pre(root),root);
int lch=son[r][0];
son[lch][1]=son[r][1];
fa[son[r][1]]=lch;
root=lch;
}
else if(!son[r][0])
root=son[r][1];
else
root=son[r][0];
fa[root]=0;
pushup(root);
}
void MIN(int L,int R)
{
RotateTo(L,0);
RotateTo(R+2,root);
printf("%d\n",Min[getPos]);
}
void buildSplay(int l,int r,int &rt,int fa)
{
if(l>r) return ;
int mid=l+r>>1;
rt=newnode(fa,a[mid]);
buildSplay(l,mid-1,lson,rt);
buildSplay(mid+1,r,rson,rt);
pushup(rt);
}
void Init()
{
cnt=root=0;
son[root][0]=son[root][1]=fa[root]=sz[root]=flip[root]=0;
Min[root]=MI;
root=newnode(0,MI);
son[root][1]=newnode(root,MI);
pushup(root);
buildSplay(1,n,getPos,son[root][1]);
pushup(son[root][1]);
pushup(root);
}
int main()
{
while(scanf("%d",&n)!=EOF && n>0)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
Init();
int m;
scanf("%d",&m);
while(m--)
{
char op[10];
int L,R,x;
scanf("%s",op);
if(op[0]=='A')
{
scanf("%d%d%d",&L,&R,&x);
ADD(L,R,x);
}
else if(op[0]=='R' && op[3]=='E')
{
scanf("%d%d",&L,&R);
REVERSE(L,R);
}
else if(op[0]=='R' && op[3]=='O')
{
scanf("%d%d%d",&L,&R,&x);
REVOLVE(L,R,x);
}
else if(op[0]=='I')
{
scanf("%d%d",&L,&x);
INSERT(L,x);
}
else if(op[0]=='D')
{
scanf("%d",&x);
DELETE(x+1);
}
else
{
scanf("%d%d",&L,&R);
MIN(L,R);
}
}
}
return 0;
}
更多推荐
POJ3580 SuperMemo (Splay)
发布评论