QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198301#7103. Red Black TreeFlamireWA 0ms12112kbC++142.4kb2023-10-03 12:35:252023-10-03 12:35:25

Judging History

你现在查看的是最新测评结果

  • [2023-10-03 12:35:25]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:12112kb
  • [2023-10-03 12:35:25]
  • 提交

answer

#include <bits/stdc++.h>
#define N 100011
#define ll long long
using namespace std;
int n,m,q,fa[N][21],dfn[N],siz[N],dep[N],clk,rn[N],rred[N],k[N];bool red[N];ll wdep[N],w[N];
struct edge{int v,w,next;edge(){}edge(int _v,int _w,int _next){v=_v;w=_w;next=_next;}}e[N*2];int head[N],sz;
void init(){memset(head,-1,sizeof(head));sz=0;}void insert(int u,int v,int w){e[++sz]=edge(v,w,head[u]);head[u]=sz;}
void dfs0(int u,int f)
{
	fa[u][0]=f;for(int i=1;i<=20;++i)fa[u][i]=fa[fa[u][i-1]][i-1];
	for(int i=head[u];~i;i=e[i].next)if(e[i].v^f)dep[e[i].v]=dep[u]+1,wdep[e[i].v]=wdep[u]+e[i].w,dfs0(e[i].v,u);
}
void dfs(int u,int f,int rt)
{
	dfn[u]=++clk;siz[u]=1;rred[u]=rt;
	for(int i=head[u];~i;i=e[i].next)if(!red[e[i].v]&&e[i].v^f)dfs(e[i].v,u,rt),siz[u]+=siz[e[i].v];
}
struct sgt
{
	ll mx[N*4];
	void pushup(int x){mx[x]=max(mx[x<<1],mx[x<<1|1]);}
	void add(int k,ll p,int L=1,int R=n,int x=1)
	{
		if(L==R){mx[x]=p;return;}
		if(k<=L+R>>1)add(k,p,L,L+R>>1,x<<1);else add(k,p,(L+R>>1)+1,R,x<<1|1);pushup(x);
	}
	ll query(int l,int r,int L=1,int R=n,int x=1)
	{
		if(l>r)return 0;
		if(l<=L&&R<=r)return mx[x];ll ans=0;
		if(l<=L+R>>1)ans=max(ans,query(l,r,L,L+R>>1,x<<1));if(r>L+R>>1)ans=max(ans,query(l,r,(L+R>>1)+1,R,x<<1|1));return ans;
	}
}sgt1,sgt2;
pair<ll,ll> solve(int y)
{
	ll res1=sgt1.query(dfn[y],dfn[y]+siz[y]-1)-wdep[y];
	ll res2=max(sgt2.query(1,dfn[y]-1),sgt2.query(dfn[y]+siz[y],n));
	return make_pair(res1,res2);
}
int main()
{
	scanf("%d%d%d",&n,&m,&q);init();
	for(int i=1;i<=m;++i){int x;scanf("%d",&x);red[x]=1;rn[++rn[0]]=x;}
	for(int i=1;i<n;++i){int u,v,w;scanf("%d%d%d",&u,&v,&w);insert(u,v,w);insert(v,u,w);}
	dfs0(1,0);
	for(int i=1;i<=m;++i)dfs(rn[i],fa[rn[i]][0],rn[i]);
	for(int i=1;i<=n;++i)w[i]=wdep[i]-wdep[rred[i]];
	while(q--)
	{
		int kn;scanf("%d",&kn);
		for(int i=1;i<=kn;++i)scanf("%d",k+i),sgt1.add(dfn[k[i]],wdep[k[i]]),sgt2.add(dfn[k[i]],w[k[i]]);
		int id=0;w[0]=-1e9;
		for(int i=1;i<=kn;++i)if(w[k[i]]>w[id])id=k[i];
		ll ans=1e18;
		int cur=id;
		for(int i=20;~i;--i)if(dep[cur]-(1<<i)>dep[rred[id]])
		{
			int y=fa[cur][i];
			auto tmp=solve(y);
			if(tmp.first<=tmp.second)cur=y;
		}
		auto tmp=solve(cur);
		ans=min(ans,max(tmp.first,tmp.second));
		tmp=solve(fa[cur][0]);
		ans=min(ans,max(tmp.first,tmp.second));
		printf("%lld\n",ans);
		for(int i=1;i<=kn;++i)sgt1.add(dfn[k[i]],0),sgt2.add(dfn[k[i]],0);
	}return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 12112kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

0
0

result:

wrong answer 1st lines differ - expected: '4', found: '0'