QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#692655#5439. Meet in the Middlejinqihao2023TL 0ms0kbC++144.7kb2024-10-31 14:52:322024-10-31 14:52:32

Judging History

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

  • [2024-10-31 14:52:32]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-31 14:52:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5,M=5e5+5;
ll ans[M],d2[N],d1[N];
int val1[N];
int n,tot,T,num,sz[N],a[M],b[M],q,fa[N];
struct abc{int to,v,p;};
vector<abc>son1[N],son2[N],son[N];
bool vis[N];
int dfn[N],d[N],sign,fp[N];
vector<pair<int,int> >que[N];
int minn[19][N];
void dfs2(int x,int prt)
{
	sign++,dfn[x]=sign,fp[sign]=x;
	for(auto y:son2[x])if(y.to!=prt)fa[y.to]=x,d[y.to]=d[x]+1,d2[y.to]=d2[x]+y.v,dfs2(y.to,x);
}
int ask(int l,int r)
{
	int k=__lg(r-l+1);
	if(d[minn[k][l]]<d[minn[k][r-(1<<k)+1]])return minn[k][l];
	return minn[k][r-(1<<k)+1];
}
int lca(int x,int y)
{
	if(x==y)return x;if(dfn[x]>dfn[y])swap(x,y);
	return fa[ask(dfn[x]+1,dfn[y])];
}
ll gdis(int x,int y){return d2[x]+d2[y]-2*d2[lca(x,y)];}
void rebuild(int x,int prt)
{
	int st=tot;
	if(son1[x].size()>3)
	{
		for(auto y:son1[x])if(y.to!=prt)
		{
			tot++;
			if(tot>st+1)num++,son[tot-1].push_back((abc){tot,0,num}),son[tot].push_back((abc){tot-1,0,num}),val1[num]=0;
			else num++,son[x].push_back((abc){tot,0,num}),son[tot].push_back((abc){x,0,num}),val1[num]=0;
			num++,son[tot].push_back((abc){y.to,y.v,num}),son[y.to].push_back((abc){tot,y.v,num}),val1[num]=y.v;
		}
	}
	else for(auto y:son1[x])if(y.to!=prt)num++,son[x].push_back((abc){y.to,y.v,num}),son[y.to].push_back((abc){x,y.v,num}),val1[num]=y.v;
	for(auto y:son1[x])if(y.to!=prt)rebuild(y.to,x);
}
int nowv,rtu,rtv,rtp,fa1[N],fv1[N];
void pre(int x,int prt)
{
	nowv++;
	for(auto y:son[x])if(y.to!=prt && !vis[y.p])fa1[y.to]=x,fv1[y.to]=y.p,pre(y.to,x);
}
void get_rt(int x,int prt)
{
	sz[x]=1;
	for(auto y:son[x])if(y.to!=prt && !vis[y.p])get_rt(y.to,x),sz[x]+=sz[y.to];
	if(!rtu || max(sz[x],nowv-sz[x])<max(sz[rtu],nowv-sz[rtu]))rtu=x,rtv=fa1[x],rtp=fv1[x];
}
vector<int>ver,query;
void dfs1(int x,int prt)
{
	if(x<=n)ver.push_back(x);
	for(auto y:son[x])if(y.to!=prt && !vis[y.p])dfs1(y.to,x);
}
void dfs0(int x,int prt)
{
	for(auto y:son[x])if(y.to!=prt && !vis[y.p])d1[y.to]=d1[x]+y.v,dfs0(y.to,x);
}
void get_que(int x,int prt)
{
	for(auto i:que[x])query.push_back(i.second);
	for(auto y:son[x])if(y.to!=prt && !vis[y.p])get_que(y.to,x);
}
void slv()
{
	if(!ver.size() || !query.size())return ;
	int u=ver[0],v=ver[0],len=ver.size();
	ll nd=2*d1[u];
	for(int i=1;i<len;i++)
	{
		ll nd1=gdis(u,ver[i])+d1[ver[i]]+d1[u],nd2=gdis(v,ver[i])+d1[ver[i]]+d1[v];
		if(nd1>nd2)
		{
			if(nd1>nd)v=ver[i],nd=nd1;
		}
		else if(nd2>nd)u=ver[i],nd=nd2;
	}
	for(auto i:query)
	{
		ans[i]=max(ans[i],max(gdis(b[i],u)+d1[u]+d1[a[i]],gdis(b[i],v)+d1[v]+d1[a[i]]));
	}
}
void solve(int u,int v,int p)
{
	vis[p]=1;
	d1[u]=val1[p],d1[v]=0,dfs0(u,v),dfs0(v,u);
	ver.clear(),query.clear(),dfs1(u,v),get_que(v,u),slv();
	ver.clear(),query.clear(),dfs1(v,u),get_que(u,v),slv();
	nowv=0,rtu=0,rtv=0,rtp=0,pre(u,v),get_rt(u,v);if(nowv>1)solve(rtu,rtv,rtp);
	nowv=0,rtu=0,rtv=0,rtp=0,pre(v,u),get_rt(v,u);if(nowv>1)solve(rtu,rtv,rtp);
}
namespace IO {
    const int SIZE=(1<<21)+1;
    char ibuf[SIZE],*iS,*iT,obuf[SIZE],*oS=obuf,*oT=oS+SIZE-1,c,qu[55];int f, qr;
    #define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SIZE,stdin),(iS==iT?EOF:*iS++)):*iS++)
    inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
    inline void putc(char x){*oS++=x;if(oS==oT)flush();}
    template <class I>
    inline void read(I &x){
        for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
        for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
    }
    template <class I>
    inline void print(I x){
        if(!x)putc('0');if(x<0)putc('-'),x=-x;
        while(x)qu[++qr]=x%10+'0',x/=10;
        while(qr)putc(qu[qr --]);
    }
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using IO::read;
using IO::putc;
using IO::print;
void solve()
{
	read(n),tot=n,num=0,sign=0;
	for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),son1[x].push_back((abc){y,z,0}),son1[y].push_back({x,z,0});
	for(int i=1,x,y,z;i<n;i++)read(x),read(y),read(z),son2[x].push_back((abc){y,z,0}),son2[y].push_back({x,z,0});
	read(q);
	for(int i=1,x,y;i<=q;i++)read(x),read(y),que[x].push_back({y,i}),a[i]=x,b[i]=y;
	dfs2(1,0);
	for(int i=1;i<=n;i++)minn[0][i]=fp[i];
	for(int i=1;(1<<i)<=n;i++)for(int j=1;j+(1<<i)-1<=n;j++)
	{
		if(d[minn[i-1][j]]<d[minn[i-1][j+(1<<i-1)]])minn[i][j]=minn[i-1][j];
		else minn[i][j]=minn[i-1][j+(1<<i-1)];
	}

	rebuild(1,0);
	fa1[1]=0,fv1[1]=0,nowv=0,rtu=0,rtv=0,rtp=0,pre(1,0),get_rt(1,0);
	solve(rtu,rtv,rtp);
	for(int i=1;i<=q;i++)print(ans[i]),putc('\n');

	for(int i=1;i<=q;i++)ans[i]=0;
	for(int i=1;i<=n;i++)son1[i].clear(),son2[i].clear(),que[i].clear();
	for(int i=1;i<=tot;i++)son[i].clear();
	for(int i=1;i<=num;i++)vis[i]=0;
}
int main()
{
	read(T);
	while(T--)solve();
	return 0;
}

詳細信息

Test #1:

score: 0
Time Limit Exceeded

input:

3 4
1 2 1
2 3 2
1 2 2
2 3 1
1 1
1 2
2 1
2 2

output:


result: