一部分TLE一部分MLE 0pts求助
查看原帖
一部分TLE一部分MLE 0pts求助
382274
暗影之梦楼主2022/5/20 23:21
#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long
using namespace std;
int n,m;
struct node
{
	int val,d,l,r,fa;
}zps[100001];
int merge(int x,int y)
{
//	cout<<x<<" "<<y<<endl;
	if(!x||!y)
	{
//		cout<<x<<" "<<y<<"xrh"<<" "<<(x|y)<<endl;
		return x|y;
	}
	if(zps[x].val<zps[y].val) swap(x,y);
	zps[x].r=merge(zps[x].r,y);
	if(zps[zps[x].r].d>zps[zps[x].l].d) swap(zps[zps[x].l],zps[zps[x].r]);
	zps[x].d=zps[zps[x].r].d+1;
	return x;
}
int find(int x)
{
	if(zps[x].fa==x) return x;
	return zps[x].fa=find(zps[x].fa);
}
signed main()
{
	while(scanf("%lld",&n))
	{
		memset(zps,0,sizeof(zps)); 
		for(int i=1;i<=n;i++) scanf("%lld",&zps[i].val),zps[i].fa=i;
		scanf("%lld",&m);
		for(int i=1;i<=m;i++)
		{
			int x,y;
			scanf("%lld%lld",&x,&y);
			x=find(x);
			y=find(y);
			if(x==y) 
			{
				printf("-1\n");
				continue;
			}
//			cout<<x<<" "<<y<<endl;
            int nwx=zps[zps[x].l].fa=zps[zps[x].r].fa=merge(zps[x].l,zps[x].r);
//            cout<<nwx<<endl;
            zps[x].l=zps[x].r=0;
            zps[x].d=0;
            zps[x].val/=2;
            int xnw=zps[nwx].fa=zps[x].fa=merge(x,nwx);
//            cout<<xnw<<endl;
            int nwy=zps[zps[y].l].fa=zps[zps[y].r].fa=merge(zps[y].l,zps[y].r);
//            cout<<nwy<<endl;
            zps[y].l=zps[y].r=0;
            zps[y].d=0;
            zps[y].val/=2;
            int ynw=zps[nwy].fa=zps[y].fa=merge(y,nwy);
//            cout<<ynw<<endl;
//			cout<<endl;
//			cout<<merge(xnw,ynw)<<endl;
            int ans=zps[xnw].fa=zps[ynw].fa=merge(xnw,ynw);
//            cout<<"hjcgn "<<ans<<endl;
            printf("%lld\n",zps[ans].val);
		}
	}
	return 0;
}
2022/5/20 23:21
加载中...