Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

提供一个Splay版AC代码、、大神勿喷、、效率有点低

Posted by 452181625 at 2014-10-21 08:03:36 on Problem 3468
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 101000
#define ll __int64

ll num[N];
struct SplayTree
{
	ll ch[N][2],pre[N],val[N],add[N],sz[N],sum[N];
	ll root,top;

	inline void AddNode(ll x,ll c)
	{
		if(x==0) return;
		val[x]+=c;
		add[x]+=c;
		sum[x]+=c*sz[x];
	}
	inline void PushUp(ll x)
	{
		if(x==0) return;
		sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
		sum[x]=val[x]+sum[ch[x][0]]+sum[ch[x][1]];
	}
	inline void PushDown(ll x)
	{
		if(x==0) return;
		if(add[x])
		{
			AddNode(ch[x][0],add[x]);
			AddNode(ch[x][1],add[x]);
			add[x]=0;
		}
	}
	inline void Rotate(ll x,ll c) 
	{
		ll y=pre[x];
		PushDown(y);
		PushDown(x);
		ch[y][!c]=ch[x][c];
		if(ch[x][c]) pre[ch[x][c]]=y;
		pre[x]=pre[y];
		if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
		ch[x][c]=y;
		pre[y]=x;
		PushUp(y);
		if(y==root) root=x;
	}
	inline void Splay(ll x,ll f)
	{ 
		PushDown(x);
		while(pre[x]!=f) 
		{
			PushDown(pre[pre[x]]); 
			PushDown(pre[x]);
			PushDown(x);
			if(pre[pre[x]]==f) Rotate(x,ch[pre[x]][0]==x);
			else
			{
				ll y=pre[x],z=pre[y];
				ll c=(ch[z][0]==y);
				if(ch[y][c]==x) Rotate(x,!c),Rotate(x,c);
				else Rotate(y,c),Rotate(x,c);
			}
		}
		PushUp(x);
		if(f==0) root=x;
	}
	inline void SplayKth(ll k,ll f)
	{ 
		ll x=root;
		k+=1;
		while(1)
		{
			PushDown(x);
			if(k==sz[ch[x][0]]+1) break;
			else if(k<=sz[ch[x][0]]) x=ch[x][0];
			else k-=sz[ch[x][0]]+1,x=ch[x][1];
		}
		Splay(x,f);
	}
	inline void NewNode(ll &x,ll c)
	{
		x=++top;
		ch[x][0]=ch[x][1]=pre[x]=0;
		sz[x]=1;
		add[x]=0;
		sum[x]=val[x]=c;
	}
	inline void Build(ll &x,ll l,ll r,ll f)
	{
		if(l>r) return;
		ll m=(l+r)>>1;
		NewNode(x,num[m]);
		Build(ch[x][0],l,m-1,x);
		Build(ch[x][1],m+1,r,x);
		pre[x]=f;
		PushUp(x);
	}
	inline void Init(ll n)
	{
		ch[0][0]=ch[0][1]=pre[0]=val[0]=add[0]=sz[0]=sum[0]=0;
		root=top=0;
		for(ll i=1;i<=n;i++) scanf("%I64d",&num[i]);
		Build(root,0,n+1,0);
	}
	inline void Add(ll a,ll b,ll c)
	{
		SplayKth(a-1,0);
		SplayKth(b+1,root);
		AddNode(ch[ch[root][1]][0],c);
	}
	inline void GetSum(ll a,ll b)
	{
		SplayKth(a-1,0);
		SplayKth(b+1,root);
		printf("%I64d\n",sum[ch[ch[root][1]][0]]);
	}

}t;
int main()
{
	ll n,m;
	while(scanf("%I64d%I64d",&n,&m)!=EOF)
	{
		t.Init(n);
		while(m--)
		{
			char op;
			ll a,b,c;
			scanf(" %c",&op);
			if(op=='Q')
			{
				scanf("%I64d%I64d",&a,&b);
				t.GetSum(a,b);
			}
			else 
			{
				scanf("%I64d%I64d%I64d",&a,&b,&c);
				t.Add(a,b,c);
			}
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator