• 洛谷4847 银河英雄传说(LCT+LCSPLAY)


    QWQ硬是把一个(splay)好题,做成了(LCT)

    首先,根据题目性质,我们可以发现序列之间是具有前后性质的。

    那么,我们就不可以进行(makeroot)等操作。

    我们定义(findroot(x)表示x所在splay最前面的点(深度最小的点))
    (findymh(x)表示x所在splay最后面的点(深度最大的点))

    对于1操作,我们只需要让前面序列最后面的点,连接到后面序列最前面的点,然后(access(findymh(x)))

    对于2操作,我们直接(splay)到根,然后断开和左儿子的联系

    对于(3)操作,我们可以分别splay到跟,然后计算(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<<"
    ";
    		   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<<"
    ";
    	  }
       }
       return 0;
    }
    
    
  • 相关阅读:
    phpExcel中文帮助手册之常用功能指南
    linux下使用远程图形界面
    分支限界法 0-1背包问题-优先队列式
    分支限界法 0-1背包问题-队列式
    回溯法 0-1背包问题
    贪心算法 哈夫曼树编码
    贪心算法 二.活动安排问题
    贪心算法 一.部分背包问题
    动态规划 四.0-1背包问题
    动态规划 三.最大子段和
  • 原文地址:https://www.cnblogs.com/yimmortal/p/10161923.html
Copyright © 2020-2023  润新知