QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360977 | #5439. Meet in the Middle | qwqUwU_ | WA | 71ms | 32408kb | C++14 | 4.2kb | 2024-03-22 17:12:08 | 2024-03-22 17:12:09 |
Judging History
answer
// clang-format off
#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define st first
#define nd second
#define bit(s,x) (((s)>>(x))&1)
#define ctz(s) __builtin_popcount(s)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 i128;
typedef long double ld;
typedef pair<int,int> pii; typedef pair<ll,int> pli; typedef pair<ll,ll> pll; typedef pair<int,ll> pil;
inline ll read(){
ll x=0,f=1,c=getchar();
while(c<'0'||c>'9')f=(c=='-'?-1:1),c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
inline void write(ll x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); }
// remember to change!!!
const int mod=998244353;
inline void plust(int &x,int y,int p=mod){x=x+y<p?x+y:x+y-p;}
inline void mul(int &x,int y,int p=mod){x=1ll*x*y%p;}
inline int fp(int x,ll p=mod-2,int m=mod,int res=1){
for(;p;p>>=1){if(p&1)mul(res,x,m);mul(x,x,m);}
return res;
}
const int N=1e5+10;
const int INF=0x3fffffff;
// clang-format on
int n,m;
vector<pil>G1[N],G2[N];
vector<pii>q[N];
int dfn1[N],dfn2[N],idfn[N],st1[17][N],siz[N],st[17][N],tim;
ll dep1[N],dep2[N],ans[N*5];
inline int pmin(int a,int b){return dfn2[a] < dfn2[b]?a:b;}
inline int pmin1(int a,int b){return dfn1[a] < dfn1[b]?a:b;}
inline void dfs1(int u,int fa){
st[0][dfn2[u]=++tim]=fa;
for(auto [v,w]:G2[u])if(v^fa){
dep2[v] = dep2[u] + w;
dfs1(v,u);
}
}
inline ll dist2(pil A,pil B){
int u=A.st,v=B.st;
if(u==v)return A.nd;
ll res = dep2[u] + dep2[v];
if((u=dfn2[u]) > (v=dfn2[v]))swap(u,v);
int k=__lg(v-u++);
int lca = pmin(st[k][u],st[k][v-(1<<k)+1]);
return res - 2*dep2[lca] + A.nd + B.nd;
}
inline ll dist1(int u,int v){
if(u==v)return 0;
ll res = dep1[u] +dep1[v];
if((u=dfn1[u]) > (v=dfn1[v]))swap(u,v);
int k=__lg(v-u++);
int lca = pmin1(st1[k][u],st1[k][v-(1<<k)+1]);
return res - 2*dep1[lca];
}
inline void dfs2(int u,int fa){
st1[0][dfn1[u] = ++tim]=fa;idfn[tim]=u;
siz[u]=1;
for(auto [v,w]:G1[u])if(v^fa){
dep1[v] = dep1[u] + w;
dfs2(v,u);
siz[u] += siz[v];
}
}
typedef pair<pil,pil> info;
info t[1<<18];
ll tag[1<<18];
inline info merge(info A,info B){
pil a[4] = {A.st,A.nd,B.st,B.nd};
info res = A;
for(int i=0;i<4;++i)for(int j=i+1;j<4;++j)if(dist2(a[i],a[j]) > dist2(res.st,res.nd))res = P(a[i],a[j]);
return res;
}
#define mid ((l+r)>>1)
inline void update(int x,int l,int r,int nl,int nr,ll k){
if(nl>nr)return;
if(nl<=l && r<=nr){
tag[x]+=k;
t[x].st.nd+=k,t[x].nd.nd+=k;
return ;
}
tag[x<<1]+=tag[x];tag[x<<1|1]+=tag[x];
t[x<<1].st.nd+=tag[x];
t[x<<1].nd.nd+=tag[x];
t[x<<1|1].st.nd+=tag[x];
t[x<<1|1].nd.nd+=tag[x];
tag[x]=0;
if(nl<=mid)update(x<<1,l,mid,nl,nr,k);
if(mid<nr) update(x<<1|1,mid+1,r,nl,nr,k);
t[x] = merge(t[x<<1],t[x<<1|1]);
}
inline void build(int x,int l,int r){
if(l==r){
int u=idfn[l];
t[x] = P(P(u,dep1[u]),P(u,dep1[u]));
return;
}
build(x<<1,l,mid),build(x<<1|1,mid+1,r);
t[x] = merge(t[x<<1],t[x<<1|1]);
}
inline void dfs3(int u,int fa){
int d1=t[1].st.st,d2=t[1].nd.st;
for(auto [v,id]:q[u]) ans[id] = max(dist1(u,d1)+dist2(P(v,0),P(d1,0)),dist1(u,d2)+dist2(P(v,0),P(d2,0)));
for(auto [v,w]:G1[u])if(v^fa){
update(1,1,n,dfn1[v],dfn1[v]+siz[v]-1,-w);
update(1,1,n,1,dfn1[v]-1,w);
update(1,1,n,dfn1[v]+siz[v],n,w);
dfs3(v,u);
update(1,1,n,dfn1[v],dfn1[v]+siz[v]-1,w);
update(1,1,n,1,dfn1[v]-1,-w);
update(1,1,n,dfn1[v]+siz[v],n,-w);
}
}
int main() {
//freopen("data.in", "r", stdin);
// freopen(".in","r",stdin);
// freopen("myans.out","w",stdout);
n=read(),m=read();
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
G1[u].pb(P(v,w));
G1[v].pb(P(u,w));
}
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
G2[u].pb(P(v,w));
G2[v].pb(P(u,w));
}
for(int i=1;i<=m;++i){
int a=read(),b=read();
q[a].pb(P(b,i));
}
tim=0,dfs1(1,0);
for(int i=1;i<17;++i)for(int j=1;j+(1<<i)-1<=n;++j)st[i][j]=pmin(st[i-1][j],st[i-1][j+(1<<(i-1))]);
tim=0,dfs2(1,0);
build(1,1,n);
for(int i=1;i<17;++i)for(int j=1;j+(1<<i)-1<=n;++j)st1[i][j]=pmin1(st1[i-1][j],st1[i-1][j+(1<<(i-1))]);
dfs3(1,0);
for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22208kb
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: 22016kb
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: 3ms
memory: 23152kb
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: 71ms
memory: 32408kb
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:
-701676852 -525432040 -1122133816 1411233330 99977036 1597821560 1587732154 -1921112708 -163518930 716567114 60684514 324639300 1833473233 -725785172 -550896024 -1537152649 488363482 -1939487614 -594511066 961965136 299215128 1042061082 1495724724 2027560398 2110460392 489253578 161525729 -232327947...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '-701676852'