POJ3580 SuperMemo (Splay)

题目:http://poj.org/problem?id=3580

题意:

SuperMemo
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12596 Accepted: 3952
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, {A1A2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:

  1. 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}
  2. 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}
  3. REVOLVE x y T: rotate sub-sequence {Ax ... AyT times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
  4. 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}
  5. DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
  6. 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 (≤ 100000).

The following n lines describe the sequence.

Then follows 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

[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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值