QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#704366 | #5439. Meet in the Middle | Mini_PEKKA | WA | 3ms | 11908kb | C++20 | 2.9kb | 2024-11-02 19:51:15 | 2024-11-02 19:51:15 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
using namespace std;
typedef pair<int,int> pii;
const int N=1e5+5,M=5e5+5;
int n,t1,t2,fa1[N],fa2[N],dis1[N],dis2[N],st1[N],st2[N],id1[N],ed1[N],ans[M];
pii f[2*N][22];
vector<pii> T1[N],T2[N],Q[M];
int LCA(int u,int v){
u=st2[u],v=st2[v];
if(u>v)swap(u,v);
int k=__lg(v-u+1);
return min(f[u][k],f[v-(1<<k)+1][k]).se;
}
int cal(int u,int v){return dis2[u]+dis2[v]-2*dis2[LCA(u,v)];}
struct Node{
int u,v,wu,wv,len,add;
bool operator<(const Node& b)const{return len<b.len;}
};
struct Sgt{
#define ls (u<<1)
#define rs (u<<1|1)
Node ST[4*N];
Node merge(const Node& x,const Node& y){
Node res1={x.u,y.v,x.wu,y.wv,x.wu+y.wv+cal(x.u,y.v),0};
Node res2={x.v,y.u,x.wv,y.wu,x.wv+y.wu+cal(x.v,y.u),0};
Node res3={x.u,y.u,x.wu,y.wu,x.wu+y.wu+cal(x.u,y.u),0};
Node res4={x.v,y.v,x.wv,y.wv,x.wv+y.wv+cal(x.v,y.v),0};
Node res=max(max(max(x,y),max(res1,res2)),max(res3,res4));
res.add=0;
return res;
}
void build(int u,int l,int r){
if(l==r){
int x=id1[l],d=dis1[x];
return void(ST[u]={x,x,d,d,d,0});
}
int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r),ST[u]=merge(ST[ls],ST[rs]);
}
void upd(int u,int val){
ST[u].add+=val,ST[u].wu+=val,ST[u].wv+=val,ST[u].len+=(2-(ST[u].u==ST[u].v))*val;
}
void pushdown(int u){
if(ST[u].add)upd(ls,ST[u].add),upd(rs,ST[u].add),ST[u].add=0;
}
void update(int u,int l,int r,int x,int y,int val){
if(x<=l&&r<=y)return upd(u,val);
pushdown(u);
int mid=(l+r)>>1;
if(x<=mid)update(ls,l,mid,x,y,val);
if(y>mid)update(rs,mid+1,r,x,y,val);
ST[u]=merge(ST[ls],ST[rs]);
}
}sgt;
void dfs(int u){
id1[st1[u]=++t1]=u;
for(pii i:T1[u]){
int v=i.fi,w=i.se;
if(v!=fa1[u])fa1[v]=u,dis1[v]=dis1[u]+w,dfs(v);
}
ed1[u]=t1;
}
void dfs2(int u){
Node res=sgt.ST[1];
for(pii i:Q[u]){
int v=i.fi,id=i.se;
ans[id]=max(cal(v,res.u)+res.wu,cal(v,res.v)+res.wv);
}
for(pii i:T1[u]){
int v=i.fi,w=i.se;
if(v!=fa1[u])sgt.update(1,1,n,1,n,w),sgt.update(1,1,n,st1[v],ed1[v],-2*w),dfs2(v),sgt.update(1,1,n,1,n,-w),sgt.update(1,1,n,st1[v],ed1[v],2*w);
}
}
void dfs3(int u){
f[st2[u]=++t2][0]={dis2[u],u};
for(pii i:T2[u]){
int v=i.fi,w=i.se;
if(v!=fa2[u])fa2[v]=u,dis2[v]=dis2[u]+w,dfs3(v),f[++t2][0]={dis2[u],u};
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t;cin>>t;
while(t--){
cin>>n,t1=t2=0;
rep(i,1,n)T1[i].clear(),T2[i].clear(),Q[i].clear();
rep(i,1,n-1){int u,v,w;cin>>u>>v>>w,T1[u].pb({v,w}),T1[v].pb({u,w});}
rep(i,1,n-1){int u,v,w;cin>>u>>v>>w,T2[u].pb({v,w}),T2[v].pb({u,w});}
dfs(1),dfs3(1);
int q;cin>>q;
rep(i,1,q){int u,v;cin>>u>>v,Q[u].pb({v,i});}
int k=__lg(2*n-1);
rep(i,1,k)rep(j,1,2*n-(1<<i))f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]);
sgt.build(1,1,n),dfs2(1);
rep(i,1,q)cout<<ans[i]<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 11908kb
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 6 4 4 4 4
result:
wrong answer 2nd numbers differ - expected: '4', found: '6'