P4847 银河英雄传说V2 题解(Splay)
题目链接
解题思路
我天哪!!!\(splay\)在\(rotate\)的时候先\(upd(y)\)再\(upd(x)\)!!以后不能再因为这个\(WA\)一晚上了!!!
AC代码
#include<stdio.h>
#define ls t[x].s[0]
#define rs t[x].s[1]
#define rt t[0].s[1]
struct Splay{
int f,s[2];
long long sum,data;
}t[200010];
int tot,siz;
int id(int x){return x==t[t[x].f].s[1];}
int find(int x){while(t[x].f)x=t[x].f;return x;}
void upd(int x){t[x].sum=t[ls].sum+t[rs].sum+t[x].data;}
void connect(int x,int f,int son){if(!x&&!f)return;t[x].f=f;t[f].s[son]=x;}
void rotate(int x){
int y=t[x].f,r=t[y].f,idx=id(x),idy=id(y),b=t[x].s[idx^1];
connect(b,y,idx);connect(y,x,idx^1);connect(x,r,idy);
upd(y);upd(x);
}
void splay(int now,int to){
while(to!=t[now].f){
int up=t[now].f;
if(t[up].f==to)rotate(now);
else if(id(now)==id(up))rotate(up),rotate(now);
else rotate(now),rotate(now);
}
}
long long query(int x,int y){
if(x==y)return t[x].data;
if(find(x)!=find(y))return -1;
splay(x,0);splay(y,x);
return t[x].data+t[y].data+t[t[y].s[id(y)^1]].sum;
}
int main(){
int i,n,o,v,m;char a[5]={0};
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
scanf("%lld",&t[i].data);
for(i=0;i<m;i++){
scanf("%s%d",a,&o);
if(a[0]=='M'){
scanf("%d",&v);o=find(o);v=find(v);
int x=v;
if(o!=v){
while(rs)x=rs;
rs=o;
t[o].f=x;
splay(x,0);
}
}else if(a[0]=='Q'){
scanf("%d",&v);
printf("%lld\n",query(o,v));
}else{
splay(o,0);
t[t[o].s[0]].f=0;
t[o].s[0]=0;
upd(o);
}
}
return 0;
}
P4847 银河英雄传说V2 题解(Splay)的更多相关文章
- [洛谷P4847]银河英雄传说V2
题目大意:有$n(n\leqslant2\times10^5)$个序列,有$m(m\leqslant2\times10^5)$个操作,分三种: 1. $M\;x\;y:$把$x$所在的序列放在$y$所 ...
- NOI2002_ Galaxy银河英雄传说86
NOI2002_ Galaxy银河英雄传说86 公元五八○一年,地球居民迁移至金牛座α第二行星,:宇宙历七九九年,银河系的两大军事集团在巴米利恩星:杨威利擅长排兵布阵,巧妙运用各种战术屡次以少 ...
- NOI2002 银河英雄传说
P1196 银河英雄传说 367通过 1.1K提交 题目提供者该用户不存在 标签并查集NOI系列2001(或之前) 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 莱因哈特什么鬼? 私人代码 ...
- 【u010】银河英雄传说
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开 ...
- codevs1540 银河英雄传说
描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集 ...
- NOI2002 洛谷 P1196 银河英雄传说
神奇的并查集问题 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩 ...
- codevs 1540 银河英雄传说
题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米 ...
- 数据结构(并查集):COGS 260. [NOI2002] 银河英雄传说
260. [NOI2002] 银河英雄传说 ★★☆ 输入文件:galaxy.in 输出文件:galaxy.out 简单对比时间限制:5 s 内存限制:128 MB [问题描述] 公元五 ...
- CH4101 银河英雄传说
题意 4101 银河英雄传说 0x40「数据结构进阶」例题 描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七 ...
随机推荐
- test markdown && 代码高亮
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include ...
- 解决关闭ssh后网页停止服务的方法,利用nohup
上一篇文章提到宝塔面板无法运行,只能用ssh运行app.py. 关闭ssh时,app.py会被杀死.因为app.py的父进程就是ssh,关掉ssh会造成进程被杀死. 解决方法参考 https://w ...
- 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释
如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释 PIP $ pip install beauti ...
- macOS 需要更新软件才能连接到 iOS 设备
macOS 需要更新软件才能连接到 iOS 设备 更新 Mac 上的软件 如果您在 iPhone.iPad 或 iPod touch 上看到"需要更新软件才能连接到 iOS 设备" ...
- TypedArray & ArrayBuffer
TypedArray & ArrayBuffer Type Each element size in bytes Int8Array 1 Uint8Array 1 Uint8ClampedAr ...
- switchable css dark theme in js & html custom element
switchable css dark theme in js & html custom element dark theme / dark mode https://codepen.io/ ...
- flutter 1.5 in action
flutter 1.5 in action https://flutter.dev/docs/get-started/flutter-for/react-native-devs https://flu ...
- taro best practice
taro best practice 最佳实践 https://taro-docs.jd.com/taro/docs/best-practice.html#关于-jsx-支持程度补充说明 https: ...
- taro router
taro router https://nervjs.github.io/taro/docs/router.html bug import Taro, { Component, Config } fr ...
- Flutter: provider 使用小部件的小部件构建的依赖注入系统
文档 dependencies: provider: import 'package:dart_printf/dart_printf.dart'; import 'package:flutter/ma ...