[LOJ2865] P4899 [IOI2018] werewolf

P4899 [IOI2018] werewolf
LOJ Chen 2865. "IOI2018" werewolf , the first AC interactive question

kruskal reconstruction tree + chairman tree
In fact, if you know the algorithm of tree reconstruction, the difficulty lies in the chairman tree

It is customary to start labeling from \ (1 \), so the labeling in the following explanations starts from \ (1 \)
Start from \ (s \), only go to points \ (L, L + 1, cdots, n \), and the set of points you can go to is recorded as \ (V_1 \)
Starting from \ (e \), just walk \ (1,2,\cdots,R \), and the set of points you can walk to is recorded as \ (V_2 \)
Then, if \ (V_1\cap V_2 \neq \varnothing \), it means that there is a solution. We can change the form of human wolf at any point in the intersection

Step 1, find \ (V_1,V_2 \)
Consider kruskal to reconstruct the tree, go first Here I don't know if I read the more detailed explanation. I won't start with the content of refactoring tree here
Building two refactoring trees

  • \(A \) is A reconstruction with \ (\ max(u,v) \) as the weight and the minimum spanning tree. It can be known that the minimum value of the maximum number of points in the path of \ (u,v \) is the point weight of \ (lca(u,v) \) in the reconstruction tree
  • \(B \) is a reconstruction with \ (\ min(u,v) \) as the weight and the maximum spanning tree. It can be known that in \ (u,v \) two-point path, the maximum value of the minimum number of passing points is the point weight of \ (lca(u,v) \) in the reconstruction tree

Therefore, due to the general nature of kruskal's reconstruction tree, it can also be known that for the path between \ (A \) tree and A point \ (u \), the node whose minimum value of the maximum number of the passing points is less than or equal to \ (x \), that is, all leaf nodes in the subtree whose path from \ (u \) to the root has the lowest depth and whose weight is less than or equal to \ (x \) as the root
It can be used to practice the determination of sentence components
Then \ (x=R \), the point set thus obtained is \ (V_2 \)
Similarly, \ (B \) tree can be used to find \ (V_1 \) in a similar way

Step 2: find whether the intersection of \ (V_1,V_2 \) is not empty
Using the chairman tree, you can actually use the tree array offline, but you don't understand how to do it

In fact, the chairman tree can be understood as the prefix sum of the line tree. We rank these points (\ (2n-1 \) in the order of dfs in the \ (A \) tree, and build the chairman tree in the order of dfs in the \ (B \) tree
Then we set the two points (the root of the subtree) found by the reconstruction tree to be \ (a,b \), and since the dfs order is continuous in a subtree, whether there is a number in \ (dfn_b\cdots dfn_b+size_b-1 \) per query \ (dfn_a\cdots dfn_a+size_a-1 \)
It's not hard

#define reg register
#define EN std::puts("")
#define LL long long
inline int read(){
	register int x=0;register int y=1;
	register char c=std::getchar();
	while(c<'0'||c>'9'){if(c=='-') y=0;c=std::getchar();}
	return y?x:-x;
#define N 400006
#define M 400006
int n,m;
struct edge{
	int u,v;
inline int get_max(int x,int y){return x>y?x:y;}
inline int get_min(int x,int y){return x<y?x:y;}
struct TREE{
	int up[N*2],vertex;
	int son[2][N];
	int fa[19][N],size[N],dfn[N],dfscnt;
	int val[N];
	inline int find(int k){
		return k==up[k]?k:up[k]=find(up[k]);
	static inline int cmp_A(edge aa,edge aaa){return get_max(aa.u,aa.v)<get_max(aaa.u,aaa.v);}
	static inline int cmp_B(edge aa,edge aaa){return get_min(aa.u,aa.v)>get_min(aaa.u,aaa.v);}
	inline void build_A(){
		for(reg int i=1;i<=2*n;i++) up[i]=i;
		for(reg int u,v,i=1,cnt=1;cnt<n;i++){
			if(u==v) continue;
	inline void build_B(){
		for(reg int i=1;i<=2*n;i++) up[i]=i;
		for(reg int u,v,i=1,cnt=1;cnt<n;i++){
			if(u==v) continue;
	void dfs(int u,int father){
		for(reg int i=1;i<19;i++) fa[i][u]=fa[i-1][fa[i-1][u]];
		if(!son[0][u]) return;
	inline int get_A(int u,int x){
		for(reg int i=18;~i;i--)if(val[fa[i][u]]<=x) u=fa[i][u];
		return u;
	inline int get_B(int u,int x){
		for(reg int i=18;~i;i--)if(val[fa[i][u]]>=x) u=fa[i][u];
		return u;
//Chairman tree part 
struct tr{
	tr *ls,*rs;
	int x;
int tot;
inline int cmp(int x,int y){return A.dfn[x]<A.dfn[y];}
void build(tr *tree,int l,int r){
	if(l==r) return;
	int mid=(l+r)>>1;
void insert(tr *last,tr *tree,int l,int r,int num,int k){
	if(l==r) return tree->x=last->x+k,void();
	int mid=(l+r)>>1;
int find(tr *left,tr *right,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return right->x-left->x;
	int mid=(l+r)>>1;
		if(find(left->ls,right->ls,l,mid,ql,qr)) return 1;
		if(find(left->rs,right->rs,mid+1,r,ql,qr)) return 1;
	return 0;
int main(){
	n=read();m=read();int q=read();
	for(reg int i=1;i<=m;i++){
	int tmp[n*2];
	for(reg int i=1;i<n*2;i++) tmp[i]=i;
	for(reg int i=0;i<2*n;i++) root[i]=&dizhi[tot++];
	for(reg int i=1;i<2*n;i++) insert(root[i-1],root[i],1,2*n-1,B.dfn[tmp[i]],tmp[i]<=n);
	reg int s,t,L,R;
		//Starting from s, can walk L to n, the minimum value is not less than L, using B tree
		//Starting from t, it can walk from 1 to R, and the maximum value is not greater than R, using A tree
		//Find intersection
		int tmpa=A.get_A(t,R),tmpb=B.get_B(s,L);
	return 0;

Tags: less

Posted on Tue, 05 May 2020 08:58:38 -0400 by tonbah