QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#693348 | #5439. Meet in the Middle | yhddd | WA | 82ms | 77564kb | C++14 | 3.8kb | 2024-10-31 16:00:11 | 2024-10-31 16:00:15 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=200010;
const int inf=1e18;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
return x*f;
}
bool Mbe;
int n,q;
struct tree{
int head[maxn],tot;
struct nd{
int nxt,to,w;
}e[maxn<<1];
void add(int u,int v,int w){e[++tot]={head[u],v,w};head[u]=tot;}
int dfn[maxn],idx,rnk[maxn],out[maxn],st[16][maxn];
int dis[maxn];
void dfs(int u,int fa){
st[0][dfn[u]=++idx]=fa;rnk[idx]=u;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;if(v==fa)continue;
dis[v]=dis[u]+e[i].w;dfs(v,u);
}
out[u]=idx;
}
int mmin(int u,int v){return dfn[u]<dfn[v]?u:v;}
int lca(int u,int v){
if(u==v)return u;
u=dfn[u],v=dfn[v];
if(u>v)swap(u,v);
u++;
int k=log2(v-u+1);
return mmin(st[k][u],st[k][v-(1<<k)+1]);
}
int calc(int u,int v){return dis[u]+dis[v]-2*dis[lca(u,v)];}
void init(){
dfs(1,0);
for(int j=1;j<=16;j++){
for(int i=1;i+(1<<j)-1<=n;i++)st[j][i]=mmin(st[j-1][i],st[j-1][i+(1<<j-1)]);
}
}
void clr(){
for(int i=1;i<=n;i++)head[i]=0;tot=idx=0;
}
}t1,t2;
struct node{
int p[2],d[2],len;
}tree[maxn<<2];
node add(node u,int dd){
u.d[0]+=dd,u.d[1]+=dd,u.len+=(u.d[0]!=u.d[1])*2*dd;
return u;
}
bool operator<(node u,node v){return u.len<v.len;}
node merge(node u,node v){
node res=max(u,v);
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
node w;w.p[0]=u.p[i],w.p[1]=v.p[j],w.d[0]=u.d[i],w.d[1]=v.d[j];
w.len=t2.calc(u.p[i],v.p[j]);
res=max(res,w);
}
}
return res;
}
#define mid (l+r>>1)
#define ls nd<<1
#define rs nd<<1|1
void build(int nd,int l,int r){
if(l==r){
tree[nd].p[0]=tree[nd].p[1]=t1.rnk[l];
tree[nd].d[0]=tree[nd].d[1]=t1.calc(1,t1.rnk[l]);
tree[nd].len=0;
return ;
}
build(ls,l,mid),build(rs,mid+1,r);
tree[nd]=merge(tree[ls],tree[rs]);
}
int tag[maxn<<2];
void down(int nd){
add(tree[ls],tag[nd]),tag[ls]+=tag[nd];
add(tree[rs],tag[nd]),tag[rs]+=tag[nd];
tag[nd]=0;
}
void updata(int nd,int l,int r,int ql,int qr,int w){
if(ql>qr)return ;
if(l>=ql&&r<=qr){
tree[nd]=add(tree[nd],w),tag[nd]+=w;
return ;
}
if(tag[nd])down(nd);
if(ql<=mid)updata(ls,l,mid,ql,qr,w);
if(qr>mid)updata(rs,mid+1,r,ql,qr,w);
tree[nd]=merge(tree[ls],tree[rs]);
}
vector<pii> que[maxn];
int ans[maxn];
void dfs(int u,int fa){
for(auto[pos,id]:que[u]){
ans[id]=max(tree[1].d[0]+t2.calc(tree[1].p[0],pos),tree[1].d[1]+t2.calc(tree[1].p[1],pos));
}
for(int i=t1.head[u];i;i=t1.e[i].nxt){
int v=t1.e[i].to;if(v==fa)continue;
updata(1,1,n,1,t1.dfn[v]-1,t1.e[i].w);
updata(1,1,n,t1.out[v]+1,n,t1.e[i].w);
updata(1,1,n,t1.dfn[v],t1.out[v],-t1.e[i].w);
dfs(v,u);
updata(1,1,n,1,t1.dfn[v]-1,-t1.e[i].w);
updata(1,1,n,t1.out[v]+1,n,-t1.e[i].w);
updata(1,1,n,t1.dfn[v],t1.out[v],t1.e[i].w);
}
}
void work(){
n=read();q=read();
t1.clr(),t2.clr();
for(int i=1;i<n;i++){
int u=read(),v=read(),w=read();
t1.add(u,v,w),t1.add(v,u,w);
}
for(int i=1;i<n;i++){
int u=read(),v=read(),w=read();
t2.add(u,v,w),t2.add(v,u,w);
}
t1.init(),t2.init();
for(int i=1;i<=n;i++)que[i].clear();
for(int i=1;i<=q;i++){
int a=read(),b=read();
que[a].pb({b,i});
}
build(1,1,n);
dfs(1,0);
for(int i=1;i<=q;i++)printf("%lld\n",ans[i]);
}
// \
444
bool Med;
int T;
signed main(){
// freopen("move.in","r",stdin);
// freopen("move.out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
T=1;
while(T--)work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 36668kb
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: 38528kb
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: 36588kb
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: 82ms
memory: 77564kb
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:
445304648221 541265609267 444466815953 993989001283 427607535596 1002765524105 637944318799 422444226683 393735147219 1161571980630 670777009527 1092460576080 684001621139 702917199214 549906344701 523150284300 549512525212 685956706583 545567762363 1084507967324 592273049818 1024458521126 105849800...
result:
wrong answer 1st numbers differ - expected: '647838384844', found: '445304648221'