QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#480492#5439. Meet in the MiddleyanshanjiahongWA 0ms22396kbC++143.8kb2024-07-16 16:07:342024-07-16 16:07:35

Judging History

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

  • [2024-07-16 16:07:35]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:22396kb
  • [2024-07-16 16:07:34]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define repp(i,j,k) for(int i=j;i>=k;i--)
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define mp make_pair
#define sec second
#define fir first
#define pii pair<int,int>
#define lowbit(i) i&-i
#define int long long
using namespace std;
typedef long long ll;
const int N=1e5+5,M=5e5+5,S=(1<<15)+5,inf=1e9+7,mo=1e9+7;
const double eps=1e-8;
void read(int &p){
	int x=0,w=1;
	char ch=0;
	while(!isdigit(ch)){
		if(ch=='-')w=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	p=x*w;
}
int n,m;
struct tree{
	vector<pii>e[N];
	int dfn[N],cntp,nw[N];
	int fa[N][20],dis[N],dep[N],sz[N];
	void dfs(int x,int f){
		fa[x][0]=f,dep[x]=dep[f]+1,sz[x]=1;
		dfn[x]=++cntp,nw[cntp]=x;
		rep(i,1,17)
		    fa[x][i]=fa[fa[x][i-1]][i-1];
		for(auto p:e[x]){
			int j=p.fir,w=p.sec;
			if(j==f)continue;
			dis[j]=dis[x]+w;
			dfs(j,x),sz[x]+=sz[j];
		}
	}
	int lca(int x,int y){
		if(dep[x]<dep[y])swap(x,y);
		repp(i,17,0)
		    if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
		if(x==y)return x;
	    repp(i,17,0)
	        if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
	    return fa[x][0];
	}
	int getdis(int x,int y){
		return dis[x]+dis[y]-2*dis[lca(x,y)];
	}
	void init(){
		rep(i,1,n-1){
			int x,y,w;
			read(x),read(y),read(w);
			e[x].push_back(mp(y,w)),e[y].push_back(mp(x,w));
		}
		dfs(1,0);
	}
}T1,T2;
vector<pii>q[N];
struct seg2{
	int t[4*N];
	void build(int x,int le,int ri){
		if(le==ri){
			t[x]=T1.dis[T1.nw[x]];
			return;
		}
		int mid=(le+ri)>>1;
		build(ls(x),le,mid),build(rs(x),mid+1,ri);
	}
	void modify(int x,int le,int ri,int ql,int qr,int v){
		if(ql>qr)return;
		if(ql<=le&&qr>=ri){
			t[x]+=v;
			return;
		}
		int mid=(le+ri)>>1;
		if(ql<=mid)modify(ls(x),le,mid,ql,qr,v);
		if(qr>mid)modify(rs(x),mid+1,ri,ql,qr,v);
	}
	int query(int x,int le,int ri,int p){
		if(le==ri)return t[x];
		int mid=(le+ri)>>1;
		if(p<=mid)return query(ls(x),le,mid,p)+t[x];
		else return query(rs(x),mid+1,ri,p)+t[x];
	}
}SGT2;
int getdim(int x,int y){
	return SGT2.query(1,1,n,T1.dfn[x])+SGT2.query(1,1,n,T1.dfn[y])+T2.getdis(x,y);
}
struct seg1{
	int t[4*N][2];
	void pushup(int x){
		int res=0;
		int nwv=getdim(t[ls(x)][0],t[ls(x)][1]);
		if(nwv>res)t[x][0]=t[ls(x)][0],t[x][1]=t[ls(x)][1],res=nwv;
		nwv=getdim(t[rs(x)][0],t[rs(x)][1]);
		if(nwv>res)t[x][0]=t[rs(x)][0],t[x][1]=t[rs(x)][1],res=nwv;
		rep(i,0,1){
			rep(j,0,1){
				nwv=getdim(t[ls(x)][i],t[rs(x)][j]);
				if(nwv>res)t[x][0]=t[ls(x)][i],t[x][1]=t[rs(x)][j],res=nwv;
			}
		}
	}
	void build(int x,int le,int ri){
		if(le==ri){
			t[x][0]=t[x][1]=T1.nw[le];
			return;
		}
		int mid=(le+ri)>>1;
		build(ls(x),le,mid),build(rs(x),mid+1,ri);
		pushup(x);
	}
	void modify(int x,int le,int ri,int p){
		if(le==ri)return;
		int mid=(le+ri)>>1;
		if(p<=mid)modify(ls(x),le,mid,p);
		else modify(rs(x),mid+1,ri,p);
		pushup(x);
	}
}SGT1;
void update(int x,int v){
	SGT2.modify(1,1,n,T1.dfn[x],T1.dfn[x]+T1.sz[x]-1,-1*v);
	SGT2.modify(1,1,n,1,T1.dfn[x]-1,1*v);
	SGT2.modify(1,1,n,T1.dfn[x]+T1.sz[x],n,1*v);
	SGT1.modify(1,1,n,T1.dfn[x]),SGT1.modify(1,1,n,T1.dfn[x]+T1.sz[x]-1);
}
int ans[M];
void dfs(int x,int f){
	if(x!=1)update(x,1);
	int l=SGT1.t[1][0],r=SGT1.t[1][1];
	for(auto j:q[x]){
	    int valx=SGT2.query(1,1,n,T1.dfn[l])+T2.getdis(l,j.fir);
	    int valy=SGT2.query(1,1,n,T1.dfn[r])+T2.getdis(r,j.fir);
	    ans[j.sec]=max(valx,valy);
	}
	for(auto j:T1.e[x]){
		if(j.fir==f)continue;
		dfs(j.fir,x);
	}
	if(x!=1)update(x,-1);
}
signed main(){
	read(n),read(m);
	T1.init(),T2.init();
	rep(i,1,m){
		int x,y;
		read(x),read(y);
		q[x].push_back(mp(y,i));
	}
	SGT2.build(1,1,n),SGT1.build(1,1,n);
	dfs(1,0);
	rep(i,1,m)
	    printf("%lld\n",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 22328kb

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: -100
Wrong Answer
time: 0ms
memory: 22396kb

input:

2 1
1 2 1
1 2 1
1 1

output:

1

result:

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