QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#694249 | #5439. Meet in the Middle | liujunyi123 | RE | 0ms | 0kb | C++14 | 3.2kb | 2024-10-31 17:33:46 | 2024-10-31 17:33:47 |
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch-'0'),ch=getchar();
return x*f;
}
void write(ll x){
if(x<0)return putchar('-'),write(-x),void();
if(x<10)return putchar(x^48),void();
write(x/10),putchar((x%10)^48);
}
int T,n,q,lg[N];
struct node{int to,nxt,w;};
struct tree{int head[N],cnt,dfn[N],ed[N],id[N],tot,mn[N][20];ll d[N];node e[N<<1];
void add(int u,int v,int w){e[++cnt]={v,head[u],w},head[u]=cnt;}
int getmin(int x,int y){return dfn[x]<dfn[y]?x:y;}
void dfs(int u,int fa){dfn[u]=++tot,id[dfn[u]]=u,mn[dfn[u]][0]=fa;
for(int i=head[u],v;i;i=e[i].nxt)if((v=e[i].to)!=fa)d[v]=d[u]+e[i].w,dfs(v,u);
ed[u]=tot;
}
int lca(int u,int v){
if(u==v)return u;
if((u=dfn[u])>(v=dfn[v]))swap(u,v);
int len=lg[v-u++];
return getmin(mn[u][len],mn[v-(1<<len)+1][len]);
}
ll dis(int u,int v){return d[u]+d[v]-2*d[lca(u,v)];}
void build(){cnt=tot=0;
for(int i=1;i<=n;i++)head[i]=0;
for(int i=1,x,y,z;i<n;i++)x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z);
dfs(1,0);
for(int j=1;j<20;j++)for(int i=1;i+(1<<j)-1<=n;i++)mn[i][j]=getmin(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
}
}T1,T2;
ll getdis(int u,int v,ll x,ll y){return T2.dis(u,v)+x+y;}
struct sgt{int a[2];ll tg,b[2];
ll get(){return getdis(a[0],a[1],b[0],b[1]);}
void merge(sgt &x,sgt &y){ll mx=0,lx=0,ly=0;
if((lx=x.get())>(ly=y.get()))mx=lx,a[0]=x.a[0],a[1]=x.a[1],b[0]=x.b[0],b[1]=x.b[1];
else mx=ly,a[0]=y.a[0],a[1]=y.a[1],b[0]=y.b[0],b[1]=y.b[1];
for(int i=0;i<2;i++)for(int j=0;j<2;j++)
if((lx=getdis(x.a[i],y.a[j],x.b[i],y.b[j]))>=mx)mx=lx,a[0]=x.a[i],a[1]=y.a[j],b[0]=x.b[i],b[1]=y.b[j];
}
void addtag(ll x){tg+=x,b[0]+=x,b[1]+=x;}
}tr[N<<2];
void build(int o,int l,int r){tr[o].tg=0;
if(l==r){tr[o]={{0,0},0,{0,0}};
tr[o].a[0]=tr[o].a[1]=T1.id[l];
tr[o].b[0]=T1.d[T1.id[l]];
return ;
}
int mid=(l+r)>>1;
build(o<<1,l,mid),build(o<<1|1,mid+1,r);
tr[o].merge(tr[o<<1],tr[o<<1|1]);
}
void downtag(int o){if(tr[o].tg)tr[o<<1].addtag(tr[o].tg),tr[o<<1|1].addtag(tr[o].tg),tr[o].tg=0;}
void update(int o,int l,int r,int x,int y,int z){
if(x>y)return ;
if(x<=l&&r<=y)return tr[o].addtag(z),void();
int mid=(l+r)>>1;downtag(o);
if(x<=mid)update(o<<1,l,mid,x,y,z);
if(y>mid)update(o<<1|1,mid+1,r,x,y,z);
tr[o].merge(tr[o<<1],tr[o<<1|1]);
}
ll ans[N*5];vector<pair<int,int> > g[N];
void solve(int u,int fa){
int p=tr[1].a[0],q=tr[1].a[1];
for(auto x:g[u])ans[x.first]=max(T1.dis(u,p)+T2.dis(x.second,p),T1.dis(u,q)+T2.dis(x.second,q));
for(int i=T1.head[u],v;i;i=T1.e[i].nxt)if((v=T1.e[i].to)!=fa){
int l=T1.dfn[v],r=T1.ed[v],w=T1.e[i].w;
update(1,1,n,1,l-1,w),update(1,1,n,l,r,-w),update(1,1,n,r+1,n,w);
solve(v,u);
update(1,1,n,1,l-1,-w),update(1,1,n,l,r,w),update(1,1,n,r+1,n,-w);
}
}
void work(){n=read();
for(int i=1;i<=n;i++)g[i].clear();
T1.build(),T2.build(),build(1,1,n);
q=read();
for(int i=1,x,y;i<=q;i++)x=read(),y=read(),g[x].push_back(make_pair(i,y));
solve(1,0);
for(int i=1;i<=q;i++)write(ans[i]),puts("");
}
int main(){lg[0]=-1;
for(int i=1;i<N;i++)lg[i]=lg[i>>1]+1;
scanf("%d",&T);
while(T--)work();
return 0;
}
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