QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#694850#5439. Meet in the MiddlezhichengRE 0ms0kbC++144.5kb2024-10-31 18:47:562024-10-31 18:48:04

Judging History

This is the latest submission verdict.

  • [2024-10-31 18:48:04]
  • Judged
  • Verdict: RE
  • Time: 0ms
  • Memory: 0kb
  • [2024-10-31 18:47:56]
  • Submitted

answer

#include<bits/stdc++.h>
namespace IO{
	inline char gc(){
	static const int Rlen=1<<18|1;static char buf[Rlen],*p1,*p2;
	return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
	}
	template<typename T>T get_integer(){
		char c;bool f=false;while(!isdigit(c=gc()))f=c=='-';T x=c^48;
		while(isdigit(c=gc()))x=((x+(x<<2))<<1)+(c^48);return f?-x:x;
	}
	inline int gi(){return get_integer<int>();}
	char obuf[(int)(2e7+7)],*oh=obuf;
	inline void prt(char c){*oh++=c;}
	inline void prt(const char *s){
		while(*s)*oh++=*s++;
	}
	template<typename T>void prt(T a){
		static char ch[23];int tl=0;
		if(a<0)*oh++='-',a=-a;
		do ch[++tl]=a%10;while(a/=10);
		while(tl)*oh++=ch[tl--]^48;
	}
	template<typename T,class ...Args>
	void prt(T a,Args...args){
		prt(a),prt(args...);
	}
	struct obuf_flusher{
		~obuf_flusher(){fwrite(obuf,1,oh-obuf,stdout);}
	}Flusher;
}
#define ll long long
using namespace std;
using namespace IO;
const int N=100010,M=500010;
int n,st[17][N];
ll ans[M];
vector<array<int,2> >d[N];
struct tree{
	int tot,cnt,first[N],nnext[N<<1],to[N<<1],w[N<<1],dfn[N],dep[N],f[N],rev[N],siz[N];
	ll dis[N];
	inline void clear(){
		tot=cnt=0;
		for(int i=1;i<=n;i++){
			first[i]=0;
		}
	}
	inline void add(int x,int y,int z){
		nnext[++tot]=first[x];
		first[x]=tot;
		to[tot]=y;
		w[tot]=z;
	}
	inline void dfs(int u,int fa){
		dfn[u]=++cnt;
		dep[u]=dep[fa]+1;
		f[u]=fa;
		rev[cnt]=u;
		siz[u]=1;
		for(int e=first[u];e;e=nnext[e]){
			if(to[e]!=fa){
				dis[to[e]]=dis[u]+w[e];
				dfs(to[e],u);
				siz[u]+=siz[to[e]];
			}
		}
	}
	inline int get(int x,int y){
		return dep[x]<dep[y]?x:y;
	}
	inline int LCA(int x,int y){
		if(x==y){
			return x;
		}
		x=dfn[x];
		y=dfn[y];
		if(x>y){
			swap(x,y);
		}
		x++;
		int k=__lg(y-x+1);
		return f[get(st[k][x],st[k][y-(1<<k)+1])];
	}
}p1,p2;
struct s{
	int pl,pr;
	ll disl,disr,dis,tag;
}p[N<<2];
inline ll calc(int x,int y){
	return p2.dis[x]+p2.dis[y]-2*p2.dis[p2.LCA(x,y)];
}
inline void upd(s &x,array<ll,4>y){
	ll now=y[2]+y[3]+calc(y[0],y[1]);
	if(now>x.dis){
		x.dis=now;
		x.pl=y[0];
		x.pr=y[1];
		x.disl=y[2];
		x.disr=y[3];
	}
}
inline void pushup(int rt){
	ll las=p[rt].tag;
	s &t1=p[rt<<1],&t2=p[rt<<1|1];
	if(t1.dis>t2.dis){
		p[rt]=t1;
	}
	else{
		p[rt]=t2;
	}
	p[rt].tag=las;
	upd(p[rt],{t1.pl,t2.pl,t1.disl,t2.disl});
	if(t1.pr){
		upd(p[rt],{t1.pr,t2.pl,t1.disr,t2.disl});
		if(t2.pr){
			upd(p[rt],{t1.pr,t2.pr,t1.disr,t2.disr});
		}
	}
	if(t2.pr){
		upd(p[rt],{t1.pl,t2.pr,t1.disl,t2.disr});
	}
}
inline void build(int l,int r,int rt){
	int mid=(l+r)>>1;
	p[rt].tag=0;
	if(l==r){
		p[rt].pl=p1.rev[l];
		p[rt].disl=p1.dis[p1.rev[l]];
		p[rt].pr=p[rt].disr=p[rt].dis=0;
		return;
	}
	build(l,mid,rt<<1);
	build(mid+1,r,rt<<1|1);
	pushup(rt);
}
inline void laz(int rt,ll z){
	p[rt].tag+=z;
	if(p[rt].pl){
		p[rt].disl+=z;
	}
	if(p[rt].pr){
		p[rt].disr+=z;
	}
	if(p[rt].pl&&p[rt].pr){
		p[rt].dis+=2*z;
	}
}
inline void pushdown(int rt){
	laz(rt<<1,p[rt].tag);
	laz(rt<<1|1,p[rt].tag);
	p[rt].tag=0;
}
inline void update(int x,int y,ll z,int l,int r,int rt){
	int mid=(l+r)>>1;
	if(x<=l&&y>=r){
		laz(rt,z);
		return;
	}
	if(p[rt].tag){
		pushdown(rt);
	}
	if(x<=mid){
		update(x,y,z,l,mid,rt<<1);
	}
	if(y>=mid+1){
		update(x,y,z,mid+1,r,rt<<1|1);
	}
	pushup(rt);
}
inline void dfs(int u,int fa){
	for(auto i:d[u]){
		ans[i[1]]=max(calc(p[1].pl,i[0])+p[1].disl,calc(p[1].pr,i[0])+p[1].disr);
	}
	for(int e=p1.first[u];e;e=p1.nnext[e]){
		if(p1.to[e]!=fa){
			update(1,n,p1.w[e],1,n,1);
			update(p1.dfn[p1.to[e]],p1.dfn[p1.to[e]]+p1.siz[p1.to[e]]-1,-2*p1.w[e],1,n,1);
			dfs(p1.to[e],u);
			update(1,n,-p1.w[e],1,n,1);
			update(p1.dfn[p1.to[e]],p1.dfn[p1.to[e]]+p1.siz[p1.to[e]]-1,2*p1.w[e],1,n,1);
		}
	}
}
inline void solve(){
	int m,a,b,c;
	n=gi();
	p1.clear();
	p2.clear();
	for(int i=1;i<=n-1;i++){
		a=gi();
		b=gi();
		c=gi();
		p1.add(a,b,c);
		p1.add(b,a,c);
	}
	for(int i=1;i<=n-1;i++){
		a=gi();
		b=gi();
		c=gi();
		p2.add(a,b,c);
		p2.add(b,a,c);
	}
	p1.dfs(1,0);
	p2.dfs(1,0);
	for(int i=1;i<=n;i++){
		st[0][p2.dfn[i]]=i;
		d[i].clear();
	}
	for(int i=1;i<=16;i++){
		for(int j=1;j<=n-(1<<i)+1;j++){
			st[i][j]=p2.get(st[i-1][j],st[i-1][j+(1<<(i-1))]);
		}
	}
	build(1,n,1);
	m=gi();
	for(int i=1;i<=m;i++){
		a=gi();
		d[a].push_back({gi(),i});
	}
	dfs(1,0);
	for(int i=1;i<=m;i++){
		prt(ans[i],"\n");
	}
}
int main(){
	int t=gi();
	while(t--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

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: