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<<"\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;
}

最新文章

  1. CSS:CSS使用Tips
  2. 微信公众号开发之被逼学web服务端1-----使用SecureCRT连接Linux服务器
  3. ylbtech-Unitity-cs:计算阶乘值
  4. iOS 小知识 - #if , #ifdef , #ifndef.
  5. Jquery学习笔记:利用parent和parents方法获取父节点
  6. WPF和Expression Blend开发实例:一个样式实现的数字输入框
  7. 微信小程序-video详解
  8. 分针网——每日分享: jquery选择器的用法
  9. Oracle 11gR2 用exp无法导出空表解决方法
  10. vs2017和vs2019专业版和企业版
  11. 第一周——数据分析之表示 —— Numpy入门
  12. python框架django-web层
  13. python note 17 random、time、sys、os模块
  14. 概率DP求解例题
  15. redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: 断开的管道 (Write failed)
  16. 在centos7中安装python3并设置为默认版本
  17. JMeter:响应结果乱码解决方法
  18. prettier &amp; codes format
  19. 180714、JRebel插件安装配置与破解激活(多方案)详细教程
  20. (转)CSS3:nth-child()伪类选择器,奇偶数行自定义样式first-child

热门文章

  1. Go测试--子测试
  2. Python - 导入的位置
  3. python库--tensorflow
  4. 【第十九篇】- Maven NetBeans之Spring Cloud直播商城 b2b2c电子商务技术总结
  5. 安卓开发 利用百度识图api进行物体识别(java版)
  6. [源码解析] PyTorch 流水线并行实现 (1)--基础知识
  7. 队列,一种&quot;公平&quot;的数据结构
  8. 获取用户id的方法
  9. cas的基础配置
  10. 定要过python二级 选择题第5套