QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#486896 | #5439. Meet in the Middle | luanyanjia | WA | 43ms | 48324kb | C++20 | 4.0kb | 2024-07-22 10:55:05 | 2024-07-22 10:55:06 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline void rd(){}
template<typename T,typename ...U>
inline void rd(T &x,U &...args){
char ch=getchar();
T f=1;x=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x*=f;rd(args...);
}
const int N=1e5+5;
int n,q;
namespace T2{
int fst[N],nxt[N<<1],v[N<<1],w[N<<1],idx;
int lg[N<<1],st[19][N<<1],fir[N],dep[N],cnt;
inline void Add(int a,int b,int c){
v[idx]=b,w[idx]=c;
nxt[idx]=fst[a];fst[a]=idx++;
}
void DFS(int x,int fa){
st[0][++cnt]=x,fir[x]=cnt;
for(int i=fst[x];~i;i=nxt[i]){
int y=v[i];
if(y==fa)continue;
dep[y]=dep[x]+w[i];
DFS(y,x);
st[0][++cnt]=x;
}
}
inline int LCA(int x,int y){
x=fir[x],y=fir[y];if(x>y)swap(x,y);int k=lg[y-x+1];
return min(st[k][x],st[k][y-(1<<k)+1]);
}
inline void Prework(){
for(int i=2,j=1;i<=2*n;i++){if((1<<(j+1))<=i)++j;lg[i]=j;}
DFS(1,1);
for(int len=1,j=1;len<cnt;len<<=1,j++)
for(int i=1;i+len<=cnt;i++)
st[j][i]=min(st[j-1][i],st[j-1][i+len]);
}
inline int Dist(int x,int y){
return dep[x]+dep[y]-2*dep[LCA(x,y)];
}
}
namespace T1{
int fst[N],nxt[N<<1],v[N<<1],w[N<<1],idx;
int sz[N],dep[N],dfn[N],cnt,ans[N],fdfn[N];
vector<pair<int,int> >vc[N];
inline void Add(int a,int b,int c){
v[idx]=b,w[idx]=c;
nxt[idx]=fst[a];fst[a]=idx++;
}
namespace BIT{
int t[N];
inline void Add(int x,int v){while(x<=n)t[x]+=v,x+=x&-x;}
inline int Query(int x){int ans=0;while(x)ans+=t[x],x-=x&-x;return ans;}
}
inline int Dist(int x,int y){return T2::Dist(x,y)+BIT::Query(dfn[x])+BIT::Query(dfn[y]);}
namespace SGT{
struct node{
int x,y,dis;
node(){}
node(int _x,int _y){x=_x,y=_y,dis=Dist(x,y);}
node(int _x,int _y,int _dis){x=_x,y=_y,dis=_dis;}
bool friend operator<(const node &a,const node &b){return a.dis<b.dis;}
node friend operator+(node &a,node &b){
if(a.x!=a.y)a.dis=Dist(a.x,a.y);
if(b.x!=b.y)b.dis=Dist(b.x,b.y);
node res=max(a,b);
res=max(res,max(node(a.x,b.x),node(a.x,b.y)));
res=max(res,max(node(a.y,b.x),node(a.y,b.y)));
return res;
}
}t[N<<2];
void Build(int i,int l,int r){
if(l==r){return t[i]=node(fdfn[l],fdfn[l],0),void();}
int mid=(l+r)>>1;
Build(i*2,l,mid);
Build(i*2+1,mid+1,r);
t[i]=t[i*2]+t[i*2+1];
}
void Update(int i,int l,int r,int x){
if(l==r)return ;
int mid=(l+r)>>1;
if(x<=mid)Update(i*2,l,mid,x);
else Update(i*2+1,mid+1,r,x);
t[i]=t[i*2]+t[i*2+1];
}
}
void DFS(int x,int fa){
sz[x]=1;dfn[x]=++cnt;fdfn[cnt]=x;
BIT::Add(x,dep[x]);BIT::Add(x+1,-dep[x]);
for(int i=fst[x];~i;i=nxt[i]){
int y=v[i];
if(y==fa)continue;
dep[y]=dep[x]+w[i];
DFS(y,x);
sz[x]+=sz[y];
}
}
void Solve(int x,int fa){
for(auto p:vc[x])ans[p.second]=max(Dist(SGT::t[1].x,p.first),Dist(SGT::t[1].y,p.first))-BIT::Query(dfn[p.first]);
for(int i=fst[x];~i;i=nxt[i]){
int y=v[i];
if(y==fa)continue;
BIT::Add(dfn[y],-w[i]);BIT::Add(dfn[y]+sz[y],w[i]);
BIT::Add(1,w[i]);BIT::Add(dfn[y],-w[i]);
BIT::Add(dfn[y]+sz[y],w[i]);BIT::Add(n+1,-w[i]);
SGT::Update(1,1,n,dfn[y]),SGT::Update(1,1,n,dfn[y]+sz[y]-1);
Solve(y,x);
BIT::Add(dfn[y],w[i]);BIT::Add(dfn[y]+sz[y],-w[i]);
BIT::Add(1,-w[i]);BIT::Add(dfn[y],w[i]);
BIT::Add(dfn[y]+sz[y],-w[i]);BIT::Add(n+1,w[i]);
SGT::Update(1,1,n,dfn[y]),SGT::Update(1,1,n,dfn[y]+sz[y]-1);
}
// printf("%d %d %d\n",x,SGT::t[1].x,SGT::t[1].y);
}
inline void Main(){
memset(fst,-1,sizeof fst);
memset(T2::fst,-1,sizeof T2::fst);
rd(n,q);
for(int i=1;i<n;i++){
int x,y,z;rd(x,y,z);
Add(x,y,z);Add(y,x,z);
}
for(int i=1;i<n;i++){
int x,y,z;rd(x,y,z);
T2::Add(x,y,z);T2::Add(y,x,z);
}
T2::Prework();
for(int i=1;i<=q;i++){
int a,b;rd(a,b);
vc[a].push_back({b,i});
}
DFS(1,1);
SGT::Build(1,1,n);
Solve(1,1);
for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
}
}
signed main(){
T1::Main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22840kb
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: 3ms
memory: 23196kb
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: 0ms
memory: 23076kb
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: 43ms
memory: 48324kb
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:
516490060446 669972336866 573173543552 505718068740 524975620764 514494591562 703845000563 551150954282 522441874818 681708991061 736677691291 612597586511 812708348738 831623926813 615807026465 586633432850 678219252811 751857388347 609050910913 604644977755 720979777417 560880861406 569151219768 5...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '516490060446'