QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#692997 | #5439. Meet in the Middle | qwqwf | RE | 0ms | 0kb | C++14 | 3.4kb | 2024-10-31 15:21:01 | 2024-10-31 15:21:02 |
answer
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("Ofast","unroll-loops","inline")
#include<bits/stdc++.h>
#define ll long long
//#define int ll
#define pb push_back
#define pii pair<int,int>
#define MP make_pair
#define fi first
#define se second
using namespace std;
const int N=1e5+60,M=4e7+20,LGN=18,mod=998244353;
const int Inf=1e9;
int n;
struct Tree{
vector<pii> e[N];
int dep[N];ll d[N];
int dfn[N],id[N],sz[N],dfc,st[LGN][N];
inline int Min(int x,int y){return dep[x]<dep[y]?x:y;}
inline void sv(){
for(int j=1;j<LGN;j++){
for(int i=1;i+(1<<j)-1<=n;i++) st[j][i]=Min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline int lca(int x,int y){
if(x==y) return x;
x=id[x],y=id[y];
if(x>y) swap(x,y);
int k=__lg(y-x++);
return Min(st[k][x],st[k][y-(1<<k)+1]);
}
void dfs(int u,int F){
st[0][id[u]=++dfc]=F;dfn[dfc]=u;dep[u]=dep[F]+1;
sz[u]=1;
for(auto x:e[u]) if(x.fi!=F){
int v=x.fi,w=x.se;
d[v]=d[u]+w;dfs(v,u);sz[u]+=sz[v];
}
}
inline ll dis(int x,int y){return d[x]+d[y]-2ll*d[lca(x,y)];}
inline void init(){
for(int i=1;i<=n;i++) e[i].clear(),dep[i]=0,d[i]=0;
for(int i=1,u,v,w;i<n;i++) cin>>u>>v>>w,e[u].pb(MP(v,w)),e[v].pb(MP(u,w));
dfc=0;dfs(1,0),sv();
}
}t1,t2;
struct Data{int x[2];ll v[2],L;};
inline Data operator +(Data a,Data b){
Data z;
if(a.L>b.L) z=a;else z=b;
for(int i=0;i<2;i++) for(int j=0;j<2;j++){
Data c;c.x[0]=a.x[i],c.x[1]=b.x[j];c.v[0]=a.v[i],c.v[1]=b.v[j];
c.L=t2.dis(c.x[0],c.x[1])+c.v[0]+c.v[1];
if(c.L>z.L) z=c;
}
return z;
}
inline Data operator +(Data a,ll b){a.v[0]+=b,a.v[1]+=b,a.L+=(a.x[0]!=a.x[1])?2*b:0;return a;}
int q,dfn[N],id[N];ll a[N];
struct Seg{
Data s[N<<2];ll t[N<<2];
inline void ph(int rt){s[rt]=s[rt<<1]+s[rt<<1|1];}
inline void ptag(int rt,ll v){s[rt]=s[rt]+v,t[rt]+=v;}
inline void pd(int rt){if(t[rt]) ptag(rt<<1,t[rt]),ptag(rt<<1|1,t[rt]),t[rt]=0;}
inline void build(int rt,int l,int r){
t[rt]=0;
if(l==r) return s[rt].x[0]=s[rt].x[1]=dfn[l],s[rt].v[0]=s[rt].v[1]=a[l],s[rt].L=0,void();
int mid=(l+r)>>1;
build(rt<<1,l,mid),build(rt<<1|1,mid+1,r);
ph(rt);
}
inline void upd(int rt,int l,int r,int ql,int qr,ll v){
if(ql<=l&&r<=qr) return ptag(rt,v),void();
int mid=(l+r)>>1;pd(rt);
if(ql<=mid) upd(rt<<1,l,mid,ql,qr,v);
if(qr>mid) upd(rt<<1|1,mid+1,r,ql,qr,v);
ph(rt);
}
inline void upd(int l,int r,ll w){if(l<=r) upd(1,1,n,l,r,w);}
inline Data qry(){return s[1];}
}t;
vector<pii> vec[N];
ll ans[N];
void dfs(int u,int F){
Data y=t.qry();
for(pii x:vec[u]) ans[x.se]=max(t2.dis(y.x[0],x.fi)+y.v[0],t2.dis(y.x[1],x.fi)+y.v[1]);
for(auto x:t1.e[u]) if(x.fi!=F){
int v=x.fi,w=x.se;
t.upd(id[v],id[v]+t1.sz[v]-1,-w);
t.upd(1,id[v]-1,w),t.upd(id[v]+t1.sz[v],n,w);
dfs(v,u);
t.upd(id[v],id[v]+t1.sz[v]-1,w);
t.upd(1,id[v]-1,-w),t.upd(id[v]+t1.sz[v],n,-w);
}
}
void solve(){
cin>>n;
t1.init(),t2.init();
for(int i=1;i<=n;i++) vec[i].clear(),dfn[i]=t1.dfn[i],id[i]=t1.id[i],a[i]=t1.dis(1,dfn[i]);
t.build(1,1,n);
cin>>q;
for(int i=1,x,y;i<=q;i++) cin>>x>>y,vec[x].pb(MP(y,i));
dfs(1,0);
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
}
signed main(){
// freopen("move.in","r",stdin);
// freopen("move.out","w",stdout);
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;cin>>T;
while(T--) solve();
return 0;
}
詳細信息
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