栈溢出求助
查看原帖
栈溢出求助
313082
Roden楼主2021/2/19 11:41

树剖dfs1时栈溢出了,还望大佬帮忙

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N=1e5+100,MAX_NLOG_N=20*MAX_N;
int N;

int head[MAX_N],nxt[MAX_N*2],to[MAX_N*2];
void addedge(int u,int v)
{
	static int total = 0;
	nxt[++total]=head[u];
	head[u]=total;
	to[total]=v;
}
int fa[MAX_N],depth[MAX_N],son[MAX_N],siz[MAX_N],seg[MAX_N],top[MAX_N];
void dfs1(int u)
{
	depth[u]=depth[fa[u]]+1;
	siz[u]=1;
	for(int e=head[u];e!=0;e=nxt[e])
	if(to[e]!=fa[u])
	{
		fa[to[e]]=u;
		dfs1(to[e]);
		siz[u]+=siz[to[e]];
		if(siz[son[u]]<siz[to[e]])
			son[u]=to[e];
	}
}
void dfs2(int u,int t)
{
	static int total = 0;
	top[u]=t;
	seg[u]=++total;
	if(!son[u])return;
	dfs2(son[u],t);
	for(int e=head[u];e!=0;e=nxt[e])
		if(to[e]!=son[u]&&to[e]!=fa[u])
			dfs2(to[e],to[e]);
}

int root[MAX_N],sum[MAX_NLOG_N],maxval[MAX_NLOG_N],ls[MAX_NLOG_N],rs[MAX_NLOG_N];
int updatepos,updateval;
void update(int &rt,int l,int r)
{
	static int total = 0;
	if (r - l < 1)return;
	if(l>updatepos||r<=updatepos)return;
	if(!rt)rt=++total;
	if(l==updatepos&&r==l+1){
		sum[rt]=maxval[rt]=updateval;
		return;
	}
	int mid=(l+r)>>1;
	update(ls[rt],l,mid);
	update(rs[rt],mid,r);
	sum[rt]=sum[ls[rt]]+sum[rs[rt]];
	maxval[rt]=max(maxval[ls[rt]],maxval[rs[rt]]);
}
int a,b;
int querysum(int rt,int l,int r)
{
	if(rt==0||r-l<1)return 0;
	if(a<=l&&r<=b)return sum[rt];
	int mid=(l+r)>>1;
	return querysum(ls[rt],l,mid)
		+querysum(rs[rt],mid,r);
}
int querymax(int rt,int l,int r)
{
	if(rt==0||r-l<1)return 0;
	if(a<=l&&r<=b)return maxval[rt];
	int mid=(l+r)>>1;
	return max(querymax(ls[rt],l,mid),querymax(rs[rt],mid,r));
}

int grade[MAX_N],color[MAX_N];
int querysum(int u,int v)
{
	int res=0,rt=root[color[u]];
	while(top[u]!=top[v])
	{
		if(depth[top[u]]<depth[top[v]])swap(u,v);
		a=seg[top[u]];b=seg[u]+1;
		res+=querysum(rt,1,N+1);
		u=fa[top[u]];
	}
	if(depth[u]>depth[v])swap(u,v);
	a=seg[u];b=seg[v]+1;
	res+=querysum(rt,1,N+1);
	return res;
}
int querymax(int u,int v)
{
	int res=0,rt=root[color[u]];
	while(top[u]!=top[v])
	{
		if(depth[top[u]]<depth[top[v]])swap(u,v);
		a=seg[top[u]];b=seg[u]+1;
		res=max(res,querymax(rt,1,N+1));
		u=fa[top[u]];
	}
	if(depth[u]>depth[v])swap(u,v);
	a=seg[u];b=seg[v]+1;
	res=max(res,querymax(rt,1,N+1));
	return res;
}

int readint()
{
	register int x=0;
	register char c=getchar();
	while(c<'-')c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
		  x=x*10+c-'0';
	return x;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("stdin.txt", "r", stdin);
	freopen("stdout.txt", "w", stdout);
#endif
	N=readint();int Q=readint();
	for(int i=1;i<=N;i++)
	{
		grade[i]=readint();
		color[i]=readint();
	}
	for(int i=1;i<N;i++)
	{
		int u=readint(),v=readint();
		addedge(u,v);
		addedge(v,u);
	}
	dfs1(1);
	dfs2(1,1);
	for(int i=1;i<=N;i++)
	{
		updatepos=seg[i];
		updateval=grade[i];
		update(root[color[i]],1,N+1);
	}
	while(Q--)
	{
		char qu[3];scanf("%s",qu);
		int x=readint(),y=readint();
		switch(qu[1])
		{
			case 'C':
				updatepos=seg[x];
				updateval=0;
				update(root[color[x]],1,N+1);
				color[x]=y;
				updateval=grade[x];
				update(root[color[x]],1,N+1);
				break;
			case 'W':
				updatepos=seg[x];
				grade[x]=updateval=y;
				update(root[color[x]],1,N+1);
				break;
			case 'S':
				printf("%d\n",querysum(x,y));
				break;
			case 'M':
				printf("%d\n",querymax(x,y));
				break;
		}
	}
	return 0;
}
2021/2/19 11:41
加载中...