MnZn球跳玄关淀粉鼠
查看原帖
MnZn球跳玄关淀粉鼠
754310
Pepsee楼主2024/5/22 16:09

TLE on #8 #10 #11 不知如何卡常

#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
void write(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
int n,m,flag;
int head[100005],tot;
int fa[100005],col[100005];
struct node{int nex,to;} edge[100005<<1];
void add(int x,int y){edge[++tot] = (node){head[x],y}; head[x] = tot;}
//---------------------可删堆------------------
struct Heap
{
	priority_queue<int> ins,del;
	void insert(int v){ins.push(v);};
	void delet(int v){del.push(v);};
	int size(){return ins.size()-del.size();}
	void pop()
	{
		while (!ins.empty()&&!del.empty()&&ins.top()==del.top())
			ins.pop(),del.pop();
		ins.pop();
	}
	int top_first()
	{
		while (!ins.empty()&&!del.empty()&&ins.top()==del.top())
			ins.pop(),del.pop();
		return ins.empty()?0:ins.top();
	}
	int top_second()
	{
		if (size() < 2) return 0;
		int x = top_first(),res;
		pop();
		res = top_first();
		insert(x);
		return res;
	}
};
Heap heap1[100005],heap2[100005],heap_ans;
//---------------------树链剖分---------------------
namespace shiro
{
	int dep[100005],f[100005],siz[100005],son[100005],top[100005];
	void dfs1(int x,int Fa)
	{
		f[x] = Fa; dep[x] = dep[Fa]+1;
		siz[x] = 1;
		for (int i = head[x]; i; i = edge[i].nex)
		{
			int v = edge[i].to;
			if (v == Fa) continue;
			dfs1(v,x);
			siz[x] += siz[v];
			if (siz[son[x]] < siz[v])
				son[x] = v;
		}
	}
	void dfs2(int x,int tp)
	{
		top[x] = tp;
		if (son[x]) dfs2(son[x],tp);
		for (int i = head[x]; i; i = edge[i].nex)
		{
			int v = edge[i].to;
			if (v == son[x] || v == f[x])
				continue;
			dfs2(v,v);
		}
	}
	int get_dis(int x,int y)
	{
		int X = x,Y = y;
		while (top[x] != top[y])
		{
			if (dep[top[x]] < dep[top[y]])
				swap(x,y);
			x = f[top[x]];
		}
		x = dep[x]<dep[y]?x:y;
		return dep[X]+dep[Y]-(dep[x]<<1);
	}
	void init(){dfs1(1,0); dfs2(1,1);}
}
//---------------------BuildTree--------------------
namespace elia
{
	int vis[100005],siz[100005],son[100005];
	int Size,rt;
	void get_root(int x,int Fa)
	{
		siz[x] = 1; son[x] = 0;
		for (int i = head[x]; i; i = edge[i].nex)
		{
			int v = edge[i].to;
			if (v == Fa || vis[v])
				continue;
			get_root(v,x);
			siz[x] += siz[v];
			son[x] = max(son[x],siz[v]);
		}
		son[x] = max(son[x],Size-siz[x]);
		if (son[x] < son[rt]) rt = x;
	}
	void search(int x)
	{
		vis[x] = 1;
		for (int i = head[x]; i; i = edge[i].nex)
		{
			int v = edge[i].to;
			if (vis[v]) continue;
			rt = v; Size = siz[v];
			get_root(v,x);
			fa[rt] = x;
			search(rt);
		}
	}
	void init()
	{
		rt = 0;
		Size = son[0] = n;
		get_root(1,0);
		search(rt);
	}
}
void modify(int x)
{
	if (col[x])
	{
		if (heap1[x].size() > 1) heap_ans.delet(heap1[x].top_first()+heap1[x].top_second());
		heap1[x].insert(0);
		if (heap1[x].size() > 1) heap_ans.insert(heap1[x].top_first()+heap1[x].top_second());
	}
	else
	{
		if (heap1[x].size() > 1) heap_ans.delet(heap1[x].top_first()+heap1[x].top_second());
		heap1[x].delet(0);
		if (heap1[x].size() > 1) heap_ans.insert(heap1[x].top_first()+heap1[x].top_second());
	}
	for (int u = x; fa[u]; u = fa[u])
	{
		int dis = shiro::get_dis(x,fa[u]),nowtop = heap2[u].size()?heap2[u].top_first():-1;
		if (col[x])
		{
			heap2[u].insert(dis);
			if (heap1[fa[u]].size() > 1) heap_ans.delet(heap1[fa[u]].top_first()+heap1[fa[u]].top_second());
			if ((~nowtop) && heap1[fa[u]].size()) heap1[fa[u]].delet(nowtop);
			heap1[fa[u]].insert(heap2[u].top_first());
			if (heap1[fa[u]].size() > 1) heap_ans.insert(heap1[fa[u]].top_first()+heap1[fa[u]].top_second());
		}
		else
		{
			if (heap1[fa[u]].size() > 1) heap_ans.delet(heap1[fa[u]].top_first()+heap1[fa[u]].top_second());
			heap1[fa[u]].delet(heap2[u].top_first()); heap2[u].delet(dis);
			if (heap2[u].size()) heap1[fa[u]].insert(heap2[u].top_first());
			if (heap1[fa[u]].size() > 1) heap_ans.insert(heap1[fa[u]].top_first()+heap1[fa[u]].top_second());
		}
	}
	col[x] ^= 1;
	if (!col[x]) flag++;
	else flag--;
}
int main()
{
	n = read();
	for (int i = 1,u,v; i < n; i++)
		u = read(),v = read(),add(u,v),add(v,u);
	shiro::init(); elia::init();
	for (int i = 1; i <= n; i++)
		col[i] = 1,modify(i);
	m = read();
	char opt;
	int x;
	while (m--)
	{
		scanf("%s",&opt);
		if (opt == 'G')
		{
			if (flag <= 1) write(flag-1);
			else write(heap_ans.top_first());
			puts("");
		}
		else
			x = read(),modify(x);
	}
	return 0;
}
2024/5/22 16:09
加载中...