指针版P3690 【模板】Link Cut Tree (动态树)
题面
题解
鉴于数组版实在是太慢我用指针版重新写了一遍
代码基本是借鉴了lxl某道关于\(LCT\)的题
//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
inline int getop(){R char ch;while((ch=getc())>'9'||ch<'0');return ch-'0';}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=3e5+5;
struct node;typedef node* ptr;
inline void swap(R ptr &x,R ptr &y){R ptr t=x;x=y,y=t;}
struct node{
ptr lc,rc,fa;int s,v,r;
inline node();
inline void ppd(){swap(lc,rc),r^=1;}
inline void pd(){if(r)lc->ppd(),rc->ppd(),r=0;}
inline ptr upd(){return s=lc->s^rc->s^v,this;}
}e[N];
inline node::node(){fa=lc=rc=e;}
inline bool isrt(R ptr p){return p->fa->lc!=p&&p->fa->rc!=p;}
void rotate(ptr p){
ptr s=p->fa,t=s->fa;
if(!isrt(s))(t->lc==s?t->lc:t->rc)=p;
p->fa=t,s->fa=p;
if(s->lc==p)s->lc=p->rc,p->rc->fa=s,p->rc=s->upd();
else s->rc=p->lc,p->lc->fa=s,p->lc=s->upd();
}
void push(ptr p){if(!isrt(p))push(p->fa);p->pd();}
ptr splay(ptr p){
push(p);
while(!isrt(p)){
if(!isrt(p->fa))rotate(p->fa->lc==p^p->fa->fa->lc==p->fa?p:p->fa);
rotate(p);
}
return p->upd();
}
ptr exp(ptr p){
ptr s=e;
while(p!=e)splay(p)->rc=s,s=p->upd(),p=p->fa;
return s;
}
inline void makert(ptr p){exp(p),splay(p)->ppd();}
inline ptr split(ptr s,ptr t){return makert(s),exp(t);}
ptr findrt(ptr p){
exp(p),splay(p)->pd();
while(p->lc!=e)(p=p->lc)->pd();
return p;
}
void link(ptr s,ptr t){
makert(s);
if(findrt(t)!=s)s->fa=t;
//虚儿子接上之后根本不用upd
}
void cut(ptr s,ptr t){
makert(s);
if(findrt(t)==s&&s->fa==t&&s->rc==e)
s->fa=t->lc=e,t->upd();
}
int n,m,op,u,v;
int main(){
// freopen("testdata.in","r",stdin);
// freopen("testdata.out","w",stdout);
n=read(),m=read();
fp(i,1,n)(e+i)->v=read(),(e+i)->upd();
while(m--){
op=getop(),u=read(),v=read();
switch(op){
case 0:print(split(e+u,e+v)->s);break;
case 1:link(e+u,e+v);break;
case 2:cut(e+u,e+v);break;
case 3:splay(e+u),(e+u)->v=v,(e+u)->upd();break;
}
}
return Ot(),0;
}
指针版P3690 【模板】Link Cut Tree (动态树)的更多相关文章
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- 洛谷.3690.[模板]Link Cut Tree(动态树)
题目链接 LCT(良心总结) #include <cstdio> #include <cctype> #include <algorithm> #define gc ...
- 洛谷P3690 [模板] Link Cut Tree [LCT]
题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...
- Link Cut Tree 动态树 小结
动态树有些类似 树链剖分+并查集 的思想,是用splay维护的 lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链 动态树的操作核心是把你要把 修改/询问/... 等 ...
- 洛谷P3690 Link Cut Tree (动态树)
干脆整个LCT模板吧. 缺个链上修改和子树操作,链上修改的话join(u,v)然后把v splay到树根再打个标记就好. 至于子树操作...以后有空的话再学(咕咕咕警告) #include<bi ...
- LCT(link cut tree) 动态树
模板参考:https://blog.csdn.net/saramanda/article/details/55253627 综合各位大大博客后整理的模板: #include<iostream&g ...
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)
题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
随机推荐
- 前端开发之jQuery篇--选择器
主要内容: 1.jQuery简介 2.jQuery文件的引入 3.jQuery选择器 4.jQuery对象与DOM对象的转换 一.jQuery简介 1.介绍 jQuery是一个JavaScript库: ...
- solr 的edismax与dismax比较与分析
edismax支持boost函数与score相乘作为,而dismax只能使用bf作用效果是相加,所以在处理多个维度排序时,score其实也应该是其中一个维度 ,用相加的方式处理调整麻烦. 而disma ...
- jquery中的属性和样式设置
添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目 ...
- win8上部署.net4.0程序到iis
在win8.1上默认的iis版本为8.5版,不做任何配置回报3个错误, 一下是错误提示内容及解决方案 1>HTTP 错误 404.3 – Not Found由于扩展配置问题而无法提供您请求的页面 ...
- C#获取mp3文件时长、解决发布到服务器无法使用问题
首先引用COM组件:Microsoft Shell Controls And Automation,需要引用1.2版本的,1.0的会出问题. 这里需要注意DLL的属性Embed Interop Typ ...
- sql批量插入添加自动编号
使用: ROW_NUMBER() over(order by ID desc) insert into dbo.Aa(Name,Nums) select top 10 NickName,ROW_NUM ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...
- cakephp获取最后一条sql语句
.在app\config\core.php中设置Configure::write(); .页面上追加如下代码: $dbo = ConnectionManager::getDataSource('def ...
- Wap版
Wap版:又叫h5.M版.移动网页版: Mobile:存储wap版调用的接口
- 2016年,你读过的最好的IT技术书有哪几本?
def 程序员 原文 https://www.zhihu.com/question/54350343 陈硕 等 54 人赞同了该回答 1 知乎 陈硕大牛的 服务器多线程编程muduo 输的好不好可能更 ...