1. 程式人生 > >洛谷4847 銀河英雄傳說(LCT+LCSPLAY)

洛谷4847 銀河英雄傳說(LCT+LCSPLAY)

題目連結

QWQ硬是把一個 s p l a y splay 好題,做成了 L C

T LCT

首先,根據題目性質,我們可以發現序列之間是具有前後性質的。

那麼,我們就不可以進行 m a k e r o

o t makeroot 等操作。

我們定義 f i n d r o

o t ( x ) x s p l a y findroot(x)表示x所在splay最前面的點(深度最小的點)
f i n d y m h ( x ) x s p l a y findymh(x)表示x所在splay最後面的點(深度最大的點)

對於1操作,我們只需要讓前面序列最後面的點,連線到後面序列最前面的點,然後 a c c e s s ( f i n d y m h ( x ) ) access(findymh(x))

對於2操作,我們直接 s p l a y splay 到根,然後斷開和左兒子的聯絡

對於 3 3 操作,我們可以分別splay到跟,然後計算 v a l 1   y v a l 1   x val_{1~y} -val_{1~x}

全程不要打亂順序QWQ

上程式碼

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define int long long

using namespace std;

inline int read()
{
  int x=0,f=1;char ch=getchar();
  while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
  while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
  return x*f;
}

const int maxn = 5e5+1e2;

int ch[maxn][3];
int fa[maxn],rev[maxn];
int n,m;
int st[maxn];
int sum[maxn];
int val[maxn];

int son(int x)
{
	if (ch[fa[x]][0]==x) return 0;
	else return 1;
}

bool notroot(int x)
{
	return ch[fa[x]][0]==x || ch[fa[x]][1]==x;
}

void update(int x)
{
	sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
}

void rotate(int x)
{
    int y=fa[x],z=fa[y];
    int b=son(x),c=son(y);
    if (notroot(y)) ch[z][c]=x;
    fa[x]=z;
    ch[y][b]=ch[x][!b];
    fa[ch[x][!b]]=y;
    ch[x][!b]=y;
    fa[y]=x;
    update(y);
    update(x);
}

void splay(int x)
{
	while (notroot(x))
	{
		//cout<<1<<endl;
		int y=fa[x],z=fa[y];
		int b=son(x),c=son(y);
		if (notroot(y))
		{
			if (b==c) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
	update(x);
}

void access(int x)
{
	for (int y=0;x;y=x,x=fa[x])
	{
		splay(x);
		ch[x][1]=y;
		update(x);
	}
}

int findroot(int x)
{
	splay(x);
	while (ch[x][0])
	{
		x=ch[x][0];
		//cout<<x<<endl;
	}
	return x;
}

int findymh(int x)
{
	splay(x);
	while (ch[x][1])
	{
		x=ch[x][1];
	}
	return x;
}

void link(int x,int y)
{
	int xx = findroot(x);
	int xxx = findymh(x);
	int yy = findymh(y);
  splay(xx);
  fa[xx]=yy;
  //ch[findymh(y)][1]=findroot(x);
  access(findymh(x));	
  //splay(findymh(x));
}

void cut(int x)
{
	splay(x);
	fa[ch[x][0]]=0;
	ch[x][0]=0;
	update(x);
}

signed main()
{
   n=read(),m=read();
   for (int i=1;i<=n;i++) val[i]=read();
   for (int i=1;i<=m;i++)
   {
      char s[10];
      int ans=0;
      scanf("%s",s+1);
      if (s[1]=='M')
      {
      	 int x=read(),y=read();
      	 if (findroot(x)==findroot(y)) continue;
      	 link(x,y);
	  }
	  if (s[1]=='D')
	  {
	  	int x=read();
	  	cut(x);
	  }
	  if (s[1]=='Q')
	  {
	  	 int x=read(),y=read();
	  	 if (findroot(x)!=findroot(y)) 
		   {
		   cout<<-1<<"\n";
		   continue;
		 }
	  	 int ymh = findroot(x);
	     splay(ymh);
	     splay(x);
	     ans=ans-sum[ch[x][0]];
	    // access(y);
	     splay(y);
	     ans=ans+sum[ch[y][0]]+val[y];
	     //ans=ans+val[x];
	     if (ans<=0)
	     {
	     	ans=0;
	     	 //splay(ymh1);
		    // access(y);
	         splay(y);
	         ans=ans-sum[ch[y][0]];
	         //access(x);
	         splay(x);
	         ans=ans+sum[ch[x][0]]+val[x];
	         //ans=ans+val[y];
		 }
		 splay(ymh);
		 //access(oo);
		 cout<<ans<<"\n";
	  }
   }
   return 0;
}