【线段树 Splay】 hdu 1754 I Hate It

线段树
/*happywu
 * hdu 1754
 * 2014.8.14
 */
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=200000+10;
struct Node
{
	int key,Max;
}Tree[maxn*4];
int n,A[maxn],m,l,r,x;
char com=0;
inline void Push_Down(int k){}
inline int read(){
	static int r, sign;
	static char c;
	r = 0, sign = 1;
	do c = getchar(); while(c != '-' && (c < '0' || c > '9'));
	if( c == '-')sign = -1, c = getchar();
	while( c >= '0' && c <='9') r = r*10 +(int)(c - '0'), c = getchar();
	return sign * r;
}

inline int max(int x,int y)
{
	int m = (x-y) >> 31;
	return y & m | x& ~m;
}


inline void Update(int k)
{
	Tree[k].Max=max(Tree[k<<1].Max,Tree[(k<<1)|1].Max);
}
inline void buildtree(int k,int s,int t)
{
	if(s==t)
	{
		Tree[k].Max=Tree[k].key=A[s];
		return;
	}
	int mid=(s+t)>>1;
	buildtree(k<<1,s,mid);
	buildtree((k<<1)|1,mid+1,t);
	Update(k);
}
inline void Insert(int k,int s,int t)
{
	Push_Down(k);
	if(s==t)
	{
		Tree[k].Max=Tree[k].key=x;
		return ;
	}
	int mid=(s+t)>>1;
	if(l<=mid)Insert(k<<1,s,mid);
	else Insert((k<<1)|1,mid+1,t);
	Update(k);
}

inline int Query(int k,int s,int t)
{
	Push_Down(k);
	if(l<=s && t<=r)return Tree[k].Max;
	int mid=(s+t)>>1;
	int ans=0;
	if(l<=mid)ans=max(ans,Query(k<<1,s,mid));
	if(mid<r)ans=max(ans,Query((k<<1)|1,mid+1,t));
	return ans;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
	for(int i=1;i<=n;i++)
		A[i]=read();
	buildtree(1,1,n);
	com=0;
	for(int i=1;i<=m;i++)
	{
		while(com!='Q'&&com!='U')com=getchar();
		if(com=='Q')
		{
			l=read(),r=read();
			printf("%d\n",Query(1,1,n));
		}
		else
		{
			l=read(),x=read();
			Insert(1,1,n);
		}
		com=0;
	}
	}
	return 0;
}


Splay

/*happywu
 * 2014.8.15
 * hdu 1754
 */
#include<cstdio>
#include<iostream>
using namespace std;
const int maxn=2000000+10;

struct Node
{
	int key,Max,fa;
	int son[2];
}Tree[maxn];

int n,m,x,l,r,root,tot=0,A[maxn];
char com=0;

inline int read(){
	static int r, sign;
	static char c;
	r = 0, sign = 1;
	do c = getchar(); while(c != '-' && (c < '0' || c > '9'));
	if( c == '-')sign = -1, c = getchar();
	while( c >= '0' && c <='9') r = r*10 +(int)(c - '0'), c = getchar();
	return sign * r;
}

inline int max(int x,int y)
{
	int m = (x-y) >> 31;
	return y & m | x& ~m;
}

inline void Update(int x)
{
	Tree[x].Max=max(Tree[x].key,max(Tree[Tree[x].son[0]].Max,Tree[Tree[x].son[1]].Max));
}

inline void Push_Down(int k){}

inline void Rotate(int x,int c)
{
	int y=Tree[x].fa;
	Push_Down(y),Push_Down(x);
	Tree[y].son[!c]=Tree[x].son[c];
	if(Tree[x].son[c])Tree[Tree[x].son[c]].fa=y;
	Tree[x].fa=Tree[y].fa;
	if(Tree[y].fa)
		Tree[Tree[y].fa].son[Tree[Tree[y].fa].son[1]==y]=x;
	Tree[y].fa=x;Tree[x].son[c]=y;
	Update(y);Update(x);
	if(Tree[x].fa==0)root=x;
}

inline void Splay(int x,int f)
{
	Push_Down(x);
	while(Tree[x].fa!=f)
	{
		if(Tree[Tree[x].fa].fa==f)
			Rotate(x,Tree[Tree[x].fa].son[0]==x);
		else
		{
			int y=Tree[x].fa;
			int kind=Tree[Tree[y].fa].son[1]==y;
			if(Tree[y].son[kind]==x)
				Rotate(y,!kind) , Rotate(x,!kind);
			else
				Rotate(x,kind)  , Rotate(x,!kind);
		}
	}
	if(f==0)root=x;
	Update(x);
}

inline void Insert(int val,int x)
{
	if(!x)
	{
		root=++tot;
		Tree[tot].Max=Tree[tot].key=val;
		Tree[tot].fa=0;
		return ;
	}
	if(!Tree[x].son[val>=Tree[x].key])
	{
		Tree[x].son[val>=Tree[x].key]=++tot;
		Tree[tot].key=val;Tree[tot].fa=x;
		Update(x);
		Splay(tot,0);
		return;
	}
	Insert(val,Tree[x].son[val>=Tree[x].key]);
	Update(x);
}
inline int Query(int l,int r)
{
	Splay(l-1,0);
	Splay(r+1,root);
	return Tree[Tree[r+1].son[0]].Max;
}
inline void Change(int pos,int val)
{
	Splay(pos,0);
	Tree[root].key=val;
	Update(root);
}
inline void Travel(int x)
{
    if(Tree[x].son[0])Travel(Tree[x].son[0]);
    printf("%d ",Tree[x].key);
    if(Tree[x].son[1])Travel(Tree[x].son[1]);
}
inline int NewNode(int pos,int val)
{
	Tree[pos].key=Tree[pos].Max=val;
	return pos;
}

inline int buildtree(int l,int r,int f)
{
	if(l>r)return 0;
	int mid=(l+r)>>1;
	int p=NewNode(mid,A[mid]);
	Tree[p].fa=f;
	Tree[p].son[0]=buildtree(l,mid-1,p);
	Tree[p].son[1]=buildtree(mid+1,r,p);
	Update(p);
	return p;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		root=tot=0;
		A[0]=0;
		for(int i=1;i<=n;i++)
		{
			A[i+1]=read();
		}
		A[n+2]=0;
		root=buildtree(1,n+2,0);
		for(int i=1;i<=m;i++)
		{
			while(com!='Q'&&com!='U')com=getchar();
			if(com=='Q')
			{
				l=read(),r=read();
				printf("%d\n",Query(l+1,r+1));
			}
			else
			{
			    l=read(),x=read();
				Change(l+1,x);
			}
			com=0;
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值