[CF414E]Mashmokh's Designed Problem

题意:给一棵树,有三个操作:①询问两点$(x,y)$之间的距离②把$x$和原来的父亲断开并连到它的$h$级祖先,作为新父亲最右的儿子③询问与根节点距离为$k$的点中最右的点是哪个点

用出栈入栈序$s_{1\cdots 2n}$来维护整棵树,入栈记$1$出栈记$-1$,那么一个节点$x$的深度就是$\sum\limits_{i=1}^{in_x}s_x$

每个平衡树节点记1.这个节点是出栈还是入栈2.子树和3.最大前缀和4.最小前缀和,那么我们就可以在平衡树上二分找到最右的深度为$d$的节点(注意如果找到的是出栈点应该返回父亲,因为有个$-1$)

对于操作①,把$(in_x,in_y)$提出来,那么这个区间内深度最小的节点就是$lca_{x,y}$

对于操作②,找到那个$h$级祖先,直接序列移动即可

对于操作③,直接找

为了使我的splay不残废就用splay写了一下

注意因为邻接表的性质,加边要倒着加

#include<stdio.h>
int ch[200010][2],fa[200010],v[200010],s[200010],mx[200010],mn[200010],h[100010],nex[100010],to[100010],pa[100010],tmp[100010],p[200010],M,rt;
void add(int a,int b){
	M++;
	to[M]=b;
	nex[M]=h[a];
	h[a]=M;
}
void dfs(int x){
	M++;
	p[M]=(x<<1)-1;
	v[(x<<1)-1]=1;
	for(int i=h[x];i;i=nex[i]){
		pa[to[i]]=x;
		dfs(to[i]);
	}
	M++;
	p[M]=x<<1;
	v[x<<1]=-1;
}
#define ls ch[x][0]
#define rs ch[x][1]
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
void pushup(int x){
	s[x]=s[ls]+s[rs]+v[x];
	mx[x]=max(mx[ls],s[ls]+v[x]+max(mx[rs],0));
	mn[x]=min(mn[ls],s[ls]+v[x]+min(mn[rs],0));
}
int build(int l,int r){
	int mid=(l+r)>>1;
	int&x=p[mid];
	if(l<mid){
		ls=build(l,mid-1);
		fa[ls]=x;
	}
	if(mid<r){
		rs=build(mid+1,r);
		fa[rs]=x;
	}
	pushup(x);
	return x;
}
void rot(int x){
	int y,z,f,B;
	y=fa[x];
	z=fa[y];
	f=(ch[y][0]==x);
	B=ch[x][f];
	fa[x]=z;
	fa[y]=x;
	if(B)fa[B]=y;
	ch[x][f]=y;
	ch[y][f^1]=B;
	if(ch[z][0]==y)ch[z][0]=x;
	if(ch[z][1]==y)ch[z][1]=x;
	pushup(y);
	pushup(x);
}
void splay(int x,int gl){
	int y,z;
	while(fa[x]!=gl){
		y=fa[x];
		z=fa[y];
		if(z!=gl)rot((ch[z][0]==y&&ch[y][0]==x)||(ch[z][1]==y&&ch[y][1]==x)?y:x);
		rot(x);
	}
}
int getdis(int x,int y){
	x=(x<<1)-1;
	y=(y<<1)-1;
	int dx,dy,dl;
	splay(x,0);
	dx=s[ls]+v[x];
	splay(y,0);
	dy=s[ch[y][0]]+v[y];
	splay(x,0);
	splay(y,x);
	rt=x;
	dl=min(dx,dy);
	if(ls==y)
		dl=min(dl,s[ch[y][0]]+v[y]+mn[ch[y][1]]);
	else
		dl=min(dl,s[ls]+v[x]+mn[ch[y][0]]);
	return dx+dy-(dl<<1);
}
int find(int x,int d){
	if(mx[rs]>=d-s[ls]-v[x]&&mn[rs]<=d-s[ls]-v[x])return find(rs,d-s[ls]-v[x]);
	if(s[ls]+v[x]==d)return(x&1)?(x+1)>>1:pa[x>>1];
	return find(ls,d);
}
int pre(int x){
	splay(x,0);
	for(x=ls;rs;x=rs);
	return x;
}
int nx(int x){
	splay(x,0);
	for(x=rs;ls;x=ls);
	return x;
}
void change(int u,int h){
	int x=(u<<1)-1,L,R,t;
	splay(x,0);
	pa[u]=find(ls,s[ls]+v[x]-h);
	L=pre(x);
	R=nx(u<<1);
	splay(L,0);
	splay(R,L);
	t=ch[R][0];
	ch[R][0]=0;
	pushup(R);
	pushup(L);
	L=pre(pa[u]<<1);
	R=(pa[u]<<1);
	splay(L,0);
	splay(R,L);
	ch[R][0]=t;
	fa[t]=R;
	pushup(R);
	pushup(L);
	rt=L;
}
#define inf 1000000000
int main(){
	mx[0]=-inf;
	mn[0]=inf;
	int n,m,i,x,y;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		scanf("%d",&y);
		for(x=1;x<=y;x++)scanf("%d",tmp+x);
		for(x=y;x>0;x--)add(i,tmp[x]);
	}
	M=0;
	dfs(1);
	rt=build(1,n<<1);
	while(m--){
		scanf("%d%d",&i,&x);
		if(i!=3)scanf("%d",&y);
		if(i==1)printf("%d\n",getdis(x,y));
		if(i==2)change(x,y);
		if(i==3)printf("%d\n",find(rt,x+1));
	}
}

