QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#694112 | #5439. Meet in the Middle | lin1991122 | WA | 0ms | 16140kb | C++14 | 3.3kb | 2024-10-31 17:17:05 | 2024-10-31 17:17:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+3;
int n;
struct Edge{int nxt,to,w;};
struct Tree{
Edge e[N<<1];
int head[N],f[19][N],dep[N],dfn[N];
ll dis[N];
int tot,cnt;
void add(int u,int v,int w){
e[++tot]={head[u],v,w};
head[u]=tot;
}
void dfs(int x,int fa){
f[0][++cnt]=fa;dfn[x]=cnt;
dep[x]=dep[fa]+1;
for(int i=head[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa)continue;
dis[y]=dis[x]+e[i].w;
dfs(y,x);
}
}
int ckmx(int x,int y){return dep[x]<dep[y]?x:y;}
int lca(int x,int y){
if(x==y)return x;
if((x=dfn[x])>(y=dfn[y]))swap(x,y);
int k=__lg(y-x);
return ckmx(f[k][x+1],f[k][y-(1<<k)+1]);
}
ll calc(int x,int y){return dis[x]+dis[y]-2*dis[lca(x,y)];}
void init(){
int u,v,w;
for(int i=1;i<n;++i)cin>>u>>v>>w,add(u,v,w),add(v,u,w);
dfs(1,0);
for(int j=1;1<<j<=n;++j)
for(int i=1;i+(1<<j)-1<=n;++i)
f[j][i]=ckmx(f[j-1][i],f[j-1][i+(1<<j-1)]);
}
void clear(){cnt=tot=0;fill_n(head+1,n,0);}
};
Tree t1,t2;
struct Node{
int x,y;
ll k1,k2,dis;
Node()=default;
Node(int _x,int _y,ll _k1,ll _k2){
x=_x;y=_y;k1=_k1;k2=_k2;
dis=k1+k2+t2.calc(x,y);
}
friend bool operator<(Node a,Node b){
return a.dis<b.dis;
}
friend Node operator+(Node a,Node b){
vector<Node>tmp={
a,b,
{a.x,b.x,a.k1,b.k1},
{a.x,b.y,a.k1,b.k2},
{a.y,b.x,a.k2,b.k1},
{a.y,b.y,a.k2,b.k2},
};
return *max_element(tmp.begin(),tmp.end());
}
};
#define ls p<<1
#define rs p<<1|1
Node t[N<<2];
ll tag[N<<2];
void fix(int p,ll k){
t[p].k1+=k;t[p].k2+=k;
tag[p]+=k;t[p].dis+=k*2;
}
void push_down(int p){
if(!tag[p])return;
fix(ls,tag[p]);
fix(rs,tag[p]);
tag[p]=0;
}
void upd(int x,int y,ll k,int l=1,int r=n,int p=1){
if(x<=l&&r<=y)return fix(p,k);
int mid=l+r>>1;push_down(p);
if(x<=mid)upd(x,y,k,l,mid,ls);
if(y>mid)upd(x,y,k,mid+1,r,rs);
t[p]=t[ls]+t[rs];
}
int tot,q;
int fr[N];
struct Ed{int nxt,b,id;}E[N*5];
void add(int u,int v,int id){
E[++tot]={fr[u],v,id};
fr[u]=tot;
}
int *head=t1.head,*dfn=t1.dfn,*dep=t1.dep;
Edge *e=t1.e;
int sz[N],id[N];
ll *dis=t1.dis;
ll ans[N*5];
void dfs(int x,int fa){
sz[x]=1;id[dfn[x]]=x;
for(int i=head[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa)continue;
dfs(y,x);
sz[x]+=sz[y];
}
}
void build(int l=1,int r=n,int p=1){
if(l==r){t[p]={id[l],id[l],dis[id[l]],dis[id[l]]};return;}
int mid=l+r>>1;
build(l,mid,ls);
build(mid+1,r,rs);
t[p]=t[ls]+t[rs];
}
void solve(int x,int fa){
for(int i=fr[x];i;i=E[i].nxt){
auto now=t[1];
ans[E[i].id]=max(
t2.calc(now.x,E[i].b)+now.k1,
t2.calc(now.y,E[i].b)+now.k2
);
}
for(int i=head[x];i;i=e[i].nxt){
int y=e[i].to;
if(y==fa)continue;
int L=dfn[y],R=dfn[y]+sz[y]-1;
if(L>1)upd(1,L-1,e[i].w);
if(R<n)upd(R+1,n,e[i].w);
upd(L,R,-e[i].w);
solve(y,x);
if(L>1)upd(1,L-1,-e[i].w);
if(R<n)upd(R+1,n,-e[i].w);
upd(L,R,e[i].w);
}
}
void solve(){
int u,v,w,x,y,z;tot=0;
t1.clear();t2.clear();fill_n(fr+1,n,0);
cin>>n;t1.init();t2.init();
cin>>q;for(int i=1;i<=q;++i)cin>>u>>v,add(u,v,i);
dfs(1,0);build();solve(1,0);
for(int i=1;i<=q;++i)printf("%lld\n",ans[i]);
}
int main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;for(T=1;T--;)solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 16140kb
input:
3 4 1 2 1 2 3 2 1 2 2 2 3 1 1 1 1 2 2 1 2 2
output:
12
result:
wrong answer 1st numbers differ - expected: '6', found: '12'