首页 > 其他分享 >LCT板子

LCT板子

时间:2023-10-09 19:22:19浏览次数:24  
标签:LCT ch const int 板子 getchar define

//我坚信LCT可以平替树剖
#include<bits/stdc++.h>
#define ls t[o].ch[0]
#define rs t[o].ch[1]
#define int long long
using namespace std;

const int N=500010;
const int inf=1e9;

int read() {

    int x=0,f=1;
    char ch=getchar();
    while(ch<48||ch>57) {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>=48&&ch<=57) {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return x*f;

}

struct LCT {
	int ch[2],fa;
	int val,sum,tag;
	//other_tag,other_values...
}t[N];

bool is_rt(int o) {//判断是否为根节点
	int f=t[o].fa;
	if(t[f].ch[0]!=o&&t[f].ch[1]!=o) return true;
	else return false;
}

void rev(int o) {
	if(!o) return;
	swap(ls,rs);
	t[o].tag^=1;
}

void push_up(int o) {
	//...upd ls rs
	return ;
}

void push_down(int o) {
	//...other_tag
	if(!t[o].tag) return ;
	if(ls) rev(ls);
	if(rs) rev(rs);
	t[o].tag=0;
}

void push(int o) {//翻转整条链
	if(!is_rt(o)) push(t[o].fa);
	push_down(o);
}

void rotate(int o) {//rotate
	int f=t[o].fa;
	int g=t[f].fa;
	int k=t[f].ch[1]==o;
	if(!is_rt(f)) {
		t[g].ch[t[g].ch[1]==f]=o;
	}
	t[o].fa=g;
	t[f].ch[k]=t[o].ch[k^1];
	if(t[o].ch[k^1]) t[t[o].ch[k^1]].fa=f;
	t[f].fa=o;
	t[o].ch[k^1]=f;
	push_up(f);
}

void splay(int o) {//splay

	int f,g;
	push(o);
	while(!is_rt(o)) {
		f=t[o].fa;
		g=t[f].fa;
		if(!is_rt(f)) {
			if((t[g].ch[0]==f)^(t[f].ch[0]==o)) rotate(o);
			else rotate(f);
		}
		rotate(o);
	}
	push_up(o);

}

void access(int o) {//建立从原树根节点到x的一条实链
	for(int i=0;o;i=o,o=t[o].fa) {
		splay(o);
		rs=i;
		push_up(o);
	}
}

void make_rt(int o) {//使x成为原树的根
	access(o);
	splay(o);
	rev(o);//翻转ls与rs,维护中序遍历性质
}

void link(int x,int y) {//x,y之间连一条边
	make_rt(x);
	t[x].fa=y;
}

void split(int x,int y) {//建立从x-y的一条实链
	make_rt(x);
	access(y);
	splay(y);//便利cut操作
}

void cut(int x,int y) {//剪断边(x,y)
	split(x,y);
	if(t[y].ch[0]!=x||t[x].ch[1]) return ;//保证x,y有直接一条边链接
	t[y].ch[0]=t[x].fa=0;
	push_up(x);
}

int LCA(int x,int y) {//lca
	int lca;
	access(x);
	for(int i=0;y;i=y,y=t[y].fa) {
		splay(y);
		t[y].ch[1]=i;
		push_up(y);
		lca=y;
	}
	return lca;
}

int find_rt(int o) {//找到x在原树中的根节点
	access(o);
	splay(o);
	while(ls) {
		push_down(o);
		o=ls;
	}
	return o;
}

signed main() {


    return 0;
}

标签:LCT,ch,const,int,板子,getchar,define
From: https://www.cnblogs.com/Diamondan/p/17752950.html

相关文章

  • 多项式板子
    FFTconstdoublepi=acos(-1.0);intrev[N];voidFFT(complex<double>*a,intnr,intflag){for(inti=0;i<nr;i++){if(i<rev[i])swap(a[i],a[rev[i]]);}for(inti=1;i<nr;i<<=1){complex<double>wn(cos(p......
  • 【学习笔记】(13) 平衡树——记住不的板子
    TreapSplay无旋Treap——fhqtreap简介就是没有旋转操作的Treap,一些性质什么的都跟Treap类似。算法介绍(1)merge(x,y)将两棵“有序”(x中元素的权值最大值小于y中元素权值最小值)的Treap合并成一棵。intch[N][2],sz[N],pri[N],val[N];//val为权值,pri为优先级,sz......
  • 板子
    图论Tarjan求强连通分量intn,m,tot,top,cnt;intdfn[N],low[N];intq[N],ins[N],c[N];vector<int>eg[N],scc[N],neg[N];intcd[N];voidtarjan(intu){dfn[u]=low[u]=++tot;q[++top]=u,ins[u]=1;for(autox:eg[u]){if(!dfn[x]){......
  • 初中生都能看懂的 LCT 学习笔记
    初中生都能看懂的LCT学习笔记这篇文章偏向入门,旨在尽可能解决一类问题——动态树,主要讲述并且整理LCT算法及其一些变式。目前其变式例题作者还在整理之中,编者保证会把变式例题持续更新。0.前置知识splay。我可以猜测一下,你们可能看到splay,然后就可能去学了splay树,然......
  • 字符串哈希板子
    字符串哈希板子http://oj.daimayuan.top/course/7/problem/485单哈希#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+10;constintp=9999971,base=101;intn,m;chara[N],b[N];//字符串a,bintha[N],hb[N],c[N];//ha是a的哈希函数,hb是b的哈希......
  • LCT的简陋总结
    不想了解基础知识的可以直接从\(LCT\)基础操作部分开始,前面不是很重要目录\(LCT\)基础知识实链剖分辅助树一些性质\(LCT\)基础操作函数定义函数实现主要参考oi-wiki\(LCT\)基础知识树上操作是算法竞赛中重要的操作由于树的特殊性,使得维护一些子树信息和路径信息变得较为困......
  • CF70D Professor's task 题解 & 动态凸包板子
    CF70DProfessor'stask题解前言此篇题解用的是\(Andrew\),不想看这种做法的可以绕道。题意动态凸包板子题。维护动态凸包。两种操作,加一个点或查询一个点是否在凸包内。题解首先你得会静态二维凸包。维护二维凸包的方法挺多的,比如什么\(Andrew\)算法,\(Jarvis\)算法还......
  • 一点板子
    快读、关同步intread(){intf=1,x=0;charc=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}returnx*f;}ios::s......
  • 【树套树,LCT,出栈序】P4027 [NOI2007] 货币兑换
    其实是我Li-Chao-Tree哒!!考虑转移\(f_x=\minf_{anc}+(d_{x}-d_{anc})p_x+q_x\)其中\(anc\)为\(x\)的祖先,然后满足\(d_{anc}\geqd_{x}-li_{x})\)。考虑如果用权值线段树+带撤销的李超树可以维护\(li_{x}\)可以维护\(li_{x}<0\)的情况。但是这个题......
  • k8s安装etcd出现Job for etcd.service failed......"journalctl -xe" for details.
    错误如下先按照提示,输入journalctl-xe看一些详细信息1、针对:startrequestrepeatedtooquicklyforetcd.service错误,解决办法如下vi/usr/lib/systemd/system/etcd.service在[service]部分添加:RestartSec=5(参数作用:如果服务需要被重启,这个参数的值为服务被重启前的......