QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#88857 | #5439. Meet in the Middle | Appleblue17 | WA | 115ms | 12348kb | C++14 | 3.5kb | 2023-03-17 19:48:59 | 2023-03-17 19:49:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define Pii pair<int,int>
const int N=1e5+5;
int n,q,ans[N];
vector < pair<int,int> > G[N];
long long dep_[N];
int dfn_[N],dfout_[N],id_,mp_[N];
void dfs_(int u,int fa){
dfn_[u]=++id_;
mp_[id_]=u;
for(pair<int,int> e: G[u]){
int v=e.first,w=e.second;
if(v==fa) continue;
dep_[v]=dep_[u]+w;
dfs_(v,u);
}
dfout_[u]=id_;
}
struct nod{
int to,nxt,w;
}e[N*2];
int head[N],ecnt;
inline void add(int u,int v,int w){
e[++ecnt]={v,head[u],w};
head[u]=ecnt;
}
long long dep[N];
int log_[N*2],st[N*2][22];
int pos[N],id;
inline int getmin(int x,int y){
if(dep[x]>dep[y]) return y;
else return x;
}
void dfs0(int u,int fa){
st[++id][0]=u;
pos[u]=id;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa) continue;
dep[v]=dep[u]+e[i].w;
dfs0(v,u);
st[++id][0]=u;
}
}
inline int glca(int x,int y){
int l=pos[x],r=pos[y];
if(l>r) swap(l,r);
int k=log_[r-l+1];
return getmin(st[l][k],st[r-(1<<k)+1][k]);
}
inline long long gdis(int x,int y){
return dep[x]+dep[y]-2*dep[glca(x,y)];
}
//////////////////////////////////////////////////////
Pii f[N*4];
int g[N*4];
int query(int l,int r,int o,int pos){
if(l==r) return g[o];
int mid=(l+r)>>1;
if(pos<=mid) return g[o]+query(l,mid,o<<1,pos);
else return g[o]+query(mid+1,r,o<<1|1,pos);
}
inline long long cal(Pii V){
if(!V.first) return -1;
return gdis(V.first,V.second)+query(1,n,1,V.first)+query(1,n,1,V.second);
}
inline Pii gmax(Pii P,Pii Q){
if(cal(P)<cal(Q)) return Q;
else return P;
}
Pii upd(Pii P,Pii Q){
if(!P.first || !Q.first) return {0,0};
long long mx=-1;
int A,B; long long dis;
dis=gdis(P.first,Q.first);
if(dis>mx) mx=dis,A=P.first,B=Q.first;
dis=gdis(P.first,Q.second);
if(dis>mx) mx=dis,A=P.first,B=Q.second;
dis=gdis(P.second,Q.second);
if(dis>mx) mx=dis,A=P.second,B=Q.second;
dis=gdis(P.second,Q.first);
if(dis>mx) mx=dis,A=P.second,B=Q.first;
return {A,B};
}
void build(int l,int r,int o){
if(l==r){
int u=mp_[l];
f[o]={u,u}; g[o]=dep_[u];
return ;
}
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o<<1|1);
f[o]=gmax(upd(f[o<<1],f[o<<1|1]),gmax(f[o<<1],f[o<<1|1]));
}
void modify(int l,int r,int o,int L,int R,int x){
if(L<=l && r<=R){
g[o]+=x;
return ;
}
int mid=(l+r)>>1;
if(L<=mid) modify(l,mid,o<<1,L,R,x);
if(R>mid) modify(mid+1,r,o<<1|1,L,R,x);
f[o]=gmax(upd(f[o<<1],f[o<<1|1]),gmax(f[o<<1],f[o<<1|1]));
}
/////////////////////////////////////
vector< pair<int,int> > Q[N];
void dfs(int u,int fa){
int x=f[1].first,y=f[1].second;
for(pair<int,int> e: Q[u]){
int v=e.first,num=e.second;
ans[num]=max(cal({x,v}),cal({y,v}))-query(1,n,1,v);
}
for(pair<int,int> e: G[u]){
int v=e.first,w=e.second;
if(v==fa) continue;
modify(1,n,1,1,n,w);
modify(1,n,1,dfn_[v],dfout_[v],-2*w);
dfs(v,u);
modify(1,n,1,1,n,-w);
modify(1,n,1,dfn_[v],dfout_[v],2*w);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<n;i++){
int u,v,w; cin>>u>>v>>w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
dfs_(1,0);
for(int i=1;i<n;i++){
int u,v,w; cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
dfs0(1,0);
for(int j=1;j<=20;j++)
for(int i=1;i<=id-(1<<j)+1;i++)
st[i][j]=getmin(st[i][j-1],st[i+(1<<(j-1))][j-1]);
for(int i=1;i<=id;i++) log_[i]=log2(i);
build(1,n,1);
for(int i=1;i<=q;i++){
int x,y; cin>>x>>y;
Q[x].push_back({y,i});
}
dfs(1,0);
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
}
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 8168kb
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: 0
Accepted
time: 5ms
memory: 8136kb
input:
2 1 1 2 1 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 2ms
memory: 8168kb
input:
2 1 1 2 1 1 2 1 1 2
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 115ms
memory: 12348kb
input:
10000 50000 8101 5996 108427744 5996 7605 870838849 5996 5599 603303696 8101 3006 339377417 8101 6463 442516687 6463 5560 109174079 5560 4063 127596224 3006 1682 947915262 5996 1986 130416311 6463 5455 279771516 6463 2634 516688796 4063 3034 217886863 7605 5092 742375061 5599 2266 193804402 5092 140...
output:
1497490437 -1364897982 1690502413 -568948167 -1457828107 2022456325 2112605271 816049445 -1805223818 -765763953 1208105921 -1317065588 -2013351468 -939790338 939432242 411696220 94458492 -1102417650 -880970629 -520365931 -613468543 1125811954 -1862576480 -1575407325 628129325 -993077489 645220136 21...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '1497490437'