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

Posted by aa2985759 at 2011-10-25 16:14:31 on Problem 3468
#include<stdio.h>
#include<string.h>
const int maxn=110000;

int ch[maxn][2],fa[maxn],num[maxn],lazy[maxn],root,key[maxn];
__int64 sum[maxn];

inline void push(int n)
{
	int l=ch[n][0],r=ch[n][1];
	lazy[l]+=lazy[n],lazy[r]+=lazy[n];
	sum[l]+=(num[l])*(__int64)lazy[n],sum[r]+=num[r]*(__int64)lazy[n];
	key[l]+=lazy[n],key[r]+=lazy[n];
	lazy[n]=0;
}

inline void flow(int n)
{
	num[n]=num[ch[n][0]]+num[ch[n][1]]+1;
	sum[n]=sum[ch[n][0]]+sum[ch[n][1]]+key[n];
}
inline void rotate(int n)
{
	int t=fa[n];
	bool isr=(ch[t][1]==n);

	ch[t][isr]=ch[n][!isr],fa[ch[n][!isr]]=t;
	fa[n]=fa[t],ch[fa[t]][ch[fa[t]][1]==t]=n;
	ch[n][!isr]=t,fa[t]=n;
	flow(t),flow(n);
}
void P(int n)
{
	if(fa[n])
		P(fa[n]);
	push(n);
}
inline void splay(int n,int goal)	
{
	int f,ff;
	P(n);
	while(fa[n]-goal)
	{
		f=fa[n],ff=fa[f];
		if(ff==goal)
			rotate(n);
		else if((ch[ff][1]==f)==(ch[f][1]==n))
			rotate(f),rotate(n);
		else
			rotate(n),rotate(n);
	}
	if(!goal)
		root=n;
}
void ADD(int a,int b,int c)
{
	splay(a-1,0);
	splay(b+1,root);
	push(root),push(ch[root][1]);
		
	int t=ch[ch[root][1]][0];
	lazy[t]+=c,sum[t]+=num[t]*c,key[t]+=c;
}

__int64 GET(int a,int b)
{
	splay(a-1,0);
	splay(b+1,root);
	push(root),push(ch[root][1]);

	return sum[ch[ch[root][1]][0]];
}
int main()
{
	int n,i,q,a,b,c;
	char s[10];

	scanf("%d%d",&n,&q);
	for(i=2;i<=n+1;i++)
	{
		scanf("%d",&key[i]);
		num[i]=i,sum[i]=sum[i-1]+key[i];
		ch[i][0]=i-1,fa[i-1]=i;
	}
	num[1]=1,num[n+2]=n+2;
	sum[n+2]=sum[n+1],ch[n+2][0]=n+1,fa[n+1]=n+2;
	root=n+2,fa[n+2]=0;

	while(q--)
	{
		scanf("%s%d%d",s,&a,&b);
		if(s[0]=='C')
			scanf("%d",&c),ADD(a+1,b+1,c);
		else
			printf("%I64d\n",GET(a+1,b+1));
	}
}

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