QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87025#5439. Meet in the MiddleAFewSunsWA 5ms9480kbC++143.7kb2023-03-11 16:19:032023-03-11 16:19:06

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-11 16:19:06]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:9480kb
  • [2023-03-11 16:19:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
#define LC x<<1
#define RC x<<1|1
ll n,q,now,ans[100010][2];
ll tree[400040][2];
struct edge{
	ll nxt,to,w;
};
struct Tree{
	ll head[100010],cnt=0,dfn[100010],st[100010],ed[100010],tot=0,dep[100010];
	ll dfnn[200020],stt[200020],tott=0,minn[200020][18],lg[200020];
	edge e[200020];
	void add(ll u,ll v,ll ww){
		e[++cnt].nxt=head[u];
		e[cnt].to=v;
		e[cnt].w=ww;
		head[u]=cnt;
	}
	void dfs(ll fa,ll u){
		dfn[++tot]=u;
		st[u]=tot;
		dfnn[++tott]=u;
		stt[u]=tott;
		go(u){
			ll v=e[i].to;
			if(v==fa) continue;
			dep[v]=dep[u]+e[i].w;
			dfs(u,v);
			dfnn[++tott]=u;
		}
		ed[u]=tot;
	}
	void init(){
		dfs(0,1);
		lg[1]=0;
		fr(i,2,tott) lg[i]=lg[i>>1]+1;
		fr(i,1,tott) minn[i][0]=dep[dfnn[i]];
		fr(j,1,17) fr(i,1,tott-(1ll<<j)+1) minn[i][j]=min(minn[i][j-1],minn[i+(1ll<<(j-1))][j-1]);
	}
	il ll dis(ll x,ll y){
		ll l=stt[x],r=stt[y];
		if(l>r) swap(l,r);
		ll tmp=lg[r-l+1];
		return dep[x]+dep[y]-2*min(minn[l][tmp],minn[r-(1ll<<tmp)+1][tmp]);
	}
}T1,T2;
il ll calc(ll x,ll y){
	return T2.dis(x,y)+T1.dis(now,x)+T1.dis(now,y);
}
il void pushup(ll x){
	ll maxx=calc(tree[LC][0],tree[LC][1]);
	tree[x][0]=tree[LC][0];
	tree[x][1]=tree[LC][1];
	ll tmp=calc(tree[RC][0],tree[RC][1]);
	if(tmp>maxx){
		maxx=tmp;
		tree[x][0]=tree[RC][0];
		tree[x][1]=tree[RC][1];
	}
	fr(i,0,1){
		fr(j,0,1){
			tmp=calc(tree[LC][i],tree[RC][j]);
			if(tmp>maxx){
				maxx=tmp;
				tree[x][0]=tree[LC][i];
				tree[x][1]=tree[RC][j];
			}
		}
	}
}
void build(ll x,ll l,ll r){
	if(l==r){
		tree[x][0]=tree[x][1]=T1.dfn[l];
		return;
	}
	ll mid=(l+r)>>1;
	build(LC,l,mid);
	build(RC,mid+1,r);
	pushup(x);
}
void mdf(ll x,ll l,ll r,ll ql,ll qr){
	if(r<ql||l>qr||(ql<=l&&r<=qr)) return;
	ll mid=(l+r)>>1;
	mdf(LC,l,mid,ql,qr);
	mdf(RC,mid+1,r,ql,qr);
	pushup(x);
}
void dfs(ll fa,ll u){
	ans[u][0]=tree[1][0];
	ans[u][1]=tree[1][1];
	for(ll i=T1.head[u];i;i=T1.e[i].nxt){
		ll v=T1.e[i].to;
		if(v==fa) continue;
		now=v;
		mdf(1,1,n,T1.st[v],T1.ed[v]);
		dfs(u,v);
		now=u;
		mdf(1,1,n,T1.st[v],T1.ed[v]);
	}
}
int main(){
	n=read();
	q=read();
	fr(i,2,n){
		ll u=read(),v=read(),w=read();
		T1.add(u,v,w);
	}
	fr(i,2,n){
		ll u=read(),v=read(),w=read();
		T2.add(u,v,w);
	}
	T1.init();
	T2.init();
	now=1;
	build(1,1,n);
	dfs(0,1);
	while(q--){
		ll a=read(),b=read();
		writeln(max(T1.dis(a,ans[a][0])+T2.dis(b,ans[a][0]),T1.dis(a,ans[a][1])+T2.dis(b,ans[a][1])));
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9480kb

input:

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

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 1ms
memory: 7532kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 2ms
memory: 5408kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 6992kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st numbers differ - expected: '647838384844', found: '0'