QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#88863#5439. Meet in the MiddleAppleblue17WA 113ms12792kbC++143.6kb2023-03-17 20:03:522023-03-17 20:03:54

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-17 20:03:54]
  • 评测
  • 测评结果:WA
  • 用时:113ms
  • 内存:12792kb
  • [2023-03-17 20:03:52]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define Pii pair<int,int>
const int N=1e5+5;
int n,q;
long long ans[N];

vector < pair<int,int> > G[N];
long long dep_[N];
int dfn_[N],dfout_[N],id_,mp_[N];
void dfs_(int u,int fa){
	dfn_[u]=++id_;
	mp_[id_]=u;
	for(pair<int,int> e: G[u]){
		int v=e.first,w=e.second;
		if(v==fa) continue;
		dep_[v]=dep_[u]+w;
		dfs_(v,u);
	}
	dfout_[u]=id_;
}


struct nod{
	int to,nxt,w;
}e[N*2];
int head[N],ecnt;

inline void add(int u,int v,int w){
	e[++ecnt]={v,head[u],w};
	head[u]=ecnt;
}

long long dep[N];
int log_[N*2],st[N*2][22];
int pos[N],id;
inline int getmin(int x,int y){
	if(dep[x]>dep[y]) return y;
	else return x;
}
void dfs0(int u,int fa){
	st[++id][0]=u;
	pos[u]=id;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==fa) continue;
		dep[v]=dep[u]+e[i].w;
		dfs0(v,u);
		st[++id][0]=u;
	}
}
inline int glca(int x,int y){
	int l=pos[x],r=pos[y];
	if(l>r) swap(l,r);
	int k=log_[r-l+1];
	return getmin(st[l][k],st[r-(1<<k)+1][k]);
}
inline long long gdis(int x,int y){
	return dep[x]+dep[y]-2*dep[glca(x,y)];
}

//////////////////////////////////////////////////////

Pii f[N*4];
long long g[N*4];
long long query(int l,int r,int o,int pos){
	if(l==r) return g[o];
	int mid=(l+r)>>1;
	if(pos<=mid) return g[o]+query(l,mid,o<<1,pos);
	else return g[o]+query(mid+1,r,o<<1|1,pos);
}
inline long long cal(Pii V){
	if(!V.first) return -1;
	return gdis(V.first,V.second)+query(1,n,1,dfn_[V.first])+query(1,n,1,dfn_[V.second]);
}
inline Pii gmax(Pii P,Pii Q){
	if(cal(P)<cal(Q)) return Q;
	else return P;
}

Pii upd(Pii P,Pii Q){
	if(!P.first || !Q.first) return {0,0};
	long long mx=-1;
	
	int A,B; long long dis;
	dis=gdis(P.first,Q.first);
	if(dis>mx) mx=dis,A=P.first,B=Q.first;
	dis=gdis(P.first,Q.second);
	if(dis>mx) mx=dis,A=P.first,B=Q.second;
	dis=gdis(P.second,Q.second);
	if(dis>mx) mx=dis,A=P.second,B=Q.second;
	dis=gdis(P.second,Q.first);
	if(dis>mx) mx=dis,A=P.second,B=Q.first;
	
	return {A,B};
}

void build(int l,int r,int o){
	if(l==r){
		int u=mp_[l];
		f[o]={u,u}; g[o]=dep_[u];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,o<<1);
	build(mid+1,r,o<<1|1);
	f[o]=gmax(upd(f[o<<1],f[o<<1|1]),gmax(f[o<<1],f[o<<1|1]));
}
void modify(int l,int r,int o,int L,int R,int x){
	if(L<=l && r<=R){
		g[o]+=x;
		return ;
	}
	int mid=(l+r)>>1;
	if(L<=mid) modify(l,mid,o<<1,L,R,x);
	if(R>mid) modify(mid+1,r,o<<1|1,L,R,x);
	f[o]=gmax(upd(f[o<<1],f[o<<1|1]),gmax(f[o<<1],f[o<<1|1]));
}

/////////////////////////////////////
vector< pair<int,int> > Q[N];

void dfs(int u,int fa){
	int x=f[1].first,y=f[1].second;
	for(pair<int,int> e: Q[u]){
		int v=e.first,num=e.second;
		ans[num]=max(cal({x,v}),cal({y,v}))-query(1,n,1,dfn_[v]);
	}
	
	for(pair<int,int> e: G[u]){
		int v=e.first,w=e.second;
		if(v==fa) continue;
		modify(1,n,1,1,n,w);
		modify(1,n,1,dfn_[v],dfout_[v],-2*w);
		dfs(v,u);
		modify(1,n,1,1,n,-w);
		modify(1,n,1,dfn_[v],dfout_[v],2*w);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>n>>q;
	for(int i=1;i<n;i++){
		int u,v,w; cin>>u>>v>>w;
		G[u].push_back({v,w});
		G[v].push_back({u,w});
	}
	dfs_(1,0);
	
	for(int i=1;i<n;i++){
		int u,v,w; cin>>u>>v>>w;
		add(u,v,w);
		add(v,u,w);
	}
	dfs0(1,0);
	
	for(int j=1;j<=20;j++)
		for(int i=1;i<=id-(1<<j)+1;i++)
			st[i][j]=getmin(st[i][j-1],st[i+(1<<(j-1))][j-1]);
	for(int i=1;i<=id;i++) log_[i]=log2(i);
	
	build(1,n,1);
	for(int i=1;i<=q;i++){
		int x,y; cin>>x>>y;
		Q[x].push_back({y,i});
	}
	dfs(1,0);
	
	for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 8172kb

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: 0ms
memory: 8096kb

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: 5ms
memory: 8140kb

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: 113ms
memory: 12792kb

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:

645502096611
626539793176
514273941704
634730104905
472546379596
643506627727
641537859258
573604504956
644081575470
803875451466
674370549986
734764046916
744862815441
763778393516
553499885160
526743824759
610373719514
689550247042
549161302822
726811438160
653134244120
666761991962
699239105739
6...

result:

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