求助数据结构题
查看原帖
求助数据结构题
132533
FutaRimeWoawaSete楼主2021/12/26 10:30

为什么还是会挂啊/ll

拍子已经拍不出锅了,已经改到看不出什么问题。

#include "bits/stdc++.h"
using namespace std;
const int Len = 2e5 + 5 , Inf = 1e9 + 1;
int n,m,head[Len],cnt,fa[Len],col[Len],val[Len];
struct node
{
	int next,to;
}edge[Len << 1];
void add(int from,int to)
{
	edge[++ cnt].to = to;
	edge[cnt].next = head[from];
	head[from] = cnt;
}
void dfs(int x,int f)
{
	fa[x] = f;
	for(int e = head[x] ; e ; e = edge[e].next)
	{
		int to = edge[e].to;
		if(to == f) continue;
		dfs(to , x);
	}
}
struct SSplay
{
	int ch[2],f,rev,pre;
	multiset<int> emp;
	int maxn;
	SSplay(){ch[0] = ch[1] = f = rev = pre = 0 , maxn = -Inf;emp.clear();}
};
multiset<int>::reverse_iterator it;
struct LLCT
{
	SSplay t[Len];int tot;
	void push_up(int x)
	{
		t[x].pre = t[x].ch[0] ? t[t[x].ch[0]].pre : x;
		t[x].maxn = max(t[t[x].ch[0]].maxn , max(t[t[x].ch[1]].maxn , val[x]));
		if(!t[x].emp.size()) return;
		it = t[x].emp.rbegin();
		t[x].maxn = max(t[x].maxn , *it);
	}
	void push_rev(int x)
	{
		if(!x) return;
		swap(t[x].ch[0] , t[x].ch[1]) , t[x].rev ^= 1;
	}
	void push_down(int x)
	{
		if(t[x].rev)
		{
			push_rev(t[x].ch[0]);
			push_rev(t[x].ch[1]);
			t[x].rev = 0;
		}
	}
	int idf(int x)
	{
		if(!t[x].f) return -1;
		if(t[t[x].f].ch[0] == x) return 0;
		if(t[t[x].f].ch[1] == x) return 1;
		return -1;
	}
	void llcon(int son,int x,int op)
	{
		if(op == 0) t[x].ch[0] = son;
		if(op == 1) t[x].ch[1] = son;
		t[son].f = x;
	}
	void rotate(int x)
	{
		int y = t[x].f , z = t[y].f , opx = idf(x) , opy = idf(y) , u = t[x].ch[opx ^ 1];
		llcon(u , y , opx);
		llcon(y , x , opx ^ 1);
		llcon(x , z , opy);
		push_up(y) , push_up(x);
	}
	void push_all(int x)
	{
		if(idf(x) != -1) push_all(t[x].f);
		push_down(x); 
	}
	void Splay(int x)
	{
		push_all(x);
		while(idf(x) != -1)
		{
			int y = t[x].f;
			if(idf(y) == -1) rotate(x);
			else
			{
				if(idf(y) == idf(x)) rotate(y) , rotate(x);
				else rotate(x) , rotate(x);
			}
		}
	}
	void access(int x)
	{
		int lst = 0 , z;
		while(x)
		{
			Splay(x) , z = t[x].ch[1];
			if(lst) t[x].emp.erase(t[lst].maxn);
			//if(x == 1) 
			//{
				//it = t[x].emp.rbegin();
				//printf("!!!!!%d\n",*it);
			//}
			if(z) t[x].emp.insert(t[z].maxn);
			t[x].ch[1] = lst;
			push_up(x);
			//printf("%d %d %d %d %d %d %d ",x,t[x].maxn,lst,z,t[z].maxn,t[x].ch[0],t[x].ch[1]);
			//it = t[x].emp.rbegin();
			//printf("%d\n",*it);
			lst = x;
			x = t[x].f;
		}
	}
	int findroot(int x)
	{
		access(x);
		Splay(x);
		int u = x;
		while(t[u].ch[0]) u = t[u].ch[0];
		return u;
	}
	void link(int x,int y)
	{
		access(y) , Splay(y);
		access(x) , Splay(x);
		t[x].ch[1] = y , t[y].f = x;
		push_up(y) , push_up(x);
	}
	void cut(int x,int y)
	{
		access(y) , Splay(x);
		t[x].ch[1] = 0 , t[y].f = 0;
		push_up(y) , push_up(x);
	}
	int ask(int x)
	{
		int rt = findroot(x);
		//it = t[1].emp.rbegin();
		access(rt);Splay(rt);access(x);
		return t[t[rt].ch[1]].maxn;
	}
	void upd(int x)
	{
		push_up(x);
		access(x);
		Splay(x);
		push_up(x);
	}
	/*void Print(int x)
	{
		printf("!!!%d\n",x);
		if(t[x].ch[0]) Print(t[x].ch[0]);
		if(t[x].ch[1]) Print(t[x].ch[1]);	
	}*/
}LCT[2];
int main()
{
	scanf("%d",&n);
	for(int i = 1 ; i < n ; i ++)
	{
		int x,y;scanf("%d %d",&x,&y);
		add(x , y) , add(y , x);
	}
	add(n + 1 , 1);
	dfs(n + 1 , 0);
	for(int i = 1 ; i <= n ; i ++) scanf("%d",&col[i]);
	for(int i = 1 ; i <= n ; i ++)
	{
		int w;scanf("%d",&w);
		val[i] = w;
		LCT[0].push_up(i);
		LCT[1].push_up(i);
	}
	val[0] = val[n + 1] = -Inf;
	LCT[0].push_up(n + 1) , LCT[1].push_up(n + 1);
	LCT[0].push_up(0) , LCT[1].push_up(0);
	for(int i = 1 ; i <= n ; i ++) LCT[col[i]].link(fa[i] , i);
	scanf("%d",&m);
	int opt,x,w;
	for(int i = 1 ; i <= m ; i ++)
	{
		scanf("%d %d",&opt,&x);
		if(opt == 0) printf("%d\n",LCT[col[x]].ask(x));
		else if(opt == 1)
		{
			LCT[col[x]].cut(fa[x] , x);
			col[x] ^= 1;
			LCT[col[x]].link(fa[x] , x);
			//printf("??%d %d %d %d ",1,LCT[col[1]].t[1].maxn,LCT[col[1]].t[1].ch[0],LCT[col[1]].t[1].ch[1]);
			//it = LCT[col[1]].t[1].emp.rbegin();
			//printf("%d\n",*it);
		}
		else if(opt == 2)
		{
			scanf("%d",&w);
			val[x] = w;
			LCT[0].upd(x);
			LCT[1].upd(x);
		}
	}
	return 0;
}
2021/12/26 10:30
加载中...