[CF414E]Mashmokh's Designed Problem

原文地址:https://www.cnblogs.com/jefflyy/p/8352751.html

时间: 2024-10-09 01:29:50

[CF414E]Mashmokh's Designed Problem的相关文章

Mashmokh&#39;s Designed Problem 01生成树

题意 给一张无向连通图, 每条边可能是黑边或白边, 问是否存在一种生成树构造使得树上黑边数量 = 白边数量 ? $1\le n,m\le 100000$ . 分析 我们可以把 黑边 当做 权值为1的边 , 白边 当做 权值为0的边 . 这样如果存在一种生成树构造, 使得树上黑边数量 = 白边数量 , 意味着 $n$ 为奇数, 且生成树的边权之和为 $\frac{n-1}{2}$ . 问题转化为: 对于一张边权为 $0$ 或 $1$ 的无向连通图, 是否有一种生成树, 其边权之和恰好等于 $m$

@codeforces - [email&#160;protected] Mashmokh&#39;s Designed Problem

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 给定一棵 n 个点的树,每个点的儿子是有序的. 现给定 m 次操作,每次操作是下列三种中的一种: (1)给定 u, v,询问 u, v 之间的距离. (2)给定 v, h,断开 v 到父亲的边,将 v 这棵子树加入到它的第 h 个祖先的最后一个儿子. (3)给定 k,询问在当前这棵树上

生成树

[BZOJ 3534] 重建 题意 给定一张 n (1 < n <= 50) 个点 m 条边的无向图, 每条边有一定出现的概率, 问出现的边恰好形成一个生成树的概率. 实现 注意到对于出现概率为 1 的边, 分母可能为 0 . 我们对 d = 1 变换为 d = 1-EPS 就好了. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cctype>

HDOJ 2769 Disgruntled Judge 扩展GCD

扩展GCD: 枚举a,扩展GCD求b,再暴力检查 Disgruntled Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 329    Accepted Submission(s): 142 Problem Description Once upon a time, there was an nwerc judge with

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 4227 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC.

C#学习日志 day10 -------------- problem statement

Revision History Date Issue Description Author 15/May/2015 1.0 Finish most of the designed function. Only the Windows application is finished. litianpeng.yanwenxiongandyuxuehui 21/May/2015 V1.1 Finish all of the function on windows store and windowsp

HDU1086 You can Solve a Geometry Problem too(计算几何)

You can Solve a Geometry Problem too                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                         Problem Description Many geometry(几何)problems wer

HDU 4716 A Computer Graphics Problem(模拟啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 Problem Description In this problem we talk about the study of Computer Graphics. Of course, this is very, very hard. We have designed a new mobile phone, your task is to write a interface to displa