QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#486893 | #5439. Meet in the Middle | luanyanjia | WA | 40ms | 42404kb | C++20 | 4.0kb | 2024-07-22 10:43:20 | 2024-07-22 10:43:21 |
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];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;(1<<len)<cnt;len++)
for(int i=1;i+(1<<len)-1<=n;i++)
st[len][i]=min(st[len-1][i],st[len-1][i+(1<<(len-1))]);
}
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){if(x>n)return ;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(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);
}
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]);
// 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: 4ms
memory: 20928kb
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: 0ms
memory: 21000kb
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: 19036kb
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: 40ms
memory: 42404kb
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:
651046562921 446722759908 547278972410 640274571215 603078292376 649051094037 839075137999 384898397156 443221997786 475640188100 871907828727 406528783550 615096983210 793758278621 751037163901 724281103500 511966695685 887087525783 746698581563 473080519324 581149297441 394849354206 363082416807 6...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '651046562921'